-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
123 lines (96 loc) · 3.17 KB
/
Copy pathMainViewModel.cs
File metadata and controls
123 lines (96 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using CommonHelpers.Collections;
using CommonHelpers.Common;
using CommonHelpers.Mvvm;
using CommonHelpers.Services;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Windows.AI.Imaging;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using XkcdViewer.Common.Models;
using XkcdViewer.Common.Services;
using XkcdViewer.Windows.Utils;
namespace XkcdViewer.Windows;
public partial class MainViewModel : ViewModelBase
{
private readonly ComicDataService comicDataService;
public MainViewModel()
{
comicDataService = new ComicDataService(new XkcdApiService());
FetchComicCommand = new RelayCommand(async () => await FetchComicAsync());
DeleteComicCommand = new RelayCommand(async () => await DeleteComicAsync());
AnalyzeComicCommand = new RelayCommand(async () => await AnalyzeCurrentComicAsync());
InitializeCopilotCapabilities();
}
public ObservableRangeCollection<Comic> Comics { get; } = [];
public ObservableRangeCollection<ImageDescriptionKind> DescriptionLevels { get; } = [];
public Comic? CurrentComic
{
get;
set => SetProperty(ref field, value);
}
public ImageDescriptionKind PreferredDescriptionLevel
{
get;
set => SetProperty(ref field, value);
}
public RelayCommand FetchComicCommand { get; set; }
public RelayCommand DeleteComicCommand { get; set; }
public RelayCommand AnalyzeComicCommand { get; set; }
public required IDialogService DialogService { get; set; }
public Visibility CopilotCapVisibility
{
get;
set => SetProperty(ref field, value);
}
public async Task InitialLoadAsync()
{
await comicDataService.LoadComicsAsync(Comics);
}
public async Task FetchComicAsync()
{
try
{
if (IsBusy)
return;
IsBusy = true;
IsBusyMessage = "Fetching Comic...";
await comicDataService.GetComic(Comics, Comics.LastOrDefault()?.Num - 1);
CurrentComic = Comics.Last();
}
catch (Exception ex)
{
Debug.WriteLine($"FetchComicAsync Exception\r\n{ex}");
}
finally
{
IsBusy = false;
IsBusyMessage = "";
}
}
private async Task DeleteComicAsync()
{
if (CurrentComic is null)
return;
var dialog = new ContentDialog
{
Title = "Delete Confirmation",
Content = "Are you sure you want to remove this comic?",
PrimaryButtonText = "YES",
CloseButtonText = "NO",
DefaultButton = ContentDialogButton.Close
};
var result = await this.DialogService.ShowDialogAsync(dialog);
if (result != ContentDialogResult.Primary)
return;
var nextIndex = Comics.IndexOf(CurrentComic) - 1;
if(nextIndex <= 0)
nextIndex = 0;
DeleteCachedComicImage(CurrentComic.Num);
Comics.Remove(CurrentComic);
CurrentComic = Comics.ElementAtOrDefault(nextIndex);
await comicDataService.SaveComicsAsync(Comics);
}
}