Skip to content

Commit 22c61b2

Browse files
Merge branch 'master' into rename/animationset-ended
2 parents 7b4acf3 + 10fbbad commit 22c61b2

File tree

6 files changed

+192
-8
lines changed

6 files changed

+192
-8
lines changed

Microsoft.Toolkit.Uwp.Notifications/Toasts/Builder/ToastContentBuilder.Actions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public ToastContentBuilder AddButton(string content, ToastActivationType activat
112112
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
113113
public ToastContentBuilder AddButton(IToastButton button)
114114
{
115-
if (button is ToastButton toastButton && toastButton.Content == null)
115+
if (button is ToastButton toastButton && toastButton.Content == null && toastButton.NeedsContent())
116116
{
117117
throw new InvalidOperationException("Content is required on button.");
118118
}

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

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

2121
private bool _usingCustomArguments;
2222

23+
private bool _usingSnoozeActivation;
24+
private string _snoozeSelectionBoxId;
25+
26+
private bool _usingDismissActivation;
27+
28+
internal bool NeedsContent()
29+
{
30+
// Snooze/dismiss buttons don't need content (the system will auto-add the localized strings).
31+
return !_usingDismissActivation && !_usingSnoozeActivation;
32+
}
33+
2334
/// <summary>
2435
/// Initializes a new instance of the <see cref="ToastButton"/> class.
2536
/// </summary>
@@ -213,6 +224,11 @@ private ToastButton AddArgumentHelper(string key, string value)
213224
throw new InvalidOperationException("You cannot use the AddArgument methods when using protocol activation.");
214225
}
215226

227+
if (_usingDismissActivation || _usingSnoozeActivation)
228+
{
229+
throw new InvalidOperationException("You cannot use the AddArgument methods when using dismiss or snooze activation.");
230+
}
231+
216232
bool alreadyExists = _arguments.ContainsKey(key);
217233

218234
_arguments[key] = value;
@@ -314,6 +330,48 @@ public ToastButton SetAfterActivationBehavior(ToastAfterActivationBehavior after
314330
return this;
315331
}
316332

333+
/// <summary>
334+
/// Configures the button to use system snooze activation when the button is clicked, using the default system snooze time.
335+
/// </summary>
336+
/// <returns>The current instance of <see cref="ToastButton"/></returns>
337+
public ToastButton SetSnoozeActivation()
338+
{
339+
return SetSnoozeActivation(null);
340+
}
341+
342+
/// <summary>
343+
/// Configures the button to use system snooze activation when the button is clicked, with a snooze time defined by the specified selection box.
344+
/// </summary>
345+
/// <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>
346+
/// <returns>The current instance of <see cref="ToastButton"/></returns>
347+
public ToastButton SetSnoozeActivation(string selectionBoxId)
348+
{
349+
if (_arguments.Count > 0)
350+
{
351+
throw new InvalidOperationException($"{nameof(SetSnoozeActivation)} cannot be used in conjunction with ${nameof(AddArgument)}.");
352+
}
353+
354+
_usingSnoozeActivation = true;
355+
_snoozeSelectionBoxId = selectionBoxId;
356+
357+
return this;
358+
}
359+
360+
/// <summary>
361+
/// Configures the button to use system dismiss activation when the button is clicked (the toast will simply dismiss rather than activating).
362+
/// </summary>
363+
/// <returns>The current instance of <see cref="ToastButton"/></returns>
364+
public ToastButton SetDismissActivation()
365+
{
366+
if (_arguments.Count > 0)
367+
{
368+
throw new InvalidOperationException($"{nameof(SetDismissActivation)} cannot be used in conjunction with ${nameof(AddArgument)}.");
369+
}
370+
371+
_usingDismissActivation = true;
372+
return this;
373+
}
374+
317375
/// <summary>
318376
/// 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.
319377
/// </summary>
@@ -349,7 +407,7 @@ public ToastButton SetTextBoxId(string textBoxId)
349407

350408
internal bool CanAddArguments()
351409
{
352-
return ActivationType != ToastActivationType.Protocol && !_usingCustomArguments;
410+
return ActivationType != ToastActivationType.Protocol && !_usingCustomArguments && !_usingDismissActivation && !_usingSnoozeActivation;
353411
}
354412

355413
internal bool ContainsArgument(string key)
@@ -362,13 +420,44 @@ internal Element_ToastAction ConvertToElement()
362420
var el = new Element_ToastAction()
363421
{
364422
Content = Content,
365-
Arguments = Arguments,
366-
ActivationType = Element_Toast.ConvertActivationType(ActivationType),
367423
ImageUri = ImageUri,
368424
InputId = TextBoxId,
369425
HintActionId = HintActionId
370426
};
371427

428+
if (_usingSnoozeActivation)
429+
{
430+
el.ActivationType = Element_ToastActivationType.System;
431+
el.Arguments = "snooze";
432+
433+
if (_snoozeSelectionBoxId != null)
434+
{
435+
el.InputId = _snoozeSelectionBoxId;
436+
}
437+
438+
// Content needs to be specified as empty for auto-generated Snooze content
439+
if (el.Content == null)
440+
{
441+
el.Content = string.Empty;
442+
}
443+
}
444+
else if (_usingDismissActivation)
445+
{
446+
el.ActivationType = Element_ToastActivationType.System;
447+
el.Arguments = "dismiss";
448+
449+
// Content needs to be specified as empty for auto-generated Dismiss content
450+
if (el.Content == null)
451+
{
452+
el.Content = string.Empty;
453+
}
454+
}
455+
else
456+
{
457+
el.ActivationType = Element_Toast.ConvertActivationType(ActivationType);
458+
el.Arguments = Arguments;
459+
}
460+
372461
ActivationOptions?.PopulateElement(el);
373462

374463
return el;

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Toast/ToastCode.bind

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ private void PopToast()
1313
("60", "1 hour"),
1414
("240", "4 hours"),
1515
("1440", "1 day"))
16-
.AddButton(new ToastButtonSnooze() { SelectionBoxId = "snoozeTime" })
17-
.AddButton(new ToastButtonDismiss())
16+
.AddButton(new ToastButton()
17+
.SetSnoozeActivation("snoozeTime"))
18+
.AddButton(new ToastButton()
19+
.SetDismissActivation())
1820
.Show();
1921
}

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/Toast/ToastPage.xaml.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ public static ToastContent GenerateToastContent()
4040
("60", "1 hour"),
4141
("240", "4 hours"),
4242
("1440", "1 day"))
43-
.AddButton(new ToastButtonSnooze() { SelectionBoxId = "snoozeTime" })
44-
.AddButton(new ToastButtonDismiss());
43+
.AddButton(new ToastButton()
44+
.SetSnoozeActivation("snoozeTime"))
45+
.AddButton(new ToastButton()
46+
.SetDismissActivation());
4547

4648
return builder.Content;
4749
}

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 content='' activationType='system' arguments='snooze'/><action content='' 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)