Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 43b8dbd

Browse files
VladislavAntonyukjfversluisAndreiMisiukevich
authored
#474 Add setters to the snackbar options, #477 Snackbar default settings (#508)
* #474 Add setters to the snackbar options #477 Snackbar default settings * Use TimeSpan for Duration Co-authored-by: Gerald Versluis <[email protected]> Co-authored-by: Andrei <[email protected]> Co-authored-by: Vladislav Antonyuk <[email protected]>
1 parent d41a3b0 commit 43b8dbd

File tree

16 files changed

+213
-107
lines changed

16 files changed

+213
-107
lines changed

README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,87 @@ The toolkit is available via NuGet, and should be installed into all your projec
2525

2626
Now all you need to do is use it!
2727

28-
For example, to use the `AvatarView` you first include the toolkit namespace:
28+
## Documentation
29+
30+
### Toast
31+
32+
There are 2 different ways to use Toast.
33+
1. On your Page call the method:
34+
```csharp
35+
await MyPage.DisplayToastAsync(message, duration);
36+
```
37+
where `message` is your text, and duration is optional parameter. Default duration = 3000;
38+
39+
2. For advanced settings:
40+
```csharp
41+
var messageOptions = new MessageOptions
42+
{
43+
Foreground = Color.Black,
44+
FontSize = 14,
45+
FontFamily = "Arial",
46+
Message = "My text"
47+
};
48+
var options = new ToastOptions
49+
{
50+
MessageOptions = messageOptions,
51+
Duration = 3000,
52+
BackgroundColor = Color.Default,
53+
IsRtl = false,
54+
};
55+
await this.DisplayToastAsync(options);
56+
```
57+
58+
### Snackbar
59+
60+
Snackbar has API which is similar to the Toast.
61+
1. On your Page call the method:
62+
```csharp
63+
var result = await MyPage.DisplaySnackbarAsync(message, actionButtonText, action, duration);
64+
```
65+
where `message` is your text, `actionButtonText` is the text for the button, `action` is a `Func<Task>` and duration is optional parameter. Default duration = 3000;
66+
67+
2. For advanced settings:
68+
```csharp
69+
var messageOptions = new MessageOptions
70+
{
71+
Foreground = Color.Black,
72+
FontSize = 14,
73+
FontFamily = "Arial",
74+
Message = "My text"
75+
};
76+
var actionOptions = new List<SnackBarActionOptions>
77+
{
78+
new SnackBarActionOptions
79+
{
80+
ForegroundColor = Color.Black,
81+
BackgroundColor = Color.White,
82+
FontSize = 14,
83+
FontFamily = "Arial",
84+
Text = "My text",
85+
Action = () => // null by default
86+
{
87+
Debug.WriteLine("1");
88+
return Task.CompletedTask;
89+
}
90+
}
91+
};
92+
var options = new SnackbarOptions
93+
{
94+
MessageOptions = messageOptions,
95+
Duration = 3000,
96+
BackgroundColor = Color.Default,
97+
IsRtl = false,
98+
Actions = actionOptions
99+
};
100+
var result = await this.DisplayToastAsync(options);
101+
```
102+
The result is `Boolean`. True - if snackbar is closed by user. False - if snackbar is closed by timeout.
103+
104+
105+
106+
### AvatarView
107+
108+
You first include the toolkit namespace:
29109

30110
```xaml
31111
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"

XamarinCommunityToolkit.UnitTests/Views/SnackBar_Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public async void PageExtension_DisplayToastAsync_PlatformNotSupportedException(
3636
public async void PageExtension_DisplayToastAsyncWithOptions_PlatformNotSupportedException()
3737
{
3838
var page = new ContentPage();
39-
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => page.DisplayToastAsync(Arg.Any<ActionOptions>()));
39+
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => page.DisplayToastAsync(Arg.Any<ToastOptions>()));
4040
}
4141
#endif
4242
}

XamarinCommunityToolkit/Extensions/PageExtension.shared.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,40 @@ namespace Xamarin.CommunityToolkit.Extensions
1010
{
1111
public static class PageExtension
1212
{
13-
public static Task<bool> DisplayToastAsync(this Page page, string message, int duration = 3000)
13+
public static Task DisplayToastAsync(this Page page, string message, int durationMilliseconds = 3000)
1414
{
1515
var messageOptions = new MessageOptions { Message = message };
16-
var args = new SnackBarOptions(messageOptions,
17-
duration,
18-
Color.Default,
16+
var args = new SnackBarOptions
17+
{
18+
MessageOptions = messageOptions,
19+
Duration = TimeSpan.FromMilliseconds(durationMilliseconds),
1920
#if NETSTANDARD1_0
20-
false,
21+
IsRtl = false,
2122
#else
22-
CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
23+
IsRtl = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
2324
#endif
24-
new List<SnackBarActionOptions>());
25+
};
2526
var snackBar = new SnackBar();
2627
snackBar.Show(page, args);
2728
return args.Result.Task;
2829
}
2930

30-
public static Task<bool> DisplayToastAsync(this Page page, ActionOptions actionOptions)
31+
public static Task DisplayToastAsync(this Page page, ToastOptions toastOptions)
3132
{
3233
var snackBar = new SnackBar();
33-
var arguments = actionOptions ?? new ActionOptions();
34-
var options = new SnackBarOptions(arguments.MessageOptions, arguments.Duration, arguments.BackgroundColor, arguments.IsRtl, null);
34+
var arguments = toastOptions ?? new ToastOptions();
35+
var options = new SnackBarOptions
36+
{
37+
MessageOptions = arguments.MessageOptions,
38+
Duration = arguments.Duration,
39+
BackgroundColor = arguments.BackgroundColor,
40+
IsRtl = arguments.IsRtl
41+
};
3542
snackBar.Show(page, options);
3643
return options.Result.Task;
3744
}
3845

39-
public static Task<bool> DisplaySnackBarAsync(this Page page, string message, string actionButtonText, Func<Task> action, int duration = 3000)
46+
public static Task<bool> DisplaySnackBarAsync(this Page page, string message, string actionButtonText, Func<Task> action, int durationMilliseconds = 3000)
4047
{
4148
var messageOptions = new MessageOptions { Message = message };
4249
var actionOptions = new List<SnackBarActionOptions>
@@ -46,18 +53,20 @@ public static Task<bool> DisplaySnackBarAsync(this Page page, string message, st
4653
Text = actionButtonText, Action = action
4754
}
4855
};
49-
var args = new SnackBarOptions(messageOptions,
50-
duration,
51-
Color.Default,
56+
var options = new SnackBarOptions
57+
{
58+
MessageOptions = messageOptions,
59+
Duration = TimeSpan.FromMilliseconds(durationMilliseconds),
60+
Actions = actionOptions,
5261
#if NETSTANDARD1_0
53-
false,
62+
IsRtl = false,
5463
#else
55-
CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
64+
IsRtl = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
5665
#endif
57-
actionOptions);
66+
};
5867
var snackBar = new SnackBar();
59-
snackBar.Show(page, args);
60-
return args.Result.Task;
68+
snackBar.Show(page, options);
69+
return options.Result.Task;
6170
}
6271

6372
public static Task<bool> DisplaySnackBarAsync(this Page page, SnackBarOptions snackBarOptions)

XamarinCommunityToolkit/Views/SnackBar/Options/ActionOptions.shared.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

XamarinCommunityToolkit/Views/SnackBar/Options/MessageOptions.shared.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,29 @@ public class MessageOptions
77
/// <summary>
88
/// Gets or sets the message for the SnackBar.
99
/// </summary>
10-
public string Message { get; set; }
10+
public string Message { get; set; } = DefaultMessage;
11+
12+
public static string DefaultMessage { get; set; }
1113

1214
/// <summary>
1315
/// Gets or sets the font size for the SnackBar message.
1416
/// </summary>
15-
public double FontSize { get; set; } = 14;
17+
public double FontSize { get; set; } = DefaultFontSize;
18+
19+
public static double DefaultFontSize { get; set; } = 14;
1620

1721
/// <summary>
1822
/// Gets or sets the font family for the SnackBar message.
1923
/// </summary>
20-
public string FontFamily { get; set; } = "Arial";
24+
public string FontFamily { get; set; } = DefaultFontFamily;
25+
26+
public static string DefaultFontFamily { get; set; } = "Arial";
2127

2228
/// <summary>
2329
/// Gets or sets the font color for the SnackBar message.
2430
/// </summary>
25-
public Color Foreground { get; set; } = Color.Black;
31+
public Color Foreground { get; set; } = DefaultForeground;
32+
33+
public static Color DefaultForeground { get; set; } = Color.Black;
2634
}
2735
}

XamarinCommunityToolkit/Views/SnackBar/Options/SnackBarActionOptions.shared.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,43 @@ public class SnackBarActionOptions
99
/// <summary>
1010
/// Gets or sets the action for the SnackBar action button.
1111
/// </summary>
12-
public Func<Task> Action { get; set; }
12+
public Func<Task> Action { get; set; } = DefaultAction;
13+
14+
public static Func<Task> DefaultAction { get; set; } = null;
1315

1416
/// <summary>
1517
/// Gets or sets the text for the SnackBar action button.
1618
/// </summary>
17-
public string Text { get; set; }
19+
public string Text { get; set; } = DefaultText;
20+
21+
public static string DefaultText { get; set; }
1822

1923
/// <summary>
2024
/// Gets or sets the font size for the SnackBar action button.
2125
/// </summary>
22-
public double FontSize { get; set; } = 14;
26+
public double FontSize { get; set; } = DefaultFontSize;
27+
28+
public static double DefaultFontSize { get; set; } = 14;
2329

2430
/// <summary>
2531
/// Gets or sets the font family for the SnackBar action button.
2632
/// </summary>
27-
public string FontFamily { get; set; } = "Arial";
33+
public string FontFamily { get; set; } = DefaultFontFamily;
34+
35+
public static string DefaultFontFamily { get; set; } = "Arial";
2836

2937
/// <summary>
3038
/// Gets or sets the background color for the SnackBar action button.
3139
/// </summary>
32-
public Color BackgroundColor { get; set; } = Color.White;
40+
public Color BackgroundColor { get; set; } = DefaultBackgroundColor;
41+
42+
public static Color DefaultBackgroundColor { get; set; } = Color.White;
3343

3444
/// <summary>
3545
/// Gets or sets the font color for the SnackBar action button.
3646
/// </summary>
37-
public Color ForegroundColor { get; set; } = Color.Black;
47+
public Color ForegroundColor { get; set; } = DefaultForegroundColor;
48+
49+
public static Color DefaultForegroundColor { get; set; } = Color.Black;
3850
}
3951
}

XamarinCommunityToolkit/Views/SnackBar/Options/SnackBarOptions.shared.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44

55
namespace Xamarin.CommunityToolkit.UI.Views.Options
66
{
7-
public class SnackBarOptions : ActionOptions
7+
public class SnackBarOptions : ToastOptions
88
{
9-
public SnackBarOptions(MessageOptions message, int duration, Color backgroundColor, bool isRtl, IEnumerable<SnackBarActionOptions> actions)
10-
: base(message, duration, backgroundColor, isRtl) =>
11-
Actions = actions ?? Enumerable.Empty<SnackBarActionOptions>();
12-
13-
public SnackBarOptions() => Actions = Enumerable.Empty<SnackBarActionOptions>();
14-
159
/// <summary>
16-
/// Gets the text for the action buttons
10+
/// Action options
1711
/// </summary>
18-
public IEnumerable<SnackBarActionOptions> Actions { get; }
12+
public IEnumerable<SnackBarActionOptions> Actions { get; set; } = DefaultActions;
13+
14+
public static IEnumerable<SnackBarActionOptions> DefaultActions { get; set; } = Enumerable.Empty<SnackBarActionOptions>();
1915
}
2016
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Xamarin.Forms;
4+
5+
namespace Xamarin.CommunityToolkit.UI.Views.Options
6+
{
7+
/// <summary>
8+
/// Toast options
9+
/// </summary>
10+
public class ToastOptions
11+
{
12+
public ToastOptions() => Result = new TaskCompletionSource<bool>(false);
13+
14+
/// <summary>
15+
/// Message options: Message, color, font
16+
/// </summary>
17+
public MessageOptions MessageOptions { get; set; } = DefaultMessageOptions;
18+
19+
public static MessageOptions DefaultMessageOptions { get; set; } = new MessageOptions();
20+
21+
/// <summary>
22+
/// Background color.
23+
/// </summary>
24+
public Color BackgroundColor { get; set; } = DefaultBackgroundColor;
25+
26+
public static Color DefaultBackgroundColor { get; set; } = Color.Default;
27+
28+
/// <summary>
29+
/// Is Right to left
30+
/// </summary>
31+
public bool IsRtl { get; set; } = DefaultIsRtl;
32+
33+
public static bool DefaultIsRtl { get; set; } = false;
34+
35+
/// <summary>
36+
/// The duration for the SnackBar.
37+
/// </summary>
38+
public TimeSpan Duration { get; set; } = DefaultDuration;
39+
40+
public static TimeSpan DefaultDuration { get; set; } = TimeSpan.FromMilliseconds(3000);
41+
42+
/// <summary>
43+
/// Result is true if ActionButton is clicked.
44+
/// </summary>
45+
public TaskCompletionSource<bool> Result { get; }
46+
47+
public void SetResult(bool result) => Result.TrySetResult(result);
48+
}
49+
}

0 commit comments

Comments
 (0)