Files
Wino-Mail/Wino.Mail.ViewModels/MergedAccountDetailsPageViewModel.cs

220 lines
7.7 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
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.Interfaces;
using Wino.Core.Domain.Models.Navigation;
using Wino.Core.ViewModels.Data;
2024-04-18 01:44:37 +02:00
using Wino.Mail.ViewModels.Data;
Full trust Wino Server implementation. (#295) * Separation of messages. Introducing Wino.Messages library. * Wino.Server and Wino.Packaging projects. Enabling full trust for UWP and app service connection manager basics. * Remove debug code. * Enable generating assembly info to deal with unsupported os platform warnings. * Fix server-client connection. * UIMessage communication. Single instancing for server and re-connection mechanism on suspension. * Removed IWinoSynchronizerFactory from UWP project. * Removal of background task service from core. * Delegating changes to UI and triggering new background synchronization. * Fix build error. * Moved core lib messages to Messaging project. * Better client-server communication. Handling of requests in the server. New synchronizer factory in the server. * WAM broker and MSAL token caching for OutlookAuthenticator. Handling account creation for Outlook. * WinoServerResponse basics. * Delegating protocol activation for Gmail authenticator. * Adding margin to searchbox to match action bar width. * Move libraries into lib folder. * Storing base64 encoded mime on draft creation instead of MimeMessage object. Fixes serialization/deserialization issue with S.T.Json * Scrollbar adjustments * WınoExpander for thread expander layout ıssue. * Handling synchronizer state changes. * Double init on background activation. * FIxing packaging issues and new Wino Mail launcher protocol for activation from full thrust process. * Remove debug deserialization. * Remove debug code. * Making sure the server connection is established when the app is launched. * Thrust -> Trust string replacement... * Rename package to Wino Mail * Enable translated values in the server. * Fixed an issue where toast activation can't find the clicked mail after the folder is initialized. * Revert debug code. * Change server background sync to every 3 minute and Inbox only synchronization. * Revert google auth changes. * App preferences page. * Changing tray icon visibility on preference change. * Start the server with invisible tray icon if set to invisible. * Reconnect button on the title bar. * Handling of toast actions. * Enable x86 build for server during packaging. * Get rid of old background tasks and v180 migration. * Terminate client when Exit clicked in server. * Introducing SynchronizationSource to prevent notifying UI after server tick synchronization. * Remove confirmAppClose restricted capability and unused debug code in manifest. * Closing the reconnect info popup when reconnect is clicked. * Custom RetryHandler for OutlookSynchronizer and separating client/server logs. * Running server on Windows startup. * Fix startup exe. * Fix for expander list view item paddings. * Force full sync on app launch instead of Inbox. * Fix draft creation. * Fix an issue with custom folder sync logic. * Reporting back account sync progress from server. * Fix sending drafts and missing notifications for imap. * Changing imap folder sync requirements. * Retain file count is set to 3. * Disabled swipe gestures temporarily due to native crash with SwipeControl * Save all attachments implementation. * Localization for save all attachments button. * Fix logging dates for logs. * Fixing ARM64 build. * Add ARM64 build config to packaging project. * Comment out OutOfProcPDB for ARM64. * Hnadling GONE response for Outlook folder synchronization.
2024-08-05 00:36:26 +02:00
using Wino.Messaging.Client.Navigation;
using Wino.Messaging.UI;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
namespace Wino.Mail.ViewModels;
public partial class MergedAccountDetailsPageViewModel : MailBaseViewModel,
IRecipient<MergedInboxRenamed>
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
[ObservableProperty]
private MergedAccountProviderDetailViewModel editingMergedAccount;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
[ObservableProperty]
private string mergedAccountName;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public ObservableCollection<AccountProviderDetailViewModel> LinkedAccounts { get; set; } = [];
public ObservableCollection<AccountProviderDetailViewModel> UnlinkedAccounts { get; set; } = [];
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Empty Guid is passed for new created merged inboxes.
public bool IsMergedInboxSaved => EditingMergedAccount != null && EditingMergedAccount.MergedInbox.Id != Guid.Empty;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public bool CanUnlink => IsMergedInboxSaved;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// There must be at least 2 accounts linked to a merged account for link to exist.
public bool ShouldDeleteMergedAccount => LinkedAccounts.Count < 2;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public bool CanSaveChanges
{
get
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
if (IsMergedInboxSaved)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
return ShouldDeleteMergedAccount || IsEditingAccountsDirty();
}
else
{
return LinkedAccounts.Any();
2024-04-18 01:44:37 +02:00
}
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private readonly IMailDialogService _dialogService;
private readonly IAccountService _accountService;
private readonly IPreferencesService _preferencesService;
private readonly IProviderService _providerService;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public MergedAccountDetailsPageViewModel(IMailDialogService dialogService,
IAccountService accountService,
IPreferencesService preferencesService,
IProviderService providerService)
{
_dialogService = dialogService;
_accountService = accountService;
_preferencesService = preferencesService;
_providerService = providerService;
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
[RelayCommand(CanExecute = nameof(CanUnlink))]
private async Task UnlinkAccountsAsync()
{
if (EditingMergedAccount == null) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var isConfirmed = await _dialogService.ShowConfirmationDialogAsync(Translator.DialogMessage_UnlinkAccountsConfirmationMessage, Translator.DialogMessage_UnlinkAccountsConfirmationTitle, Translator.Buttons_Yes);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (!isConfirmed) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
await _accountService.UnlinkMergedInboxAsync(EditingMergedAccount.MergedInbox.Id);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
Messenger.Send(new BackBreadcrumNavigationRequested());
}
2025-02-16 11:54:23 +01:00
[RelayCommand(CanExecute = nameof(CanSaveChanges))]
private async Task SaveChangesAsync()
{
if (ShouldDeleteMergedAccount)
{
await UnlinkAccountsAsync();
}
else
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
if (IsMergedInboxSaved)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
await _accountService.UpdateMergedInboxAsync(EditingMergedAccount.MergedInbox.Id, LinkedAccounts.Select(a => a.Account.Id).ToList());
2024-04-18 01:44:37 +02:00
}
else
{
2025-02-16 11:54:23 +01:00
await _accountService.CreateMergeAccountsAsync(EditingMergedAccount.MergedInbox, LinkedAccounts.Select(a => a.Account).ToList());
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
// Startup entity is linked now. Change the startup entity.
if (_preferencesService.StartupEntityId != null && LinkedAccounts.Any(a => a.StartupEntityId == _preferencesService.StartupEntityId))
{
_preferencesService.StartupEntityId = EditingMergedAccount.MergedInbox.Id;
}
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
Messenger.Send(new BackBreadcrumNavigationRequested());
}
2025-02-16 11:54:23 +01:00
[RelayCommand]
private async Task RenameLinkAsync()
{
if (EditingMergedAccount == null) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var newName = await _dialogService.ShowTextInputDialogAsync(EditingMergedAccount.MergedInbox.Name,
Translator.DialogMessage_RenameLinkedAccountsTitle,
Translator.DialogMessage_RenameLinkedAccountsMessage,
Translator.FolderOperation_Rename);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (string.IsNullOrWhiteSpace(newName)) return;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
EditingMergedAccount.MergedInbox.Name = newName;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// Update database record as well.
if (IsMergedInboxSaved)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
await _accountService.RenameMergedAccountAsync(EditingMergedAccount.MergedInbox.Id, newName);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
else
{
2025-02-16 11:54:23 +01:00
// Publish the message manually since the merged inbox is not saved yet.
// This is only for breadcrump item update.
Messenger.Send(new MergedInboxRenamed(EditingMergedAccount.MergedInbox.Id, newName));
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
[RelayCommand]
private void LinkAccount(AccountProviderDetailViewModel account)
{
LinkedAccounts.Add(account);
UnlinkedAccounts.Remove(account);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
[RelayCommand]
private void UnlinkAccount(AccountProviderDetailViewModel account)
{
UnlinkedAccounts.Add(account);
LinkedAccounts.Remove(account);
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private bool IsEditingAccountsDirty()
{
if (EditingMergedAccount == null) return false;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
return EditingMergedAccount.HoldingAccounts.Count != LinkedAccounts.Count ||
EditingMergedAccount.HoldingAccounts.Any(a => !LinkedAccounts.Any(la => la.Account.Id == a.Account.Id));
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public override void OnNavigatedFrom(NavigationMode mode, object parameters)
{
base.OnNavigatedFrom(mode, parameters);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
LinkedAccounts.CollectionChanged -= LinkedAccountsUpdated;
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
private void LinkedAccountsUpdated(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged(nameof(ShouldDeleteMergedAccount));
SaveChangesCommand.NotifyCanExecuteChanged();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
// TODO: Preview common folders for all linked accounts.
// Basically showing a preview of how menu items will look.
}
2025-02-16 11:54:23 +01:00
public override async 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
LinkedAccounts.CollectionChanged -= LinkedAccountsUpdated;
LinkedAccounts.CollectionChanged += LinkedAccountsUpdated;
if (parameters is MergedAccountProviderDetailViewModel editingMergedAccount)
{
MergedAccountName = editingMergedAccount.MergedInbox.Name;
EditingMergedAccount = editingMergedAccount;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
foreach (var account in editingMergedAccount.HoldingAccounts)
{
LinkedAccounts.Add(account);
}
// Load unlinked accounts.
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
var allAccounts = await _accountService.GetAccountsAsync();
2025-02-16 11:54:23 +01:00
foreach (var account in allAccounts)
{
if (!LinkedAccounts.Any(a => a.Account.Id == account.Id))
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
var provider = _providerService.GetProviderDetail(account.ProviderType);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
UnlinkedAccounts.Add(new AccountProviderDetailViewModel(provider, account));
2024-04-18 01:44:37 +02:00
}
}
}
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
UnlinkAccountsCommand.NotifyCanExecuteChanged();
}
public void Receive(MergedInboxRenamed message)
{
if (EditingMergedAccount?.MergedInbox.Id == message.MergedInboxId)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
EditingMergedAccount.MergedInbox.Name = message.NewName;
2024-04-18 01:44:37 +02:00
}
}
}