Skip to content

Commit fb504de

Browse files
committed
Refactor schedule view model
1 parent ccd7985 commit fb504de

21 files changed

+723
-313
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Bible.Alarm.Common.Interfaces.Battery;
2+
3+
public interface IBatteryOptimizationService
4+
{
5+
Task MarkModalAsShownAsync();
6+
Task<bool> ShouldShowModalAsync();
7+
void ShowOptimizationSettingsPage();
8+
bool CanShowOptimizeActivity();
9+
}
10+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Bible.Alarm.Models.Schedule;
2+
3+
namespace Bible.Alarm.Common.Interfaces.Media;
4+
5+
public interface IBibleNavigationService
6+
{
7+
Task<bool> MoveToPreviousBookAsync(BibleReadingSchedule schedule);
8+
Task<bool> MoveToNextBookAsync(BibleReadingSchedule schedule);
9+
Task<bool> MoveToPreviousChapterAsync(BibleReadingSchedule schedule);
10+
Task<bool> MoveToNextChapterAsync(BibleReadingSchedule schedule);
11+
}
12+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Bible.Alarm.Common.Interfaces.Media;
2+
3+
public interface IMediaCacheSetupService
4+
{
5+
Task SetupAlarmCacheAsync(long scheduleId);
6+
}
7+
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using System.Threading.Tasks;
1+
using Bible.Alarm.Models.Schedule;
22

33
namespace Bible.Alarm.Common.Interfaces.Media;
44

55
public interface IScheduleDisplayService
66
{
77
Task<string> GetChapterDisplayNameAsync(long scheduleId, bool force = false);
8+
Task<string> GetChapterDisplayNameForBibleReadingAsync(long scheduleId, BibleReadingSchedule bibleReadingSchedule, bool force = false);
89
}
910

src/Bible.Alarm/Common/Interfaces/Media/ISchedulePlaybackService.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Threading.Tasks;
2-
31
namespace Bible.Alarm.Common.Interfaces.Media;
42

53
public interface ISchedulePlaybackService
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Bible.Alarm.Models.Schedule;
2+
3+
namespace Bible.Alarm.Common.Interfaces.Scheduler;
4+
5+
public interface ISchedulePersistenceService
6+
{
7+
Task<bool> SaveScheduleAsync(AlarmSchedule schedule, bool isNewSchedule, bool musicUpdated = true, bool bibleReadingUpdated = true);
8+
Task DeleteScheduleAsync(long scheduleId);
9+
Task<AlarmSchedule> GetSampleScheduleAsync();
10+
}
11+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Bible.Alarm.Models.Schedule;
2+
3+
namespace Bible.Alarm.Common.Interfaces.Scheduler;
4+
5+
public interface IScheduleSelectionService
6+
{
7+
Task<AlarmMusic> LoadMusicForSelectionAsync(long scheduleId, bool isNewSchedule, bool musicUpdated, AlarmMusic currentMusic);
8+
Task<BibleReadingSchedule> LoadBibleReadingForSelectionAsync(long scheduleId, bool isNewSchedule, bool bibleReadingUpdated, BibleReadingSchedule currentBibleReading);
9+
}
10+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Bible.Alarm.Common.Interfaces.UI;
2+
3+
public interface INavigationService
4+
{
5+
Task NavigateToMusicSelectionAsync();
6+
Task NavigateToBibleSelectionAsync();
7+
Task OpenNumberOfChaptersModalAsync(object bindingContext);
8+
Task CloseModalAsync();
9+
}
10+

src/Bible.Alarm/MauiProgram.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// using Bible.Alarm.Views.Shared; // Shared is a folder, not a namespace
33

44
using System.Diagnostics;
5+
using Bible.Alarm.Common.Interfaces.Battery;
56
using Bible.Alarm.Common.Interfaces.Media;
67
using Bible.Alarm.Common.Interfaces.Network;
78
using Bible.Alarm.Common.Interfaces.Platform;
@@ -10,12 +11,14 @@
1011
using Bible.Alarm.Common.Interfaces.UI;
1112
using Bible.Alarm.Database;
1213
using Bible.Alarm.Models.Schedule;
14+
using Bible.Alarm.Services.Battery;
1315
using Bible.Alarm.Services.Database;
1416
using Bible.Alarm.Services.Media;
1517
using Bible.Alarm.Services.Media.Interfaces;
1618
using Bible.Alarm.Services.Network;
1719
using Bible.Alarm.Services.Scheduler;
1820
using Bible.Alarm.Services.Scheduler.Interfaces;
21+
using Bible.Alarm.Services.UI;
1922
using Bible.Alarm.Shared.Constants;
2023
using Bible.Alarm.Shared.Database;
2124
using Bible.Alarm.Stores;
@@ -135,6 +138,11 @@ private static void RegisterCommonServices(IServiceCollection services)
135138
services.AddSingleton<IScheduleStateService, ScheduleStateService>();
136139
services.AddSingleton<ISchedulePlaybackService, SchedulePlaybackService>();
137140
services.AddSingleton<IScheduleDisplayService, ScheduleDisplayService>();
141+
services.AddSingleton<ISchedulePersistenceService, SchedulePersistenceService>();
142+
services.AddSingleton<IBibleNavigationService, BibleNavigationService>();
143+
services.AddSingleton<IMediaCacheSetupService, MediaCacheSetupService>();
144+
services.AddSingleton<INavigationService, NavigationService>();
145+
services.AddSingleton<IScheduleSelectionService, ScheduleSelectionService>();
138146
services.AddSingleton<INetworkStatusService, NetworkStatusService>();
139147
services.AddSingleton<IMediaElementAudioService, MediaElementAudioService>();
140148
services.AddSingleton<IPlaybackService, PlaybackService>();
@@ -156,6 +164,7 @@ private static void RegisterCommonServices(IServiceCollection services)
156164
#if ANDROID
157165
services.AddSingleton<INotificationService, DroidNotificationService>();
158166
services.AddSingleton<IToastService, DroidToastService>();
167+
services.AddSingleton<IBatteryOptimizationService, BatteryOptimizationService>();
159168
services.AddSingleton<IAndroidAlarmHandler, AndroidAlarmHandler>();
160169
services.AddSingleton<IStorageService, AndroidStorageService>();
161170
services.AddSingleton<IBatteryOptimizationManager, BatteryOptimizationManager>();
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Bible.Alarm.Common.Interfaces.Battery;
2+
using Bible.Alarm.Database;
3+
using Bible.Alarm.Models;
4+
using Microsoft.EntityFrameworkCore;
5+
using Serilog;
6+
7+
namespace Bible.Alarm.Services.Battery;
8+
9+
public class BatteryOptimizationService(
10+
ILogger logger,
11+
IServiceScopeFactory scopeFactory,
12+
IBatteryOptimizationManager batteryOptimizationManager)
13+
: IBatteryOptimizationService
14+
{
15+
private readonly ILogger _logger = logger;
16+
private readonly IServiceScopeFactory _scopeFactory = scopeFactory;
17+
private readonly IBatteryOptimizationManager _batteryOptimizationManager = batteryOptimizationManager;
18+
19+
public async Task MarkModalAsShownAsync()
20+
{
21+
try
22+
{
23+
using var scope = _scopeFactory.CreateScope();
24+
var scheduleDbContext = scope.ServiceProvider.GetRequiredService<ScheduleDbContext>();
25+
26+
if (!await scheduleDbContext.GeneralSettings.AnyAsync(x =>
27+
x.Key == "AndroidBatteryOptimizationExclusionPromptShown"))
28+
{
29+
await scheduleDbContext.GeneralSettings.AddAsync(new GeneralSettings
30+
{
31+
Key = "AndroidBatteryOptimizationExclusionPromptShown",
32+
Value = "True"
33+
});
34+
35+
await scheduleDbContext.SaveChangesAsync();
36+
}
37+
}
38+
catch (Exception ex)
39+
{
40+
_logger.Error(ex, "Error marking battery optimization modal as shown");
41+
}
42+
}
43+
44+
public async Task<bool> ShouldShowModalAsync()
45+
{
46+
try
47+
{
48+
using var scope = _scopeFactory.CreateScope();
49+
var scheduleDbContext = scope.ServiceProvider.GetRequiredService<ScheduleDbContext>();
50+
return !await scheduleDbContext.GeneralSettings.AnyAsync(x =>
51+
x.Key == "AndroidBatteryOptimizationExclusionPromptShown");
52+
}
53+
catch (Exception ex)
54+
{
55+
_logger.Error(ex, "Error checking if battery optimization modal should be shown");
56+
return false;
57+
}
58+
}
59+
60+
public void ShowOptimizationSettingsPage()
61+
{
62+
_batteryOptimizationManager?.ShowBatteryOptimizationExclusionSettingsPage();
63+
}
64+
65+
public bool CanShowOptimizeActivity()
66+
{
67+
return _batteryOptimizationManager?.CanShowOptimizeActivity() ?? false;
68+
}
69+
}
70+

0 commit comments

Comments
 (0)