Files
WinWARP/WinWARP.Core/ViewModels/SubsSettingsViewModel.cs
T
2026-09-22 15:23:39 +03:00

80 lines
2.5 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<StandaloneServerViewModel> Servers { get; } = [];
public async Task LoadSubscriptionsAsync()
{
Subscriptions.Clear();
var data = await _dbService.GetSubscriptionsAsync();
foreach (var subscription in data)
{
Subscriptions.Add(new SubscriptionViewModel(subscription));
}
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;
var sub = new SubscriptionViewModel(subscription);
Subscriptions.Add(sub);
ShowLoadingIndicator = false;
}
public void Receive(DeleteServerItemMessage message)
{
Debug.WriteLine($"Deleting server: {message.Value.Title}");
// TODO: add deletion logic
}
}