123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- using CommunityToolkit.Mvvm.ComponentModel;
- using CommunityToolkit.Mvvm.Input;
- using DialogHostAvalonia;
- using InABox.Avalonia;
- using InABox.Avalonia.Components;
- namespace PRS.DigitalKey;
- public abstract partial class ViewModelBase : ObservableObject, IViewModelBase
- {
-
- private CancellationTokenSource _cts = new();
- [ObservableProperty] private bool _backButtonVisible = true;
- [ObservableProperty] private AvaloniaMenuItemCollection _primaryMenu = new();
- [ObservableProperty] private AvaloniaMenuItemCollection _secondaryMenu;
- [ObservableProperty] private bool _reverseTransition;
-
- [RelayCommand]
- private void BackButtonPressed()
- {
- if (OnBackButtonPressed())
- Navigation.Back();
- }
- protected virtual bool OnBackButtonPressed()
- {
- return true;
- }
- public Task Activate()
- {
-
- var token = _cts.Token;
- _ = Task.Run(
- async () =>
- {
- var result = await Refresh();
- while (result != TimeSpan.Zero)
- {
- if (token.IsCancellationRequested)
- break;
- await Task.Delay(result);
- if (!token.IsCancellationRequested)
- result = await Refresh();
- }
- },
- token
- );
- return OnActivated();
- }
- protected virtual Task OnActivated()
- {
- return Task.CompletedTask;
- }
- private async Task<TimeSpan> Refresh()
- {
- return await OnRefresh();
- }
- protected virtual async Task<TimeSpan> OnRefresh()
- {
- return TimeSpan.FromSeconds(30);
- }
- public async Task Deactivate()
- {
- await _cts.CancelAsync();
- _cts.Dispose();
- _cts = new CancellationTokenSource();
- await OnDeactivated();
- }
- public bool ProgressVisible { get; set; }
- protected virtual Task OnDeactivated()
- {
- return Task.CompletedTask;
- }
-
- }
- public abstract class PopupViewModel : ViewModelBase, IPopupViewModel
- {
- public bool IsClosed { get; private set; }
- public void Close()
- {
- IsClosed = true;
- DialogHost.GetDialogSession(null)?.Close();
- }
- }
- public abstract class PopupViewModel<TResult> : PopupViewModel, IPopupViewModel<TResult>
- {
- private TResult? _result;
- public TResult? GetResult()
- {
- return _result ?? default;
- }
- public void Close(TResult result)
- {
- _result = result;
- Close();
- }
- }
|