/
VM_MainListHelper.cs
1122 lines (950 loc) · 38.8 KB
/
VM_MainListHelper.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using NLog;
using Shoko.Commons.Extensions;
using Shoko.Commons.Notification;
using Shoko.Desktop.Utilities;
using Shoko.Models.Enums;
using Shoko.Desktop.ViewModel.Helpers;
using Shoko.Desktop.ViewModel.Server;
using Shoko.Models;
using Shoko.Models.Client;
// ReSharper disable InconsistentNaming
namespace Shoko.Desktop.ViewModel
{
public class VM_MainListHelper : INotifyPropertyChangedExt
{
private static VM_MainListHelper _instance;
public ICollectionView ViewGroups { get; set; }
public ICollectionView ViewGroupsForms { get; set; }
public ICollectionView ViewAVDumpFiles { get; set; }
public ICollectionView ViewBookmarkedAnime { get; set; }
// contains a value for each AnimeSeries and the last highlighted episode for that series
public Dictionary<int, int> LastEpisodeForSeries { get; set; }
public ObservableCollection<IListWrapper> BreadCrumbs { get; set; }
public ObservableCollection<IListWrapper> CurrentWrapperList { get; set; }
public ObservableCollection<VM_AVDump> AVDumpFiles { get; set; }
public ObservableCollection<VM_BookmarkedAnime> BookmarkedAnime { get; set; }
public DateTime LastChange { get; set; } = DateTime.MinValue;
public ObservableListDictionary<int, VM_AnimeGroup_User> AllGroupsDictionary { get; set; }
public ObservableListDictionary<int, VM_AnimeSeries_User> AllSeriesDictionary { get; set; }
public Dictionary<int, VM_AniDB_Anime> AllAnimeDictionary { get; set; }
public Dictionary<int, VM_GroupFilter> AllGroupFiltersDictionary { get; set; }
public SeriesSearchType SerSearchType { get; set; }
public int SearchResultCount;
public ObservableCollection<VM_AnimeEpisode_User> EpisodesForSeries { get; set; }
//public ObservableCollection<FileDetailedVM> FilesForSeries { get; set; }
//public MainListWrapper CurrentWrapper { get; set; }
public VM_GroupFilter CurrentGroupFilter { get; set; }
public TextBox SearchTextBox { get; set; }
public ICollectionView ViewSeriesSearch { get; set; }
private Timer searchTimer;
public bool BookmarkFilter_Downloading = true;
public bool BookmarkFilter_NotDownloading = true;
private TextBox seriesSearchTextBox;
public TextBox SeriesSearchTextBox
{
get { return seriesSearchTextBox; }
set
{
seriesSearchTextBox = value;
seriesSearchTextBox.TextChanged += SearchTextBox_TextChanged;
}
}
//public int LastAnimeGroupID { get; set; }
//public int LastAnimeSeriesID { get; set; }
//public int LastGroupFilterID { get; set; }
public string CurrentOpenGroupFilter { get; set; }
public Dictionary<string, int> LastGroupForGF { get; set; } // GroupFilterID, position in list of last selected group
public VM_GroupFilter AllGroupFilter
{
get
{
return Instance.AllGroupFiltersDictionary.Values.FirstOrDefault(a => a.FilterType == 4);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propname)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
}
private IListWrapper currentWrapper;
public IListWrapper CurrentWrapper
{
get { return currentWrapper; }
set
{
this.SetField(()=>currentWrapper,value);
}
}
private double fullScrollerWidth = 10;
public double FullScrollerWidth
{
get { return fullScrollerWidth; }
set
{
this.SetField(()=>fullScrollerWidth,value);
}
}
private double fullScrollerHeight = 500;
public double FullScrollerHeight
{
get { return fullScrollerHeight; }
set
{
this.SetField(()=>fullScrollerHeight,value);
}
}
private double downloadRecScrollerWidth = 10;
public double DownloadRecScrollerWidth
{
get { return downloadRecScrollerWidth; }
set
{
this.SetField(()=>downloadRecScrollerWidth,value);
}
}
private double mainScrollerWidth = 10;
public double MainScrollerWidth
{
get { return mainScrollerWidth; }
set
{
this.SetField(()=>mainScrollerWidth,value);
double temp = mainScrollerWidth - 10;
if (temp < 10) temp = 10;
MainScrollerChildrenWidth = temp;
temp = mainScrollerWidth - 80;
if (temp < 10) temp = 10;
MainScrollerSeriesChildrenWidth = temp;
}
}
private double playlistWidth = 10;
public double PlaylistWidth
{
get { return playlistWidth; }
set
{
this.SetField(()=>playlistWidth,value);
}
}
private double mainScrollerChildrenWidth = 10;
public double MainScrollerChildrenWidth
{
get { return mainScrollerChildrenWidth; }
set
{
this.SetField(()=>mainScrollerChildrenWidth,value);
}
}
private double mainScrollerSeriesChildrenWidth = 10;
public double MainScrollerSeriesChildrenWidth
{
get { return mainScrollerSeriesChildrenWidth; }
set
{
this.SetField(()=>mainScrollerSeriesChildrenWidth,value);
}
}
private bool currentWrapperIsGroup;
public bool CurrentWrapperIsGroup
{
get { return currentWrapperIsGroup; }
set
{
this.SetField(()=>currentWrapperIsGroup,value);
}
}
private bool currentListWrapperIsGroup;
public bool CurrentListWrapperIsGroup
{
get { return currentListWrapperIsGroup; }
set
{
this.SetField(()=>currentListWrapperIsGroup,value);
}
}
private bool showEpisodes;
public bool ShowEpisodes
{
get { return showEpisodes; }
set
{
this.SetField(()=>showEpisodes,value);
}
}
private VM_AnimeSeries_User currentSeries;
public VM_AnimeSeries_User CurrentSeries
{
get { return currentSeries; }
set
{
this.SetField(()=>currentSeries,value);
}
}
private Dictionary<int, VM_AniDB_AnimeDetailed> allAnimeDetailedDictionary;
public Dictionary<int, VM_AniDB_AnimeDetailed> AllAnimeDetailedDictionary
{
get
{
if (allAnimeDetailedDictionary == null) {
DateTime start = DateTime.Now;
allAnimeDetailedDictionary = VM_ShokoServer.Instance.ShokoServices.GetAllAnimeDetailed().Cast<VM_AniDB_AnimeDetailed>().ToDictionary(a => a.AniDBAnime.AnimeID, a => a);
TimeSpan ts = DateTime.Now - start;
LogManager.GetCurrentClassLogger().Trace("Got all anime detailed in {0} ms", ts.TotalMilliseconds);
}
return allAnimeDetailedDictionary;
}
set
{
allAnimeDetailedDictionary = value;
}
}
public event EventHandler Refreshed;
public static VM_MainListHelper Instance => _instance ?? (_instance = new VM_MainListHelper());
private VM_MainListHelper()
{
SerSearchType = SeriesSearchType.TitleOnly;
CurrentWrapperList = new ObservableCollection<IListWrapper>();
EpisodesForSeries = new ObservableCollection<VM_AnimeEpisode_User>();
AVDumpFiles = new ObservableCollection<VM_AVDump>();
BookmarkedAnime = new ObservableCollection<VM_BookmarkedAnime>();
AllGroupsDictionary = new ObservableListDictionary<int, VM_AnimeGroup_User>();
AllSeriesDictionary = new ObservableListDictionary<int, VM_AnimeSeries_User>();
AllAnimeDictionary = new Dictionary<int, VM_AniDB_Anime>();
AllGroupFiltersDictionary = new Dictionary<int, VM_GroupFilter>();
ViewGroups = CollectionViewSource.GetDefaultView(CurrentWrapperList);
ViewGroupsForms = CollectionViewSource.GetDefaultView(AllGroupsDictionary.Values);
ViewAVDumpFiles = CollectionViewSource.GetDefaultView(AVDumpFiles);
ViewBookmarkedAnime = CollectionViewSource.GetDefaultView(BookmarkedAnime);
ViewSeriesSearch = CollectionViewSource.GetDefaultView(AllSeriesDictionary.Values);
ViewSeriesSearch.SortDescriptions.Add(new SortDescription("SeriesName", ListSortDirection.Ascending));
ViewSeriesSearch.Filter = SeriesSearchFilter;
BreadCrumbs = new ObservableCollection<IListWrapper>();
//LastAnimeGroupID = 0;
//LastAnimeSeriesID = 0;
//LastGroupFilterID = 0;
CurrentOpenGroupFilter = "";
LastGroupForGF = new Dictionary<string, int>();
LastEpisodeForSeries = new Dictionary<int, int>();
}
void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
searchTimer?.Stop();
searchTimer = new Timer
{
AutoReset = false,
Interval = 500
};
// 500ms
searchTimer.Elapsed += searchTimer_Elapsed;
searchTimer.Enabled = true;
}
void searchTimer_Elapsed(object sender, ElapsedEventArgs e)
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate
{
SearchResultCount = 0;
ViewSeriesSearch.Refresh();
});
}
private bool SeriesSearchFilter(object obj)
{
VM_AnimeSeries_User ser = obj as VM_AnimeSeries_User;
if (ser == null) return false;
if (SearchResultCount > 100) return false;
var passed = GroupSearchFilterHelper.EvaluateSeriesTextSearch(ser, SeriesSearchTextBox.Text, SerSearchType);
if (passed)
SearchResultCount++;
return passed;
}
public void UpdateAnime(int animeID)
{
Application.Current?.Dispatcher.Invoke( () =>
{
try
{
VM_AniDB_Anime anime = (VM_AniDB_Anime)VM_ShokoServer.Instance.ShokoServices.GetAnime(animeID);
if (anime != null)
{
AllAnimeDictionary[anime.AnimeID] = anime;
// update the series
VM_AnimeSeries_User ser = AllSeriesDictionary.Values.FirstOrDefault(a => a.AniDB_ID == anime.AnimeID);
if (ser != null)
{
VM_MainListHelper.Instance.UpdateAll();
AllSeriesDictionary[ser.AnimeSeriesID] = ser;
}
}
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
});
}
public void MoveBackUpHeirarchy()
{
if (BreadCrumbs.Count <= 1) return;
try
{
// get the second last child wrapper
IListWrapper wrapper = BreadCrumbs[BreadCrumbs.Count - 2];
ShowChildWrappers(wrapper);
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
}
public void ShowAllGroups()
{
try
{
CurrentWrapper = null;
CurrentWrapperIsGroup = true;
BreadCrumbs.Clear();
BreadCrumbs.Add(null);
CurrentWrapperList.Clear();
foreach (VM_GroupFilter grpFilter in AllGroupFiltersDictionary.Values)
{
//if (grpFilter.FilterConditions.Count == 0)
// AllGroupFilter = grpFilter;
if (!grpFilter.ParentGroupFilterID.HasValue)
CurrentWrapperList.Add(grpFilter);
}
ShowChildWrappers(AllGroupFilter);
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
}
//[LogExecutionTime]
public void SetGroupFilterSortingOnMainList()
{
if (CurrentWrapper is VM_GroupFilter)
{
VM_GroupFilter gf = (VM_GroupFilter) CurrentWrapper;
ViewGroups.SortDescriptions.Clear();
List<SortDescription> sortlist = gf.GetSortDescriptions();
foreach (SortDescription sd in sortlist)
ViewGroups.SortDescriptions.Add(sd);
}
else
ViewGroups.SortDescriptions.Clear();
}
public void SetGroupFilterSortingOnForms(VM_GroupFilter gf)
{
ViewGroupsForms.SortDescriptions.Clear();
if (gf?.IsDirectoryFilter != true) return;
List<SortDescription> sortlist = gf.GetSortDescriptions();
foreach (SortDescription sd in sortlist)
ViewGroupsForms.SortDescriptions.Add(sd);
}
//[LogExecutionTime]
public void ShowChildWrappers(IListWrapper wrapper)
{
try
{
CurrentWrapper = wrapper;
CurrentWrapperIsGroup = wrapper is VM_GroupFilter;
CurrentListWrapperIsGroup = wrapper is VM_AnimeGroup_User;
if (wrapper is VM_AnimeGroup_User)
{
//LastAnimeGroupID = ((VM_AnimeGroup_User)wrapper).AnimeGroupID.Value;
Instance.CurrentOpenGroupFilter = "VM_AnimeGroup_User|" + ((VM_AnimeGroup_User)wrapper).AnimeGroupID;
}
if (wrapper is VM_GroupFilter)
{
CurrentGroupFilter = (VM_GroupFilter)wrapper;
//LastGroupFilterID = ((GroupFilterVM)wrapper).GroupFilterID.Value;
Instance.CurrentOpenGroupFilter = "GroupFilterVM|" + ((VM_GroupFilter)wrapper).GroupFilterID;
}
if (wrapper is VM_AnimeSeries_User)
{
CurrentSeries = (VM_AnimeSeries_User) wrapper;
//LastAnimeSeriesID = ((VM_AnimeSeries_User)wrapper).AnimeSeriesID.Value;
Instance.CurrentOpenGroupFilter = "NoGroup";
}
if (wrapper == null)
Instance.CurrentOpenGroupFilter = "Init";
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate
{
// update wrappers
// check where this wrapper sits in the heirarchy and remove anything below it
int pos = -1;
if (wrapper != null)
{
for (int i = 0; i < BreadCrumbs.Count; i++)
{
if (BreadCrumbs[i] == null) continue;
if (wrapper is VM_GroupFilter && BreadCrumbs[i] is VM_GroupFilter)
{
VM_GroupFilter wrapObj = (VM_GroupFilter) wrapper;
VM_GroupFilter bcObj = (VM_GroupFilter) BreadCrumbs[i];
if (wrapObj.GroupFilterName == bcObj.GroupFilterName) pos = i;
}
else if (wrapper is VM_AnimeGroup_User && BreadCrumbs[i] is VM_AnimeGroup_User)
{
VM_AnimeGroup_User wrapObj = (VM_AnimeGroup_User) wrapper;
VM_AnimeGroup_User bcObj = (VM_AnimeGroup_User) BreadCrumbs[i];
if (wrapObj.AnimeGroupID == bcObj.AnimeGroupID) pos = i;
}
else if (wrapper is VM_AnimeSeries_User && BreadCrumbs[i] is VM_AnimeSeries_User)
{
VM_AnimeSeries_User wrapObj = (VM_AnimeSeries_User) wrapper;
VM_AnimeSeries_User bcObj = (VM_AnimeSeries_User) BreadCrumbs[i];
if (wrapObj.AnimeSeriesID == bcObj.AnimeSeriesID) pos = i;
}
else if (wrapper is VM_AnimeEpisodeType && BreadCrumbs[i] is VM_AnimeEpisodeType)
{
VM_AnimeEpisodeType wrapObj = (VM_AnimeEpisodeType) wrapper;
VM_AnimeEpisodeType bcObj = (VM_AnimeEpisodeType) BreadCrumbs[i];
if (wrapObj.EpisodeTypeDescription == bcObj.EpisodeTypeDescription) pos = i;
}
else if (wrapper is VM_AnimeEpisode_User && BreadCrumbs[i] is VM_AnimeEpisode_User)
{
VM_AnimeEpisode_User wrapObj = (VM_AnimeEpisode_User) wrapper;
VM_AnimeEpisode_User bcObj = (VM_AnimeEpisode_User) BreadCrumbs[i];
if (wrapObj.AnimeEpisodeID == bcObj.AnimeEpisodeID) pos = i;
}
}
}
else pos = 0;
if (pos > 0)
{
for (int i = BreadCrumbs.Count - 1; i >= 0; i--)
{
if (i >= pos) BreadCrumbs.RemoveAt(i);
}
}
else if(pos == 0) BreadCrumbs.Clear();
BreadCrumbs.Add(wrapper);
if (wrapper is VM_GroupFilter)
{
if (AllGroupsDictionary.Count == 0)
RefreshGroupsSeriesData();
// apply sorting
// get default sorting from the group filter
}
// means we are at the top level
if (wrapper == null)
{
CurrentWrapperList.Clear();
foreach (VM_GroupFilter grpFilter in AllGroupFiltersDictionary.Values
.Where(a => a != null && !a.ParentGroupFilterID.HasValue).OrderBy(a => a.GroupFilterName))
{
CurrentWrapperList.Add(grpFilter);
}
}
else
{
CurrentWrapperList.Clear();
foreach (IListWrapper wp in wrapper.GetDirectChildren())
{
CurrentWrapperList.Add(wp);
}
}
SetGroupFilterSortingOnMainList();
ViewGroups.Refresh();
//new FilterMainListBox(ViewGroups, SearchTextBox, CurrentGroupFilter);
});
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
}
/*
void AllGroups_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
//ViewGroups.Refresh();
}
*/
public int GroupCount
{
get
{
int i = 0;
// ReSharper disable once UnusedVariable
foreach (object obj in ViewGroups)
i++;
return i;
}
}
public void RefreshBookmarkedAnime()
{
try
{
// set this to null so that it will be refreshed the next time it is needed
List<VM_BookmarkedAnime> baRaw = VM_ShokoServer.Instance.ShokoServices.GetAllBookmarkedAnime().CastList<VM_BookmarkedAnime>();
//if (baRaw.Count == 0) return;
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate
{
BookmarkedAnime.Clear();
// must series before groups the binding is based on the groups, and will refresh when that is changed
foreach (VM_BookmarkedAnime ba in baRaw)
{
if (ba.DownloadingBool && BookmarkFilter_Downloading)
BookmarkedAnime.Add(ba);
if (!ba.DownloadingBool && BookmarkFilter_NotDownloading)
BookmarkedAnime.Add(ba);
}
ViewBookmarkedAnime.Refresh();
});
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
}
public void ClearData()
{
AllGroupsDictionary.Clear();
AllSeriesDictionary.Clear();
AllAnimeDictionary.Clear();
AllGroupFiltersDictionary.Clear();
ViewSeriesSearch.Refresh();
LastChange = DateTime.MinValue;
OnRefreshed();
}
public void RefreshGroupsSeriesData()
{
//LoadTestData();
//return;
try
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate
{
UpdateAll();
OnRefreshed();
});
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
}
public void RefreshGroupFiltersOnly()
{
//LoadTestData();
//return;
try
{
// set this to null so that it will be refreshed the next time it is needed
List<VM_GroupFilter> gfRaw = VM_ShokoServer.Instance.ShokoServices.GetAllGroupFilters().CastList<VM_GroupFilter>();
if (gfRaw.Count == 0) return;
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate
{
object p = CurrentWrapper;
VM_GroupFilter vms = AllGroupFiltersDictionary.Values.FirstOrDefault(a => a.GroupFilterID == 0);
AllGroupFiltersDictionary.Clear();
if (vms != null)
AllGroupFiltersDictionary.Add(0, vms);
foreach (VM_GroupFilter gf in gfRaw.OrderBy(a => a.GroupFilterName))
{
AllGroupFiltersDictionary[gf.GroupFilterID] = gf;
}
//Restore previous condition
if (p is VM_GroupFilter)
{
int id = ((VM_GroupFilter)p).GroupFilterID;
if (AllGroupFiltersDictionary.ContainsKey(id))
{
CurrentWrapper = AllGroupFiltersDictionary[id];
}
}
else if (p is VM_AnimeGroup_User)
{
int id = ((VM_AnimeGroup_User)p).AnimeGroupID;
if (AllGroupsDictionary.ContainsKey(id))
{
CurrentWrapper = AllGroupsDictionary[id];
}
}
else if (p is VM_AnimeSeries_User)
{
int id = ((VM_AnimeSeries_User)p).AnimeSeriesID;
if (AllSeriesDictionary.ContainsKey(id))
{
CurrentWrapper = AllSeriesDictionary[id];
}
}
OnRefreshed();
});
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
}
public void UpdateAll()
{
try
{
CL_MainChanges changes = VM_ShokoServer.Instance.ShokoServices.GetAllChanges(LastChange, VM_ShokoServer.Instance.CurrentUser.JMMUserID);
AllAnimeDetailedDictionary = null;
//Update Anime Groups (Must be done before Series)
foreach (int gr in changes.Groups.RemovedItems)
{
AllGroupsDictionary.Remove(gr);
}
foreach (VM_AnimeGroup_User g in changes.Groups.ChangedItems.CastList<VM_AnimeGroup_User>())
{
VM_AnimeGroup_User v;
if (AllGroupsDictionary.TryGetValue(g.AnimeGroupID, out v))
{
v.Populate(g);
}
else
{
AllGroupsDictionary[g.AnimeGroupID] = g;
}
}
//Update Anime Series (NOTE: relies on AllGroupsDictionary being up to date)
foreach (int sr in changes.Series.RemovedItems)
{
AllSeriesDictionary.Remove(sr);
}
foreach (VM_AnimeSeries_User s in changes.Series.ChangedItems.CastList<VM_AnimeSeries_User>())
{
VM_AnimeSeries_User v;
if (AllSeriesDictionary.TryGetValue(s.AnimeSeriesID, out v))
{
v.Populate(s);
}
else
{
AllSeriesDictionary[s.AnimeSeriesID] = s;
}
}
foreach (VM_AnimeGroup_User v in AllGroupsDictionary.Values)
{
v.PopulateSerieInfo(AllGroupsDictionary, AllSeriesDictionary);
}
//Update Group Filters
foreach (int gfr in changes.Filters.RemovedItems)
{
AllGroupFiltersDictionary.Remove(gfr);
}
foreach (VM_GroupFilter gf in changes.Filters.ChangedItems.CastList<VM_GroupFilter>())
{
VM_GroupFilter v;
if (AllGroupFiltersDictionary.TryGetValue(gf.GroupFilterID, out v))
{
v.Populate(gf);
}
else
{
AllGroupFiltersDictionary[gf.GroupFilterID] = gf;
}
}
foreach (int gf in changes.Filters.ChangedItems.Select(a => a.GroupFilterID))
{
VM_GroupFilter v = AllGroupFiltersDictionary[gf];
//Recalculate Groups Count
v.GetDirectChildren();
}
LastChange = changes.LastChange;
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
}
public void UpdateAnimeGroups()
{
CL_MainChanges changes = VM_ShokoServer.Instance.ShokoServices.GetAllChanges(LastChange,
VM_ShokoServer.Instance.CurrentUser.JMMUserID);
//Update Anime Groups
foreach (int gr in changes.Groups.RemovedItems)
{
AllGroupsDictionary.Remove(gr);
}
foreach (CL_AnimeGroup_User g in changes.Groups.ChangedItems)
{
VM_AnimeGroup_User v;
if (AllGroupsDictionary.TryGetValue(g.AnimeGroupID, out v))
{
v.Populate(g);
}
else
{
AllGroupsDictionary[g.AnimeGroupID] = (VM_AnimeGroup_User)g;
}
}
foreach (VM_AnimeGroup_User v in AllGroupsDictionary.Values)
{
v.PopulateSerieInfo(AllGroupsDictionary, AllSeriesDictionary);
}
GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
GC.Collect();
}
public void InitGroupsSeriesData()
{
LastChange = DateTime.MinValue;
UpdateAll();
}
public VM_AnimeSeries_User GetSeriesForEpisode(VM_AnimeEpisode_User ep)
{
try
{
return AllSeriesDictionary.SureGet(ep.AnimeSeriesID);
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
return null;
}
public VM_AnimeSeries_User GetSeriesForAnime(int animeID)
{
try
{
return AllSeriesDictionary.Values.FirstOrDefault(a => a.AniDB_ID == animeID);
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
return null;
}
public VM_AnimeSeries_User GetSeries(int animeSeriesID)
{
try
{
VM_AnimeSeries_User vm;
AllSeriesDictionary.TryGetValue(animeSeriesID, out vm);
return vm;
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
return null;
}
public VM_AnimeSeries_User GetSeriesForVideo(int videoLocalID)
{
try
{
// get the episodes that this file applies to
foreach (CL_AnimeEpisode_User epcontract in VM_ShokoServer.Instance.ShokoServices.GetEpisodesForFile(videoLocalID, VM_ShokoServer.Instance.CurrentUser.JMMUserID))
{
VM_AnimeSeries_User thisSeries = AllSeriesDictionary.SureGet(epcontract.AnimeSeriesID);
if (thisSeries != null)
return thisSeries;
}
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
return null;
}
/*
public VM_AnimeEpisode_User GetEpisodeForVideo(VM_VideoDetailed vid, EpisodeList epList)
{
// get the episodes that this file applies to
try
{
List<JMMServerBinary.Contract_AnimeEpisode> eps = VM_ShokoServer.Instance.ShokoServices.GetEpisodesForFile(vid.VideoLocalID,
VM_ShokoServer.Instance.CurrentUser.JMMUserID.Value);
foreach (JMMServerBinary.Contract_AnimeEpisode epcontract in eps)
{
foreach (object epObj in epList.lbEpisodes.ItemsSource)
{
VM_AnimeEpisode_User epItem = epObj as VM_AnimeEpisode_User;
if (epItem.AnimeEpisodeID == vid.AnimeEpisodeID)
return epItem;
}
}
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
return null;
}
*/
public void RefreshHeirarchy(VM_AnimeEpisode_User ep)
{
try
{
// update the episode first
VM_AnimeEpisode_User contract = (VM_AnimeEpisode_User)VM_ShokoServer.Instance.ShokoServices.GetEpisode(ep.AnimeEpisodeID, VM_ShokoServer.Instance.CurrentUser.JMMUserID);
UpdateHeirarchy(contract);
}
catch (Exception ex)
{
Utils.ShowErrorMessage(ex);
}
}
public void UpdateHeirarchy(VM_AnimeEpisode_User epcontract)
{
try
{
foreach (VM_AnimeSeries_User ser in AllSeriesDictionary.Values)
{
if (ser.AnimeSeriesID!=0 && epcontract.AnimeSeriesID == ser.AnimeSeriesID)
{
foreach (VM_AnimeEpisode_User ep in ser.AllEpisodes)
{
if (ep.AnimeEpisodeID == epcontract.AnimeEpisodeID)
{
ep.Populate(epcontract);
// update the attached videos if they are visible
IEnumerable<VM_VideoDetailed> vids = VM_ShokoServer.Instance.ShokoServices.GetFilesForEpisode(ep.AnimeEpisodeID,
VM_ShokoServer.Instance.CurrentUser.JMMUserID).Cast<VM_VideoDetailed>();
foreach (VM_VideoDetailed vidcontract in vids)
{
foreach (VM_VideoDetailed vid in ep.FilesForEpisode)
{
if (vid.VideoLocalID == vidcontract.VideoLocalID)
{
vid.Populate(vidcontract);
break;
}
}
}
// update all the attached groups
UpdateGroupAndSeriesForEpisode(ep);
}
}
break;
}
}
// update the episodes
/*if (CurrentSeries != null && CurrentSeries.AnimeSeriesID.HasValue && CurrentSeries.AnimeSeriesID.Value == epcontract.AnimeSeriesID)
{
foreach (VM_AnimeEpisode_User ep in CurrentSeries.AllEpisodes)
{
if (ep.AnimeEpisodeID == epcontract.AnimeEpisodeID)
{
ep.Populate(epcontract);
// update the attached videos if they are visible