Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds better ability to manage SnackbarModels #130

Merged
merged 1 commit into from
Dec 20, 2021
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
16 changes: 14 additions & 2 deletions Material.Demo/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Avalonia;
using System;
using System.Collections.Generic;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using Material.Styles;
using Material.Styles.Models;

namespace Material.Demo {
public class MainWindow : Window {
Expand Down Expand Up @@ -67,13 +70,22 @@ private void TemplatedControl_OnTemplateApplied(object? sender, TemplateAppliedE
SnackbarHost.Post("Welcome to demo of Material.Avalonia!");
}

private List<SnackbarModel> helloSnackBars = new List<SnackbarModel>();

private void HelloButtonMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
SnackbarHost.Post("Hello, user!");
var helloSnackBar = new SnackbarModel("Hello, user!", TimeSpan.Zero);
SnackbarHost.Post(helloSnackBar);
helloSnackBars.Add(helloSnackBar);
}

private void GoodbyeButtonMenuItem_OnClick(object? sender, RoutedEventArgs e)
{
foreach (var snackbarModel in helloSnackBars)
{
SnackbarHost.Remove(snackbarModel);
}

SnackbarHost.Post("See ya next time, user!");
}
}
Expand Down
7 changes: 7 additions & 0 deletions Material.Styles/Models/SnackbarModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public SnackbarModel(object content, Orientation orientation = Orientation.Horiz
//_button = button;
}

public SnackbarModel(object content, TimeSpan duration, Orientation orientation = Orientation.Horizontal) : this(content, orientation)
{
_duration = duration;
}


private object _content;
public object Content => _content;

Expand All @@ -24,6 +30,7 @@ public SnackbarModel(object content, Orientation orientation = Orientation.Horiz
private object _button;
public object Button => _button;

// Setting duration to TimeSpan.Zero, will make it stay forever/til you manually delete it
private TimeSpan _duration = TimeSpan.FromSeconds(5);
public TimeSpan Duration => _duration;
}
Expand Down
53 changes: 39 additions & 14 deletions Material.Styles/SnackbarHost.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,55 @@ public static void Post(SnackbarModel model, string targetHost = null)
if (host is null)
throw new ArgumentNullException(nameof(targetHost), $"The target host named \"{targetHost}\" is not exist.");

ElapsedEventHandler onExpired = null;
onExpired = delegate(object sender, ElapsedEventArgs args)
// If duration is TimeSpan.Zero, dont expire it.
if (model.Duration != TimeSpan.Zero)
{
if (sender is Timer timer)
ElapsedEventHandler onExpired = null;
onExpired = delegate(object sender, ElapsedEventArgs args)
{
// Remove timer.
timer.Stop();
timer.Elapsed -= onExpired;
timer.Dispose();
if (sender is Timer timer)
{
// Remove timer.
timer.Stop();
timer.Elapsed -= onExpired;
timer.Dispose();

OnSnackbarDurationExpired(host, model);
}
};

var timer = new Timer(model.Duration.TotalMilliseconds);
timer.Elapsed += onExpired;
timer.Start();
OnSnackbarDurationExpired(host, model);
}
};

var timer = new Timer(model.Duration.TotalMilliseconds);
timer.Elapsed += onExpired;
timer.Start();
}

Dispatcher.UIThread.Post(delegate
{
host.SnackbarModels.Add(model);
});
}

/// <summary>
/// Removes a snackbar manually
/// </summary>
/// <param name="model">snackbar data model.</param>
/// <param name="targetHost">the snackbar host that you wanted to use.</param>
public static void Remove(SnackbarModel model, string targetHost = null)
{
if (targetHost is null)
targetHost = GetFirstHostName();

var host = GetHost(targetHost);

if (host is null)
throw new ArgumentNullException(nameof(targetHost), $"The target host named \"{targetHost}\" is not exist.");

Dispatcher.UIThread.Post(delegate
{
host.SnackbarModels.Remove(model);
});
}

private static void OnSnackbarDurationExpired(SnackbarHost host, SnackbarModel model)
{
Dispatcher.UIThread.Post(delegate
Expand Down