using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Threading; using HC_APTBS.ViewModels.Dialogs; namespace HC_APTBS.Views.UserControls { public partial class UnlockSnackbarView : UserControl { private DispatcherTimer? _autoHideTimer; public UnlockSnackbarView() { InitializeComponent(); DataContextChanged += OnDataContextChanged; } private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (e.OldValue is UnlockProgressViewModel oldVm) oldVm.PropertyChanged -= OnVmPropertyChanged; if (e.NewValue is UnlockProgressViewModel newVm) newVm.PropertyChanged += OnVmPropertyChanged; } private void OnVmPropertyChanged(object? sender, PropertyChangedEventArgs e) { if (e.PropertyName != nameof(UnlockProgressViewModel.IsSuccess)) return; if (DataContext is not UnlockProgressViewModel vm) return; if (vm.IsSuccess == true) { _autoHideTimer?.Stop(); _autoHideTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(3) }; _autoHideTimer.Tick += (_, _) => { _autoHideTimer!.Stop(); if (vm.CloseCommand.CanExecute(null)) vm.CloseCommand.Execute(null); }; _autoHideTimer.Start(); } } private void OnDismissClick(object sender, RoutedEventArgs e) { if (DataContext is UnlockProgressViewModel vm && vm.CloseCommand.CanExecute(null)) vm.CloseCommand.Execute(null); } } }