-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBulkUpdateCheck.axaml.cs
148 lines (139 loc) · 5.01 KB
/
BulkUpdateCheck.axaml.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Avalonia.Controls;
using Avalonia.Interactivity;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using static Manga_Manager.Globals;
using MangaDex_Library;
using System.Linq;
using Avalonia.Media;
using MsBox.Avalonia.Enums;
using MsBox.Avalonia;
using Avalonia.Threading;
namespace Manga_Manager;
public partial class BulkUpdateCheck : Window
{
private bool checking = true, cancelled = false, destroyInput1 = false, destroyInput2 = false;
private List<int> mangaIndexes = new List<int>();
private Dictionary<int, int> mangaIndexesNewChapters = new Dictionary<int, int>();
private CancellationTokenSource tokenSource = new CancellationTokenSource();
private CancellationToken cancelCheck;
public BulkUpdateCheck()
{
InitializeComponent();
StartChecking();
}
private async void StartChecking()
{
for (int i = 0; i < mangaList.Count; i++)
if (mangaList[i].CheckInBulk == true)
mangaIndexes.Add(i);
ProgressBar.Maximum = mangaIndexes.Count;
cancelCheck = tokenSource.Token;
IProgress<int> progress = new Progress<int>(v => { ProgressBar.Value++; });
await Task.Run(() =>
{
foreach (int i in mangaIndexes)
{
if (cancelCheck.IsCancellationRequested)
{
cancelled = true;
return;
}
MDLParameters.MangaID = mangaList[i].ID;
MDLGetData.GetLastChapter();
if (apiError == true)
{
apiError = false;
mangaIndexesNewChapters[i] = -1;
continue;
}
decimal onlineChapter = MDLGetData.GetLastChapter();
mangaList[i].OnlineLastChapter = onlineChapter;
mangaList[i].LastChecked = new DateOnly(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
int newChapters = 0;
for (int j = 0; j < MDLGetData.GetChapterNumbers().Count; j++)
if (MDLGetData.GetChapterNumbers()[j] > mangaList[i].FileLastChapter)
newChapters++;
mangaIndexesNewChapters[i] = newChapters;
progress.Report(0);
}
mangaIndexesNewChapters = mangaIndexesNewChapters.OrderByDescending(pair => pair.Value).ToDictionary();
}, cancelCheck);
checking = false;
CancelButton.Content = "Close";
if (cancelled == true)
return;
foreach (KeyValuePair<int, int> result in mangaIndexesNewChapters)
{
MangaNameList.Items.Add(new TextBlock
{
Foreground = new SolidColorBrush(Colors.White),
FontSize = 12,
Text = mangaList[result.Key].Title
});
if (result.Value == 0)
ChapterStatusList.Items.Add(new TextBlock
{
Foreground = new SolidColorBrush(Colors.White),
FontSize = 12,
Text = "-> Up to date!"
});
else if (result.Value == 1)
ChapterStatusList.Items.Add(new TextBlock
{
Foreground = new SolidColorBrush(Colors.White),
FontSize = 12,
Text = "-> 1 chapter ahead."
});
else if (result.Value < 0)
ChapterStatusList.Items.Add(new TextBlock
{
Foreground = new SolidColorBrush(Colors.White),
FontSize = 12,
Text = "-> Please check book link!"
});
else
ChapterStatusList.Items.Add(new TextBlock
{
Foreground = new SolidColorBrush(Colors.White),
FontSize = 12,
Text = "-> " + Convert.ToString(result.Value) + " chapters ahead."
});
}
}
private void MangaNameList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (destroyInput1 == true)
{
destroyInput1 = false;
return;
}
destroyInput2 = true;
ChapterStatusList.SelectedIndex = MangaNameList.SelectedIndex;
}
private void ChapterStatusList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (destroyInput2 == true)
{
destroyInput2 = false;
return;
}
destroyInput1 = true;
MangaNameList.SelectedIndex = ChapterStatusList.SelectedIndex;
}
private void CancelButton_Clicked(object sender, RoutedEventArgs args)
{
if (checking == true)
tokenSource.Cancel();
else
this.Close();
}
protected override void OnClosing(WindowClosingEventArgs e)
{
if (checking == true)
tokenSource.Cancel();
base.OnClosing(e);
}
}