mirror of
https://github.com/bitwarden/mobile.git
synced 2024-11-23 11:45:38 +01:00
re-worked message sending
This commit is contained in:
parent
4b7366e9b3
commit
7c1549bb95
@ -44,8 +44,8 @@ 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 broadcasterService = new BroadcasterService();
|
||||
var messagingService = new MobileBroadcasterMessagingService(broadcasterService);
|
||||
var i18nService = new MobileI18nService(localizeService.GetCurrentCultureInfo());
|
||||
var secureStorageService = new SecureStorageService();
|
||||
var cryptoPrimitiveService = new CryptoPrimitiveService();
|
||||
|
@ -44,21 +44,25 @@ namespace Bit.App
|
||||
});
|
||||
|
||||
ServiceContainer.Resolve<MobilePlatformUtilsService>("platformUtilsService").Init();
|
||||
_broadcasterService.Subscribe<DialogDetails>("showDialog", async (details) =>
|
||||
_broadcasterService.Subscribe(nameof(App), async (message) =>
|
||||
{
|
||||
var confirmed = true;
|
||||
var confirmText = string.IsNullOrWhiteSpace(details.ConfirmText) ?
|
||||
AppResources.Ok : details.ConfirmText;
|
||||
if(!string.IsNullOrWhiteSpace(details.CancelText))
|
||||
if(message.Command == "showDialog")
|
||||
{
|
||||
confirmed = await MainPage.DisplayAlert(details.Title, details.Text, confirmText,
|
||||
details.CancelText);
|
||||
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
|
||||
{
|
||||
await MainPage.DisplayAlert(details.Title, details.Text, details.ConfirmText);
|
||||
}
|
||||
_messagingService.Send("showDialogResolve", new Tuple<int, bool>(details.DialogId, confirmed));
|
||||
}
|
||||
else
|
||||
{
|
||||
await MainPage.DisplayAlert(details.Title, details.Text, details.ConfirmText);
|
||||
}
|
||||
_messagingService.Send("showDialogResolve", new Tuple<int, bool>(details.DialogId, confirmed));
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,12 @@
|
||||
using System;
|
||||
using Bit.Core.Models.Domain;
|
||||
using System;
|
||||
|
||||
namespace Bit.Core.Abstractions
|
||||
{
|
||||
public interface IBroadcasterService
|
||||
{
|
||||
void Send<T>(T message, string id = null);
|
||||
void Subscribe<T>(string id, Action<T> messageCallback);
|
||||
void Send(Message message, string id = null);
|
||||
void Subscribe(string id, Action<Message> messageCallback);
|
||||
void Unsubscribe(string id);
|
||||
}
|
||||
}
|
@ -2,6 +2,6 @@
|
||||
{
|
||||
public interface IMessagingService
|
||||
{
|
||||
void Send<T>(string subscriber, T arg = default(T));
|
||||
void Send(string subscriber, object arg = null);
|
||||
}
|
||||
}
|
8
src/Core/Models/Domain/Message.cs
Normal file
8
src/Core/Models/Domain/Message.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace Bit.Core.Models.Domain
|
||||
{
|
||||
public class Message
|
||||
{
|
||||
public string Command { get; set; }
|
||||
public object Data { get; set; }
|
||||
}
|
||||
}
|
@ -140,7 +140,7 @@ namespace Bit.Core.Services
|
||||
public void LogOut(Action callback)
|
||||
{
|
||||
callback.Invoke();
|
||||
_messagingService.Send<object>("loggedOut");
|
||||
_messagingService.Send("loggedOut");
|
||||
}
|
||||
|
||||
public List<TwoFactorProvider> GetSupportedTwoFactorProviders()
|
||||
@ -312,7 +312,7 @@ namespace Bit.Core.Services
|
||||
await _cryptoService.SetEncPrivateKeyAsync(tokenResponse.PrivateKey);
|
||||
}
|
||||
|
||||
_messagingService.Send<object>("loggedIn");
|
||||
_messagingService.Send("loggedIn");
|
||||
return result;
|
||||
}
|
||||
|
||||
|
45
src/Core/Services/BroadcasterService.cs
Normal file
45
src/Core/Services/BroadcasterService.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Models.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Bit.App.Services
|
||||
{
|
||||
public class BroadcasterService : IBroadcasterService
|
||||
{
|
||||
private readonly Dictionary<string, Action<Message>> _subscribers = new Dictionary<string, Action<Message>>();
|
||||
|
||||
public void Send(Message message, string id = null)
|
||||
{
|
||||
if(!string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
if(_subscribers.ContainsKey(id))
|
||||
{
|
||||
_subscribers[id].Invoke(message);
|
||||
}
|
||||
return;
|
||||
}
|
||||
foreach(var sub in _subscribers)
|
||||
{
|
||||
sub.Value.Invoke(message);
|
||||
}
|
||||
}
|
||||
|
||||
public void Subscribe(string id, Action<Message> messageCallback)
|
||||
{
|
||||
if(_subscribers.ContainsKey(id))
|
||||
{
|
||||
return;
|
||||
}
|
||||
_subscribers.Add(id, messageCallback);
|
||||
}
|
||||
|
||||
public void Unsubscribe(string id)
|
||||
{
|
||||
if(_subscribers.ContainsKey(id))
|
||||
{
|
||||
_subscribers.Remove(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -255,7 +255,7 @@ namespace Bit.Core.Services
|
||||
private void SyncStarted()
|
||||
{
|
||||
SyncInProgress = true;
|
||||
_messagingService.Send<object>("syncStarted");
|
||||
_messagingService.Send("syncStarted");
|
||||
}
|
||||
|
||||
private bool SyncCompleted(bool successfully)
|
||||
|
Loading…
Reference in New Issue
Block a user