Skip to content

Commit a82319f

Browse files
committed
Snooze/dismiss support in button builders
1 parent 5553f45 commit a82319f

File tree

3 files changed

+165
-3
lines changed

3 files changed

+165
-3
lines changed

Microsoft.Toolkit.Uwp.Notifications/Toasts/ToastButton.cs

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ public sealed class ToastButton :
2020

2121
private bool _usingCustomArguments;
2222

23+
private bool _usingSnoozeActivation;
24+
private string _snoozeSelectionBoxId;
25+
26+
private bool _usingDismissActivation;
27+
2328
/// <summary>
2429
/// Initializes a new instance of the <see cref="ToastButton"/> class.
2530
/// </summary>
@@ -213,6 +218,11 @@ private ToastButton AddArgumentHelper(string key, string value)
213218
throw new InvalidOperationException("You cannot use the AddArgument methods when using protocol activation.");
214219
}
215220

221+
if (_usingDismissActivation || _usingSnoozeActivation)
222+
{
223+
throw new InvalidOperationException("You cannot use the AddArgument methods when using dismiss or snooze activation.");
224+
}
225+
216226
bool alreadyExists = _arguments.ContainsKey(key);
217227

218228
_arguments[key] = value;
@@ -314,6 +324,48 @@ public ToastButton SetAfterActivationBehavior(ToastAfterActivationBehavior after
314324
return this;
315325
}
316326

327+
/// <summary>
328+
/// Configures the button to use system snooze activation when the button is clicked, using the default system snooze time.
329+
/// </summary>
330+
/// <returns>The current instance of <see cref="ToastButton"/></returns>
331+
public ToastButton SetSnoozeActivation()
332+
{
333+
return SetSnoozeActivation(null);
334+
}
335+
336+
/// <summary>
337+
/// Configures the button to use system snooze activation when the button is clicked, with a snooze time defined by the specified selection box.
338+
/// </summary>
339+
/// <param name="selectionBoxId">The ID of an existing <see cref="ToastSelectionBox"/> which allows the user to pick a custom snooze time. The ID's of the <see cref="ToastSelectionBoxItem"/>s inside the selection box must represent the snooze interval in minutes. For example, if the user selects an item that has an ID of "120", then the notification will be snoozed for 2 hours. When the user clicks this button, if you specified a SelectionBoxId, the system will parse the ID of the selected item and snooze by that amount of minutes.</param>
340+
/// <returns>The current instance of <see cref="ToastButton"/></returns>
341+
public ToastButton SetSnoozeActivation(string selectionBoxId)
342+
{
343+
if (_arguments.Count > 0)
344+
{
345+
throw new InvalidOperationException($"{nameof(SetSnoozeActivation)} cannot be used in conjunction with ${nameof(AddArgument)}.");
346+
}
347+
348+
_usingSnoozeActivation = true;
349+
_snoozeSelectionBoxId = selectionBoxId;
350+
351+
return this;
352+
}
353+
354+
/// <summary>
355+
/// Configures the button to use system dismiss activation when the button is clicked (the toast will simply dismiss rather than activating).
356+
/// </summary>
357+
/// <returns>The current instance of <see cref="ToastButton"/></returns>
358+
public ToastButton SetDismissActivation()
359+
{
360+
if (_arguments.Count > 0)
361+
{
362+
throw new InvalidOperationException($"{nameof(SetDismissActivation)} cannot be used in conjunction with ${nameof(AddArgument)}.");
363+
}
364+
365+
_usingDismissActivation = true;
366+
return this;
367+
}
368+
317369
/// <summary>
318370
/// Sets an identifier used in telemetry to identify your category of action. This should be something like "Delete", "Reply", or "Archive". In the upcoming toast telemetry dashboard in Dev Center, you will be able to view how frequently your actions are being clicked.
319371
/// </summary>
@@ -349,7 +401,7 @@ public ToastButton SetTextBoxId(string textBoxId)
349401

350402
internal bool CanAddArguments()
351403
{
352-
return ActivationType != ToastActivationType.Protocol && !_usingCustomArguments;
404+
return ActivationType != ToastActivationType.Protocol && !_usingCustomArguments && !_usingDismissActivation && !_usingSnoozeActivation;
353405
}
354406

355407
internal bool ContainsArgument(string key)
@@ -362,13 +414,32 @@ internal Element_ToastAction ConvertToElement()
362414
var el = new Element_ToastAction()
363415
{
364416
Content = Content,
365-
Arguments = Arguments,
366-
ActivationType = Element_Toast.ConvertActivationType(ActivationType),
367417
ImageUri = ImageUri,
368418
InputId = TextBoxId,
369419
HintActionId = HintActionId
370420
};
371421

422+
if (_usingSnoozeActivation)
423+
{
424+
el.ActivationType = Element_ToastActivationType.System;
425+
el.Arguments = "snooze";
426+
427+
if (_snoozeSelectionBoxId != null)
428+
{
429+
el.InputId = _snoozeSelectionBoxId;
430+
}
431+
}
432+
else if (_usingDismissActivation)
433+
{
434+
el.ActivationType = Element_ToastActivationType.System;
435+
el.Arguments = "dismiss";
436+
}
437+
else
438+
{
439+
el.ActivationType = Element_Toast.ConvertActivationType(ActivationType);
440+
el.Arguments = Arguments;
441+
}
442+
372443
ActivationOptions?.PopulateElement(el);
373444

374445
return el;

UnitTests/UnitTests.Notifications.Shared/TestToastContentBuilder.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,36 @@ public void ToastButtonBuilders_InvalidProtocolAfterArguments_ReturnSelf()
405405
.SetProtocolActivation(new Uri("https://msn.com"));
406406
}
407407

408+
[TestMethod]
409+
[ExpectedException(typeof(InvalidOperationException))]
410+
public void ToastButtonBuilders_InvalidDismissAfterArguments_ReturnSelf()
411+
{
412+
new ToastButton()
413+
.SetContent("View")
414+
.AddArgument("action", "view")
415+
.SetDismissActivation();
416+
}
417+
418+
[TestMethod]
419+
[ExpectedException(typeof(InvalidOperationException))]
420+
public void ToastButtonBuilders_InvalidSnoozeAfterArguments_ReturnSelf()
421+
{
422+
new ToastButton()
423+
.SetContent("View")
424+
.AddArgument("action", "view")
425+
.SetSnoozeActivation();
426+
}
427+
428+
[TestMethod]
429+
[ExpectedException(typeof(InvalidOperationException))]
430+
public void ToastButtonBuilders_InvalidSnoozeWithIdAfterArguments_ReturnSelf()
431+
{
432+
new ToastButton()
433+
.SetContent("View")
434+
.AddArgument("action", "view")
435+
.SetSnoozeActivation("snoozeId");
436+
}
437+
408438
[TestMethod]
409439
[ExpectedException(typeof(InvalidOperationException))]
410440
public void ToastButtonBuilders_InvalidArgumentsAfterProtocol_ReturnSelf()
@@ -424,6 +454,36 @@ public void ToastButtonBuilders_InvalidArgumentsAfterCustomArguments_ReturnSelf(
424454
button.AddArgument("action", "view");
425455
}
426456

457+
[TestMethod]
458+
[ExpectedException(typeof(InvalidOperationException))]
459+
public void ToastButtonBuilders_InvalidArgumentsAfterSnooze_ReturnSelf()
460+
{
461+
new ToastButton()
462+
.SetContent("Later")
463+
.SetSnoozeActivation()
464+
.AddArgument("action", "later");
465+
}
466+
467+
[TestMethod]
468+
[ExpectedException(typeof(InvalidOperationException))]
469+
public void ToastButtonBuilders_InvalidArgumentsAfterSnoozeWithId_ReturnSelf()
470+
{
471+
new ToastButton()
472+
.SetContent("Later")
473+
.SetSnoozeActivation("myId")
474+
.AddArgument("action", "later");
475+
}
476+
477+
[TestMethod]
478+
[ExpectedException(typeof(InvalidOperationException))]
479+
public void ToastButtonBuilders_InvalidArgumentsAfterDismissActivation_ReturnSelf()
480+
{
481+
new ToastButton()
482+
.SetContent("Later")
483+
.SetDismissActivation()
484+
.AddArgument("action", "later");
485+
}
486+
427487
[TestMethod]
428488
public void SetToastDurationTest_WithCustomToastDuration_ReturnSelfWithCustomToastDurationSet()
429489
{

UnitTests/UnitTests.Notifications.Shared/Test_Toast_Xml.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,37 @@ public void Test_Toast_Xml_Actions_SixTotal()
977977
Assert.Fail("Exception should have been thrown, only 5 actions are allowed.");
978978
}
979979

980+
[TestMethod]
981+
public void Test_Toast_Xml_Actions_SnoozeAndDismissUsingBuilders()
982+
{
983+
AssertActionsPayload("<actions><action activationType='system' arguments='snooze'/><action activationType='system' arguments='dismiss'/><action content='Hide' activationType='system' arguments='dismiss'/><action content='Later' activationType='system' arguments='snooze'/><action content='Remind me' activationType='system' arguments='snooze' hint-inputId='snoozePicker'/></actions>", new ToastActionsCustom()
984+
{
985+
Buttons =
986+
{
987+
// Allowing system to auto-generate text content
988+
new ToastButton()
989+
.SetSnoozeActivation(),
990+
991+
// Allowing system to auto-generate text content
992+
new ToastButton()
993+
.SetDismissActivation(),
994+
995+
// Specifying specific content
996+
new ToastButton()
997+
.SetContent("Hide")
998+
.SetDismissActivation(),
999+
1000+
new ToastButton()
1001+
.SetContent("Later")
1002+
.SetSnoozeActivation(),
1003+
1004+
new ToastButton()
1005+
.SetContent("Remind me")
1006+
.SetSnoozeActivation("snoozePicker")
1007+
}
1008+
});
1009+
}
1010+
9801011
[TestMethod]
9811012
public void Test_Toast_Xml_Actions_TwoContextMenuItems()
9821013
{

0 commit comments

Comments
 (0)