85 lines
3.0 KiB
C#
85 lines
3.0 KiB
C#
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<DeleteServerItemMessage>
|
|
{
|
|
private readonly ISubscriptionDbService _dbService = dbService;
|
|
private readonly ISubscriptionFetcher _fetcher = fetcher;
|
|
private readonly IDialogService _dialogService = dialogService;
|
|
|
|
protected override void OnActivated()
|
|
{
|
|
Messenger.RegisterAll(this);
|
|
}
|
|
|
|
public ObservableCollection<SubscriptionViewModel> Subscriptions { get; } = [];
|
|
public ObservableCollection<ServerItemViewModel> 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)
|
|
{
|
|
var serverVM = new SubscriptionServerViewModel(server);
|
|
foreach (var tag in _fetcher.GetTags(server.ConnectionString)) serverVM.Tags.Add(tag);
|
|
Servers.Add(serverVM);
|
|
}
|
|
ShowLoadingIndicator = false;
|
|
}
|
|
|
|
public void Receive(DeleteServerItemMessage message)
|
|
{
|
|
Debug.WriteLine($"Deleting server: {message.Value.Title}");
|
|
// TODO: add deletion logic
|
|
}
|
|
}
|