changes for new messaging

This commit is contained in:
Kyle Spearrin 2019-04-19 12:33:57 -04:00
parent 7c1549bb95
commit 480f954433
4 changed files with 44 additions and 60 deletions

View file

@ -0,0 +1,21 @@
using Bit.Core.Abstractions;
using Bit.Core.Models.Domain;
namespace Bit.App.Services
{
public class MobileBroadcasterMessagingService : IMessagingService
{
private readonly IBroadcasterService _broadcasterService;
public MobileBroadcasterMessagingService(IBroadcasterService broadcasterService)
{
_broadcasterService = broadcasterService;
}
public void Send(string subscriber, object arg = null)
{
var message = new Message { Command = subscriber, Data = arg };
_broadcasterService.Send(message);
}
}
}

View file

@ -1,29 +0,0 @@
using Bit.Core.Abstractions;
using System;
using Xamarin.Forms;
namespace Bit.App.Services
{
public class MobileBroadcasterService : IBroadcasterService
{
public void Send<T>(T message, string id = null)
{
if(string.IsNullOrWhiteSpace(id))
{
throw new NotSupportedException("Cannot send a message to all subscribers.");
}
MessagingCenter.Send(Application.Current, id, message);
}
public void Subscribe<T>(string id, Action<T> messageCallback)
{
MessagingCenter.Subscribe<Application, T>(Application.Current, id,
(sender, message) => messageCallback(message));
}
public void Unsubscribe(string id)
{
MessagingCenter.Unsubscribe<Application, object>(Application.Current, id);
}
}
}

View file

@ -1,12 +0,0 @@
using Bit.Core.Abstractions;
namespace Bit.App.Services
{
public class MobileMessagingService : IMessagingService
{
public void Send<T>(string subscriber, T arg = default(T))
{
Xamarin.Forms.MessagingCenter.Send(Xamarin.Forms.Application.Current, subscriber, arg);
}
}
}

View file

@ -35,29 +35,33 @@ namespace Bit.App.Services
public void Init() public void Init()
{ {
_broadcasterService.Subscribe<Tuple<int, bool>>("showDialogResolve", (details) => _broadcasterService.Subscribe("showDialogResolve", (message) =>
{ {
var dialogId = details.Item1; if(message.Command == "")
var confirmed = details.Item2;
if(_showDialogResolves.ContainsKey(dialogId))
{ {
var resolveObj = _showDialogResolves[dialogId].Item1; var details = message.Data as Tuple<int, bool>;
resolveObj.TrySetResult(confirmed); var dialogId = details.Item1;
} var confirmed = details.Item2;
if(_showDialogResolves.ContainsKey(dialogId))
// 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)
{ {
deleteIds.Add(item.Key); var resolveObj = _showDialogResolves[dialogId].Item1;
resolveObj.TrySetResult(confirmed);
}
// 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)
{
deleteIds.Add(item.Key);
}
}
foreach(var id in deleteIds)
{
_showDialogResolves.Remove(id);
} }
}
foreach(var id in deleteIds)
{
_showDialogResolves.Remove(id);
} }
}); });
} }