Bitwarden-app-android-iphon.../src/App/App.xaml.cs

178 lines
7.2 KiB
C#
Raw Normal View History

2019-04-10 05:24:03 +02:00
using Bit.App.Models;
using Bit.App.Pages;
2019-04-11 21:33:10 +02:00
using Bit.App.Resources;
using Bit.App.Services;
2019-03-30 02:23:34 +01:00
using Bit.App.Utilities;
2019-04-11 21:33:10 +02:00
using Bit.Core.Abstractions;
using Bit.Core.Utilities;
2019-03-28 22:10:10 +01:00
using System;
2019-04-19 22:40:20 +02:00
using System.Threading.Tasks;
2019-03-28 01:12:44 +01:00
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
[assembly: XamlCompilation(XamlCompilationOptions.Compile)]
namespace Bit.App
{
public partial class App : Application
{
2019-04-11 21:33:10 +02:00
private readonly MobileI18nService _i18nService;
2019-04-19 15:42:55 +02:00
private readonly IUserService _userService;
private readonly IBroadcasterService _broadcasterService;
private readonly IMessagingService _messagingService;
2019-05-15 21:47:50 +02:00
private readonly IStateService _stateService;
2019-05-15 23:37:59 +02:00
private readonly ILockService _lockService;
private readonly ISyncService _syncService;
private readonly ITokenService _tokenService;
private readonly ICryptoService _cryptoService;
private readonly ICipherService _cipherService;
private readonly IFolderService _folderService;
private readonly ICollectionService _collectionService;
private readonly ISettingsService _settingsService;
private readonly IPasswordGenerationService _passwordGenerationService;
private readonly ISearchService _searchService;
private readonly IPlatformUtilsService _platformUtilsService;
private readonly IAuthService _authService;
2019-04-11 21:33:10 +02:00
2019-03-28 01:12:44 +01:00
public App()
{
2019-04-19 15:42:55 +02:00
_userService = ServiceContainer.Resolve<IUserService>("userService");
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
2019-05-15 21:47:50 +02:00
_stateService = ServiceContainer.Resolve<IStateService>("stateService");
2019-05-15 23:37:59 +02:00
_lockService = ServiceContainer.Resolve<ILockService>("lockService");
_syncService = ServiceContainer.Resolve<ISyncService>("syncService");
_tokenService = ServiceContainer.Resolve<ITokenService>("tokenService");
_cryptoService = ServiceContainer.Resolve<ICryptoService>("cryptoService");
_cipherService = ServiceContainer.Resolve<ICipherService>("cipherService");
_folderService = ServiceContainer.Resolve<IFolderService>("folderService");
_settingsService = ServiceContainer.Resolve<ISettingsService>("settingsService");
_collectionService = ServiceContainer.Resolve<ICollectionService>("collectionService");
_searchService = ServiceContainer.Resolve<ISearchService>("searchService");
_authService = ServiceContainer.Resolve<IAuthService>("authService");
_platformUtilsService = ServiceContainer.Resolve<IPlatformUtilsService>("platformUtilsService");
_passwordGenerationService = ServiceContainer.Resolve<IPasswordGenerationService>(
"passwordGenerationService");
2019-04-11 21:33:10 +02:00
_i18nService = ServiceContainer.Resolve<II18nService>("i18nService") as MobileI18nService;
2019-03-29 22:54:03 +01:00
2019-04-11 21:33:10 +02:00
InitializeComponent();
SetCulture();
2019-04-22 17:32:17 +02:00
ThemeManager.SetThemeStyle("light");
2019-04-19 13:42:36 +02:00
MainPage = new HomePage();
2019-04-19 22:40:20 +02:00
var mainPageTask = SetMainPageAsync();
2019-04-10 05:24:03 +02:00
2019-04-11 21:33:10 +02:00
ServiceContainer.Resolve<MobilePlatformUtilsService>("platformUtilsService").Init();
2019-04-19 18:29:37 +02:00
_broadcasterService.Subscribe(nameof(App), async (message) =>
2019-04-10 05:24:03 +02:00
{
2019-04-19 18:29:37 +02:00
if(message.Command == "showDialog")
2019-04-10 05:24:03 +02:00
{
2019-04-19 18:29:37 +02:00
var details = message.Data as DialogDetails;
var confirmed = true;
var confirmText = string.IsNullOrWhiteSpace(details.ConfirmText) ?
AppResources.Ok : details.ConfirmText;
if(!string.IsNullOrWhiteSpace(details.CancelText))
{
confirmed = await MainPage.DisplayAlert(details.Title, details.Text, confirmText,
details.CancelText);
}
else
{
2019-04-27 06:19:44 +02:00
await MainPage.DisplayAlert(details.Title, details.Text, confirmText);
2019-04-19 18:29:37 +02:00
}
_messagingService.Send("showDialogResolve", new Tuple<int, bool>(details.DialogId, confirmed));
2019-04-10 05:24:03 +02:00
}
2019-05-15 21:47:50 +02:00
else if(message.Command == "locked")
{
await _stateService.PurgeAsync();
2019-05-15 23:37:59 +02:00
MainPage = new NavigationPage(new LockPage());
}
else if(message.Command == "lockVault")
{
await _lockService.LockAsync(true);
}
else if(message.Command == "logout")
{
await LogOutAsync(false);
}
else if(message.Command == "loggedOut")
{
// TODO
}
else if(message.Command == "unlocked" || message.Command == "loggedIn")
{
2019-05-15 21:47:50 +02:00
// TODO
}
2019-04-10 05:24:03 +02:00
});
2019-03-28 01:12:44 +01:00
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
2019-04-11 21:33:10 +02:00
private void SetCulture()
{
// Calendars are removed by linker. ref https://bugzilla.xamarin.com/show_bug.cgi?id=59077
new System.Globalization.ThaiBuddhistCalendar();
new System.Globalization.HijriCalendar();
new System.Globalization.UmAlQuraCalendar();
}
2019-04-19 22:40:20 +02:00
2019-05-15 23:37:59 +02:00
private async Task LogOutAsync(bool expired)
{
var userId = await _userService.GetUserIdAsync();
await Task.WhenAll(
_syncService.SetLastSyncAsync(DateTime.MinValue),
_tokenService.ClearTokenAsync(),
_cryptoService.ClearKeysAsync(),
_userService.ClearAsync(),
_settingsService.ClearAsync(userId),
_cipherService.ClearAsync(userId),
_folderService.ClearAsync(userId),
_collectionService.ClearAsync(userId),
_passwordGenerationService.ClearAsync(),
_lockService.ClearAsync());
_lockService.PinLocked = false;
_searchService.ClearIndex();
_authService.LogOut(() =>
{
if(expired)
{
// TODO: Toast?
}
MainPage = new HomePage();
});
}
2019-04-19 22:40:20 +02:00
private async Task SetMainPageAsync()
{
var authed = await _userService.IsAuthenticatedAsync();
if(authed)
{
2019-05-15 23:37:59 +02:00
var locked = await _lockService.IsLockedAsync();
if(locked)
{
Current.MainPage = new NavigationPage(new LockPage());
}
else
{
Current.MainPage = new TabsPage();
}
2019-04-19 22:40:20 +02:00
}
else
{
Current.MainPage = new HomePage();
}
}
2019-03-28 01:12:44 +01:00
}
}