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

Commit

Permalink
#474 Add setters to the snackbar options, #477 Snackbar default setti…
Browse files Browse the repository at this point in the history
…ngs (#508)

* #474 Add setters to the snackbar options

#477 Snackbar default settings

* Use TimeSpan for Duration

Co-authored-by: Gerald Versluis <gerald.versluis@microsoft.com>
Co-authored-by: Andrei <andrei.misiukevich@gmail.com>
Co-authored-by: Vladislav Antonyuk <vlad.antonyuk@outlook.com>
  • Loading branch information
4 people authored Nov 17, 2020
1 parent d41a3b0 commit 43b8dbd
Show file tree
Hide file tree
Showing 16 changed files with 213 additions and 107 deletions.
82 changes: 81 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,87 @@ The toolkit is available via NuGet, and should be installed into all your projec

Now all you need to do is use it!

For example, to use the `AvatarView` you first include the toolkit namespace:
## Documentation

### Toast

There are 2 different ways to use Toast.
1. On your Page call the method:
```csharp
await MyPage.DisplayToastAsync(message, duration);
```
where `message` is your text, and duration is optional parameter. Default duration = 3000;

2. For advanced settings:
```csharp
var messageOptions = new MessageOptions
{
Foreground = Color.Black,
FontSize = 14,
FontFamily = "Arial",
Message = "My text"
};
var options = new ToastOptions
{
MessageOptions = messageOptions,
Duration = 3000,
BackgroundColor = Color.Default,
IsRtl = false,
};
await this.DisplayToastAsync(options);
```

### Snackbar

Snackbar has API which is similar to the Toast.
1. On your Page call the method:
```csharp
var result = await MyPage.DisplaySnackbarAsync(message, actionButtonText, action, duration);
```
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;

2. For advanced settings:
```csharp
var messageOptions = new MessageOptions
{
Foreground = Color.Black,
FontSize = 14,
FontFamily = "Arial",
Message = "My text"
};
var actionOptions = new List<SnackBarActionOptions>
{
new SnackBarActionOptions
{
ForegroundColor = Color.Black,
BackgroundColor = Color.White,
FontSize = 14,
FontFamily = "Arial",
Text = "My text",
Action = () => // null by default
{
Debug.WriteLine("1");
return Task.CompletedTask;
}
}
};
var options = new SnackbarOptions
{
MessageOptions = messageOptions,
Duration = 3000,
BackgroundColor = Color.Default,
IsRtl = false,
Actions = actionOptions
};
var result = await this.DisplayToastAsync(options);
```
The result is `Boolean`. True - if snackbar is closed by user. False - if snackbar is closed by timeout.



### AvatarView

You first include the toolkit namespace:

```xaml
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
Expand Down
2 changes: 1 addition & 1 deletion XamarinCommunityToolkit.UnitTests/Views/SnackBar_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async void PageExtension_DisplayToastAsync_PlatformNotSupportedException(
public async void PageExtension_DisplayToastAsyncWithOptions_PlatformNotSupportedException()
{
var page = new ContentPage();
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => page.DisplayToastAsync(Arg.Any<ActionOptions>()));
await Assert.ThrowsAsync<PlatformNotSupportedException>(() => page.DisplayToastAsync(Arg.Any<ToastOptions>()));
}
#endif
}
Expand Down
47 changes: 28 additions & 19 deletions XamarinCommunityToolkit/Extensions/PageExtension.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,40 @@ namespace Xamarin.CommunityToolkit.Extensions
{
public static class PageExtension
{
public static Task<bool> DisplayToastAsync(this Page page, string message, int duration = 3000)
public static Task DisplayToastAsync(this Page page, string message, int durationMilliseconds = 3000)
{
var messageOptions = new MessageOptions { Message = message };
var args = new SnackBarOptions(messageOptions,
duration,
Color.Default,
var args = new SnackBarOptions
{
MessageOptions = messageOptions,
Duration = TimeSpan.FromMilliseconds(durationMilliseconds),
#if NETSTANDARD1_0
false,
IsRtl = false,
#else
CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
IsRtl = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
#endif
new List<SnackBarActionOptions>());
};
var snackBar = new SnackBar();
snackBar.Show(page, args);
return args.Result.Task;
}

public static Task<bool> DisplayToastAsync(this Page page, ActionOptions actionOptions)
public static Task DisplayToastAsync(this Page page, ToastOptions toastOptions)
{
var snackBar = new SnackBar();
var arguments = actionOptions ?? new ActionOptions();
var options = new SnackBarOptions(arguments.MessageOptions, arguments.Duration, arguments.BackgroundColor, arguments.IsRtl, null);
var arguments = toastOptions ?? new ToastOptions();
var options = new SnackBarOptions
{
MessageOptions = arguments.MessageOptions,
Duration = arguments.Duration,
BackgroundColor = arguments.BackgroundColor,
IsRtl = arguments.IsRtl
};
snackBar.Show(page, options);
return options.Result.Task;
}

public static Task<bool> DisplaySnackBarAsync(this Page page, string message, string actionButtonText, Func<Task> action, int duration = 3000)
public static Task<bool> DisplaySnackBarAsync(this Page page, string message, string actionButtonText, Func<Task> action, int durationMilliseconds = 3000)
{
var messageOptions = new MessageOptions { Message = message };
var actionOptions = new List<SnackBarActionOptions>
Expand All @@ -46,18 +53,20 @@ public static Task<bool> DisplaySnackBarAsync(this Page page, string message, st
Text = actionButtonText, Action = action
}
};
var args = new SnackBarOptions(messageOptions,
duration,
Color.Default,
var options = new SnackBarOptions
{
MessageOptions = messageOptions,
Duration = TimeSpan.FromMilliseconds(durationMilliseconds),
Actions = actionOptions,
#if NETSTANDARD1_0
false,
IsRtl = false,
#else
CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
IsRtl = CultureInfo.CurrentCulture.TextInfo.IsRightToLeft,
#endif
actionOptions);
};
var snackBar = new SnackBar();
snackBar.Show(page, args);
return args.Result.Task;
snackBar.Show(page, options);
return options.Result.Task;
}

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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,29 @@ public class MessageOptions
/// <summary>
/// Gets or sets the message for the SnackBar.
/// </summary>
public string Message { get; set; }
public string Message { get; set; } = DefaultMessage;

public static string DefaultMessage { get; set; }

/// <summary>
/// Gets or sets the font size for the SnackBar message.
/// </summary>
public double FontSize { get; set; } = 14;
public double FontSize { get; set; } = DefaultFontSize;

public static double DefaultFontSize { get; set; } = 14;

/// <summary>
/// Gets or sets the font family for the SnackBar message.
/// </summary>
public string FontFamily { get; set; } = "Arial";
public string FontFamily { get; set; } = DefaultFontFamily;

public static string DefaultFontFamily { get; set; } = "Arial";

/// <summary>
/// Gets or sets the font color for the SnackBar message.
/// </summary>
public Color Foreground { get; set; } = Color.Black;
public Color Foreground { get; set; } = DefaultForeground;

public static Color DefaultForeground { get; set; } = Color.Black;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,43 @@ public class SnackBarActionOptions
/// <summary>
/// Gets or sets the action for the SnackBar action button.
/// </summary>
public Func<Task> Action { get; set; }
public Func<Task> Action { get; set; } = DefaultAction;

public static Func<Task> DefaultAction { get; set; } = null;

/// <summary>
/// Gets or sets the text for the SnackBar action button.
/// </summary>
public string Text { get; set; }
public string Text { get; set; } = DefaultText;

public static string DefaultText { get; set; }

/// <summary>
/// Gets or sets the font size for the SnackBar action button.
/// </summary>
public double FontSize { get; set; } = 14;
public double FontSize { get; set; } = DefaultFontSize;

public static double DefaultFontSize { get; set; } = 14;

/// <summary>
/// Gets or sets the font family for the SnackBar action button.
/// </summary>
public string FontFamily { get; set; } = "Arial";
public string FontFamily { get; set; } = DefaultFontFamily;

public static string DefaultFontFamily { get; set; } = "Arial";

/// <summary>
/// Gets or sets the background color for the SnackBar action button.
/// </summary>
public Color BackgroundColor { get; set; } = Color.White;
public Color BackgroundColor { get; set; } = DefaultBackgroundColor;

public static Color DefaultBackgroundColor { get; set; } = Color.White;

/// <summary>
/// Gets or sets the font color for the SnackBar action button.
/// </summary>
public Color ForegroundColor { get; set; } = Color.Black;
public Color ForegroundColor { get; set; } = DefaultForegroundColor;

public static Color DefaultForegroundColor { get; set; } = Color.Black;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@

namespace Xamarin.CommunityToolkit.UI.Views.Options
{
public class SnackBarOptions : ActionOptions
public class SnackBarOptions : ToastOptions
{
public SnackBarOptions(MessageOptions message, int duration, Color backgroundColor, bool isRtl, IEnumerable<SnackBarActionOptions> actions)
: base(message, duration, backgroundColor, isRtl) =>
Actions = actions ?? Enumerable.Empty<SnackBarActionOptions>();

public SnackBarOptions() => Actions = Enumerable.Empty<SnackBarActionOptions>();

/// <summary>
/// Gets the text for the action buttons
/// Action options
/// </summary>
public IEnumerable<SnackBarActionOptions> Actions { get; }
public IEnumerable<SnackBarActionOptions> Actions { get; set; } = DefaultActions;

public static IEnumerable<SnackBarActionOptions> DefaultActions { get; set; } = Enumerable.Empty<SnackBarActionOptions>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Xamarin.CommunityToolkit.UI.Views.Options
{
/// <summary>
/// Toast options
/// </summary>
public class ToastOptions
{
public ToastOptions() => Result = new TaskCompletionSource<bool>(false);

/// <summary>
/// Message options: Message, color, font
/// </summary>
public MessageOptions MessageOptions { get; set; } = DefaultMessageOptions;

public static MessageOptions DefaultMessageOptions { get; set; } = new MessageOptions();

/// <summary>
/// Background color.
/// </summary>
public Color BackgroundColor { get; set; } = DefaultBackgroundColor;

public static Color DefaultBackgroundColor { get; set; } = Color.Default;

/// <summary>
/// Is Right to left
/// </summary>
public bool IsRtl { get; set; } = DefaultIsRtl;

public static bool DefaultIsRtl { get; set; } = false;

/// <summary>
/// The duration for the SnackBar.
/// </summary>
public TimeSpan Duration { get; set; } = DefaultDuration;

public static TimeSpan DefaultDuration { get; set; } = TimeSpan.FromMilliseconds(3000);

/// <summary>
/// Result is true if ActionButton is clicked.
/// </summary>
public TaskCompletionSource<bool> Result { get; }

public void SetResult(bool result) => Result.TrySetResult(result);
}
}
Loading

0 comments on commit 43b8dbd

Please sign in to comment.