| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Reflection;
- using System.Threading;
- using System.Threading.Tasks;
- using Avalonia.Svg.Skia;
- using Avalonia.Threading;
- using Comal.Classes;
- using CommunityToolkit.Mvvm.ComponentModel;
- using CommunityToolkit.Mvvm.Input;
- using DialogHostAvalonia;
- using InABox.Avalonia;
- using InABox.Avalonia.Components;
- using InABox.Configuration;
- using InABox.Core;
- using PRS.Avalonia.Components;
- using PRS.Avalonia.Dialogs;
- namespace PRS.Avalonia;
- public abstract partial class ViewModelBase : ObservableValidator, IViewModelBase
- {
- private static DataAccessLayer? _dataAccessLayer;
- private static RepositoryLayer? _repositories;
- private static Dictionary<string, MobileDatabaseSettings>? _databaseSettings;
- private static MobileDatabaseSettings? _currentDatabase;
- [ObservableProperty] private bool _backButtonVisible = true;
- private CancellationTokenSource _cts = new();
- [ObservableProperty] private AvaloniaMenuItemCollection _primaryMenu = new();
- [ObservableProperty] private bool _reverseTransition;
- [ObservableProperty] private AvaloniaMenuItemCollection _secondaryMenu = new();
- [ObservableProperty]
- private bool _progressVisible = false;
- public static DataAccessLayer DataAccess
- {
- get
- {
- if (_dataAccessLayer == null)
- {
- _dataAccessLayer = new DataAccessLayer();
- _dataAccessLayer.Connected += (s, e) => Connected();
- _dataAccessLayer.Disconnected += (s, e) => Disconnected();
- _dataAccessLayer.Validated += (s, e) => Validated();
- }
- _dataAccessLayer ??= new DataAccessLayer();
- return _dataAccessLayer;
- }
- }
- public static RepositoryLayer Repositories
- {
- get
- {
- _repositories ??= new RepositoryLayer(DataAccess);
- return _repositories;
- }
- }
- public static Dictionary<string, MobileDatabaseSettings> DatabaseSettings
- {
- get
- {
- if (_databaseSettings == null)
- LoadDatabaseSettings();
- return _databaseSettings;
- }
- }
- public static MobileDatabaseSettings CurrentDatabase
- {
- get
- {
- if (_currentDatabase == null)
- LoadDatabaseSettings();
- return DatabaseSettings.Any(x => x.Value.IsDefault)
- ? DatabaseSettings.FirstOrDefault(x => x.Value.IsDefault).Value
- : DatabaseSettings.First().Value;
- }
- }
- public static string CurrentDatabaseName => DatabaseSettings.Any(x => x.Value.IsDefault)
- ? DatabaseSettings.FirstOrDefault(x => x.Value.IsDefault).Key
- : DatabaseSettings.First().Key;
- public static bool IsSharedDevice =>
- string.IsNullOrWhiteSpace(CurrentDatabase.UserID)
- || string.IsNullOrWhiteSpace(CurrentDatabase.Password);
- private static void Connected()
- {
- }
- private static void Validated()
- {
- Repositories.MeModel.Refresh(true);
- }
- private static void Disconnected()
- {
- }
- public string DefaultCacheFileName<T>(Guid? id = null, string? tag = null) where T : IShell
- {
- return string.IsNullOrWhiteSpace(tag)
- ? id == null
- ? $"{GetType().Name}.{typeof(T).Name}"
- : $"{GetType().Name}.{typeof(T).Name}.{id}"
- : id == null
- ? $"{GetType().Name}.{typeof(T).Name}-{tag}"
- : $"{GetType().Name}.{typeof(T).Name}-{tag}.{id}";
- }
- [RelayCommand]
- private void BackButtonPressed()
- {
- if (OnBackButtonPressed())
- Navigation.Back();
- }
- public virtual bool OnBackButtonPressed()
- {
- return true;
- }
-
- public async Task Activate()
- {
- await OnActivated();
- 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
- ).ContinueWith(task =>
- {
- if(task.Exception is not null)
- {
- App.HandleException(task.Exception);
- }
- });
- }
- protected virtual Task OnActivated()
- {
- return Task.CompletedTask;
- }
- private async Task<TimeSpan> Refresh()
- {
- return await OnRefresh();
- }
- /// <summary>
- /// Refresh the data on the screen. If this method returns a value other than <see cref="TimeSpan.Zero"/>, then that value is used to
- /// schedule the next refresh.
- /// </summary>
- /// <returns>Time until next refresh is triggered, or <see cref="TimeSpan.Zero"/> if periodic refreshing is unwanted.</returns>
- protected virtual Task<TimeSpan> OnRefresh()
- {
- return Task.FromResult(TimeSpan.FromSeconds(30));
- }
- public async Task Deactivate()
- {
- await _cts.CancelAsync();
- _cts.Dispose();
- _cts = new CancellationTokenSource();
- await OnDeactivated();
- await Task.CompletedTask;
- }
- protected virtual Task OnDeactivated()
- {
- return Task.CompletedTask;
- }
- public static void SaveDatabaseSettings()
- {
- var config = new LocalConfiguration<MobileDatabaseSettings>();
- var old = config.LoadAll();
- foreach (var key in old.Keys)
- if (!DatabaseSettings.ContainsKey(key))
- new LocalConfiguration<MobileDatabaseSettings>(key).Delete();
- config.SaveAll(DatabaseSettings);
- }
- [MemberNotNull(nameof(_databaseSettings))]
- public static void LoadDatabaseSettings()
- {
- _databaseSettings = new LocalConfiguration<MobileDatabaseSettings>().LoadAll();
-
- // Contruct Defaults if required
- if (!_databaseSettings.Any())
- {
- _currentDatabase = new MobileDatabaseSettings();
- _currentDatabase.URLs = new[]
- {
- "demo.prsdigital.com.au:8033"
- };
- _currentDatabase.UserID = "GUEST";
- _currentDatabase.Password = "guest";
- _currentDatabase.IsDefault = true;
- _currentDatabase.CacheID = Guid.NewGuid();
- _databaseSettings["Demo Database"] = _currentDatabase;
- new LocalConfiguration<MobileDatabaseSettings>().SaveAll(_databaseSettings);
- }
- // Retrieve the Default (or the first, if there is no default)
- _currentDatabase = _databaseSettings.Any(x => x.Value.IsDefault)
- ? _databaseSettings.FirstOrDefault(x => x.Value.IsDefault).Value
- : _databaseSettings.First().Value;
- if (_currentDatabase.CacheID == Guid.Empty)
- {
- _currentDatabase.CacheID = Guid.NewGuid();
- new LocalConfiguration<MobileDatabaseSettings>().SaveAll(_databaseSettings);
- }
- }
- }
- 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();
- }
- }
|