using System.Collections.ObjectModel; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Messaging; using Wino.Core.Domain; using Wino.Core.Domain.Entities.Shared; using Wino.Core.Domain.Interfaces; using Wino.Core.Domain.Models.Navigation; using Wino.Core.ViewModels.Data; using Wino.Mail.ViewModels.Data; using Wino.Messaging.Client.Navigation; namespace Wino.Core.ViewModels; public abstract partial class AccountManagementPageViewModelBase : CoreBaseViewModel { public ObservableCollection Accounts { get; set; } = []; public IEnumerable StartupAccounts => Accounts.Where(IsStartupEligible); public bool HasAccountsDefined => Accounts != null && Accounts.Any(); public bool CanReorderAccounts => Accounts?.Sum(a => a.HoldingAccountCount) > 1; [ObservableProperty] public partial IAccountProviderDetailViewModel StartupAccount { get; set; } protected IDialogServiceBase DialogService { get; } protected INavigationService NavigationService { get; } protected IAccountService AccountService { get; } protected IProviderService ProviderService { get; } protected IWinoAccountProfileService WinoAccountProfileService { get; } protected IAuthenticationProvider AuthenticationProvider { get; } protected IPreferencesService PreferencesService { get; } public AccountManagementPageViewModelBase(IDialogServiceBase dialogService, INavigationService navigationService, IAccountService accountService, IProviderService providerService, IWinoAccountProfileService winoAccountProfileService, IAuthenticationProvider authenticationProvider, IPreferencesService preferencesService) { DialogService = dialogService; NavigationService = navigationService; AccountService = accountService; ProviderService = providerService; WinoAccountProfileService = winoAccountProfileService; AuthenticationProvider = authenticationProvider; PreferencesService = preferencesService; } public AccountProviderDetailViewModel GetAccountProviderDetails(MailAccount account) { var provider = ProviderService.GetProviderDetail(account.ProviderType); return new AccountProviderDetailViewModel(provider, account); } public abstract Task InitializeAccountsAsync(); public override void OnNavigatedTo(NavigationMode mode, object parameters) { base.OnNavigatedTo(mode, parameters); Accounts.CollectionChanged -= AccountsChanged; Accounts.CollectionChanged += AccountsChanged; } 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)); OnPropertyChanged(nameof(StartupAccounts)); } protected static string GetAccountDetailsTitle(MailAccount account) => !string.IsNullOrWhiteSpace(account?.Address) ? string.Format(Translator.SettingsAccountDetails_NavigationTitle, account.Address) : account?.Name ?? Translator.AccountDetailsPage_Title; private static bool IsStartupEligible(IAccountProviderDetailViewModel account) { return account switch { AccountProviderDetailViewModel accountViewModel => accountViewModel.Account.IsMailAccessGranted, MergedAccountProviderDetailViewModel mergedAccountViewModel => mergedAccountViewModel.HoldingAccounts.Any(a => a.Account.IsMailAccessGranted), _ => true }; } }