using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core; using Tango.Core.Commands; using Tango.Core.DI; namespace Tango.FSE.Common.Notifications { public class SnackbarItem : ExtendedObject { private Action _closeAction; private Action _pressAction; private bool _isClosed; public bool HoldTimeout { get; set; } private MessageType _type; public MessageType Type { get { return _type; } set { _type = value; RaisePropertyChangedAuto(); } } public String Title { get; set; } private String _message; public String Message { get { return _message; } set { _message = value; RaisePropertyChangedAuto(); } } private bool _isProgress; public bool IsProgress { get { return _isProgress; } set { _isProgress = value; RaisePropertyChangedAuto(); } } public TimeSpan? Timeout { get; set; } private bool _canClose; public bool CanClose { get { return _canClose; } set { _canClose = value; RaisePropertyChangedAuto(); } } public RelayCommand CloseCommand { get; private set; } public RelayCommand PressCommand { get; private set; } private bool _isClosing; public bool IsClosing { get { return _isClosing; } private set { _isClosing = value; RaisePropertyChangedAuto(); } } public SnackbarItem(MessageType type, String title, bool canClose, String message = null, TimeSpan? timeout = null, Action closeAction = null, Action pressAction = null) { Type = type; Title = title; CanClose = canClose; Message = message; Timeout = timeout; _pressAction = () => { if (IsProgress) return; if (pressAction != null) { Close(); pressAction.Invoke(); } else if (CanClose) { Close(); } }; _closeAction = () => { Close(); closeAction?.Invoke(); }; PressCommand = new RelayCommand(() => { _pressAction?.Invoke(); }); CloseCommand = new RelayCommand(() => { _closeAction?.Invoke(); }); StartCloseTimeout(Timeout); } public void Close() { if (!_isClosed) { _isClosed = true; TangoIOC.Default.GetInstance().PopSnackbarItem(this); } } public void ProgressCompleted(String message, TimeSpan? timeout = null, Action pressAction = null) { if (pressAction != null) { _pressAction = () => { Close(); pressAction.Invoke(); }; } IsProgress = false; CanClose = true; Message = message; Type = MessageType.Success; StartCloseTimeout(timeout); } public void ProgressFailed(String message, TimeSpan? timeout = null) { IsProgress = false; CanClose = true; Message = message; Type = MessageType.Error; StartCloseTimeout(timeout); } private void StartCloseTimeout(TimeSpan? timeout) { if (timeout != null) { Task.Delay(timeout.Value).ContinueWith((x) => { if (HoldTimeout) { StartCloseTimeout(timeout); return; } if (!_isClosed) { IsClosing = true; Task.Delay(TimeSpan.FromSeconds(2)).ContinueWith((y) => { if (HoldTimeout) { IsClosing = false; StartCloseTimeout(timeout); return; } Close(); }); } }); } } } }