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