forked from chaincase-app/Chaincase-iOS-Beta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBlazorApp.cs
135 lines (111 loc) · 3.48 KB
/
BlazorApp.cs
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
using System;
using System.IO;
using System.Threading.Tasks;
using Chaincase.Background;
using Chaincase.Common;
using Chaincase.Common.Contracts;
using Chaincase.Services;
using Chaincase.UI.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.MobileBlazorBindings;
using Splat;
using WalletWasabi.Blockchain.Keys;
using WalletWasabi.Logging;
using Xamarin.Forms;
namespace Chaincase
{
public class BlazorApp : Application
{
private readonly IHost _host;
public IServiceProvider ServiceProvider => _host.Services;
public BlazorApp(IFileProvider fileProvider = null, Action<IServiceCollection> configureDI = null)
{
var hostBuilder = MobileBlazorBindingsHost.CreateDefaultBuilder()
.ConfigureServices((hostContext, services) =>
{
// Adds web-specific services such as NavigationManager
services.AddBlazorHybrid();
services.AddScoped<IClipboard, XamarinClipboard>();
services.AddSingleton<IDataDirProvider, XamarinDataDirProvider>();
services.AddSingleton<IMainThreadInvoker, XamarinMainThreadInvoker>();
services.AddSingleton<IShare, XamarinShare>();
services.AddSingleton<IThemeManager, XamarinThemeManager>();
configureDI?.Invoke(services);
services.AddUIServices();
})
.UseWebRoot("wwwroot");
if (fileProvider != null)
{
hostBuilder.UseStaticFiles(fileProvider);
}
else
{
hostBuilder.UseStaticFiles();
}
_host = hostBuilder.Build();
MainPage = new ContentPage { Title = "Chaincase" };
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
_host.AddComponent<Main>(parent: MainPage);
}
public void InitializeNoWallet()
{
// This relies on Global registered
var message = new InitializeNoWalletTaskMessage();
MessagingCenter.Send(message, "InitializeNoWalletTaskMessage");
}
protected override void OnSleep()
{
// Execute Sleeping Background Task
var message = new OnSleepingTaskMessage();
MessagingCenter.Send(message, "OnSleepingTaskMessage");
}
protected override void OnResume()
{
Resuming += OnResuming;
// Execute Async code
Resuming(this, EventArgs.Empty);
}
private event EventHandler Resuming = delegate { };
private async void OnResuming(object sender, EventArgs args)
{
//unsubscribe from event
Resuming -= OnResuming;
//perform non-blocking actions
await _host.Services.GetRequiredService<Global>().OnResuming();
}
private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
Logger.LogWarning(e?.Exception, "UnobservedTaskException");
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Logger.LogWarning(e?.ExceptionObject as Exception, "UnhandledException");
}
public static async Task LoadWalletAsync()
{
var global = Locator.Current.GetService<Global>();
string walletName = global.Network.ToString();
KeyManager keyManager = global.WalletManager.GetWalletByName(walletName).KeyManager;
if (keyManager is null)
{
return;
}
try
{
global.Wallet = await global.WalletManager.StartWalletAsync(keyManager);
// Successfully initialized.
}
catch (OperationCanceledException ex)
{
Logger.LogTrace(ex);
}
catch (Exception ex)
{
Logger.LogError(ex);
}
}
}
}