Skip to content

Commit

Permalink
#238 Toast (#239)
Browse files Browse the repository at this point in the history
* 238 Toast

* Add cancellation token tests

* Fix action button is missed on long text

* `dotnet format`

Update code via new `.editorconfig`

* Fix Namine Violation (remove `_` prefix)

* Update Toast_Tests.cs

* fix naming policy

* Fix build

* Update `CommunityToolkit.Maui.Markup`

* IAlert, fix possible race conditions, add tests and fix sample

* Add `Assert.IsAssignableFrom<IAlert>`

* Remove Null Forgiving Operator

* Fix CharacterSpacing, extract default values, add toast text size

* Fix Android Toast text size, add tests

* Remove `<InternalsVisibleTo Include="CommunityToolkit.Maui" />`

* Remove `SemaphoreSlim`,  Ensure `IAlert.Show` + `IAlert.Dismiss` always runs on MainThread

* Use `ValueTask.CompletedTask`

* Fix partial methods on Windows

* Register ToastPage

* Update dispose

* Remove empty lines

* Update src/CommunityToolkit.Maui/Alerts/Snackbar/SnackBar.shared.cs

* Update src/CommunityToolkit.Maui/Alerts/Toast/Toast.shared.cs

* Remove reference to `nativeSnackbar` and `nativeToast` backing field; Reference Property instead

* Init only for snackbar and toast, small refactoring

* Update Fluent Assertions NuGet Package

* Keep static methods grouped together

* Group partial methods together

Co-authored-by: Brandon Minnick <[email protected]>
  • Loading branch information
VladislavAntonyuk and brminnick authored Feb 10, 2022
1 parent 170ffc5 commit 26007f7
Show file tree
Hide file tree
Showing 26 changed files with 684 additions and 152 deletions.
3 changes: 1 addition & 2 deletions samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using CommunityToolkit.Maui.Sample.ViewModels.Alerts;
using CommunityToolkit.Maui.Sample.ViewModels.Behaviors;
using CommunityToolkit.Maui.Sample.ViewModels.Converters;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;

namespace CommunityToolkit.Maui.Sample;

Expand All @@ -16,6 +14,7 @@ public partial class AppShell : Shell
{
// Add Alerts View Models
{ typeof(SnackbarViewModel), (typeof(AlertsGalleryPage), typeof(SnackbarPage)) },
{ typeof(ToastViewModel), (typeof(AlertsGalleryPage), typeof(ToastPage)) },

// Add Behaviors View Models
{ typeof(CharactersValidationBehaviorViewModel), (typeof(BehaviorsGalleryPage), typeof(CharactersValidationBehaviorPage)) },
Expand Down
36 changes: 36 additions & 0 deletions samples/CommunityToolkit.Maui.Sample/Pages/Alerts/ToastPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8" ?>
<pages:BasePage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:CommunityToolkit.Maui.Sample.Pages"
xmlns:converters="clr-namespace:CommunityToolkit.Maui.Converters;assembly=CommunityToolkit.Maui"
x:Class="CommunityToolkit.Maui.Sample.Pages.Alerts.ToastPage">

<pages:BasePage.Resources>
<ResourceDictionary>
<converters:ColorToColorForTextConverter x:Key="ColorToColorForTextConverter"/>
</ResourceDictionary>
</pages:BasePage.Resources>

<VerticalStackLayout Spacing="12">

<Label Text="The Toast is a timed alert that appears at the bottom of the screen by default. It is dismissed after a configurable duration of time."
LineBreakMode = "WordWrap" />

<Label Text="Windows uses toast notifications to display toast. Make sure you switched off Focus Assist."
IsVisible="false">
<Label.IsVisible>
<OnPlatform x:TypeArguments="x:Boolean">
<On Platform="UWP" Value="true" />
</OnPlatform>
</Label.IsVisible>
</Label>

<Button Clicked="ShowToastButtonClicked"
Text="Display Toast"/>

<Button Clicked="ShowCustomToastButtonClicked"
Text="Display Custom Toast"/>

</VerticalStackLayout>

</pages:BasePage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;

namespace CommunityToolkit.Maui.Sample.Pages.Alerts;

public partial class ToastPage : BasePage
{
public ToastPage()
{
InitializeComponent();
}

async void ShowToastButtonClicked(object? sender, EventArgs args)
{
var toast = Toast.Make("This is a default Toast.");
await toast.Show();
}

async void ShowCustomToastButtonClicked(object? sender, EventArgs args)
{
var toast = Toast.Make("This is a big Toast.", ToastDuration.Long, 30d);
await toast.Show();
}
}
2 changes: 0 additions & 2 deletions samples/CommunityToolkit.Maui.Sample/Pages/Base/BasePage.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Diagnostics;
using CommunityToolkit.Maui.Sample.ViewModels;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;

namespace CommunityToolkit.Maui.Sample.Pages;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ public class AlertsGalleryViewModel : BaseGalleryViewModel
public AlertsGalleryViewModel()
: base(new[]
{
SectionModel.Create<SnackbarViewModel>("Snackbar", "Show Snackbar")
SectionModel.Create<SnackbarViewModel>("Snackbar", "Show Snackbar"),
SectionModel.Create<ToastViewModel>("Toast", "Show Toast")
})
{
}
}

public class SnackbarViewModel : BaseViewModel
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace CommunityToolkit.Maui.Sample.ViewModels.Alerts;

public class SnackbarViewModel : BaseViewModel
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace CommunityToolkit.Maui.Sample.ViewModels.Alerts;

public class ToastViewModel : BaseViewModel
{
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace CommunityToolkit.Maui.Sample.ViewModels.Converters;
Expand Down
22 changes: 22 additions & 0 deletions src/CommunityToolkit.Maui.Core/Interfaces/IAlert.shared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace CommunityToolkit.Maui.Core;

/// <summary>
/// Alert
/// </summary>
public interface IAlert : IAsyncDisposable
{
/// <summary>
/// Message text
/// </summary>
string Text { get; }

/// <summary>
/// Dismiss the alert
/// </summary>
Task Dismiss(CancellationToken token = default);

/// <summary>
/// Show the alert
/// </summary>
Task Show(CancellationToken token = default);
}
17 changes: 1 addition & 16 deletions src/CommunityToolkit.Maui.Core/Interfaces/ISnackbar.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Snackbar
/// </summary>
public interface ISnackbar : IAsyncDisposable
public interface ISnackbar : IAlert
{
/// <summary>
/// Action to invoke on action button click
Expand All @@ -25,23 +25,8 @@ public interface ISnackbar : IAsyncDisposable
/// </summary>
TimeSpan Duration { get; }

/// <summary>
/// Snackbar message
/// </summary>
string Text { get; }

/// <summary>
/// Snackbar visual options
/// </summary>
SnackbarOptions VisualOptions { get; }

/// <summary>
/// Dismiss the snackbar
/// </summary>
Task Dismiss(CancellationToken token = default);

/// <summary>
/// Show the snackbar
/// </summary>
Task Show(CancellationToken token = default);
}
33 changes: 33 additions & 0 deletions src/CommunityToolkit.Maui.Core/Interfaces/IToast.shared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace CommunityToolkit.Maui.Core;

/// <summary>
/// Toast
/// </summary>
public interface IToast : IAlert
{
/// <summary>
/// Toast duration
/// </summary>
ToastDuration Duration { get; }

/// <summary>
/// Toast font size
/// </summary>
double TextSize { get; }
}

/// <summary>
/// Toast duration
/// </summary>
public enum ToastDuration
{
/// <summary>
/// Displays Toast for a short time
/// </summary>
Short,

/// <summary>
/// Displays Toast for a long time
/// </summary>
Long
}
33 changes: 33 additions & 0 deletions src/CommunityToolkit.Maui.Core/Primitives/Defaults.shared.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.ComponentModel;

namespace CommunityToolkit.Maui.Core;

/// <summary>
/// Default Values for CommunityToolkit.Maui
/// </summary>
public static class Defaults
{
/// <summary>
/// Default Font Size
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public const double FontSize = 14;

/// <summary>
/// Default Character Spacing
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public const double CharacterSpacing = 0.0d;

/// <summary>
/// Default Text Color
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly Color TextColor = Colors.Black;

/// <summary>
/// Default Background Color
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static readonly Color BackgroundColor = Colors.LightGray;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@ public class SnackbarOptions : ITextStyle
/// <summary>
/// Snackbar message character spacing
/// </summary>
public double CharacterSpacing { get; set; } = 0.0d;
public double CharacterSpacing { get; set; } = Defaults.CharacterSpacing;

/// <summary>
/// Snackbar message font
/// </summary>
public Font Font { get; set; } = Font.SystemFontOfSize(14);
public Font Font { get; set; } = Font.SystemFontOfSize(Defaults.FontSize);

/// <summary>
/// Snackbar message text color
/// </summary>
public Color TextColor { get; set; } = Colors.Black;
public Color TextColor { get; set; } = Defaults.TextColor;

/// <summary>
/// Snackbar button font
/// </summary>
public Font ActionButtonFont { get; set; } = Font.SystemFontOfSize(14);
public Font ActionButtonFont { get; set; } = Font.SystemFontOfSize(Defaults.FontSize);

/// <summary>
/// Snackbar action button text color
/// </summary>
public Color ActionButtonTextColor { get; set; } = Colors.Black;
public Color ActionButtonTextColor { get; set; } = Defaults.TextColor;

/// <summary>
/// Snackbar background color
/// </summary>
public Color BackgroundColor { get; set; } = Colors.LightGray;
public Color BackgroundColor { get; set; } = Defaults.BackgroundColor;

/// <summary>
/// Snackbar corner radius
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ void Initialize()
VisualOptions.CornerRadius.X,
VisualOptions.CornerRadius.Y,
VisualOptions.CornerRadius.Width,
VisualOptions.CornerRadius.Height);
VisualOptions.CornerRadius.Height)
{
Alignment = UIStackViewAlignment.Fill,
Distribution = UIStackViewDistribution.EqualCentering
};

AddSubview(Container);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CoreGraphics;
using CoreText;
using Foundation;
using Microsoft.Maui.Platform;
using ObjCRuntime;
using UIKit;

Expand Down Expand Up @@ -67,7 +68,7 @@ public string Message
/// </summary>
public UIColor TextColor
{
get => messageLabel.TextColor ??= UIColor.Black;
get => messageLabel.TextColor ??= Defaults.TextColor.ToNative();
private init => messageLabel.TextColor = value;
}

Expand All @@ -87,7 +88,7 @@ public double CharacterSpacing
{
init
{
var em = GetEmFromPx(Font.PointSize, value);
var em = Font.PointSize > 0 ? GetEmFromPx(Font.PointSize, value) : 0;
messageLabel.AttributedText = new NSAttributedString(Message, new CTStringAttributes() { KerningAdjustment = (float)em });
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/CommunityToolkit.Maui.UnitTests/Alerts/SnackBar_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public class Snackbar_Tests : BaseTest
{
readonly ISnackbar snackbar = new Snackbar();

public Snackbar_Tests()
{
Assert.IsAssignableFrom<IAlert>(snackbar);
}

[Fact]
public async Task SnackbarShow_IsShownTrue()
{
Expand Down Expand Up @@ -51,7 +56,7 @@ public async Task SnackbarDismiss_DismissedEventRaised()
public async Task VisualElement_DisplaySnackbar_ShownEventReceived()
{
var receivedEvents = new List<EventArgs>();
Snackbar.Shown += (sender, e) =>
Snackbar.Shown += (_, e) =>
{
receivedEvents.Add(e);
};
Expand Down
Loading

0 comments on commit 26007f7

Please sign in to comment.