using Avalonia.Threading; using ReactiveUI; using System.Threading.Tasks; using System; namespace SPTInstaller.ViewModels { public class ViewModelBase : ReactiveObject, IActivatableViewModel, IRoutableViewModel { public ViewModelActivator Activator { get; } = new ViewModelActivator(); public string? UrlPathSegment => Guid.NewGuid().ToString().Substring(0, 7); public IScreen HostScreen { get; } /// /// Delay the return of the viewmodel /// /// The amount of time in milliseconds to delay /// The viewmodel after the delay time /// Useful to delay the navigation to another view. For instance, to allow an animation to complete. private async Task WithDelay(int Milliseconds) { await Task.Delay(Milliseconds); return this; } /// /// Navigate to another viewmodel after a delay /// /// /// /// public async Task NavigateToWithDelay(ViewModelBase ViewModel, int Milliseconds) { await Dispatcher.UIThread.InvokeAsync(async () => { HostScreen.Router.Navigate.Execute(await ViewModel.WithDelay(Milliseconds)); }); } /// /// Navigate to another viewmodel /// /// public void NavigateTo(ViewModelBase ViewModel) { Dispatcher.UIThread.InvokeAsync(() => { HostScreen.Router.Navigate.Execute(ViewModel); }); } public ViewModelBase(IScreen Host) { HostScreen = Host; } } }