Skip to content

Commit

Permalink
Merge pull request #130 from OsaPL/dev
Browse files Browse the repository at this point in the history
Adds better ability to manage SnackbarModels
  • Loading branch information
SKProCH committed Dec 20, 2021
2 parents 92261ed + f92cf65 commit 9d72abb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
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

0 comments on commit 9d72abb

Please sign in to comment.