using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Text; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using WinWARP.Core.Enums; using WinWARP.Core.Messages; using WinWARP.Core.Models.Entities; using WinWARP.Core.Services.Interfaces; using WinWARP.Core.ViewModels.Wrappers; namespace WinWARP.Core.ViewModels; public partial class SubsSettingsViewModel(ISubscriptionDbService dbService, ISubscriptionFetcher fetcher, IDialogService dialogService) : ObservableRecipient, IRecipient { private readonly ISubscriptionDbService _dbService = dbService; private readonly ISubscriptionFetcher _fetcher = fetcher; private readonly IDialogService _dialogService = dialogService; protected override void OnActivated() { Messenger.RegisterAll(this); } public ObservableCollection Subscriptions { get; } = []; public ObservableCollection Servers { get; } = []; public async Task LoadSubscriptionsAsync() { Subscriptions.Clear(); var data = await _dbService.GetSubscriptionsAsync(); foreach (var subscription in data) { Subscriptions.Add(new SubscriptionViewModel(subscription)); } Servers.Add(new StandaloneServerViewModel(new StandaloneServer() { Region = "NO", Title = "Not default title", DefaultTitle = "Default title" }) { Tags = { "VLESS", "GRPC", "REALITY" } }); ShowLoadingIndicator = false; } [ObservableProperty] public partial bool ShowLoadingIndicator { get; set; } = false; [RelayCommand] public async Task AddSubscriptionAsync() { (var result, var customName, var subscriptionLink, var updateInterval) = await _dialogService.ShowAddSubscriptionAsync(); if (result is CommonDialogResult.Cancel or CommonDialogResult.None or CommonDialogResult.Secondary) return; ShowLoadingIndicator = true; var subscription = await _fetcher.FetchAsync(subscriptionLink); // TODO: error handling here and in the fetcher, dialog that will display exception if (subscription == null) { ShowLoadingIndicator = false; return; } subscription.Title = customName; subscription.UpdateInterval = updateInterval; Subscriptions.Add(new SubscriptionViewModel(subscription)); foreach (var server in subscription.Servers) { Servers.Add(new SubscriptionServerViewModel(server) { Tags = { "VLESS", "WS", "TLS" } }); } ShowLoadingIndicator = false; } public void Receive(DeleteServerItemMessage message) { Debug.WriteLine($"Deleting server: {message.Value.Title}"); // TODO: add deletion logic } }