Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public ToastContentBuilder AddButton(string content, ToastActivationType activat
/// <returns>The current instance of <see cref="ToastContentBuilder"/></returns>
public ToastContentBuilder AddButton(IToastButton button)
{
if (button is ToastButton toastButton && toastButton.Content == null)
if (button is ToastButton toastButton && toastButton.Content == null && toastButton.NeedsContent())
{
throw new InvalidOperationException("Content is required on button.");
}
Expand Down
95 changes: 92 additions & 3 deletions Microsoft.Toolkit.Uwp.Notifications/Toasts/ToastButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ public sealed class ToastButton :

private bool _usingCustomArguments;

private bool _usingSnoozeActivation;
private string _snoozeSelectionBoxId;

private bool _usingDismissActivation;

internal bool NeedsContent()
{
// Snooze/dismiss buttons don't need content (the system will auto-add the localized strings).
return !_usingDismissActivation && !_usingSnoozeActivation;
}

/// <summary>
/// Initializes a new instance of the <see cref="ToastButton"/> class.
/// </summary>
Expand Down Expand Up @@ -213,6 +224,11 @@ private ToastButton AddArgumentHelper(string key, string value)
throw new InvalidOperationException("You cannot use the AddArgument methods when using protocol activation.");
}

if (_usingDismissActivation || _usingSnoozeActivation)
{
throw new InvalidOperationException("You cannot use the AddArgument methods when using dismiss or snooze activation.");
}

bool alreadyExists = _arguments.ContainsKey(key);

_arguments[key] = value;
Expand Down Expand Up @@ -314,6 +330,48 @@ public ToastButton SetAfterActivationBehavior(ToastAfterActivationBehavior after
return this;
}

/// <summary>
/// Configures the button to use system snooze activation when the button is clicked, using the default system snooze time.
/// </summary>
/// <returns>The current instance of <see cref="ToastButton"/></returns>
public ToastButton SetSnoozeActivation()
{
return SetSnoozeActivation(null);
}

/// <summary>
/// Configures the button to use system snooze activation when the button is clicked, with a snooze time defined by the specified selection box.
/// </summary>
/// <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>
/// <returns>The current instance of <see cref="ToastButton"/></returns>
public ToastButton SetSnoozeActivation(string selectionBoxId)
{
if (_arguments.Count > 0)
{
throw new InvalidOperationException($"{nameof(SetSnoozeActivation)} cannot be used in conjunction with ${nameof(AddArgument)}.");
}

_usingSnoozeActivation = true;
_snoozeSelectionBoxId = selectionBoxId;

return this;
}

/// <summary>
/// Configures the button to use system dismiss activation when the button is clicked (the toast will simply dismiss rather than activating).
/// </summary>
/// <returns>The current instance of <see cref="ToastButton"/></returns>
public ToastButton SetDismissActivation()
{
if (_arguments.Count > 0)
{
throw new InvalidOperationException($"{nameof(SetDismissActivation)} cannot be used in conjunction with ${nameof(AddArgument)}.");
}

_usingDismissActivation = true;
return this;
}

/// <summary>
/// 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.
/// </summary>
Expand Down Expand Up @@ -349,7 +407,7 @@ public ToastButton SetTextBoxId(string textBoxId)

internal bool CanAddArguments()
{
return ActivationType != ToastActivationType.Protocol && !_usingCustomArguments;
return ActivationType != ToastActivationType.Protocol && !_usingCustomArguments && !_usingDismissActivation && !_usingSnoozeActivation;
}

internal bool ContainsArgument(string key)
Expand All @@ -362,13 +420,44 @@ internal Element_ToastAction ConvertToElement()
var el = new Element_ToastAction()
{
Content = Content,
Arguments = Arguments,
ActivationType = Element_Toast.ConvertActivationType(ActivationType),
ImageUri = ImageUri,
InputId = TextBoxId,
HintActionId = HintActionId
};

if (_usingSnoozeActivation)
{
el.ActivationType = Element_ToastActivationType.System;
el.Arguments = "snooze";

if (_snoozeSelectionBoxId != null)
{
el.InputId = _snoozeSelectionBoxId;
}

// Content needs to be specified as empty for auto-generated Snooze content
if (el.Content == null)
{
el.Content = string.Empty;
}
}
else if (_usingDismissActivation)
{
el.ActivationType = Element_ToastActivationType.System;
el.Arguments = "dismiss";

// Content needs to be specified as empty for auto-generated Dismiss content
if (el.Content == null)
{
el.Content = string.Empty;
}
}
else
{
el.ActivationType = Element_Toast.ConvertActivationType(ActivationType);
el.Arguments = Arguments;
}

ActivationOptions?.PopulateElement(el);

return el;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ private void PopToast()
("60", "1 hour"),
("240", "4 hours"),
("1440", "1 day"))
.AddButton(new ToastButtonSnooze() { SelectionBoxId = "snoozeTime" })
.AddButton(new ToastButtonDismiss())
.AddButton(new ToastButton()
.SetSnoozeActivation("snoozeTime"))
.AddButton(new ToastButton()
.SetDismissActivation())
.Show();
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public static ToastContent GenerateToastContent()
("60", "1 hour"),
("240", "4 hours"),
("1440", "1 day"))
.AddButton(new ToastButtonSnooze() { SelectionBoxId = "snoozeTime" })
.AddButton(new ToastButtonDismiss());
.AddButton(new ToastButton()
.SetSnoozeActivation("snoozeTime"))
.AddButton(new ToastButton()
.SetDismissActivation());

return builder.Content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,36 @@ public void ToastButtonBuilders_InvalidProtocolAfterArguments_ReturnSelf()
.SetProtocolActivation(new Uri("https://msn.com"));
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ToastButtonBuilders_InvalidDismissAfterArguments_ReturnSelf()
{
new ToastButton()
.SetContent("View")
.AddArgument("action", "view")
.SetDismissActivation();
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ToastButtonBuilders_InvalidSnoozeAfterArguments_ReturnSelf()
{
new ToastButton()
.SetContent("View")
.AddArgument("action", "view")
.SetSnoozeActivation();
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ToastButtonBuilders_InvalidSnoozeWithIdAfterArguments_ReturnSelf()
{
new ToastButton()
.SetContent("View")
.AddArgument("action", "view")
.SetSnoozeActivation("snoozeId");
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ToastButtonBuilders_InvalidArgumentsAfterProtocol_ReturnSelf()
Expand All @@ -424,6 +454,36 @@ public void ToastButtonBuilders_InvalidArgumentsAfterCustomArguments_ReturnSelf(
button.AddArgument("action", "view");
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ToastButtonBuilders_InvalidArgumentsAfterSnooze_ReturnSelf()
{
new ToastButton()
.SetContent("Later")
.SetSnoozeActivation()
.AddArgument("action", "later");
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ToastButtonBuilders_InvalidArgumentsAfterSnoozeWithId_ReturnSelf()
{
new ToastButton()
.SetContent("Later")
.SetSnoozeActivation("myId")
.AddArgument("action", "later");
}

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ToastButtonBuilders_InvalidArgumentsAfterDismissActivation_ReturnSelf()
{
new ToastButton()
.SetContent("Later")
.SetDismissActivation()
.AddArgument("action", "later");
}

[TestMethod]
public void SetToastDurationTest_WithCustomToastDuration_ReturnSelfWithCustomToastDurationSet()
{
Expand Down
31 changes: 31 additions & 0 deletions UnitTests/UnitTests.Notifications.Shared/Test_Toast_Xml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,37 @@ public void Test_Toast_Xml_Actions_SixTotal()
Assert.Fail("Exception should have been thrown, only 5 actions are allowed.");
}

[TestMethod]
public void Test_Toast_Xml_Actions_SnoozeAndDismissUsingBuilders()
{
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()
{
Buttons =
{
// Allowing system to auto-generate text content
new ToastButton()
.SetSnoozeActivation(),

// Allowing system to auto-generate text content
new ToastButton()
.SetDismissActivation(),

// Specifying specific content
new ToastButton()
.SetContent("Hide")
.SetDismissActivation(),

new ToastButton()
.SetContent("Later")
.SetSnoozeActivation(),

new ToastButton()
.SetContent("Remind me")
.SetSnoozeActivation("snoozePicker")
}
});
}

[TestMethod]
public void Test_Toast_Xml_Actions_TwoContextMenuItems()
{
Expand Down