@@ -20,6 +20,7 @@ public class BibleSelectionViewModel : ObservableObject, IListViewModel
2020{
2121 private readonly MediaService _mediaService ;
2222 private readonly IServiceScopeFactory _scopeFactory ;
23+ private readonly INavigationService _navigationService ;
2324 private readonly IDispatcher _dispatcher ;
2425 private readonly IState < ApplicationState > _state ;
2526
@@ -32,28 +33,15 @@ public class BibleSelectionViewModel : ObservableObject, IListViewModel
3233 public ICommand CloseModalCommand { get ; set ; }
3334 public ICommand SelectLanguageCommand { get ; set ; }
3435
35- public BibleSelectionViewModel ( MediaService mediaService , IServiceScopeFactory scopeFactory )
36+ public BibleSelectionViewModel ( MediaService mediaService , IServiceScopeFactory scopeFactory , INavigationService navigationService )
3637 {
3738 _mediaService = mediaService ;
3839 _scopeFactory = scopeFactory ;
40+ _navigationService = navigationService ;
3941 _state = MauiAppHolder . Services . GetRequiredService < IState < ApplicationState > > ( ) ;
4042 _dispatcher = MauiAppHolder . Services . GetRequiredService < IDispatcher > ( ) ;
4143
42- EventHandler onBibleReadingInitialized = null ;
43- onBibleReadingInitialized = ( _ , _ ) =>
44- {
45- var stateValue = _state . Value ;
46- if ( stateValue . CurrentBibleReadingSchedule == null || stateValue . TentativeBibleReadingSchedule == null ) return ;
47- _current = stateValue . CurrentBibleReadingSchedule ;
48- _tentative = stateValue . TentativeBibleReadingSchedule ;
49- Task . Run ( async ( ) =>
50- {
51- await Initialize ( _tentative . LanguageCode ) ;
52- await MainThread . InvokeOnMainThreadAsync ( ( ) => IsBusy = false ) ;
53- } ) ;
54- _state . StateChanged -= onBibleReadingInitialized ;
55- } ;
56- _state . StateChanged += onBibleReadingInitialized ;
44+ _state . StateChanged += OnBibleReadingInitialized ;
5745
5846 BibleReadingSchedule lastCurrent = null ;
5947 BibleReadingSchedule lastTentative = null ;
@@ -63,29 +51,19 @@ public BibleSelectionViewModel(MediaService mediaService, IServiceScopeFactory s
6351 BookSelectionCommand = new AsyncRelayCommand < PublicationListViewItemModel > ( async x =>
6452 {
6553 IsBusy = true ;
54+ await _navigationService . NavigateToBookSelectionAsync ( ) ;
6655 _dispatcher . Dispatch ( new BookSelectionAction ( new BibleReadingSchedule
6756 {
6857 PublicationCode = x . Code ,
6958 LanguageCode = CurrentLanguage . Code
7059 } ) ) ;
71- using var scope = _scopeFactory . CreateScope ( ) ;
72- var navigation = scope . ServiceProvider . GetRequiredService < INavigation > ( ) ;
73- var viewModel = scope . ServiceProvider . GetRequiredService < BookSelectionViewModel > ( ) ;
74- var page = scope . ServiceProvider . GetRequiredService < BookSelection > ( ) ;
75- page . BindingContext = viewModel ;
76- await navigation . PushAsync ( page ) ;
77-
7860 IsBusy = false ;
7961 } ) ;
8062
8163 OpenModalCommand = new AsyncRelayCommand ( async ( ) =>
8264 {
8365 IsBusy = true ;
84- using var scope = _scopeFactory . CreateScope ( ) ;
85- var navigation = scope . ServiceProvider . GetRequiredService < INavigation > ( ) ;
86- var modal = scope . ServiceProvider . GetRequiredService < LanguageModal > ( ) ;
87- modal . BindingContext = this ;
88- await navigation . PushModalAsync ( modal ) ;
66+ await _navigationService . OpenLanguageModalAsync ( this ) ;
8967 IsBusy = false ;
9068 } ) ;
9169
@@ -101,13 +79,7 @@ public BibleSelectionViewModel(MediaService mediaService, IServiceScopeFactory s
10179 CloseModalCommand = new AsyncRelayCommand ( async ( ) =>
10280 {
10381 IsBusy = true ;
104- using var scope = _scopeFactory . CreateScope ( ) ;
105- var navigation = scope . ServiceProvider . GetRequiredService < INavigation > ( ) ;
106- if ( navigation . ModalStack . Count > 0 )
107- {
108- var modal = await navigation . PopModalAsync ( ) ;
109- if ( modal . BindingContext is IDisposable disposable ) disposable . Dispose ( ) ;
110- }
82+ await _navigationService . CloseModalAsync ( ) ;
11183 IsBusy = false ;
11284 } ) ;
11385
@@ -119,17 +91,26 @@ public BibleSelectionViewModel(MediaService mediaService, IServiceScopeFactory s
11991 CurrentLanguage = x ;
12092 CurrentLanguage . IsSelected = true ;
12193
122- using var scope = _scopeFactory . CreateScope ( ) ;
123- var navigation = scope . ServiceProvider . GetRequiredService < INavigation > ( ) ;
124- if ( navigation . ModalStack . Count > 0 )
125- {
126- var modal = await navigation . PopModalAsync ( ) ;
127- if ( modal . BindingContext is IDisposable disposable ) disposable . Dispose ( ) ;
128- }
94+ await _navigationService . CloseModalAsync ( ) ;
12995 await PopulateTranslations ( x . Code ) ;
13096
13197 IsBusy = false ;
13298 } ) ;
99+ return ;
100+
101+ void OnBibleReadingInitialized ( object o , EventArgs eventArgs )
102+ {
103+ var stateValue = _state . Value ;
104+ if ( stateValue . CurrentBibleReadingSchedule == null || stateValue . TentativeBibleReadingSchedule == null ) return ;
105+ _current = stateValue . CurrentBibleReadingSchedule ;
106+ _tentative = stateValue . TentativeBibleReadingSchedule ;
107+ Task . Run ( async ( ) =>
108+ {
109+ await Initialize ( _tentative . LanguageCode ) ;
110+ await MainThread . InvokeOnMainThreadAsync ( ( ) => IsBusy = false ) ;
111+ } ) ;
112+ _state . StateChanged -= OnBibleReadingInitialized ;
113+ }
133114
134115 void OnBibleReadingChanged ( object sender , EventArgs e )
135116 {
0 commit comments