mirror of
https://github.com/bitwarden/mobile
synced 2026-01-06 02:23:57 +00:00
re-worked message sending
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user