Skip to content

Commit 6ba5574

Browse files
committed
Fix state not working
1 parent 24690d2 commit 6ba5574

File tree

8 files changed

+35
-38
lines changed

8 files changed

+35
-38
lines changed

src/Bible.Alarm/ViewModels/Bible/BibleSelectionViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ public class BibleSelectionViewModel : ObservableObject, IListViewModel
3232
public ICommand CloseModalCommand { get; set; }
3333
public ICommand SelectLanguageCommand { get; set; }
3434

35-
public BibleSelectionViewModel(MediaService mediaService, IServiceScopeFactory scopeFactory, IDispatcher dispatcher)
35+
public BibleSelectionViewModel(MediaService mediaService, IServiceScopeFactory scopeFactory)
3636
{
3737
_mediaService = mediaService;
3838
_scopeFactory = scopeFactory;
39-
_dispatcher = dispatcher;
40-
4139
_state = MauiAppHolder.Services.GetRequiredService<IState<ApplicationState>>();
40+
_dispatcher = MauiAppHolder.Services.GetRequiredService<IDispatcher>();
4241

4342
EventHandler onBibleReadingInitialized = null;
4443
onBibleReadingInitialized = (_, _) =>

src/Bible.Alarm/ViewModels/Bible/BookSelectionViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ public class BookSelectionViewModel : ObservableObject, IDisposable
2929
public ICommand BackCommand { get; set; }
3030
public ICommand ChapterSelectionCommand { get; set; }
3131

32-
public BookSelectionViewModel(MediaService mediaService, IServiceScopeFactory scopeFactory, IDispatcher dispatcher)
32+
public BookSelectionViewModel(MediaService mediaService, IServiceScopeFactory scopeFactory)
3333
{
3434
_mediaService = mediaService;
3535
_scopeFactory = scopeFactory;
36-
_dispatcher = dispatcher;
37-
3836
_state = MauiAppHolder.Services.GetRequiredService<IState<ApplicationState>>();
37+
_dispatcher = MauiAppHolder.Services.GetRequiredService<IDispatcher>();
3938

4039
BackCommand = new AsyncRelayCommand(async () =>
4140
{

src/Bible.Alarm/ViewModels/Bible/ChapterSelectionViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class ChapterSelectionViewModel : ObservableObject, IDisposable
2929
private readonly IMediaCacheService _cacheService;
3030
private readonly IDownloadService _downloadService;
3131
private readonly IState<ApplicationState> _state;
32+
private readonly IDispatcher _dispatcher;
3233

3334
private readonly Dictionary<BibleChapterListViewItemModel, PropertyChangedEventHandler> _propertyChangedHandlers = [];
3435

@@ -39,8 +40,7 @@ public ChapterSelectionViewModel(
3940
IPreviewPlayService playService,
4041
INavigation navigation,
4142
IDownloadService downloadService,
42-
IMediaCacheService cacheService,
43-
IDispatcher dispatcher)
43+
IMediaCacheService cacheService)
4444
{
4545
_logger = logger;
4646
_mediaService = mediaService;
@@ -50,8 +50,8 @@ public ChapterSelectionViewModel(
5050
_downloadService = downloadService;
5151
_cacheService = cacheService;
5252

53-
5453
_state = MauiAppHolder.Services.GetRequiredService<IState<ApplicationState>>();
54+
_dispatcher = MauiAppHolder.Services.GetRequiredService<IDispatcher>();
5555

5656
BackCommand = new AsyncRelayCommand(async () =>
5757
{

src/Bible.Alarm/ViewModels/HomeViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Windows.Input;
2+
using Bible.Alarm.Common;
23
using Bible.Alarm.Database;
34
using Bible.Alarm.Models.Schedule;
45
using Bible.Alarm.Services.Database;
@@ -36,16 +37,15 @@ public HomeViewModel(
3637
INavigation navigation,
3738
IServiceScopeFactory scopeFactory,
3839
Func<AlarmSchedule, ScheduleListItem> scheduleListItemFactory,
39-
IDispatcher dispatcher,
4040
IState<ApplicationState> state,
4141
IDatabaseSeedService databaseSeedService,
4242
IScheduleMigrationService scheduleMigrationService)
4343
{
4444
_logger = logger;
4545
_scopeFactory = scopeFactory;
4646
_scheduleListItemFactory = scheduleListItemFactory;
47-
_dispatcher = dispatcher;
4847
_state = state;
48+
_dispatcher = MauiAppHolder.Services.GetRequiredService<IDispatcher>();
4949
_databaseSeedService = databaseSeedService;
5050
_scheduleMigrationService = scheduleMigrationService;
5151

src/Bible.Alarm/ViewModels/Music/MusicSelectionViewModel.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,13 @@ public class MusicSelectionViewModel : ObservableObject
2121
private readonly IDispatcher _dispatcher;
2222
private readonly IState<ApplicationState> _state;
2323

24-
public MusicSelectionViewModel(IServiceScopeFactory scopeFactory, IDispatcher dispatcher)
24+
public MusicSelectionViewModel(IServiceScopeFactory scopeFactory)
2525
{
2626
_scopeFactory = scopeFactory;
27-
_dispatcher = dispatcher;
28-
2927
_state = MauiAppHolder.Services.GetRequiredService<IState<ApplicationState>>();
28+
_dispatcher = MauiAppHolder.Services.GetRequiredService<IDispatcher>();
3029

31-
_state.StateChanged += (_, _) =>
32-
{
33-
var stateValue = _state.Value;
34-
if (stateValue.CurrentMusic == null) return;
35-
_current = stateValue.CurrentMusic;
36-
MainThread.BeginInvokeOnMainThread(() =>
37-
{
38-
SetSelectedMusicType();
39-
IsBusy = false;
40-
});
41-
};
30+
_state.StateChanged += OnStateOnStateChanged;
4231

4332
SongBookSelectionCommand = new AsyncRelayCommand<MusicTypeListItemViewModel>(async x =>
4433
{
@@ -86,6 +75,19 @@ public MusicSelectionViewModel(IServiceScopeFactory scopeFactory, IDispatcher di
8675
await navigation.PopAsync();
8776
IsBusy = false;
8877
});
78+
return;
79+
80+
void OnStateOnStateChanged(object o, EventArgs eventArgs)
81+
{
82+
var stateValue = _state.Value;
83+
if (stateValue.CurrentMusic == null) return;
84+
_current = stateValue.CurrentMusic;
85+
MainThread.BeginInvokeOnMainThread(() =>
86+
{
87+
SetSelectedMusicType();
88+
IsBusy = false;
89+
});
90+
}
8991
}
9092

9193
private void SetSelectedMusicType()

src/Bible.Alarm/ViewModels/Music/SongBookSelectionViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ public class SongBookSelectionViewModel : ObservableObject, IListViewModel
2727
private AlarmMusic _current;
2828
private AlarmMusic _tentative;
2929

30-
public SongBookSelectionViewModel(MediaService mediaService, IServiceScopeFactory scopeFactory, IDispatcher dispatcher)
30+
public SongBookSelectionViewModel(MediaService mediaService, IServiceScopeFactory scopeFactory)
3131
{
3232
_mediaService = mediaService;
3333
_scopeFactory = scopeFactory;
34-
_dispatcher = dispatcher;
35-
3634
_state = MauiAppHolder.Services.GetRequiredService<IState<ApplicationState>>();
35+
_dispatcher = MauiAppHolder.Services.GetRequiredService<IDispatcher>();
3736

3837

3938
EventHandler onMusicInitialized = null;

src/Bible.Alarm/ViewModels/Music/TrackSelectionViewModel.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ public TrackSelectionViewModel(
4444
IPreviewPlayService playService,
4545
INavigation navigation,
4646
IDownloadService downloadService,
47-
IMediaCacheService cacheService,
48-
IDispatcher dispatcher)
47+
IMediaCacheService cacheService)
4948
{
5049
_logger = logger;
5150
_mediaService = mediaService;
@@ -54,11 +53,11 @@ public TrackSelectionViewModel(
5453
_navigation = navigation;
5554
_downloadService = downloadService;
5655
_cacheService = cacheService;
57-
_dispatcher = dispatcher;
5856

59-
// Resolve IState<T> from ROOT container (singleton) to ensure we get the same instance
57+
// Resolve IState<T> and IDispatcher from ROOT container (singleton) to ensure we get the same instance
6058
// that Fluxor uses, not a scoped instance
6159
_state = MauiAppHolder.Services.GetRequiredService<IState<ApplicationState>>();
60+
_dispatcher = MauiAppHolder.Services.GetRequiredService<IDispatcher>();
6261

6362
_subscriptions.Add(_mediaService);
6463

src/Bible.Alarm/ViewModels/ScheduleViewModel.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public ScheduleViewModel(
5555
IPlaybackService playbackService,
5656
INotificationService notificationService,
5757
IServiceScopeFactory scopeFactory,
58-
IDispatcher dispatcher,
5958
ISchedulePersistenceService schedulePersistenceService,
6059
IBibleNavigationService bibleNavigationService,
6160
IMediaCacheSetupService mediaCacheSetupService,
@@ -70,6 +69,7 @@ public ScheduleViewModel(
7069
_scopeFactory = scopeFactory;
7170

7271
_state = MauiAppHolder.Services.GetRequiredService<IState<ApplicationState>>();
72+
_dispatcher = MauiAppHolder.Services.GetRequiredService<IDispatcher>();
7373

7474
_schedulePersistenceService = schedulePersistenceService;
7575
_bibleNavigationService = bibleNavigationService;
@@ -78,7 +78,6 @@ public ScheduleViewModel(
7878
_scheduleSelectionService = scheduleSelectionService;
7979
_scheduleDisplayService = scheduleDisplayService;
8080
_serviceProvider = serviceProvider;
81-
_dispatcher = dispatcher;
8281

8382
var lastScheduleId = -1;
8483
var modelInitialized = false;
@@ -162,10 +161,10 @@ public ScheduleViewModel(
162161
{
163162
IsBusy = true;
164163

165-
await _navigationService.NavigateToMusicSelectionAsync();
166-
167164
Music = await _scheduleSelectionService.LoadMusicForSelectionAsync(_scheduleId, IsNewSchedule, _musicUpdated, Music);
168165

166+
await _navigationService.NavigateToMusicSelectionAsync();
167+
169168
_dispatcher.Dispatch(new MusicSelectionAction(Music));
170169

171170
IsBusy = false;
@@ -175,8 +174,6 @@ public ScheduleViewModel(
175174
{
176175
IsBusy = true;
177176

178-
await _navigationService.NavigateToBibleSelectionAsync();
179-
180177
BibleReadingSchedule = await _scheduleSelectionService.LoadBibleReadingForSelectionAsync(
181178
_scheduleId, IsNewSchedule, _bibleReadingUpdated, BibleReadingSchedule);
182179

@@ -185,6 +182,8 @@ public ScheduleViewModel(
185182
RefreshChapterName();
186183
}
187184

185+
await _navigationService.NavigateToBibleSelectionAsync();
186+
188187
_dispatcher.Dispatch(new BibleSelectionAction(
189188
BibleReadingSchedule,
190189
new BibleReadingSchedule

0 commit comments

Comments
 (0)