Skip to content

Commit 6c3a829

Browse files
committed
Fix init
1 parent 8b3cb50 commit 6c3a829

File tree

14 files changed

+182
-245
lines changed

14 files changed

+182
-245
lines changed

src/Bible.Alarm.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Global
9393
{EF8D0ED7-45C2-4176-B01E-0BF199108B05}.Release|x86.Build.0 = Release|Any CPU
9494
{8F083081-CE5F-3DD2-A49A-CAAC866C0A5E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
9595
{8F083081-CE5F-3DD2-A49A-CAAC866C0A5E}.Debug|Any CPU.Build.0 = Debug|Any CPU
96+
{8F083081-CE5F-3DD2-A49A-CAAC866C0A5E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
9697
{8F083081-CE5F-3DD2-A49A-CAAC866C0A5E}.Debug|ARM.ActiveCfg = Debug|Any CPU
9798
{8F083081-CE5F-3DD2-A49A-CAAC866C0A5E}.Debug|ARM.Build.0 = Debug|Any CPU
9899
{8F083081-CE5F-3DD2-A49A-CAAC866C0A5E}.Debug|ARM64.ActiveCfg = Debug|Any CPU

src/Bible.Alarm/App.xaml.cs

Lines changed: 97 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,112 +8,125 @@
88

99
namespace Bible.Alarm;
1010

11-
public partial class App
11+
public partial class App : Application
1212
{
13-
private readonly ILogger _logger;
13+
private readonly Serilog.ILogger _logger;
1414
private readonly IServiceScopeFactory _scopeFactory;
1515

1616
public static bool IsInForeground { get; set; } = false;
1717

18-
public App(ILogger logger, IServiceScopeFactory scopeFactory)
18+
public App(Serilog.ILogger logger, IServiceScopeFactory scopeFactory)
1919
{
2020
System.Diagnostics.Debug.WriteLine("App constructor called!");
2121
_logger = logger;
2222
_scopeFactory = scopeFactory;
23-
Init();
23+
InitializeComponent();
2424
}
2525

2626
private INavigationService _navigationService;
2727

28-
private void Init()
28+
protected override Window CreateWindow(IActivationState? activationState)
29+
{
30+
System.Diagnostics.Debug.WriteLine("CreateWindow called!");
31+
32+
// Initialize platform-specific bootstrap helper after ServiceProviderManager is available
33+
InitializePlatformBootstrap();
34+
35+
// Create a simple window with a basic page for now
36+
var window = new Window(new ContentPage
37+
{
38+
Title = "Bible Alarm",
39+
Content = new Label
40+
{
41+
Text = "Bible Alarm is starting...",
42+
HorizontalOptions = LayoutOptions.Center,
43+
VerticalOptions = LayoutOptions.Center
44+
}
45+
});
46+
47+
// Initialize services in background
48+
Task.Run(async () =>
49+
{
50+
try
51+
{
52+
System.Diagnostics.Debug.WriteLine("Starting service initialization...");
53+
54+
// Create and store the navigation service for later use
55+
using var scope = _scopeFactory.CreateScope();
56+
_navigationService = new NavigationService(
57+
scope.ServiceProvider.GetRequiredService<ILogger>(),
58+
null, // Will be set when we have proper navigation
59+
_scopeFactory);
60+
System.Diagnostics.Debug.WriteLine("NavigationService created!");
61+
62+
// Initialize the home page
63+
await InitializeHomePage(window);
64+
65+
var playbackService = scope.ServiceProvider.GetRequiredService<IPlaybackService>();
66+
if (playbackService.IsPrepared) Messenger<object>.Publish(MvvmMessages.ShowAlarmModal);
67+
68+
System.Diagnostics.Debug.WriteLine("Service initialization completed!");
69+
}
70+
catch (Exception ex)
71+
{
72+
System.Diagnostics.Debug.WriteLine($"Error in service initialization: {ex.Message}");
73+
System.Diagnostics.Debug.WriteLine($"Stack trace: {ex.StackTrace}");
74+
}
75+
});
76+
77+
return window;
78+
}
79+
80+
private async Task InitializeHomePage(Window window)
2981
{
30-
System.Diagnostics.Debug.WriteLine("Init() called!");
3182
try
3283
{
33-
InitializeComponent();
34-
System.Diagnostics.Debug.WriteLine("InitializeComponent() completed!");
35-
36-
var navigationPage = new NavigationPage();
37-
System.Diagnostics.Debug.WriteLine("NavigationPage created!");
84+
System.Diagnostics.Debug.WriteLine("InitializeHomePage called!");
85+
using var scope = _scopeFactory.CreateScope();
86+
var homePage = new Home { BindingContext = scope.ServiceProvider.GetRequiredService<HomeViewModel>() };
3887

39-
// Use the MAUI approach for setting the main page
40-
MainPage = navigationPage;
41-
System.Diagnostics.Debug.WriteLine("MainPage set!");
42-
43-
navigationPage.SetValue(NavigationPage.BarBackgroundColorProperty, Colors.SlateBlue);
44-
navigationPage.SetValue(NavigationPage.BarTextColorProperty, Colors.White);
45-
System.Diagnostics.Debug.WriteLine("Navigation page properties set!");
46-
47-
// Try to create a simple home page first
48-
var simpleHomePage = new ContentPage
49-
{
50-
Title = "Bible Alarm",
51-
Content = new Label
52-
{
53-
Text = "Bible Alarm is starting...",
54-
HorizontalOptions = LayoutOptions.Center,
55-
VerticalOptions = LayoutOptions.Center
56-
}
57-
};
88+
// Set the main page to the home page
89+
window.Page = homePage;
90+
System.Diagnostics.Debug.WriteLine("Home page set!");
5891

59-
navigationPage.Navigation.PushAsync(simpleHomePage);
60-
System.Diagnostics.Debug.WriteLine("Simple home page pushed!");
92+
// Add a small delay to ensure proper initialization
93+
await Task.Delay(100);
94+
}
95+
catch (Exception ex)
96+
{
97+
System.Diagnostics.Debug.WriteLine($"Error in InitializeHomePage: {ex.Message}");
98+
System.Diagnostics.Debug.WriteLine($"Stack trace: {ex.StackTrace}");
99+
}
100+
}
61101

62-
// Try to initialize services in background
63-
Task.Run(async () =>
64-
{
65-
try
66-
{
67-
System.Diagnostics.Debug.WriteLine("Starting service initialization...");
68-
var taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
69-
70-
// Create and store the navigation service for later use
71-
using var scope = _scopeFactory.CreateScope();
72-
_navigationService = new NavigationService(
73-
scope.ServiceProvider.GetRequiredService<ILogger>(),
74-
navigationPage.Navigation,
75-
_scopeFactory);
76-
System.Diagnostics.Debug.WriteLine("NavigationService created!");
77-
78-
if (DeviceInfo.Platform != DevicePlatform.Android) await HomePageSetter();
79-
else
80-
{
81-
await Task.Delay(100);
82-
await HomePageSetter();
83-
}
84-
85-
var playbackService = scope.ServiceProvider.GetRequiredService<IPlaybackService>();
86-
if (playbackService.IsPrepared) Messenger<object>.Publish(MvvmMessages.ShowAlarmModal);
87-
88-
System.Diagnostics.Debug.WriteLine("Service initialization completed!");
89-
}
90-
catch (Exception ex)
91-
{
92-
System.Diagnostics.Debug.WriteLine($"Error in service initialization: {ex.Message}");
93-
System.Diagnostics.Debug.WriteLine($"Stack trace: {ex.StackTrace}");
94-
}
95-
});
96-
97-
async Task HomePageSetter()
98-
{
99-
try
100-
{
101-
System.Diagnostics.Debug.WriteLine("HomePageSetter called!");
102-
using var scope = _scopeFactory.CreateScope();
103-
var homePage = new Home { BindingContext = scope.ServiceProvider.GetRequiredService<HomeViewModel>() };
104-
await navigationPage.Navigation.PushAsync(homePage);
105-
System.Diagnostics.Debug.WriteLine("Home page pushed!");
106-
}
107-
catch (Exception ex)
108-
{
109-
System.Diagnostics.Debug.WriteLine($"Error in HomePageSetter: {ex.Message}");
110-
System.Diagnostics.Debug.WriteLine($"Stack trace: {ex.StackTrace}");
111-
}
112-
}
102+
private void InitializePlatformBootstrap()
103+
{
104+
try
105+
{
106+
System.Diagnostics.Debug.WriteLine("Initializing platform-specific bootstrap...");
107+
108+
// Initialize platform-specific bootstrap helper after ServiceProviderManager is available
109+
#if WINDOWS
110+
using var scope = _scopeFactory.CreateScope();
111+
var logger = scope.ServiceProvider.GetRequiredService<Serilog.ILogger>();
112+
Bible.Alarm.Services.Windows.Helpers.BootstrapHelper.Initialize(logger);
113+
System.Diagnostics.Debug.WriteLine("Windows bootstrap helper initialized!");
114+
#elif ANDROID
115+
using var scope = _scopeFactory.CreateScope();
116+
var logger = scope.ServiceProvider.GetRequiredService<Serilog.ILogger>();
117+
// Android bootstrap is handled in MainActivity, but we also need to call it here for database initialization
118+
_ = Task.Run(async () => await Bible.Alarm.Services.Droid.Helpers.BootstrapHelper.VerifyServices());
119+
System.Diagnostics.Debug.WriteLine("Android bootstrap helper initialized!");
120+
#elif IOS
121+
using var scope = _scopeFactory.CreateScope();
122+
var logger = scope.ServiceProvider.GetRequiredService<Serilog.ILogger>();
123+
Bible.Alarm.Services.iOS.Helpers.BootstrapHelper.Initialize(logger);
124+
System.Diagnostics.Debug.WriteLine("iOS bootstrap helper initialized!");
125+
#endif
113126
}
114127
catch (Exception ex)
115128
{
116-
System.Diagnostics.Debug.WriteLine($"Error in Init(): {ex.Message}");
129+
System.Diagnostics.Debug.WriteLine($"Error initializing platform bootstrap: {ex.Message}");
117130
System.Diagnostics.Debug.WriteLine($"Stack trace: {ex.StackTrace}");
118131
}
119132
}

src/Bible.Alarm/MauiProgram.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Bible.Alarm.Contracts.Platform;
2020
using Bible.Alarm.Shared.Constants;
2121
using Microsoft.EntityFrameworkCore;
22+
using Microsoft.Extensions.Logging;
2223
using Serilog;
2324

2425
namespace Bible.Alarm;
@@ -35,6 +36,8 @@ public static MauiApp CreateMauiApp()
3536
.UseMauiCommunityToolkitMediaElement()
3637
.ConfigureFonts(fonts => { fonts.AddFont(AppConstants.AppSettings.DefaultFontFileName, AppConstants.AppSettings.DefaultFontResourceName); });
3738

39+
// Debug logging is handled by Serilog
40+
3841
// Register services
3942
RegisterServices(builder.Services);
4043

@@ -71,7 +74,7 @@ private static void RegisterServices(IServiceCollection services)
7174
private static void RegisterCommonServices(IServiceCollection services)
7275
{
7376
// Register logging
74-
services.AddSingleton<ILogger>(sp => Serilog.Log.Logger);
77+
services.AddSingleton<Serilog.ILogger>(sp => Serilog.Log.Logger);
7578

7679
// Register core services that don't have platform dependencies
7780
services.AddSingleton<IDownloadService, DownloadService>();
@@ -135,6 +138,9 @@ private static void RegisterCommonServices(IServiceCollection services)
135138

136139
// Register TaskScheduler for compatibility
137140
services.AddSingleton<TaskScheduler>(sp => TaskScheduler.FromCurrentSynchronizationContext());
141+
142+
// Register NavigationService
143+
services.AddSingleton<INavigationService, NavigationService>();
138144
}
139145

140146
private static void RegisterViewModels(IServiceCollection services)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.WAKE_LOCK" />
7+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
8+
<uses-permission android:name="android.permission.VIBRATE" />
9+
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
10+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
11+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
12+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
13+
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
14+
</manifest>

src/Bible.Alarm/Platforms/Android/MainActivity.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
namespace Bible.Alarm.Droid;
1313

14-
[Activity(Label = "Bible Alarm", Icon = "@mipmap/ic_launcher", Theme = "@style/MainTheme", MainLauncher = true,
15-
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
14+
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
1615
public class MainActivity : MauiAppCompatActivity
1716
{
1817
private static readonly ILogger Logger = Log.ForContext<MainActivity>();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Android.App;
2+
using Android.Runtime;
3+
4+
namespace Bible.Alarm.Droid
5+
{
6+
[Application]
7+
public class MainApplication : MauiApplication
8+
{
9+
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
10+
: base(handle, ownership)
11+
{
12+
}
13+
14+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
15+
}
16+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<Application
1+
<maui:MauiWinUIApplication
22
x:Class="Bible.Alarm.WinUI.App"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:maui="using:Microsoft.Maui"
56
xmlns:local="using:Bible.Alarm.WinUI"
67
RequestedTheme="Light">
78
<Application.Resources>
@@ -10,4 +11,4 @@
1011
<Setter Property="OnContent" Value=" " />
1112
</Style>
1213
</Application.Resources>
13-
</Application>
14+
</maui:MauiWinUIApplication>
Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,21 @@
1-
using Serilog;
2-
using Bible.Alarm.Services.Windows.Helpers;
1+
using Microsoft.UI.Xaml;
32

43
namespace Bible.Alarm.WinUI
54
{
65
/// <summary>
76
/// Provides application-specific behavior to supplement the default Application class.
87
/// </summary>
9-
sealed partial class App : Microsoft.UI.Xaml.Application
8+
public partial class App : MauiWinUIApplication
109
{
11-
private static readonly ILogger Logger = Log.ForContext<App>();
12-
10+
/// <summary>
11+
/// Initializes the singleton application object. This is the first line of authored code
12+
/// executed, and as such is the logical equivalent of main() or WinMain().
13+
/// </summary>
1314
public App()
1415
{
15-
InitializeWindowsSpecific();
16-
}
17-
18-
private void InitializeWindowsSpecific()
19-
{
20-
try
21-
{
22-
// Windows-specific initialization
23-
// Set up window management
24-
// MAUI handles application lifecycle events automatically
25-
26-
// Initialize Windows-specific services
27-
// Dependency injection container is configured in MauiProgram
28-
Logger.Information("Windows application initialized successfully.");
29-
30-
// Initialize Windows-specific bootstrap helper
31-
BootstrapHelper.Initialize(Logger);
32-
}
33-
catch (Exception ex)
34-
{
35-
Logger.Error(ex, "Error initializing Windows-specific components.");
36-
}
16+
this.InitializeComponent();
3717
}
3818

39-
// Application lifecycle is managed by MAUI framework
19+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
4020
}
4121
}

src/Bible.Alarm/Platforms/Windows/MainPage.xaml

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)