|
5 | 5 | using ReactiveUI; |
6 | 6 | using System.Collections.Generic; |
7 | 7 | using System.Collections.ObjectModel; |
| 8 | +using System.Globalization; |
8 | 9 | using System.Linq; |
9 | 10 | using System.Reactive; |
10 | 11 |
|
11 | 12 | namespace CodeWF.Toolbox.ViewModels; |
12 | 13 |
|
13 | 14 | public class DashboardViewModel : ViewModelBase |
14 | 15 | { |
| 16 | + private static readonly string[] RecommendedToolViewNames = |
| 17 | + [ |
| 18 | + "DateTimeConverterView", |
| 19 | + "Base64CodecView", |
| 20 | + "GuidGeneratorView", |
| 21 | + "ImageToIconView", |
| 22 | + "ToolView?tool=json-viewer", |
| 23 | + "ToolView?tool=jwt-parser", |
| 24 | + "ToolView?tool=qr-code-generator", |
| 25 | + "ToolView?tool=hash-text" |
| 26 | + ]; |
| 27 | + |
15 | 28 | private readonly IToolMenuService _toolMenuService; |
16 | 29 | private readonly IUserProfileService _userProfileService; |
17 | 30 |
|
18 | 31 | public DashboardViewModel(IToolMenuService toolMenuService, IUserProfileService userProfileService) |
19 | 32 | { |
20 | 33 | _toolMenuService = toolMenuService; |
21 | 34 | _userProfileService = userProfileService; |
22 | | - _toolMenuService.ToolMenuChanged += RefreshMenuMetrics; |
23 | | - _userProfileService.ProfileChanged += (_, _) => RefreshFrequentTools(); |
24 | | - OpenFrequentToolCommand = ReactiveCommand.Create<UserToolUsage>(OpenFrequentTool); |
| 35 | + _toolMenuService.ToolMenuChanged += RefreshDashboard; |
| 36 | + _userProfileService.ProfileChanged += (_, _) => RefreshDashboardTools(); |
| 37 | + OpenDashboardToolCommand = ReactiveCommand.Create<DashboardToolItem>(OpenDashboardTool); |
25 | 38 |
|
26 | 39 | OSInfo = GetPlatformName(); |
27 | | - RefreshMenuMetrics(); |
28 | | - RefreshFrequentTools(); |
| 40 | + RefreshDashboard(); |
29 | 41 | } |
30 | 42 |
|
31 | | - public ObservableCollection<UserToolUsage> FrequentTools { get; } = []; |
| 43 | + public ObservableCollection<DashboardToolItem> DashboardTools { get; } = []; |
32 | 44 |
|
33 | | - public bool HasFrequentTools |
| 45 | + public bool HasDashboardTools |
34 | 46 | { |
35 | 47 | get; |
36 | 48 | set => this.RaiseAndSetIfChanged(ref field, value); |
37 | 49 | } |
38 | 50 |
|
39 | | - public ReactiveCommand<UserToolUsage, Unit> OpenFrequentToolCommand { get; } |
| 51 | + public string DashboardToolsTitleKey |
| 52 | + { |
| 53 | + get; |
| 54 | + set => this.RaiseAndSetIfChanged(ref field, value); |
| 55 | + } = Localization.DashboardView.FrequentToolsTitle; |
| 56 | + |
| 57 | + public ReactiveCommand<DashboardToolItem, Unit> OpenDashboardToolCommand { get; } |
40 | 58 |
|
41 | 59 | public int ModuleCount |
42 | 60 | { |
@@ -65,18 +83,65 @@ private void RefreshMenuMetrics() |
65 | 83 | .Count(item => !item.IsSeparator && !string.IsNullOrWhiteSpace(item.ViewName)); |
66 | 84 | } |
67 | 85 |
|
68 | | - private void RefreshFrequentTools() |
| 86 | + private void RefreshDashboard() |
69 | 87 | { |
70 | | - FrequentTools.Clear(); |
71 | | - foreach (var item in _userProfileService.FrequentTools) |
| 88 | + RefreshMenuMetrics(); |
| 89 | + RefreshDashboardTools(); |
| 90 | + } |
| 91 | + |
| 92 | + private void RefreshDashboardTools() |
| 93 | + { |
| 94 | + DashboardTools.Clear(); |
| 95 | + var frequentTools = _userProfileService.FrequentTools |
| 96 | + .Where(item => !string.IsNullOrWhiteSpace(item.ViewName)) |
| 97 | + .Take(8) |
| 98 | + .ToList(); |
| 99 | + |
| 100 | + foreach (var item in frequentTools) |
72 | 101 | { |
73 | | - FrequentTools.Add(item); |
| 102 | + DashboardTools.Add(DashboardToolItem.FromUsage(item)); |
74 | 103 | } |
75 | 104 |
|
76 | | - HasFrequentTools = FrequentTools.Count > 0; |
| 105 | + DashboardToolsTitleKey = frequentTools.Count switch |
| 106 | + { |
| 107 | + 0 => Localization.DashboardView.RecommendedToolsTitle, |
| 108 | + >= 8 => Localization.DashboardView.FrequentToolsTitle, |
| 109 | + _ => "Localization.DashboardView.QuickToolsTitle" |
| 110 | + }; |
| 111 | + |
| 112 | + var navigableItems = Flatten(_toolMenuService.MenuItems) |
| 113 | + .Where(item => !item.IsSeparator && !string.IsNullOrWhiteSpace(item.ViewName)) |
| 114 | + .ToList(); |
| 115 | + |
| 116 | + for (var index = 0; index < RecommendedToolViewNames.Length; index++) |
| 117 | + { |
| 118 | + if (DashboardTools.Count >= 8) |
| 119 | + { |
| 120 | + break; |
| 121 | + } |
| 122 | + |
| 123 | + var viewName = RecommendedToolViewNames[index]; |
| 124 | + if (DashboardTools.Any(item => string.Equals(item.ViewName, viewName, System.StringComparison.OrdinalIgnoreCase))) |
| 125 | + { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + var menuItem = navigableItems.FirstOrDefault(item => |
| 130 | + string.Equals(item.ViewName, viewName, System.StringComparison.OrdinalIgnoreCase)); |
| 131 | + if (menuItem is null) |
| 132 | + { |
| 133 | + continue; |
| 134 | + } |
| 135 | + |
| 136 | + DashboardTools.Add(DashboardToolItem.FromMenuItem( |
| 137 | + menuItem, |
| 138 | + (DashboardTools.Count + 1).ToString(CultureInfo.InvariantCulture))); |
| 139 | + } |
| 140 | + |
| 141 | + HasDashboardTools = DashboardTools.Count > 0; |
77 | 142 | } |
78 | 143 |
|
79 | | - private static void OpenFrequentTool(UserToolUsage tool) |
| 144 | + private static void OpenDashboardTool(DashboardToolItem tool) |
80 | 145 | { |
81 | 146 | if (!string.IsNullOrWhiteSpace(tool.ViewName)) |
82 | 147 | { |
@@ -112,3 +177,40 @@ private static string GetPlatformName() |
112 | 177 | #endif |
113 | 178 | } |
114 | 179 | } |
| 180 | + |
| 181 | +public sealed class DashboardToolItem |
| 182 | +{ |
| 183 | + public string ViewName { get; init; } = string.Empty; |
| 184 | + |
| 185 | + public string? Name { get; init; } |
| 186 | + |
| 187 | + public string? Description { get; init; } |
| 188 | + |
| 189 | + public string? Icon { get; init; } |
| 190 | + |
| 191 | + public string Badge { get; init; } = string.Empty; |
| 192 | + |
| 193 | + public static DashboardToolItem FromUsage(UserToolUsage item) |
| 194 | + { |
| 195 | + return new DashboardToolItem |
| 196 | + { |
| 197 | + ViewName = item.ViewName, |
| 198 | + Name = item.Name, |
| 199 | + Description = item.Description, |
| 200 | + Icon = item.Icon, |
| 201 | + Badge = item.Count.ToString(CultureInfo.InvariantCulture) |
| 202 | + }; |
| 203 | + } |
| 204 | + |
| 205 | + public static DashboardToolItem FromMenuItem(ToolMenuItem item, string badge) |
| 206 | + { |
| 207 | + return new DashboardToolItem |
| 208 | + { |
| 209 | + ViewName = item.ViewName ?? string.Empty, |
| 210 | + Name = item.Name, |
| 211 | + Description = item.Description, |
| 212 | + Icon = item.Icon, |
| 213 | + Badge = badge |
| 214 | + }; |
| 215 | + } |
| 216 | +} |
0 commit comments