use new messaging and broadcaster services

This commit is contained in:
Kyle Spearrin 2019-04-18 13:15:46 -04:00
parent da73a2f5d2
commit 2becf769c1
5 changed files with 43 additions and 30 deletions

View File

@ -44,13 +44,16 @@ namespace Bit.Droid
var liteDbStorage = new LiteDbStorageService(Path.Combine(documentsPath, "bitwarden.db")); var liteDbStorage = new LiteDbStorageService(Path.Combine(documentsPath, "bitwarden.db"));
var deviceActionService = new DeviceActionService(); var deviceActionService = new DeviceActionService();
var localizeService = new LocalizeService(); var localizeService = new LocalizeService();
var broadcasterService = new MobileBroadcasterService();
var messagingService = new MobileMessagingService(); var messagingService = new MobileMessagingService();
var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo()); var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo());
var secureStorageService = new SecureStorageService(); var secureStorageService = new SecureStorageService();
var cryptoPrimitiveService = new CryptoPrimitiveService(); var cryptoPrimitiveService = new CryptoPrimitiveService();
var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage); var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage);
var platformUtilsService = new MobilePlatformUtilsService(deviceActionService); var platformUtilsService = new MobilePlatformUtilsService(deviceActionService, messagingService,
broadcasterService);
ServiceContainer.Register<IBroadcasterService>("broadcasterService", broadcasterService);
ServiceContainer.Register<IMessagingService>("messagingService", messagingService); ServiceContainer.Register<IMessagingService>("messagingService", messagingService);
ServiceContainer.Register<ILocalizeService>("localizeService", localizeService); ServiceContainer.Register<ILocalizeService>("localizeService", localizeService);
ServiceContainer.Register<II18nService>("i18nService", i18nService); ServiceContainer.Register<II18nService>("i18nService", i18nService);

View File

@ -16,9 +16,13 @@ namespace Bit.App
public partial class App : Application public partial class App : Application
{ {
private readonly MobileI18nService _i18nService; private readonly MobileI18nService _i18nService;
private readonly IBroadcasterService _broadcasterService;
private readonly IMessagingService _messagingService;
public App() public App()
{ {
_broadcasterService = ServiceContainer.Resolve<IBroadcasterService>("broadcasterService");
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
_i18nService = ServiceContainer.Resolve<II18nService>("i18nService") as MobileI18nService; _i18nService = ServiceContainer.Resolve<II18nService>("i18nService") as MobileI18nService;
InitializeComponent(); InitializeComponent();
@ -27,7 +31,7 @@ namespace Bit.App
MainPage = new TabsPage(); MainPage = new TabsPage();
ServiceContainer.Resolve<MobilePlatformUtilsService>("platformUtilsService").Init(); ServiceContainer.Resolve<MobilePlatformUtilsService>("platformUtilsService").Init();
MessagingCenter.Subscribe<Application, DialogDetails>(Current, "ShowDialog", async (sender, details) => _broadcasterService.Subscribe<DialogDetails>("showDialog", async (details) =>
{ {
var confirmed = true; var confirmed = true;
var confirmText = string.IsNullOrWhiteSpace(details.ConfirmText) ? var confirmText = string.IsNullOrWhiteSpace(details.ConfirmText) ?
@ -41,7 +45,7 @@ namespace Bit.App
{ {
await MainPage.DisplayAlert(details.Title, details.Text, details.ConfirmText); await MainPage.DisplayAlert(details.Title, details.Text, details.ConfirmText);
} }
MessagingCenter.Send(Current, "ShowDialogResolve", new Tuple<int, bool>(details.DialogId, confirmed)); _messagingService.Send("showDialogResolve", new Tuple<int, bool>(details.DialogId, confirmed));
}); });
} }

View File

@ -4,7 +4,7 @@ namespace Bit.App.Services
{ {
public class MobileMessagingService : IMessagingService public class MobileMessagingService : IMessagingService
{ {
public void Send(string subscriber, object arg = null) public void Send<T>(string subscriber, T arg = default(T))
{ {
Xamarin.Forms.MessagingCenter.Send(Xamarin.Forms.Application.Current, subscriber, arg); Xamarin.Forms.MessagingCenter.Send(Xamarin.Forms.Application.Current, subscriber, arg);
} }

View File

@ -16,20 +16,26 @@ namespace Bit.App.Services
private const int DialogPromiseExpiration = 600000; // 10 minutes private const int DialogPromiseExpiration = 600000; // 10 minutes
private readonly IDeviceActionService _deviceActionService; private readonly IDeviceActionService _deviceActionService;
private readonly IMessagingService _messagingService;
private readonly IBroadcasterService _broadcasterService;
private readonly Dictionary<int, Tuple<TaskCompletionSource<bool>, DateTime>> _showDialogResolves = private readonly Dictionary<int, Tuple<TaskCompletionSource<bool>, DateTime>> _showDialogResolves =
new Dictionary<int, Tuple<TaskCompletionSource<bool>, DateTime>>(); new Dictionary<int, Tuple<TaskCompletionSource<bool>, DateTime>>();
public MobilePlatformUtilsService(IDeviceActionService deviceActionService) public MobilePlatformUtilsService(
IDeviceActionService deviceActionService,
IMessagingService messagingService,
IBroadcasterService broadcasterService)
{ {
_deviceActionService = deviceActionService; _deviceActionService = deviceActionService;
_messagingService = messagingService;
_broadcasterService = broadcasterService;
} }
public string IdentityClientId => "mobile"; public string IdentityClientId => "mobile";
public void Init() public void Init()
{ {
MessagingCenter.Subscribe<Xamarin.Forms.Application, Tuple<int, bool>>( _broadcasterService.Subscribe<Tuple<int, bool>>("showDialogResolve", (details) =>
Xamarin.Forms.Application.Current, "ShowDialogResolve", (sender, details) =>
{ {
var dialogId = details.Item1; var dialogId = details.Item1;
var confirmed = details.Item2; var confirmed = details.Item2;
@ -139,7 +145,7 @@ namespace Bit.App.Services
{ {
dialogId = _random.Next(0, int.MaxValue); dialogId = _random.Next(0, int.MaxValue);
} }
MessagingCenter.Send(Xamarin.Forms.Application.Current, "ShowAlert", new DialogDetails _messagingService.Send("showDialog", new DialogDetails
{ {
Text = text, Text = text,
Title = title, Title = title,
@ -167,7 +173,7 @@ namespace Bit.App.Services
{ {
var clearMs = options != null && options.ContainsKey("clearMs") ? (int?)options["clearMs"] : null; var clearMs = options != null && options.ContainsKey("clearMs") ? (int?)options["clearMs"] : null;
await Clipboard.SetTextAsync(text); await Clipboard.SetTextAsync(text);
// TODO: send message 'copiedToClipboard' _messagingService.Send("copiedToClipboard", new Tuple<string, int?>(text, clearMs));
} }
public async Task<string> ReadFromClipboardAsync(Dictionary<string, object> options = null) public async Task<string> ReadFromClipboardAsync(Dictionary<string, object> options = null)

View File

@ -2,6 +2,6 @@
{ {
public interface IMessagingService public interface IMessagingService
{ {
void Send(string subscriber, object arg = null); void Send<T>(string subscriber, T arg = default(T));
} }
} }