Files
Wino-Mail/Wino.Core.ViewModels/AccountManagementPageViewModelBase.cs
T

140 lines
6.1 KiB
C#
Raw Normal View History

2024-11-10 23:28:25 +01:00
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Wino.Core.Domain;
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Navigation;
2024-11-10 23:28:25 +01:00
using Wino.Mail.ViewModels.Data;
using Wino.Messaging.Client.Navigation;
2025-02-16 11:54:23 +01:00
namespace Wino.Core.ViewModels;
public abstract partial class AccountManagementPageViewModelBase : CoreBaseViewModel
2024-11-10 23:28:25 +01:00
{
2025-02-16 11:54:23 +01:00
public ObservableCollection<IAccountProviderDetailViewModel> Accounts { get; set; } = [];
public bool IsPurchasePanelVisible => !HasUnlimitedAccountProduct;
public bool IsAccountCreationAlmostOnLimit => Accounts != null && Accounts.Count == FREE_ACCOUNT_COUNT - 1;
public bool HasAccountsDefined => Accounts != null && Accounts.Any();
public bool CanReorderAccounts => Accounts?.Sum(a => a.HoldingAccountCount) > 1;
public string UsedAccountsString => string.Format(Translator.WinoUpgradeRemainingAccountsMessage, Accounts.Count, FREE_ACCOUNT_COUNT);
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsPurchasePanelVisible))]
public partial bool HasUnlimitedAccountProduct { get; set; }
2025-02-16 11:54:23 +01:00
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsAccountCreationAlmostOnLimit))]
[NotifyPropertyChangedFor(nameof(IsPurchasePanelVisible))]
public partial bool IsAccountCreationBlocked { get; set; }
2025-02-16 11:54:23 +01:00
[ObservableProperty]
public partial IAccountProviderDetailViewModel StartupAccount { get; set; }
2025-02-16 11:54:23 +01:00
public int FREE_ACCOUNT_COUNT { get; } = 3;
protected IDialogServiceBase DialogService { get; }
protected INavigationService NavigationService { get; }
protected IAccountService AccountService { get; }
protected IProviderService ProviderService { get; }
protected IStoreManagementService StoreManagementService { get; }
2026-03-19 01:50:14 +01:00
protected IWinoAccountProfileService WinoAccountProfileService { get; }
2025-02-16 11:54:23 +01:00
protected IAuthenticationProvider AuthenticationProvider { get; }
protected IPreferencesService PreferencesService { get; }
public AccountManagementPageViewModelBase(IDialogServiceBase dialogService,
INavigationService navigationService,
IAccountService accountService,
IProviderService providerService,
IStoreManagementService storeManagementService,
2026-03-19 01:50:14 +01:00
IWinoAccountProfileService winoAccountProfileService,
2025-02-16 11:54:23 +01:00
IAuthenticationProvider authenticationProvider,
IPreferencesService preferencesService)
2024-11-10 23:28:25 +01:00
{
2025-02-16 11:54:23 +01:00
DialogService = dialogService;
NavigationService = navigationService;
AccountService = accountService;
ProviderService = providerService;
StoreManagementService = storeManagementService;
2026-03-19 01:50:14 +01:00
WinoAccountProfileService = winoAccountProfileService;
2025-02-16 11:54:23 +01:00
AuthenticationProvider = authenticationProvider;
PreferencesService = preferencesService;
}
2024-11-10 23:28:25 +01:00
2025-02-16 11:54:23 +01:00
[RelayCommand]
private void NavigateAccountDetails(AccountProviderDetailViewModel accountDetails)
{
2026-04-16 13:45:11 +02:00
Messenger.Send(new BreadcrumbNavigationRequested(GetAccountDetailsTitle(accountDetails.Account),
2025-02-16 11:54:23 +01:00
WinoPage.AccountDetailsPage,
accountDetails.Account.Id));
}
2024-11-10 23:28:25 +01:00
2025-02-16 11:54:23 +01:00
[RelayCommand]
public async Task PurchaseUnlimitedAccountAsync()
{
2026-03-19 01:50:14 +01:00
var purchaseResult = await StoreManagementService.PurchaseAsync(WinoAddOnProductType.UNLIMITED_ACCOUNTS);
2024-11-10 23:28:25 +01:00
2025-02-16 11:54:23 +01:00
if (purchaseResult == StorePurchaseResult.Succeeded)
DialogService.InfoBarMessage(Translator.Info_PurchaseThankYouTitle, Translator.Info_PurchaseThankYouMessage, InfoBarMessageType.Success);
else if (purchaseResult == StorePurchaseResult.AlreadyPurchased)
DialogService.InfoBarMessage(Translator.Info_PurchaseExistsTitle, Translator.Info_PurchaseExistsMessage, InfoBarMessageType.Warning);
2024-11-10 23:28:25 +01:00
2025-02-16 11:54:23 +01:00
bool shouldRefreshPurchasePanel = purchaseResult == StorePurchaseResult.Succeeded || purchaseResult == StorePurchaseResult.AlreadyPurchased;
2024-11-10 23:28:25 +01:00
2025-02-16 11:54:23 +01:00
if (shouldRefreshPurchasePanel)
2024-11-10 23:28:25 +01:00
{
2025-02-16 11:54:23 +01:00
await ManageStorePurchasesAsync();
2025-02-16 11:43:30 +01:00
}
2025-02-16 11:54:23 +01:00
}
2024-11-10 23:28:25 +01:00
2025-02-16 11:54:23 +01:00
public async Task ManageStorePurchasesAsync()
{
var hasUnlimitedAccountProduct = await StoreManagementService.HasProductAsync(WinoAddOnProductType.UNLIMITED_ACCOUNTS).ConfigureAwait(false);
2026-03-19 01:50:14 +01:00
await ExecuteUIThread(() =>
{
HasUnlimitedAccountProduct = hasUnlimitedAccountProduct;
IsAccountCreationBlocked = !hasUnlimitedAccountProduct && Accounts.Count >= FREE_ACCOUNT_COUNT;
2025-02-16 11:54:23 +01:00
});
}
2025-02-16 11:54:23 +01:00
public AccountProviderDetailViewModel GetAccountProviderDetails(MailAccount account)
{
var provider = ProviderService.GetProviderDetail(account.ProviderType);
2025-02-16 11:54:23 +01:00
return new AccountProviderDetailViewModel(provider, account);
}
2025-02-16 11:54:23 +01:00
public abstract Task InitializeAccountsAsync();
2025-02-16 11:54:23 +01:00
public override void OnNavigatedTo(NavigationMode mode, object parameters)
{
base.OnNavigatedTo(mode, parameters);
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
Accounts.CollectionChanged -= AccountsChanged;
Accounts.CollectionChanged += AccountsChanged;
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
public override void OnNavigatedFrom(NavigationMode mode, object parameters)
{
base.OnNavigatedFrom(mode, parameters);
Accounts.CollectionChanged -= AccountsChanged;
}
private void AccountsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged(nameof(HasAccountsDefined));
2024-11-10 23:28:25 +01:00
}
2026-04-16 13:45:11 +02:00
private static string GetAccountDetailsTitle(MailAccount account)
=> !string.IsNullOrWhiteSpace(account?.Address)
? string.Format(Translator.SettingsAccountDetails_NavigationTitle, account.Address)
: account?.Name ?? Translator.AccountDetailsPage_Title;
2024-11-10 23:28:25 +01:00
}