mirror of
https://github.com/bitwarden/mobile.git
synced 2025-02-12 00:31:25 +01:00
use new messaging and broadcaster services
This commit is contained in:
parent
da73a2f5d2
commit
2becf769c1
@ -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<IBroadcasterService>("broadcasterService", broadcasterService);
|
||||
ServiceContainer.Register<IMessagingService>("messagingService", messagingService);
|
||||
ServiceContainer.Register<ILocalizeService>("localizeService", localizeService);
|
||||
ServiceContainer.Register<II18nService>("i18nService", i18nService);
|
||||
|
@ -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<IBroadcasterService>("broadcasterService");
|
||||
_messagingService = ServiceContainer.Resolve<IMessagingService>("messagingService");
|
||||
_i18nService = ServiceContainer.Resolve<II18nService>("i18nService") as MobileI18nService;
|
||||
|
||||
InitializeComponent();
|
||||
@ -27,7 +31,7 @@ namespace Bit.App
|
||||
MainPage = new TabsPage();
|
||||
|
||||
ServiceContainer.Resolve<MobilePlatformUtilsService>("platformUtilsService").Init();
|
||||
MessagingCenter.Subscribe<Application, DialogDetails>(Current, "ShowDialog", async (sender, details) =>
|
||||
_broadcasterService.Subscribe<DialogDetails>("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<int, bool>(details.DialogId, confirmed));
|
||||
_messagingService.Send("showDialogResolve", new Tuple<int, bool>(details.DialogId, confirmed));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,7 @@ namespace Bit.App.Services
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
@ -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<int, Tuple<TaskCompletionSource<bool>, DateTime>> _showDialogResolves =
|
||||
new Dictionary<int, Tuple<TaskCompletionSource<bool>, 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, Tuple<int, bool>>(
|
||||
Xamarin.Forms.Application.Current, "ShowDialogResolve", (sender, details) =>
|
||||
_broadcasterService.Subscribe<Tuple<int, bool>>("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<int>();
|
||||
foreach(var item in _showDialogResolves)
|
||||
// Clean up old tasks
|
||||
var deleteIds = new HashSet<int>();
|
||||
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<string, int?>(text, clearMs));
|
||||
}
|
||||
|
||||
public async Task<string> ReadFromClipboardAsync(Dictionary<string, object> options = null)
|
||||
|
@ -2,6 +2,6 @@
|
||||
{
|
||||
public interface IMessagingService
|
||||
{
|
||||
void Send(string subscriber, object arg = null);
|
||||
void Send<T>(string subscriber, T arg = default(T));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user