|
| 1 | +using System.Collections.ObjectModel; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using System.Net.Http.Json; |
| 4 | +using System.Text; |
| 5 | +using System.Text.Json.Serialization; |
| 6 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 7 | + |
| 8 | +namespace SnapX.Avalonia.ViewModels; |
| 9 | + |
| 10 | +public record GithubContentItem |
| 11 | +{ |
| 12 | + [JsonPropertyName("name")] |
| 13 | + public string Name { get; init; } = string.Empty; |
| 14 | + |
| 15 | + [JsonPropertyName("path")] |
| 16 | + public string Path { get; init; } = string.Empty; |
| 17 | + |
| 18 | + [JsonPropertyName("sha")] |
| 19 | + public string Sha { get; init; } = string.Empty; |
| 20 | + |
| 21 | + [JsonPropertyName("size")] |
| 22 | + public int Size { get; init; } |
| 23 | + |
| 24 | + [JsonPropertyName("url")] |
| 25 | + public string Url { get; init; } = string.Empty; |
| 26 | + |
| 27 | + [JsonPropertyName("html_url")] |
| 28 | + public string HtmlUrl { get; init; } = string.Empty; |
| 29 | + |
| 30 | + [JsonPropertyName("git_url")] |
| 31 | + public string GitUrl { get; init; } = string.Empty; |
| 32 | + |
| 33 | + [JsonPropertyName("download_url")] |
| 34 | + public string? DownloadUrl { get; init; } |
| 35 | + |
| 36 | + [JsonPropertyName("type")] |
| 37 | + public string Type { get; init; } = string.Empty; // "file" or "dir" |
| 38 | + |
| 39 | + [JsonIgnore] |
| 40 | + public bool IsFile => Type == "file"; |
| 41 | + |
| 42 | + [JsonIgnore] |
| 43 | + public bool IsSxcu => Name.EndsWith(".sxcu", StringComparison.OrdinalIgnoreCase); |
| 44 | +} |
| 45 | + |
| 46 | +[JsonSerializable(typeof(List<GithubContentItem>))] |
| 47 | +[JsonSerializable(typeof(GithubContentItem))] |
| 48 | +internal partial class GithubJsonContext : JsonSerializerContext { } |
| 49 | + |
| 50 | +public partial class CustomUploaderCatalogVM : ViewModelBase |
| 51 | +{ |
| 52 | + [ObservableProperty] |
| 53 | + private bool? _isAllSelected = false; |
| 54 | + |
| 55 | + partial void OnIsAllSelectedChanged(bool? value) |
| 56 | + { |
| 57 | + if (value == null) |
| 58 | + return; |
| 59 | + RefreshSelectionState(); |
| 60 | + foreach (var item in AvailableUploaders) |
| 61 | + item.IsSelected = value.Value; |
| 62 | + } |
| 63 | + |
| 64 | + // Logic to update the CheckBox state when individual items are clicked |
| 65 | + public void RefreshSelectionState() |
| 66 | + { |
| 67 | + int selectedCount = AvailableUploaders.Count(x => x.IsSelected); |
| 68 | + |
| 69 | + IsAllSelected = |
| 70 | + selectedCount == 0 ? false |
| 71 | + : selectedCount == AvailableUploaders.Count ? true |
| 72 | + : null; |
| 73 | + } |
| 74 | + |
| 75 | + public static string GetFriendlyName(string fileName) |
| 76 | + { |
| 77 | + if (string.IsNullOrWhiteSpace(fileName)) |
| 78 | + return string.Empty; |
| 79 | + |
| 80 | + // 1. Remove extension |
| 81 | + string name = fileName.EndsWith(".sxcu", StringComparison.OrdinalIgnoreCase) |
| 82 | + ? fileName[..^5] |
| 83 | + : fileName; |
| 84 | + |
| 85 | + // 2. Handle CamelCase / Formatting |
| 86 | + // If the name is "myCustomUploader", this makes it "My Custom Uploader" |
| 87 | + // If the name is "imgur", it makes it "Imgur" |
| 88 | + StringBuilder sb = new StringBuilder(); |
| 89 | + for (int i = 0; i < name.Length; i++) |
| 90 | + { |
| 91 | + char c = name[i]; |
| 92 | + if (i == 0) |
| 93 | + { |
| 94 | + sb.Append(char.ToUpper(c)); |
| 95 | + } |
| 96 | + else if (char.IsUpper(c) && !char.IsUpper(name[i - 1])) |
| 97 | + { |
| 98 | + sb.Append(' '); |
| 99 | + sb.Append(c); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + sb.Append(c); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return sb.ToString().Replace("_", " ").Replace("-", " ").Trim(); |
| 108 | + } |
| 109 | + |
| 110 | + [ObservableProperty] |
| 111 | + private ObservableCollection<UploaderInfo> _availableUploaders = new(); |
| 112 | + |
| 113 | + [RequiresUnreferencedCode("Json type abuse")] |
| 114 | + [RequiresDynamicCode("Json type abuse")] |
| 115 | + public async Task LoadCatalogAsync() |
| 116 | + { |
| 117 | + var client = Core.Utils.Miscellaneous.HttpClientFactory.Get(); |
| 118 | + |
| 119 | + var response = await client.GetFromJsonAsync( |
| 120 | + "https://api.github.com/repos/SnapXL/CustomUploaders/contents/", |
| 121 | + GithubJsonContext.Default.ListGithubContentItem |
| 122 | + ); |
| 123 | + |
| 124 | + if (response != null) |
| 125 | + { |
| 126 | + AvailableUploaders.Clear(); |
| 127 | + foreach (var file in response.Where(f => f.IsSxcu)) |
| 128 | + { |
| 129 | + AvailableUploaders.Add( |
| 130 | + new UploaderInfo |
| 131 | + { |
| 132 | + Name = GetFriendlyName(file.Name), |
| 133 | + DownloadUrl = file.DownloadUrl!, |
| 134 | + } |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + public void UpdateSelection(List<UploaderInfo> selectedItems) |
| 141 | + { |
| 142 | + var selectedSet = selectedItems.ToHashSet(); |
| 143 | + |
| 144 | + foreach (var item in AvailableUploaders) |
| 145 | + { |
| 146 | + // This updates the property on the model inside the ObservableCollection |
| 147 | + item.IsSelected = selectedSet.Contains(item); |
| 148 | + } |
| 149 | + RefreshSelectionState(); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +public partial class UploaderInfo : ObservableObject |
| 154 | +{ |
| 155 | + public string Name { get; init; } |
| 156 | + public string DownloadUrl { get; init; } |
| 157 | + |
| 158 | + [JsonIgnore] |
| 159 | + [ObservableProperty] |
| 160 | + private bool _isSelected; |
| 161 | +} |
0 commit comments