Skip to content

Commit a820524

Browse files
committed
Use factory pattern for schedule list items
1 parent a7ee728 commit a820524

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

src/Bible.Alarm/MauiProgram.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
using Bible.Alarm.Views.Shared;
3232
using Bible.Alarm.ViewModels.Bible;
3333
using Bible.Alarm.ViewModels.Music;
34+
using Bible.Alarm.Models.Schedule;
3435
using Microsoft.EntityFrameworkCore;
3536
#if IOS
3637
using Bible.Alarm.Platforms.iOS.Services.Storage;
@@ -224,6 +225,18 @@ private static void RegisterViewModels(IServiceCollection services)
224225
services.AddTransient<ChapterSelectionViewModel>();
225226
services.AddTransient<AlarmViewModal>();
226227
services.AddSingleton<MediaProgressViewModal>();
228+
229+
// Register ScheduleListItem as transient for list items
230+
services.AddTransient<ScheduleListItem>();
231+
232+
// Register factory for ScheduleListItem (takes AlarmSchedule and returns ScheduleListItem with DI)
233+
services.AddTransient<Func<AlarmSchedule, ScheduleListItem>>(serviceProvider =>
234+
schedule =>
235+
{
236+
var vm = serviceProvider.GetRequiredService<ScheduleListItem>();
237+
vm.Initialize(schedule);
238+
return vm;
239+
});
227240
}
228241

229242
private static void RegisterUiComponents(IServiceCollection services)

src/Bible.Alarm/ViewModels/HomeViewModel.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class HomeViewModel : ObservableObject, IDisposable, IRecipient<Initializ
3939
// Map AlarmSchedule IDs to ScheduleListItem ViewModels for UI binding
4040
private readonly Dictionary<long, ScheduleListItem> _scheduleViewModels = [];
4141

42+
private readonly Func<AlarmSchedule, ScheduleListItem> _scheduleListItemFactory;
4243

4344
private readonly Fluxor.IDispatcher _dispatcher;
4445
private readonly IState<ApplicationState> _state;
@@ -53,6 +54,7 @@ public HomeViewModel(
5354
IAlarmService alarmService,
5455
INotificationService notificationService,
5556
IServiceScopeFactory scopeFactory,
57+
Func<AlarmSchedule, ScheduleListItem> scheduleListItemFactory,
5658
Fluxor.IDispatcher dispatcher,
5759
IState<ApplicationState> state)
5860
{
@@ -62,6 +64,7 @@ public HomeViewModel(
6264
_alarmService = alarmService;
6365
_notificationService = notificationService;
6466
_scopeFactory = scopeFactory;
67+
_scheduleListItemFactory = scheduleListItemFactory;
6568
_dispatcher = dispatcher;
6669
_state = state;
6770

@@ -218,9 +221,6 @@ public ObservableHashSet<ScheduleListItem> Schedules
218221

219222
private void UpdateScheduleViewModels(ObservableHashSet<AlarmSchedule> schedules)
220223
{
221-
using var scope = _scopeFactory.CreateScope();
222-
var logger = scope.ServiceProvider.GetRequiredService<ILogger>();
223-
224224
var newViewModels = new ObservableHashSet<ScheduleListItem>();
225225
var currentViewModelIds = new HashSet<long>();
226226

@@ -232,14 +232,14 @@ private void UpdateScheduleViewModels(ObservableHashSet<AlarmSchedule> schedules
232232
if (_scheduleViewModels.TryGetValue(schedule.Id, out var existingViewModel))
233233
{
234234
// Update existing ViewModel's Schedule property
235-
existingViewModel.Schedule = schedule;
235+
existingViewModel.Initialize(schedule);
236236
existingViewModel.IsEnabled = schedule.IsEnabled;
237237
newViewModels.Add(existingViewModel);
238238
}
239239
else
240240
{
241-
// Create new ViewModel
242-
var viewModel = new ScheduleListItem(schedule, logger, _scopeFactory);
241+
// Create new ViewModel using factory with full DI
242+
var viewModel = _scheduleListItemFactory(schedule);
243243
_scheduleViewModels[schedule.Id] = viewModel;
244244
newViewModels.Add(viewModel);
245245
}

src/Bible.Alarm/ViewModels/ScheduleListItem.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,20 @@ public class ScheduleListItem : ObservableObject, IComparable, IDisposable, IRec
1919
private readonly ILogger _logger;
2020
private readonly IServiceScopeFactory _scopeFactory;
2121

22-
public AlarmSchedule Schedule;
22+
public AlarmSchedule Schedule { get; private set; }
2323

24-
public ScheduleListItem(AlarmSchedule schedule, ILogger logger, IServiceScopeFactory scopeFactory)
24+
// Constructor for DI - Initialize() must be called after construction
25+
public ScheduleListItem(ILogger logger, IServiceScopeFactory scopeFactory)
2526
{
2627
_logger = logger;
2728
_scopeFactory = scopeFactory;
29+
30+
// Commands will be initialized in Initialize() method
31+
}
32+
33+
// Initialize method to set the schedule data
34+
public void Initialize(AlarmSchedule schedule)
35+
{
2836
Schedule = schedule;
2937
_isEnabled = schedule.IsEnabled;
3038

@@ -66,7 +74,7 @@ await toastService.ShowMessage("Error. Network may not be available." +
6674
if (!await CanMove()) return;
6775
using var scope = _scopeFactory.CreateScope();
6876
using var playlistService = scope.ServiceProvider.GetRequiredService<IPlaylistService>();
69-
await playlistService.MoveToPreviousBibleChapter(schedule.Id);
77+
await playlistService.MoveToPreviousBibleChapter(Schedule.Id);
7078

7179
RefreshChapterName(true);
7280
});
@@ -77,7 +85,7 @@ await toastService.ShowMessage("Error. Network may not be available." +
7785

7886
using var scope = _scopeFactory.CreateScope();
7987
using var playlistService = scope.ServiceProvider.GetRequiredService<IPlaylistService>();
80-
await playlistService.MoveToNextBibleChapter(schedule.Id);
88+
await playlistService.MoveToNextBibleChapter(Schedule.Id);
8189

8290
RefreshChapterName(true);
8391
});

0 commit comments

Comments
 (0)