| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SplashScreenViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using MyNet.UI.Resources; |
| | | 11 | | using MyNet.UI.Services; |
| | | 12 | | using MyNet.UI.ViewModels.Dialog; |
| | | 13 | | |
| | | 14 | | namespace MyNet.UI.ViewModels.Shell.Startup; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Dialog view model for application startup splash screens with queued startup tasks. |
| | | 18 | | /// </summary> |
| | | 19 | | public class SplashScreenViewModel : DialogViewModel |
| | | 20 | | { |
| | 12 | 21 | | private readonly Dictionary<Func<string>, (Func<Task> Action, Func<bool> CanExecute)> _tasks = []; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="SplashScreenViewModel"/> class. |
| | | 25 | | /// </summary> |
| | 12 | 26 | | public SplashScreenViewModel(IApplicationInfo applicationInfo) |
| | | 27 | | { |
| | 12 | 28 | | ArgumentNullException.ThrowIfNull(applicationInfo); |
| | | 29 | | |
| | 12 | 30 | | Version = applicationInfo.Version; |
| | 12 | 31 | | Copyright = applicationInfo.Copyright; |
| | 12 | 32 | | ProductName = applicationInfo.ProductName; |
| | 12 | 33 | | Company = applicationInfo.Company; |
| | 12 | 34 | | Message = UiResources.Ready; |
| | 12 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the application version. |
| | | 39 | | /// </summary> |
| | | 40 | | public string? Version { get; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the current status message (suffix <c>"..."</c> is applied while a task runs). |
| | | 44 | | /// </summary> |
| | 24 | 45 | | public string? Message { get; private set => SetProperty(ref field, value); } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets the copyright notice. |
| | | 49 | | /// </summary> |
| | | 50 | | public string? Copyright { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets the product name. |
| | | 54 | | /// </summary> |
| | | 55 | | public string? ProductName { get; } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets the company name. |
| | | 59 | | /// </summary> |
| | | 60 | | public string? Company { get; } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets a value indicating whether startup tasks are currently executing. |
| | | 64 | | /// </summary> |
| | 24 | 65 | | public bool IsBusy { get; private set => SetProperty(ref field, value); } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Enqueues an asynchronous startup task. |
| | | 69 | | /// </summary> |
| | | 70 | | public void AddTask(Func<string> message, Func<Task> task, Func<bool>? canExecute = null) |
| | 15 | 71 | | => _tasks.Add(message, (task, canExecute ?? (() => true))); |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Enqueues a synchronous startup task executed on the thread pool. |
| | | 75 | | /// </summary> |
| | | 76 | | public void AddTask(Func<string> message, Action action, Func<bool>? canExecute = null) |
| | 0 | 77 | | => AddTask(message, () => Task.Run(action), canExecute); |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Enqueues a synchronous startup task with a fixed message. |
| | | 81 | | /// </summary> |
| | | 82 | | public void AddTask(string message, Action action, Func<bool>? canExecute = null) |
| | 0 | 83 | | => AddTask(() => message, action, canExecute); |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Enqueues an asynchronous startup task with a fixed message. |
| | | 87 | | /// </summary> |
| | | 88 | | public void AddTask(string message, Func<Task> task, Func<bool>? canExecute = null) |
| | 15 | 89 | | => AddTask(() => message, task, canExecute); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Enqueues multiple startup tasks. |
| | | 93 | | /// </summary> |
| | | 94 | | public void AddTasks(IEnumerable<(string Message, Func<Task> Task)> tasks) |
| | | 95 | | { |
| | 0 | 96 | | foreach (var task in tasks) |
| | 0 | 97 | | AddTask(task.Message, task.Task); |
| | 0 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// Enqueues multiple synchronous startup tasks. |
| | | 102 | | /// </summary> |
| | | 103 | | public void AddTasks(IEnumerable<(string Message, Action Action)> tasks) |
| | | 104 | | { |
| | 0 | 105 | | foreach (var task in tasks) |
| | 0 | 106 | | AddTask(task.Message, task.Action); |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Enqueues multiple startup tasks with dynamic messages. |
| | | 111 | | /// </summary> |
| | | 112 | | public void AddTasks(IEnumerable<(Func<string> Message, Func<Task> Task)> tasks) |
| | | 113 | | { |
| | 0 | 114 | | foreach (var task in tasks) |
| | 0 | 115 | | AddTask(task.Message, task.Task); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Enqueues multiple synchronous startup tasks with dynamic messages. |
| | | 120 | | /// </summary> |
| | | 121 | | public void AddTasks(IEnumerable<(Func<string> Message, Action Action)> tasks) |
| | | 122 | | { |
| | 0 | 123 | | foreach (var task in tasks) |
| | 0 | 124 | | AddTask(task.Message, task.Action); |
| | 0 | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Enqueues multiple startup tasks with optional execution guards. |
| | | 129 | | /// </summary> |
| | | 130 | | public void AddTasks(IEnumerable<(Func<string> Message, Func<Task> Task, Func<bool> CanExecute)> tasks) |
| | | 131 | | { |
| | 0 | 132 | | foreach (var task in tasks) |
| | 0 | 133 | | AddTask(task.Message, task.Task, task.CanExecute); |
| | 0 | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Enqueues multiple synchronous startup tasks with optional execution guards. |
| | | 138 | | /// </summary> |
| | | 139 | | public void AddTasks(IEnumerable<(Func<string> Message, Action Action, Func<bool> CanExecute)> tasks) |
| | | 140 | | { |
| | 0 | 141 | | foreach (var task in tasks) |
| | 0 | 142 | | AddTask(task.Message, task.Action, task.CanExecute); |
| | 0 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <summary> |
| | | 146 | | /// Runs queued startup tasks in registration order. |
| | | 147 | | /// </summary> |
| | | 148 | | public async Task ExecuteAsync(Action? completedCallBack = null, Action<Exception>? failedCallback = null) |
| | | 149 | | { |
| | | 150 | | try |
| | | 151 | | { |
| | 12 | 152 | | IsBusy = true; |
| | 51 | 153 | | foreach (var task in _tasks) |
| | | 154 | | { |
| | 15 | 155 | | if (!task.Value.CanExecute.Invoke()) |
| | | 156 | | continue; |
| | | 157 | | |
| | 12 | 158 | | var message = task.Key.Invoke(); |
| | 12 | 159 | | UpdateMessage(message); |
| | | 160 | | |
| | 12 | 161 | | await Task.Delay(200).ConfigureAwait(false); |
| | 12 | 162 | | await task.Value.Action.Invoke().ConfigureAwait(false); |
| | 9 | 163 | | } |
| | | 164 | | |
| | 9 | 165 | | completedCallBack?.Invoke(); |
| | 9 | 166 | | } |
| | 3 | 167 | | catch (Exception e) |
| | | 168 | | { |
| | 3 | 169 | | failedCallback?.Invoke(e); |
| | 3 | 170 | | } |
| | | 171 | | finally |
| | | 172 | | { |
| | 12 | 173 | | IsBusy = false; |
| | | 174 | | } |
| | 12 | 175 | | } |
| | | 176 | | |
| | 12 | 177 | | private void UpdateMessage(string message) => Message = message + "..."; |
| | | 178 | | } |
| | | 179 | | |