-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
143 lines (123 loc) · 4.22 KB
/
App.xaml.cs
File metadata and controls
143 lines (123 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using EShopNative.Interfaces;
using EShopNative.Pages;
using EShopNative.Services;
namespace EShop
{
public partial class App : Application
{
private readonly IServiceProvider _services;
public App(IServiceProvider services)
{
InitializeComponent();
_services = services;
}
protected override Window CreateWindow(IActivationState? activationState)
{
// Temporary loading screen
var loadingPage = new ContentPage
{
Content = new ActivityIndicator
{
IsRunning = true,
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center
}
};
var window = new Window(new NavigationPage(loadingPage));
// Attach global input handlers once the window is ready
window.HandlerChanged += (s, e) =>
{
if (window.Handler?.PlatformView is not null)
{
AttachGlobalInputHandlers(window);
}
};
// Run async session check AFTER window is created
_ = InitializeApp(window);
return window;
}
private async Task InitializeApp(Window window)
{
try
{
var session = _services.GetRequiredService<ISessionManager>();
await session.LoadSessionAsync();
var auth = _services.GetRequiredService<AuthService>();
Page startPage;
if (!string.IsNullOrEmpty(session.RefreshToken))
{
var refreshed = await auth.RefreshAsync(session.RefreshToken);
if (refreshed is { User: not null })
{
await session.SaveSessionAsync(
refreshed.AccessToken,
refreshed.RefreshToken,
refreshed.User
);
auth.AttachToken(); // IMPORTANT
startPage = _services.GetRequiredService<HomePage>();
}
else
{
await session.ClearSessionAsync();
startPage = _services.GetRequiredService<UserRoleEntry>();
}
}
else
{
auth.AttachToken(); // IMPORTANT
startPage = _services.GetRequiredService<UserRoleEntry>();
}
window.Page = new NavigationPage(startPage);
}
catch
{
var fallback = _services.GetRequiredService<UserRoleEntry>();
window.Page = new NavigationPage(fallback);
}
}
private void AttachGlobalInputHandlers(Window window)
{
#if ANDROID
if (window.Handler?.PlatformView is Android.Views.View view)
{
view.Touch += (s, e) =>
{
var idle = IPlatformApplication.Current?.Services?.GetService<IdleTimeoutService>();
idle?.ResetTimer();
};
}
#endif
#if IOS
if (window.Handler?.PlatformView is UIKit.UIView uiView)
{
var gesture = new UIKit.UITapGestureRecognizer(() =>
{
var idle = IPlatformApplication.Current?.Services?.GetRequiredService<IdleTimeoutService>();
idle?.ResetTimer();
});
uiView.AddGestureRecognizer(gesture);
}
#endif
#if WINDOWS
if (window.Handler?.PlatformView is Microsoft.UI.Xaml.Window win)
{
var root = win.Content as Microsoft.UI.Xaml.FrameworkElement;
if (root != null)
{
root.PointerPressed += (s, e) =>
{
var idle = IPlatformApplication.Current?.Services?.GetService<IdleTimeoutService>();
idle?.ResetTimer();
};
root.PointerMoved += (s, e) =>
{
var idle = IPlatformApplication.Current?.Services?.GetService<IdleTimeoutService>();
idle?.ResetTimer();
};
}
}
#endif
}
}
}