Skip to content

Commit

Permalink
Replace own commands with toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
DSPaul committed Apr 16, 2024
1 parent c042580 commit 149608c
Show file tree
Hide file tree
Showing 22 changed files with 219 additions and 310 deletions.
30 changes: 0 additions & 30 deletions src/Commands/ActionCommand.cs

This file was deleted.

31 changes: 0 additions & 31 deletions src/Commands/RelayCommand.cs

This file was deleted.

34 changes: 0 additions & 34 deletions src/Commands/ReturningRelayCommand.cs

This file was deleted.

10 changes: 5 additions & 5 deletions src/ViewModels/CodexBulkEditViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using COMPASS.Commands;
using CommunityToolkit.Mvvm.Input;
using COMPASS.Models;
using COMPASS.Resources.Controls.MultiSelectCombobox;
using COMPASS.Tools;
Expand Down Expand Up @@ -107,8 +107,8 @@ private void RemoveTagFromItemsControl(Tag? t)

public Action CloseAction { get; set; } = () => { };

private ActionCommand? _okCommand;
public ActionCommand OKCommand => _okCommand ??= new(OKBtn);
private RelayCommand? _okCommand;
public RelayCommand OKCommand => _okCommand ??= new(OKBtn);
public void OKBtn()
{
//Copy changes into each Codex
Expand Down Expand Up @@ -184,8 +184,8 @@ public void OKBtn()
CloseAction();
}

private ActionCommand? _cancelCommand;
public ActionCommand CancelCommand => _cancelCommand ??= new(Cancel);
private RelayCommand? _cancelCommand;
public RelayCommand CancelCommand => _cancelCommand ??= new(Cancel);
public void Cancel() => CloseAction();

#endregion
Expand Down
42 changes: 21 additions & 21 deletions src/ViewModels/CodexEditViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using COMPASS.Commands;
using CommunityToolkit.Mvvm.Input;
using COMPASS.Models;
using COMPASS.Resources.Controls.MultiSelectCombobox;
using COMPASS.Services;
Expand Down Expand Up @@ -66,8 +66,8 @@ public bool ShowLoading

#region Methods and Commands

private ActionCommand? _browsePathCommand;
public ActionCommand BrowsePathCommand => _browsePathCommand ??= new(BrowsePath);
private RelayCommand? _browsePathCommand;
public RelayCommand BrowsePathCommand => _browsePathCommand ??= new(BrowsePath);
private void BrowsePath()
{
OpenFileDialog openFileDialog = new()
Expand All @@ -81,8 +81,8 @@ private void BrowsePath()
}
}

private ActionCommand? _browseURLCommand;
public ActionCommand BrowseURLCommand => _browseURLCommand ??= new(BrowseURL);
private RelayCommand? _browseURLCommand;
public RelayCommand BrowseURLCommand => _browseURLCommand ??= new(BrowseURL);
private void BrowseURL()
{
if (CodexViewModel.CanOpenCodexOnline(TempCodex))
Expand All @@ -91,16 +91,16 @@ private void BrowseURL()
}
}

private ActionCommand? _browseISBNCommand;
public ActionCommand BrowseISBNCommand => _browseISBNCommand ??= new(BrowseISBN);
private RelayCommand? _browseISBNCommand;
public RelayCommand BrowseISBNCommand => _browseISBNCommand ??= new(BrowseISBN);
private void BrowseISBN()
{
string url = $"https://openlibrary.org/search?q={TempCodex.ISBN}&mode=everything";
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}

private ActionCommand? _tagCheckCommand;
public ActionCommand TagCheckCommand => _tagCheckCommand ??= new(UpdateTagList);
private RelayCommand? _tagCheckCommand;
public RelayCommand TagCheckCommand => _tagCheckCommand ??= new(UpdateTagList);
private void UpdateTagList()
{
TempCodex.Tags.Clear();
Expand All @@ -113,8 +113,8 @@ private void UpdateTagList()
}
}

private ActionCommand? _quickCreateTagCommand;
public ActionCommand QuickCreateTagCommand => _quickCreateTagCommand ??= new(QuickCreateTag);
private RelayCommand? _quickCreateTagCommand;
public RelayCommand QuickCreateTagCommand => _quickCreateTagCommand ??= new(QuickCreateTag);
public void QuickCreateTag()
{
//keep track of count to check of tags were created
Expand Down Expand Up @@ -148,8 +148,8 @@ public void QuickCreateTag()
}
}

private ActionCommand? _deleteCodexCommand;
public ActionCommand DeleteCodexCommand => _deleteCodexCommand ??= new(DeleteCodex);
private RelayCommand? _deleteCodexCommand;
public RelayCommand DeleteCodexCommand => _deleteCodexCommand ??= new(DeleteCodex);
private void DeleteCodex()
{
if (!CreateNewCodex)
Expand All @@ -159,8 +159,8 @@ private void DeleteCodex()
CloseAction();
}

private ActionCommand? _fetchCoverCommand;
public ActionCommand FetchCoverCommand => _fetchCoverCommand ??= new(async () => await FetchCoverAsync());
private AsyncRelayCommand? _fetchCoverCommand;
public AsyncRelayCommand FetchCoverCommand => _fetchCoverCommand ??= new(FetchCoverAsync);
private async Task FetchCoverAsync()
{
ShowLoading = true;
Expand All @@ -177,8 +177,8 @@ private async Task FetchCoverAsync()
RefreshCover();
}

private ActionCommand? _chooseCoverCommand;
public ActionCommand ChooseCoverCommand => _chooseCoverCommand ??= new(ChooseCover);
private RelayCommand? _chooseCoverCommand;
public RelayCommand ChooseCoverCommand => _chooseCoverCommand ??= new(ChooseCover);
private void ChooseCover()
{
OpenFileDialog openFileDialog = new()
Expand Down Expand Up @@ -207,8 +207,8 @@ private void RefreshCover()

public Action CloseAction { get; set; } = () => { };

private ActionCommand? _oKCommand;
public ActionCommand OKCommand => _oKCommand ??= new(OKBtn);
private RelayCommand? _oKCommand;
public RelayCommand OKCommand => _oKCommand ??= new(OKBtn);
public void OKBtn()
{
//Copy changes into Codex
Expand All @@ -231,8 +231,8 @@ public void OKBtn()
CloseAction();
}

private ActionCommand? _cancelCommand;
public ActionCommand CancelCommand => _cancelCommand ??= new(Cancel);
private RelayCommand? _cancelCommand;
public RelayCommand CancelCommand => _cancelCommand ??= new(Cancel);
public void Cancel() => CloseAction();


Expand Down
6 changes: 3 additions & 3 deletions src/ViewModels/CodexInfoViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using COMPASS.Commands;
using CommunityToolkit.Mvvm.Input;
using COMPASS.Models;

namespace COMPASS.ViewModels
Expand Down Expand Up @@ -38,8 +38,8 @@ public bool AutoHide
}
}

private ActionCommand? _toggleCodexInfoCommand;
public ActionCommand ToggleCodexInfoCommand => _toggleCodexInfoCommand ??= new(() => ShowCodexInfo = !ShowCodexInfo);
private RelayCommand? _toggleCodexInfoCommand;
public RelayCommand ToggleCodexInfoCommand => _toggleCodexInfoCommand ??= new(() => ShowCodexInfo = !ShowCodexInfo);

private RelayCommand<string>? _addAuthorFilterCommand;
public RelayCommand<string> AddAuthorFilterCommand => _addAuthorFilterCommand ??= new(AddAuthorFilter);
Expand Down
73 changes: 35 additions & 38 deletions src/ViewModels/CodexViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Autofac;
using COMPASS.Commands;
using CommunityToolkit.Mvvm.Input;
using COMPASS.Models;
using COMPASS.Services;
using COMPASS.Tools;
Expand Down Expand Up @@ -36,8 +36,8 @@ public static bool OpenCodex(Codex codex)
}

//Open codex Offline
private ReturningRelayCommand<Codex, bool>? _openCodexLocallyCommand;
public ReturningRelayCommand<Codex, bool> OpenCodexLocallyCommand => _openCodexLocallyCommand ??= new(OpenCodexLocally, CanOpenCodexLocally);
private RelayCommand<Codex>? _openCodexLocallyCommand;
public RelayCommand<Codex> OpenCodexLocallyCommand => _openCodexLocallyCommand ??= new(codex => OpenCodexLocally(codex), CanOpenCodexLocally);
public static bool OpenCodexLocally(Codex? toOpen)
{
if (toOpen is null) return false;
Expand Down Expand Up @@ -69,8 +69,8 @@ public static bool CanOpenCodexLocally(Codex? toOpen)
}

//Open codex Online
private ReturningRelayCommand<Codex, bool>? _openCodexOnlineCommand;
public ReturningRelayCommand<Codex, bool> OpenCodexOnlineCommand => _openCodexOnlineCommand ??= new(OpenCodexOnline, CanOpenCodexOnline);
private RelayCommand<Codex>? _openCodexOnlineCommand;
public RelayCommand<Codex> OpenCodexOnlineCommand => _openCodexOnlineCommand ??= new(codex => OpenCodexOnline(codex), CanOpenCodexOnline);
public static bool OpenCodexOnline(Codex? toOpen)
{
if (!CanOpenCodexOnline(toOpen)) return false;
Expand Down Expand Up @@ -99,8 +99,8 @@ public static bool CanOpenCodexOnline(Codex? toOpen)
}

//Open Multiple Files
private ReturningRelayCommand<IList, bool>? _openSelectedCodicesCommand;
public ReturningRelayCommand<IList, bool> OpenSelectedCodicesCommand => _openSelectedCodicesCommand ??= new(l => OpenSelectedCodices(l?.Cast<Codex>().ToList()));
private RelayCommand<IList>? _openSelectedCodicesCommand;
public RelayCommand<IList> OpenSelectedCodicesCommand => _openSelectedCodicesCommand ??= new(l => OpenSelectedCodices(l?.Cast<Codex>().ToList()));
public static bool OpenSelectedCodices(IList<Codex>? toOpen)
{
if (toOpen is null) return false;
Expand Down Expand Up @@ -342,26 +342,8 @@ public static void BanishCodices(IList? toBanish)
DeleteCodices(toBanish);
}

private ReturningRelayCommand<Codex, Task>? _getMetaDataCommand;
public ReturningRelayCommand<Codex, Task> GetMetaDataCommand => _getMetaDataCommand ??= new(StartGetMetaDataProcess);


private ReturningRelayCommand<IList, Task>? _getMetaDataBulkCommand;
public ReturningRelayCommand<IList, Task> GetMetaDataBulkCommand => _getMetaDataBulkCommand ??= new(
async codices =>
{
try
{
await StartGetMetaDataProcess(codices?.Cast<Codex>().ToList() ?? new());
}
catch (OperationCanceledException ex)
{
Logger.Warn("Renewing metadata has been cancelled", ex);
await Task.Run(() => ProgressViewModel.GetInstance().ConfirmCancellation());
}
}
);

private AsyncRelayCommand<Codex>? _getMetaDataCommand;
public AsyncRelayCommand<Codex> GetMetaDataCommand => _getMetaDataCommand ??= new(StartGetMetaDataProcess);
public static async Task StartGetMetaDataProcess(Codex? codex)
{
try
Expand All @@ -375,7 +357,6 @@ public static async Task StartGetMetaDataProcess(Codex? codex)
await Task.Run(() => ProgressViewModel.GetInstance().ConfirmCancellation());
}
}

public static async Task StartGetMetaDataProcess(IList<Codex> codices)
{
var progressVM = ProgressViewModel.GetInstance();
Expand All @@ -402,7 +383,6 @@ public static async Task StartGetMetaDataProcess(IList<Codex> codices)
MainViewModel.CollectionVM.CurrentCollection.Save();
MainViewModel.CollectionVM.FilterVM.ReFilter();
}

private static async Task GetMetaData(Codex codex, ChooseMetaDataViewModel chooseMetaDataVM)
{
// Lazy load metadata from all the sources, use dict to store
Expand Down Expand Up @@ -499,17 +479,34 @@ private static async Task GetMetaData(Codex codex, ChooseMetaDataViewModel choos
ProgressViewModel.GetInstance().IncrementCounter();
}

private ReturningRelayCommand<Codex, Task>? _getCoverCommand;
public ReturningRelayCommand<Codex, Task> GetCoverCommand => _getCoverCommand ??=
new(async codex =>
private AsyncRelayCommand<IList>? _getMetaDataBulkCommand;
public AsyncRelayCommand<IList> GetMetaDataBulkCommand => _getMetaDataBulkCommand ??= new(GetMetaDataBulk);

private static async Task GetMetaDataBulk(IList? codices)
{
try
{
await StartGetMetaDataProcess(codices?.Cast<Codex>().ToList() ?? new());
}
catch (OperationCanceledException ex)
{
if (codex is null) return;
await CoverService.GetCover(new List<Codex>() { codex });
});
Logger.Warn("Renewing metadata has been cancelled", ex);
await Task.Run(() => ProgressViewModel.GetInstance().ConfirmCancellation());
}
}

private AsyncRelayCommand<Codex>? _getCoverCommand;
public AsyncRelayCommand<Codex> GetCoverCommand => _getCoverCommand ??= new(GetCover);
private static async Task GetCover(Codex? codex)
{
if (codex is null) return;
await CoverService.GetCover(new List<Codex>() { codex });
}

private ReturningRelayCommand<IList, Task>? _getCoverBulkCommand;
public ReturningRelayCommand<IList, Task> GetCoverBulkCommand => _getCoverBulkCommand ??=
new(async codices => await CoverService.GetCover(codices?.Cast<Codex>().ToList() ?? new()));
private AsyncRelayCommand<IList>? _getCoverBulkCommand;
public AsyncRelayCommand<IList> GetCoverBulkCommand => _getCoverBulkCommand ??= new(GetCoverBulk);
private static async Task GetCoverBulk(IList? codices) =>
await CoverService.GetCover(codices?.Cast<Codex>().ToList() ?? new());

public static void DataGridHandleKeyDown(object sender, KeyEventArgs e)
=> HandleKeyDownOnCodex(((DataGrid)sender).SelectedItems, e);
Expand Down
2 changes: 1 addition & 1 deletion src/ViewModels/CollectionContentSelectorViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel;
using COMPASS.Commands;
using CommunityToolkit.Mvvm.Input;
using COMPASS.Models;
using COMPASS.Tools;
using System.Collections;
Expand Down
Loading

0 comments on commit 149608c

Please sign in to comment.