Skip to content

Commit

Permalink
Notification flow fix
Browse files Browse the repository at this point in the history
Translation implemented
  • Loading branch information
DjArt committed Aug 31, 2019
1 parent dd9e9f0 commit 78e8464
Showing 1 changed file with 59 additions and 31 deletions.
90 changes: 59 additions & 31 deletions WoADialer/Background/NotificationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Windows.UI.Notifications;
using Windows.UI.Notifications.Management;
using WoADialer.Helpers;
using WoADialer.UI.Conventers;
using WoADialer.UI.ViewModel;

namespace WoADialer.Background
{
Expand All @@ -33,6 +35,9 @@ public sealed class NotificationSystem
};

public const string ACTION = "Action";
public const string USED_CALLS = "UsedCalls";
public const string USED_CALLS_STATES = "UsedCallsStates";

#region Available actions
/// <summary>
/// Requires <see cref="INCOMING_CALL_ID"/>
Expand Down Expand Up @@ -85,6 +90,7 @@ public sealed class NotificationSystem
public const string SHOW_CALL_UI = "ShowCallUI";
public const string SHOW_INCOMING_CALL_UI = "ShowIncomingCallUI";
#endregion

#region Possible parameters
public const string ACTIVE_CALL_ID = "ActiveCallID";
public const string INCOMING_CALL_ID = "IncomingCallID";
Expand All @@ -105,20 +111,20 @@ private List<ToastButton> CreateButtonsForCall(Call call)
switch (call.State)
{
case CallState.ActiveTalking:
buttons.Add(new ToastButton("Hold", $"{ACTION}={HOLD}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
buttons.Add(new ToastButton("End", $"{ACTION}={END}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
buttons.Add(new ToastButton(App.Current.ResourceLoader.GetString("Button_Hold\\Text"), $"{ACTION}={HOLD}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
buttons.Add(new ToastButton(App.Current.ResourceLoader.GetString("Button_End\\Text"), $"{ACTION}={END}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
break;
case CallState.Dialing:
buttons.Add(new ToastButton("End", $"{ACTION}={END}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
buttons.Add(new ToastButton(App.Current.ResourceLoader.GetString("Button_End\\Text"), $"{ACTION}={END}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
break;
case CallState.Incoming:
buttons.Add(new ToastButton("Text reply", $"{ACTION}={TEXT_REPLY}&{INCOMING_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Foreground });
buttons.Add(new ToastButton("Reject", $"{ACTION}={REJECT}&{INCOMING_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
buttons.Add(new ToastButton("Answer", $"{ACTION}={ANSWER}&{INCOMING_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Foreground });
buttons.Add(new ToastButton(App.Current.ResourceLoader.GetString("Button_TextReply\\Text"), $"{ACTION}={TEXT_REPLY}&{INCOMING_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Foreground });
buttons.Add(new ToastButton(App.Current.ResourceLoader.GetString("Button_Reject\\Text"), $"{ACTION}={REJECT}&{INCOMING_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
buttons.Add(new ToastButton(App.Current.ResourceLoader.GetString("Button_Answer\\Text"), $"{ACTION}={ANSWER}&{INCOMING_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Foreground });
break;
case CallState.OnHold:
buttons.Add(new ToastButton("Unhold", $"{ACTION}={UNHOLD}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
buttons.Add(new ToastButton("End", $"{ACTION}={END}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
buttons.Add(new ToastButton(App.Current.ResourceLoader.GetString("Button_Unhold\\Text"), $"{ACTION}={UNHOLD}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
buttons.Add(new ToastButton(App.Current.ResourceLoader.GetString("Button_End\\Text"), $"{ACTION}={END}&{ACTIVE_CALL_ID}={call.ID}") { ActivationType = ToastActivationType.Background });
break;
}
return buttons;
Expand All @@ -141,14 +147,14 @@ private IEnumerable<ToastButton> MergeButtons(IEnumerable<ToastButton> actions)
if (end != null)
{
_actions.RemoveAll(x => queries[x].GetFirstValueByName(ACTION) == END);
end = new ToastButton("End & Answer", $"{ACTION}={END_AND_ANSWER}&{ACTIVE_CALL_ID}={queries[end].GetFirstValueByName(ACTIVE_CALL_ID)}&{INCOMING_CALL_ID}={queries[answer].GetFirstValueByName(INCOMING_CALL_ID)}") { ActivationType = ToastActivationType.Foreground };
end = new ToastButton(App.Current.ResourceLoader.GetString("Button_End_And_Answer\\Text"), $"{ACTION}={END_AND_ANSWER}&{ACTIVE_CALL_ID}={queries[end].GetFirstValueByName(ACTIVE_CALL_ID)}&{INCOMING_CALL_ID}={queries[answer].GetFirstValueByName(INCOMING_CALL_ID)}") { ActivationType = ToastActivationType.Foreground };
queries.Add(end, new WwwFormUrlDecoder(end.Arguments));
_actions.Insert(0, end);
}
if (hold != null)
{
_actions.RemoveAll(x => queries[x].GetFirstValueByName(ACTION) == HOLD);
hold = new ToastButton("Hold & Answer", $"{ACTION}={HOLD_AND_ANSWER}&{ACTIVE_CALL_ID}={queries[hold].GetFirstValueByName(ACTIVE_CALL_ID)}&{INCOMING_CALL_ID}={queries[answer].GetFirstValueByName(INCOMING_CALL_ID)}") { ActivationType = ToastActivationType.Foreground };
hold = new ToastButton(App.Current.ResourceLoader.GetString("Button_Hold_And_Answer\\Text"), $"{ACTION}={HOLD_AND_ANSWER}&{ACTIVE_CALL_ID}={queries[hold].GetFirstValueByName(ACTIVE_CALL_ID)}&{INCOMING_CALL_ID}={queries[answer].GetFirstValueByName(INCOMING_CALL_ID)}") { ActivationType = ToastActivationType.Foreground };
queries.Add(hold, new WwwFormUrlDecoder(hold.Arguments));
_actions.Insert(0, hold);
}
Expand All @@ -163,7 +169,7 @@ private IEnumerable<ToastButton> MergeButtons(IEnumerable<ToastButton> actions)
private List<object> CreateVisualForCall(Call call)
{
StringBuilder description = new StringBuilder();
description.Append(call.State);
description.Append(CallViewModel.CallStateToTextString(call.State, call.StateReason));
if (call.Phone != null)
{
description.Append(" - ");
Expand All @@ -172,7 +178,7 @@ private List<object> CreateVisualForCall(Call call)
if (call.Line != null)
{
description.Append(" - ");
description.Append(call.Line.DisplayName ?? call.Line.NetworkName);
description.Append(string.IsNullOrEmpty(call.Line.DisplayName) ? call.Line.NetworkName : call.Line.DisplayName);
}
return new List<object>()
{
Expand Down Expand Up @@ -218,6 +224,19 @@ public void Initializate()
NotificationListener.NotificationChanged += NotificationListener_NotificationChanged;
}

private void RemoveCallToastNotifications(IEnumerable<ToastNotification> notifications)
{
foreach (ToastNotification notification in notifications)
{
switch (notification.Tag)
{
case CALL_NOTIFICATION_UI:
ToastNotificationManager.History.Remove(notification.Tag);
break;
}
}
}

public ToastNotification CreateMissedCallToastNotification(Call call)
{
ToastContent toastContent = new ToastContent()
Expand All @@ -228,15 +247,15 @@ public ToastNotification CreateMissedCallToastNotification(Call call)
{
Children =
{
new AdaptiveText() { Text = $"Missed call - {call.Contact?.DisplayName}" },
new AdaptiveText() { Text = $"{App.Current.ResourceLoader.GetString("Notification\\MissedCall\\Headline")} - {call.Contact?.DisplayName}" },
new AdaptiveText() { Text = call.Phone?.Number }
}
}
},
Scenario = ToastScenario.Default
};
ToastNotification notification = new ToastNotification(toastContent.GetXml());
notification.Group = "MissedCalls";
notification.Group = App.Current.ResourceLoader.GetString("Notification\\MissedCall\\GroupHeadline");
return notification;
}

Expand Down Expand Up @@ -302,7 +321,17 @@ public ToastNotification CreateCallNotification(IEnumerable<Call> currentCalls)
};
ToastNotification notification = new ToastNotification(toastContent.GetXml())
{
Tag = CALL_NOTIFICATION_UI
Tag = CALL_NOTIFICATION_UI,
ExpiresOnReboot = true,
Priority = ToastNotificationPriority.High,
Data = new NotificationData()
{
Values =
{
{ USED_CALLS, calls.Aggregate(new StringBuilder(), (x, y) => x.Append(y.ID).Append(';'), x => x.ToString()) },
{ USED_CALLS_STATES, calls.Aggregate(new StringBuilder(), (x, y) => x.Append((uint)y.State).Append(';'), x => x.ToString()) }
}
}
};
return notification;
}
Expand All @@ -328,28 +357,27 @@ public async void RemoveSystemToastNotificationIfExist()
}
}

public void RemoveCallToastNotifications()
public void RemoveCallToastNotifications() => RemoveCallToastNotifications(ToastNotificationManager.History.GetHistory());

public void RefreshCallNotification(IEnumerable<Call> currentCalls)
{
IReadOnlyList<ToastNotification> notifications = ToastNotificationManager.History.GetHistory();
foreach (ToastNotification notification in notifications)
bool badState = notifications.Count == 0 || notifications.Any(x =>
{
switch (notification.Tag)
List<uint> ids = x.Data.Values[USED_CALLS].Split(';').Where(y => !string.IsNullOrEmpty(y)).Select(y => uint.Parse(y)).ToList();
List<CallState> states = x.Data.Values[USED_CALLS_STATES].Split(';').Where(y => !string.IsNullOrEmpty(y)).Select(y => Enum.Parse<CallState>(y)).ToList();
List<(uint ID, CallState State)> prev = ids.Join(states, y => ids.IndexOf(y), y => states.IndexOf(y), (x, y) => (x, y)).ToList();
return !prev.All(y => currentCalls.Any(z => z.ID == y.ID && z.State == y.State));
});
if (badState)
{
RemoveCallToastNotifications(notifications);
ToastNotification notification = CreateCallNotification(currentCalls);
if (notification != null)
{
case CALL_NOTIFICATION_UI:
ToastNotificationManager.History.Remove(notification.Tag);
break;
ToastNotifier.Show(notification);
}
}
}

public void RefreshCallNotification(IEnumerable<Call> currentCalls)
{
RemoveCallToastNotifications();
ToastNotification notification = CreateCallNotification(currentCalls);
if (notification != null)
{
ToastNotifier.Show(notification);
}
}
}
}

0 comments on commit 78e8464

Please sign in to comment.