33using Elva . Common . Navigation ;
44using Elva . Pages . Shared . Models ;
55using Elva . Pages . Shared . ViewModels ;
6+ using Elva . Services . Database ;
67using Microsoft . Extensions . DependencyInjection ;
78using System ;
89using System . Collections . ObjectModel ;
10+ using System . Diagnostics ;
911using System . Linq ;
12+ using System . Threading . Tasks ;
13+ using System . Windows ;
1014using WebsiteScraper . Downloadable . Books ;
1115
1216namespace Elva . Pages . Home . ViewModels
1317{
1418 internal partial class HomeVM : ViewModelObject
1519 {
1620 [ ObservableProperty ]
17- private ObservableCollection < HomeWebsiteVM > _websites = null ! ;
21+ private ObservableCollection < HomeWebsiteVM > _websites = new ( ) ;
22+
1823 [ ObservableProperty ]
19- private ObservableCollection < ComicVM > ? _favorites = null ;
24+ private ObservableCollection < ComicVM > ? _favorites = new ( ) ;
25+
2026 [ ObservableProperty ]
2127 private bool _favoritesVisible = false ;
22- private WebsiteManager _websiteManagaer ;
23- private FavoriteManager _favoriteManager ;
2428
25- public ObservableCollection < ComicVM > ? Favorites1 { get => _favorites ; set => _favorites = value ; }
29+ [ ObservableProperty ]
30+ private bool _isLoading = true ;
31+
32+ private readonly WebsiteManager _websiteManager ;
33+ private readonly FavoriteManager _favoriteManager ;
34+ private readonly ComicDatabaseManager _databaseManager ;
35+ private readonly SettingsManager _settingsManager ;
36+
37+ public ObservableCollection < ComicVM > ? Favorites1 { get => Favorites ; set => Favorites = value ; }
2638
2739 public HomeVM ( INavigationService navigation ) : base ( navigation )
2840 {
29- _websiteManagaer = App . Current . ServiceProvider . GetRequiredService < WebsiteManager > ( ) ;
41+ _websiteManager = App . Current . ServiceProvider . GetRequiredService < WebsiteManager > ( ) ;
3042 _favoriteManager = App . Current . ServiceProvider . GetRequiredService < FavoriteManager > ( ) ;
43+ _databaseManager = App . Current . ServiceProvider . GetRequiredService < ComicDatabaseManager > ( ) ;
44+ _settingsManager = App . Current . ServiceProvider . GetRequiredService < SettingsManager > ( ) ;
45+
3146 _favoriteManager . FavoriteChanged += FavoriteChanged ;
32- SetFavoriteCollection ( ) ;
33- SetWebsiteCollection ( ) ;
34- _websiteManagaer . WebsiteAdded += ( s , e ) => SetWebsiteCollection ( ) ;
47+ _websiteManager . WebsiteAdded += WebsiteManagerOnWebsiteAdded ;
48+ InitializeAsync ( ) ;
49+ }
50+
51+ private void WebsiteManagerOnWebsiteAdded ( object ? sender , EventArgs e ) => Application . Current . Dispatcher . Invoke ( SetWebsiteCollection ) ;
52+
53+ private async void InitializeAsync ( )
54+ {
55+ try
56+ {
57+ await _settingsManager . SettingsLoadedTask ;
58+ Application . Current . Dispatcher . Invoke ( ( ) =>
59+ {
60+ try
61+ {
62+ SetWebsiteCollection ( ) ;
63+ }
64+ catch ( Exception ex )
65+ {
66+ Debug . WriteLine ( $ "Error setting website collection: { ex . Message } ") ;
67+ }
68+ } ) ;
69+ await Task . Run ( LoadFavorites ) ;
70+ _databaseManager . RegisterForFullLoad ( ( ) =>
71+ {
72+ Application . Current . Dispatcher . Invoke ( ( ) =>
73+ {
74+ try
75+ {
76+ RefreshWebsiteData ( ) ;
77+ IsLoading = false ;
78+ }
79+ catch ( Exception ex )
80+ {
81+ Debug . WriteLine ( $ "Error refreshing website data: { ex . Message } ") ;
82+ IsLoading = false ;
83+ }
84+ } ) ;
85+ } ) ;
86+ }
87+ catch ( Exception ex )
88+ {
89+ Debug . WriteLine ( $ "Error in InitializeAsync: { ex . Message } ") ;
90+ Application . Current . Dispatcher . Invoke ( ( ) => IsLoading = false ) ;
91+ }
3592 }
3693
37- private void FavoriteChanged ( object ? sender , string [ ] e ) => SetFavoriteCollection ( ) ;
94+ private void FavoriteChanged ( object ? sender , string [ ] e ) => LoadFavorites ( ) ;
3895
39- private void SetFavoriteCollection ( )
96+ private void LoadFavorites ( )
4097 {
41- string [ ] favorites = ResizeArray ( _favoriteManager . Favorites . AsEnumerable ( ) . Reverse ( ) . ToArray ( ) , 9 ) ;
42- if ( favorites . Length == Favorites ? . Count && favorites . All ( x => Favorites . Select ( y => y . Url ) . Contains ( x ) ) )
43- return ;
98+ try
99+ {
100+ string [ ] favorites = ResizeArray ( _favoriteManager . Favorites . AsEnumerable ( ) . Reverse ( ) . ToArray ( ) , 9 ) ;
101+ bool needsUpdate = Favorites == null ||
102+ favorites . Length != Favorites . Count ||
103+ ! favorites . All ( x => Favorites . Select ( y => y . Url ) . Contains ( x ) ) ;
104+
105+ if ( ! needsUpdate )
106+ return ;
107+ Application . Current . Dispatcher . Invoke ( ( ) =>
108+ {
109+ try
110+ {
111+ ObservableCollection < ComicVM > newFavorites = new ( ) ;
112+
113+ foreach ( string url in favorites )
114+ {
115+ WebsiteScraper . WebsiteUtilities . Website ? website = _websiteManager . GetWebsite ( new Uri ( url ) . Host ) ;
116+ if ( website != null )
117+ {
118+ Comic comic = new ( url , url , website ) ;
119+ ComicVM comicVM = new ( comic ) ;
120+ if ( ! string . IsNullOrEmpty ( comicVM . Website ) )
121+ {
122+ newFavorites . Add ( comicVM ) ;
123+ }
124+ }
125+ }
44126
45- Favorites = new ObservableCollection < ComicVM > ( favorites . Select ( x => new ComicVM ( new Comic ( x , x , _websiteManagaer . GetWebsite ( new Uri ( x ) . Host ) ?? new ( ) ) ) ) . Where ( x => ! string . IsNullOrEmpty ( x . Website ) ) ) ;
46- FavoritesVisible = Favorites . Any ( ) ;
127+ Favorites = newFavorites ;
128+ FavoritesVisible = Favorites . Any ( ) ;
129+ }
130+ catch ( Exception ex )
131+ {
132+ Debug . WriteLine ( $ "Error creating favorite ComicVMs: { ex . Message } ") ;
133+ }
134+ } ) ;
135+ }
136+ catch ( Exception ex )
137+ {
138+ Debug . WriteLine ( $ "Error in LoadFavorites: { ex . Message } ") ;
139+ }
47140 }
48141
49- private string [ ] ResizeArray ( string [ ] array , int length )
142+ private static string [ ] ResizeArray ( string [ ] array , int length )
50143 {
51144 if ( array . Length > length )
52145 Array . Resize ( ref array , length ) ;
53146 return array ;
54147 }
55148
56- private void SetWebsiteCollection ( ) => Websites = new ObservableCollection < HomeWebsiteVM > ( _websiteManagaer . Websites . Select ( w => new HomeWebsiteVM ( w ) ) ) ;
149+ private void SetWebsiteCollection ( )
150+ {
151+ try
152+ {
153+ ObservableCollection < HomeWebsiteVM > newWebsites = [ ] ;
154+ foreach ( WebsiteScraper . WebsiteUtilities . Website website in _websiteManager . Websites )
155+ newWebsites . Add ( new HomeWebsiteVM ( website ) ) ;
156+ Websites = newWebsites ;
157+ }
158+ catch ( Exception ex )
159+ {
160+ Debug . WriteLine ( $ "Error in SetWebsiteCollection: { ex . Message } ") ;
161+ }
162+ }
163+
164+ private void RefreshWebsiteData ( )
165+ {
166+ try
167+ {
168+ foreach ( HomeWebsiteVM websiteVM in Websites )
169+ websiteVM . RefreshData ( ) ;
170+
171+ }
172+ catch ( Exception ex )
173+ {
174+ Debug . WriteLine ( $ "Error in RefreshWebsiteData: { ex . Message } ") ;
175+ }
176+ }
57177 }
58- }
178+ }
0 commit comments