1
0
mirror of https://github.com/bitwarden/mobile synced 2026-01-07 11:03:54 +00:00

refactor message center use to services

This commit is contained in:
Kyle Spearrin
2017-11-21 23:08:45 -05:00
parent b48e8eeb0e
commit 3b44ede67e
24 changed files with 439 additions and 379 deletions

View File

@@ -7,6 +7,10 @@ using Plugin.Settings.Abstractions;
using Bit.App.Models;
using System.Linq;
using Bit.App.Enums;
using Xamarin.Forms;
using Bit.App.Pages;
using Bit.App.Controls;
using Acr.UserDialogs;
namespace Bit.App.Services
{
@@ -25,6 +29,9 @@ namespace Bit.App.Services
private readonly IAccountsApiRepository _accountsApiRepository;
private readonly IAppIdService _appIdService;
private readonly IDeviceInfoService _deviceInfoService;
private readonly IDeviceApiRepository _deviceApiRepository;
private readonly IGoogleAnalyticsService _googleAnalyticsService;
private readonly IUserDialogs _userDialogs;
private string _email;
private string _userId;
@@ -39,7 +46,10 @@ namespace Bit.App.Services
IConnectApiRepository connectApiRepository,
IAccountsApiRepository accountsApiRepository,
IAppIdService appIdService,
IDeviceInfoService deviceInfoService)
IDeviceInfoService deviceInfoService,
IDeviceApiRepository deviceApiRepository,
IGoogleAnalyticsService googleAnalyticsService,
IUserDialogs userDialogs)
{
_secureStorage = secureStorage;
_tokenService = tokenService;
@@ -49,6 +59,9 @@ namespace Bit.App.Services
_accountsApiRepository = accountsApiRepository;
_appIdService = appIdService;
_deviceInfoService = deviceInfoService;
_deviceApiRepository = deviceApiRepository;
_googleAnalyticsService = googleAnalyticsService;
_userDialogs = userDialogs;
}
public string UserId
@@ -194,7 +207,7 @@ namespace Bit.App.Services
return !string.IsNullOrWhiteSpace(orgId) && (_cryptoService.OrgKeys?.ContainsKey(orgId) ?? false);
}
public void LogOut()
public void LogOut(string logoutMessage = null)
{
_tokenService.Token = null;
_tokenService.RefreshToken = null;
@@ -204,6 +217,16 @@ namespace Bit.App.Services
_settings.Remove(Constants.SecurityStamp);
_settings.Remove(Constants.PushLastRegistrationDate);
_settings.Remove(Constants.Locked);
Task.Run(async () => await _deviceApiRepository.PutClearTokenAsync(_appIdService.AppId));
_googleAnalyticsService.TrackAppEvent("LoggedOut");
Device.BeginInvokeOnMainThread(() => Application.Current.MainPage = new ExtendedNavigationPage(new HomePage()));
if(!string.IsNullOrWhiteSpace(logoutMessage))
{
_userDialogs.Toast(logoutMessage);
}
}
public async Task<FullLoginResult> TokenPostAsync(string email, string masterPassword)
@@ -249,10 +272,10 @@ namespace Bit.App.Services
return result;
}
public async Task<LoginResult> TokenPostTwoFactorAsync(TwoFactorProviderType type, string token, bool remember,
public async Task<Models.LoginResult> TokenPostTwoFactorAsync(TwoFactorProviderType type, string token, bool remember,
string email, string masterPasswordHash, SymmetricCryptoKey key)
{
var result = new LoginResult();
var result = new Models.LoginResult();
var request = new TokenRequest
{

View File

@@ -250,7 +250,7 @@ namespace Bit.App.Services
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
_authService.LogOut();
}
return response;
@@ -272,7 +272,7 @@ namespace Bit.App.Services
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
_authService.LogOut();
}
return response;
@@ -334,7 +334,7 @@ namespace Bit.App.Services
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
_authService.LogOut();
}
return response;
@@ -359,7 +359,7 @@ namespace Bit.App.Services
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
_authService.LogOut();
}
return response;

View File

@@ -75,7 +75,7 @@ namespace Bit.App.Services
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
_authService.LogOut();
}
return response;
@@ -91,7 +91,7 @@ namespace Bit.App.Services
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
_authService.LogOut();
}
return response;

View File

@@ -4,6 +4,10 @@ using Plugin.Settings.Abstractions;
using Plugin.Fingerprint.Abstractions;
using Bit.App.Enums;
using System.Threading.Tasks;
using Bit.App.Controls;
using Bit.App.Pages;
using Xamarin.Forms;
using System.Linq;
namespace Bit.App.Services
{
@@ -79,5 +83,58 @@ namespace Bit.App.Services
return LockType.Password;
}
}
public async Task CheckLockAsync(bool forceLock)
{
if(TopPageIsLock())
{
// already locked
return;
}
var lockType = await GetLockTypeAsync(forceLock);
if(lockType == LockType.None)
{
return;
}
_appSettings.Locked = true;
switch(lockType)
{
case LockType.Fingerprint:
await Application.Current.MainPage.Navigation.PushModalAsync(
new ExtendedNavigationPage(new LockFingerprintPage(!forceLock)), false);
break;
case LockType.PIN:
await Application.Current.MainPage.Navigation.PushModalAsync(
new ExtendedNavigationPage(new LockPinPage()), false);
break;
case LockType.Password:
await Application.Current.MainPage.Navigation.PushModalAsync(
new ExtendedNavigationPage(new LockPasswordPage()), false);
break;
default:
break;
}
}
public bool TopPageIsLock()
{
var currentPage = Application.Current.MainPage.Navigation.ModalStack.LastOrDefault() as ExtendedNavigationPage;
if((currentPage?.CurrentPage as LockFingerprintPage) != null)
{
return true;
}
if((currentPage?.CurrentPage as LockPinPage) != null)
{
return true;
}
if((currentPage?.CurrentPage as LockPasswordPage) != null)
{
return true;
}
return false;
}
}
}

View File

@@ -305,7 +305,7 @@ namespace Bit.App.Services
if(Application.Current != null && (accountRevisionDate.StatusCode == System.Net.HttpStatusCode.Forbidden
|| accountRevisionDate.StatusCode == System.Net.HttpStatusCode.Unauthorized))
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
_authService.LogOut();
}
return false;
@@ -477,7 +477,7 @@ namespace Bit.App.Services
}
SyncInProgress = true;
MessagingCenter.Send(Application.Current, "SyncStarted");
MessagingCenter.Send(this, "SyncStarted");
}
private void SyncCompleted(bool successfully)
@@ -488,7 +488,7 @@ namespace Bit.App.Services
}
SyncInProgress = false;
MessagingCenter.Send(Application.Current, "SyncCompleted", successfully);
MessagingCenter.Send(this, "SyncCompleted", successfully);
}
private bool CheckSuccess<T>(ApiResult<T> result, bool logout = false)
@@ -501,7 +501,7 @@ namespace Bit.App.Services
result.StatusCode == System.Net.HttpStatusCode.Forbidden ||
result.StatusCode == System.Net.HttpStatusCode.Unauthorized))
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
_authService.LogOut();
}
return false;