|
8 | 8 |
|
9 | 9 | namespace Bible.Alarm; |
10 | 10 |
|
11 | | -public partial class App |
| 11 | +public partial class App : Application |
12 | 12 | { |
13 | | - private readonly ILogger _logger; |
| 13 | + private readonly Serilog.ILogger _logger; |
14 | 14 | private readonly IServiceScopeFactory _scopeFactory; |
15 | 15 |
|
16 | 16 | public static bool IsInForeground { get; set; } = false; |
17 | 17 |
|
18 | | - public App(ILogger logger, IServiceScopeFactory scopeFactory) |
| 18 | + public App(Serilog.ILogger logger, IServiceScopeFactory scopeFactory) |
19 | 19 | { |
20 | 20 | System.Diagnostics.Debug.WriteLine("App constructor called!"); |
21 | 21 | _logger = logger; |
22 | 22 | _scopeFactory = scopeFactory; |
23 | | - Init(); |
| 23 | + InitializeComponent(); |
24 | 24 | } |
25 | 25 |
|
26 | 26 | private INavigationService _navigationService; |
27 | 27 |
|
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) |
29 | 81 | { |
30 | | - System.Diagnostics.Debug.WriteLine("Init() called!"); |
31 | 82 | try |
32 | 83 | { |
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>() }; |
38 | 87 |
|
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!"); |
58 | 91 |
|
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 | + } |
61 | 101 |
|
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 |
113 | 126 | } |
114 | 127 | catch (Exception ex) |
115 | 128 | { |
116 | | - System.Diagnostics.Debug.WriteLine($"Error in Init(): {ex.Message}"); |
| 129 | + System.Diagnostics.Debug.WriteLine($"Error initializing platform bootstrap: {ex.Message}"); |
117 | 130 | System.Diagnostics.Debug.WriteLine($"Stack trace: {ex.StackTrace}"); |
118 | 131 | } |
119 | 132 | } |
|
0 commit comments