| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using Microsoft.JSInterop; |
| | | 4 | | |
| | | 5 | | namespace UIBlazor.Services.Settings; |
| | | 6 | | |
| | | 7 | | public class ProfileService(ILocalStorageService localStorage, ILogger<ProfileService> logger, IJSRuntime jSRuntime) |
| | 7 | 8 | | : BaseSettingsProvider<ProfileOptions>(localStorage, logger, "ProfileSettings"), IProfileManager |
| | | 9 | | { |
| | 27 | 10 | | public ConnectionProfile ActiveProfile { get; private set; } |
| | | 11 | | |
| | | 12 | | protected override async Task AfterInitAsync() |
| | | 13 | | { |
| | 6 | 14 | | if (Current.Profiles.Count == 0) |
| | | 15 | | { |
| | 1 | 16 | | await ResetAsync(); |
| | | 17 | | } |
| | | 18 | | |
| | 13 | 19 | | ActiveProfile = Current.Profiles.FirstOrDefault(p => p.Id == Current.ActiveProfileId) ?? Current.Profiles.First( |
| | | 20 | | |
| | 26 | 21 | | foreach (var profile in Current.Profiles) |
| | | 22 | | { |
| | 7 | 23 | | profile.PropertyChanged += OnProfilePropertyChanged; |
| | | 24 | | } |
| | | 25 | | |
| | 6 | 26 | | if (ActiveProfile.SkipSSL) |
| | | 27 | | { |
| | 1 | 28 | | NotifySkipSsl(ActiveProfile.SkipSSL); |
| | | 29 | | } |
| | 6 | 30 | | } |
| | | 31 | | |
| | | 32 | | protected override void OnAnyPropertyChanged(string? propertyName) |
| | | 33 | | { |
| | 1 | 34 | | if (propertyName == nameof(ProfileOptions.ActiveProfileId)) |
| | | 35 | | { |
| | 2 | 36 | | ActiveProfile = Current.Profiles.FirstOrDefault(p => p.Id == Current.ActiveProfileId) ?? Current.Profiles.Fi |
| | | 37 | | } |
| | 1 | 38 | | } |
| | | 39 | | |
| | | 40 | | private void OnProfilePropertyChanged(object? sender, PropertyChangedEventArgs e) |
| | | 41 | | { |
| | 1 | 42 | | if (sender is ConnectionProfile profile && profile.Id == Current.ActiveProfileId) |
| | | 43 | | { |
| | 1 | 44 | | if (e.PropertyName == nameof(ConnectionProfile.SkipSSL)) |
| | | 45 | | { |
| | 1 | 46 | | NotifySkipSsl(profile.SkipSSL); |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | 1 | 50 | | Debouncer.Trigger(); |
| | 1 | 51 | | } |
| | | 52 | | |
| | | 53 | | private void NotifySkipSsl(bool skipSsl) |
| | | 54 | | { |
| | 3 | 55 | | jSRuntime.InvokeAsync<string>("postVsMessage", new VsRequest { Action = BasicEnum.SkipSSL, Payload = skipSsl.ToS |
| | 3 | 56 | | } |
| | | 57 | | |
| | | 58 | | public async Task DeleteProfileAsync(string profileId) |
| | | 59 | | { |
| | 2 | 60 | | if (Current.Profiles.Count < 2) |
| | | 61 | | { |
| | 1 | 62 | | return; // нельзя удалять единственный профиль. |
| | | 63 | | } |
| | | 64 | | |
| | 3 | 65 | | var profile = Current.Profiles.FirstOrDefault(p => p.Id == profileId); |
| | 1 | 66 | | if (profile != null) |
| | | 67 | | { |
| | 1 | 68 | | profile.PropertyChanged -= OnProfilePropertyChanged; |
| | 1 | 69 | | Current.Profiles.Remove(profile); |
| | | 70 | | |
| | | 71 | | // актуализация активного профиля |
| | 1 | 72 | | if (profileId == Current.ActiveProfileId) |
| | | 73 | | { |
| | 0 | 74 | | await ActivateProfileAsync(Current.Profiles.First().Id, saveImediatly: true); |
| | | 75 | | } |
| | | 76 | | else |
| | | 77 | | { |
| | 1 | 78 | | Debouncer.Trigger(); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | 1 | 82 | | return; |
| | 2 | 83 | | } |
| | | 84 | | |
| | | 85 | | public async Task ActivateProfileAsync(string profileId, bool saveImediatly = false) |
| | | 86 | | { |
| | 2 | 87 | | var profile = Current.Profiles.FirstOrDefault(p => p.Id == profileId); |
| | 1 | 88 | | if (profile != null) |
| | | 89 | | { |
| | 1 | 90 | | Current.ActiveProfileId = profileId; |
| | | 91 | | |
| | 1 | 92 | | NotifySkipSsl(profile.SkipSSL); |
| | | 93 | | |
| | 1 | 94 | | if (!saveImediatly) |
| | | 95 | | { |
| | 0 | 96 | | Debouncer.Trigger(); |
| | | 97 | | } |
| | | 98 | | else |
| | | 99 | | { |
| | 1 | 100 | | await SaveAsync(); |
| | | 101 | | } |
| | | 102 | | } |
| | 1 | 103 | | } |
| | | 104 | | |
| | | 105 | | public override async Task ResetAsync() |
| | | 106 | | { |
| | 4 | 107 | | foreach (var p in Current.Profiles) |
| | | 108 | | { |
| | 0 | 109 | | p.PropertyChanged -= OnProfilePropertyChanged; |
| | | 110 | | } |
| | | 111 | | |
| | 2 | 112 | | var defaultProfile = new ConnectionProfile |
| | 2 | 113 | | { |
| | 2 | 114 | | Name = "default", |
| | 2 | 115 | | Endpoint = "https://api.openai.com" |
| | 2 | 116 | | }; |
| | | 117 | | |
| | 2 | 118 | | defaultProfile.PropertyChanged += OnProfilePropertyChanged; |
| | 2 | 119 | | Current.Profiles = [defaultProfile]; |
| | 2 | 120 | | Current.ActiveProfileId = defaultProfile.Id; |
| | 2 | 121 | | ActiveProfile = defaultProfile; |
| | | 122 | | |
| | 2 | 123 | | await SaveAsync(); |
| | 2 | 124 | | } |
| | | 125 | | |
| | | 126 | | public override void Dispose() |
| | | 127 | | { |
| | 0 | 128 | | base.Dispose(); |
| | 0 | 129 | | foreach (var p in Current.Profiles) |
| | | 130 | | { |
| | 0 | 131 | | p.PropertyChanged -= OnProfilePropertyChanged; |
| | | 132 | | } |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | private static void CopyProfileProperties(ConnectionProfile from, ConnectionProfile to) |
| | | 136 | | { |
| | 0 | 137 | | var properties = typeof(ConnectionProfile) |
| | 0 | 138 | | .GetProperties(BindingFlags.Public | BindingFlags.Instance) |
| | 0 | 139 | | .Where(p => p is { CanRead: true, CanWrite: true } && p.Name != nameof(ConnectionProfile.Id)); |
| | | 140 | | |
| | 0 | 141 | | foreach (var prop in properties) |
| | | 142 | | { |
| | 0 | 143 | | var value = prop.GetValue(from); |
| | 0 | 144 | | prop.SetValue(to, value); |
| | | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | } |