diff --git a/src/Android/MainApplication.cs b/src/Android/MainApplication.cs index 8df498f04..9e7ea0a24 100644 --- a/src/Android/MainApplication.cs +++ b/src/Android/MainApplication.cs @@ -44,13 +44,16 @@ namespace Bit.Droid var liteDbStorage = new LiteDbStorageService(Path.Combine(documentsPath, "bitwarden.db")); var deviceActionService = new DeviceActionService(); var localizeService = new LocalizeService(); + var broadcasterService = new MobileBroadcasterService(); var messagingService = new MobileMessagingService(); var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo()); var secureStorageService = new SecureStorageService(); var cryptoPrimitiveService = new CryptoPrimitiveService(); var mobileStorageService = new MobileStorageService(preferencesStorage, liteDbStorage); - var platformUtilsService = new MobilePlatformUtilsService(deviceActionService); + var platformUtilsService = new MobilePlatformUtilsService(deviceActionService, messagingService, + broadcasterService); + ServiceContainer.Register("broadcasterService", broadcasterService); ServiceContainer.Register("messagingService", messagingService); ServiceContainer.Register("localizeService", localizeService); ServiceContainer.Register("i18nService", i18nService); diff --git a/src/App/App.xaml.cs b/src/App/App.xaml.cs index cf8020f6b..dbd13fa78 100644 --- a/src/App/App.xaml.cs +++ b/src/App/App.xaml.cs @@ -16,9 +16,13 @@ namespace Bit.App public partial class App : Application { private readonly MobileI18nService _i18nService; + private readonly IBroadcasterService _broadcasterService; + private readonly IMessagingService _messagingService; public App() { + _broadcasterService = ServiceContainer.Resolve("broadcasterService"); + _messagingService = ServiceContainer.Resolve("messagingService"); _i18nService = ServiceContainer.Resolve("i18nService") as MobileI18nService; InitializeComponent(); @@ -27,7 +31,7 @@ namespace Bit.App MainPage = new TabsPage(); ServiceContainer.Resolve("platformUtilsService").Init(); - MessagingCenter.Subscribe(Current, "ShowDialog", async (sender, details) => + _broadcasterService.Subscribe("showDialog", async (details) => { var confirmed = true; var confirmText = string.IsNullOrWhiteSpace(details.ConfirmText) ? @@ -41,7 +45,7 @@ namespace Bit.App { await MainPage.DisplayAlert(details.Title, details.Text, details.ConfirmText); } - MessagingCenter.Send(Current, "ShowDialogResolve", new Tuple(details.DialogId, confirmed)); + _messagingService.Send("showDialogResolve", new Tuple(details.DialogId, confirmed)); }); } diff --git a/src/App/Services/MobileMessagingService.cs b/src/App/Services/MobileMessagingService.cs index b1faf6c1f..450d75372 100644 --- a/src/App/Services/MobileMessagingService.cs +++ b/src/App/Services/MobileMessagingService.cs @@ -4,7 +4,7 @@ namespace Bit.App.Services { public class MobileMessagingService : IMessagingService { - public void Send(string subscriber, object arg = null) + public void Send(string subscriber, T arg = default(T)) { Xamarin.Forms.MessagingCenter.Send(Xamarin.Forms.Application.Current, subscriber, arg); } diff --git a/src/App/Services/MobilePlatformUtilsService.cs b/src/App/Services/MobilePlatformUtilsService.cs index 244c712ce..7dce994a6 100644 --- a/src/App/Services/MobilePlatformUtilsService.cs +++ b/src/App/Services/MobilePlatformUtilsService.cs @@ -16,44 +16,50 @@ namespace Bit.App.Services private const int DialogPromiseExpiration = 600000; // 10 minutes private readonly IDeviceActionService _deviceActionService; + private readonly IMessagingService _messagingService; + private readonly IBroadcasterService _broadcasterService; private readonly Dictionary, DateTime>> _showDialogResolves = new Dictionary, DateTime>>(); - public MobilePlatformUtilsService(IDeviceActionService deviceActionService) + public MobilePlatformUtilsService( + IDeviceActionService deviceActionService, + IMessagingService messagingService, + IBroadcasterService broadcasterService) { _deviceActionService = deviceActionService; + _messagingService = messagingService; + _broadcasterService = broadcasterService; } public string IdentityClientId => "mobile"; public void Init() { - MessagingCenter.Subscribe>( - Xamarin.Forms.Application.Current, "ShowDialogResolve", (sender, details) => + _broadcasterService.Subscribe>("showDialogResolve", (details) => + { + var dialogId = details.Item1; + var confirmed = details.Item2; + if(_showDialogResolves.ContainsKey(dialogId)) { - var dialogId = details.Item1; - var confirmed = details.Item2; - if(_showDialogResolves.ContainsKey(dialogId)) - { - var resolveObj = _showDialogResolves[dialogId].Item1; - resolveObj.TrySetResult(confirmed); - } + var resolveObj = _showDialogResolves[dialogId].Item1; + resolveObj.TrySetResult(confirmed); + } - // Clean up old tasks - var deleteIds = new HashSet(); - foreach(var item in _showDialogResolves) + // Clean up old tasks + var deleteIds = new HashSet(); + foreach(var item in _showDialogResolves) + { + var age = DateTime.UtcNow - item.Value.Item2; + if(age.TotalMilliseconds > DialogPromiseExpiration) { - var age = DateTime.UtcNow - item.Value.Item2; - if(age.TotalMilliseconds > DialogPromiseExpiration) - { - deleteIds.Add(item.Key); - } + deleteIds.Add(item.Key); } - foreach(var id in deleteIds) - { - _showDialogResolves.Remove(id); - } - }); + } + foreach(var id in deleteIds) + { + _showDialogResolves.Remove(id); + } + }); } public Core.Enums.DeviceType GetDevice() @@ -139,7 +145,7 @@ namespace Bit.App.Services { dialogId = _random.Next(0, int.MaxValue); } - MessagingCenter.Send(Xamarin.Forms.Application.Current, "ShowAlert", new DialogDetails + _messagingService.Send("showDialog", new DialogDetails { Text = text, Title = title, @@ -167,7 +173,7 @@ namespace Bit.App.Services { var clearMs = options != null && options.ContainsKey("clearMs") ? (int?)options["clearMs"] : null; await Clipboard.SetTextAsync(text); - // TODO: send message 'copiedToClipboard' + _messagingService.Send("copiedToClipboard", new Tuple(text, clearMs)); } public async Task ReadFromClipboardAsync(Dictionary options = null) diff --git a/src/Core/Abstractions/IMessagingService.cs b/src/Core/Abstractions/IMessagingService.cs index 08d631907..f0a23b4ef 100644 --- a/src/Core/Abstractions/IMessagingService.cs +++ b/src/Core/Abstractions/IMessagingService.cs @@ -2,6 +2,6 @@ { public interface IMessagingService { - void Send(string subscriber, object arg = null); + void Send(string subscriber, T arg = default(T)); } } \ No newline at end of file