Files
Wino-Mail/Wino.Mail.ViewModels/MergedAccountDetailsPageViewModel.cs
Burak Kaan Köse ff77b2b3dc 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

218 lines
8.2 KiB
C#

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.Mail.ViewModels.Data;
using Wino.Messaging.Client.Navigation;
using Wino.Messaging.UI;
namespace Wino.Mail.ViewModels
{
public partial class MergedAccountDetailsPageViewModel : BaseViewModel,
IRecipient<MergedInboxRenamed>
{
[ObservableProperty]
private MergedAccountProviderDetailViewModel editingMergedAccount;
[ObservableProperty]
private string mergedAccountName;
public ObservableCollection<AccountProviderDetailViewModel> LinkedAccounts { get; set; } = [];
public ObservableCollection<AccountProviderDetailViewModel> UnlinkedAccounts { get; set; } = [];
// Empty Guid is passed for new created merged inboxes.
public bool IsMergedInboxSaved => EditingMergedAccount != null && EditingMergedAccount.MergedInbox.Id != Guid.Empty;
public bool CanUnlink => IsMergedInboxSaved;
// There must be at least 2 accounts linked to a merged account for link to exist.
public bool ShouldDeleteMergedAccount => LinkedAccounts.Count < 2;
public bool CanSaveChanges
{
get
{
if (IsMergedInboxSaved)
{
return ShouldDeleteMergedAccount || IsEditingAccountsDirty();
}
else
{
return LinkedAccounts.Any();
}
}
}
private readonly IAccountService _accountService;
private readonly IPreferencesService _preferencesService;
private readonly IProviderService _providerService;
public MergedAccountDetailsPageViewModel(IDialogService dialogService,
IAccountService accountService,
IPreferencesService preferencesService,
IProviderService providerService) : base(dialogService)
{
_accountService = accountService;
_preferencesService = preferencesService;
_providerService = providerService;
}
[RelayCommand(CanExecute = nameof(CanUnlink))]
private async Task UnlinkAccountsAsync()
{
if (EditingMergedAccount == null) return;
var isConfirmed = await DialogService.ShowConfirmationDialogAsync(Translator.DialogMessage_UnlinkAccountsConfirmationMessage, Translator.DialogMessage_UnlinkAccountsConfirmationTitle, Translator.Buttons_Yes);
if (!isConfirmed) return;
await _accountService.UnlinkMergedInboxAsync(EditingMergedAccount.MergedInbox.Id);
Messenger.Send(new BackBreadcrumNavigationRequested());
}
[RelayCommand(CanExecute = nameof(CanSaveChanges))]
private async Task SaveChangesAsync()
{
if (ShouldDeleteMergedAccount)
{
await UnlinkAccountsAsync();
}
else
{
if (IsMergedInboxSaved)
{
await _accountService.UpdateMergedInboxAsync(EditingMergedAccount.MergedInbox.Id, LinkedAccounts.Select(a => a.Account.Id).ToList());
}
else
{
await _accountService.CreateMergeAccountsAsync(EditingMergedAccount.MergedInbox, LinkedAccounts.Select(a => a.Account).ToList());
}
// 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;
}
}
Messenger.Send(new BackBreadcrumNavigationRequested());
}
[RelayCommand]
private async Task RenameLinkAsync()
{
if (EditingMergedAccount == null) return;
var newName = await DialogService.ShowTextInputDialogAsync(EditingMergedAccount.MergedInbox.Name,
Translator.DialogMessage_RenameLinkedAccountsTitle,
Translator.DialogMessage_RenameLinkedAccountsMessage,
Translator.FolderOperation_Rename);
if (string.IsNullOrWhiteSpace(newName)) return;
EditingMergedAccount.MergedInbox.Name = newName;
// Update database record as well.
if (IsMergedInboxSaved)
{
await _accountService.RenameMergedAccountAsync(EditingMergedAccount.MergedInbox.Id, newName);
}
else
{
// 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));
}
}
[RelayCommand]
private void LinkAccount(AccountProviderDetailViewModel account)
{
LinkedAccounts.Add(account);
UnlinkedAccounts.Remove(account);
}
[RelayCommand]
private void UnlinkAccount(AccountProviderDetailViewModel account)
{
UnlinkedAccounts.Add(account);
LinkedAccounts.Remove(account);
}
private bool IsEditingAccountsDirty()
{
if (EditingMergedAccount == null) return false;
return EditingMergedAccount.HoldingAccounts.Count != LinkedAccounts.Count ||
EditingMergedAccount.HoldingAccounts.Any(a => !LinkedAccounts.Any(la => la.Account.Id == a.Account.Id));
}
public override void OnNavigatedFrom(NavigationMode mode, object parameters)
{
base.OnNavigatedFrom(mode, parameters);
LinkedAccounts.CollectionChanged -= LinkedAccountsUpdated;
}
private void LinkedAccountsUpdated(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged(nameof(ShouldDeleteMergedAccount));
SaveChangesCommand.NotifyCanExecuteChanged();
// TODO: Preview common folders for all linked accounts.
// Basically showing a preview of how menu items will look.
}
public override async void OnNavigatedTo(NavigationMode mode, object parameters)
{
base.OnNavigatedTo(mode, parameters);
LinkedAccounts.CollectionChanged -= LinkedAccountsUpdated;
LinkedAccounts.CollectionChanged += LinkedAccountsUpdated;
if (parameters is MergedAccountProviderDetailViewModel editingMergedAccount)
{
MergedAccountName = editingMergedAccount.MergedInbox.Name;
EditingMergedAccount = editingMergedAccount;
foreach (var account in editingMergedAccount.HoldingAccounts)
{
LinkedAccounts.Add(account);
}
// Load unlinked accounts.
var allAccounts = await _accountService.GetAccountsAsync();
foreach (var account in allAccounts)
{
if (!LinkedAccounts.Any(a => a.Account.Id == account.Id))
{
var provider = _providerService.GetProviderDetail(account.ProviderType);
UnlinkedAccounts.Add(new AccountProviderDetailViewModel(provider, account));
}
}
}
UnlinkAccountsCommand.NotifyCanExecuteChanged();
}
public void Receive(MergedInboxRenamed message)
{
if (EditingMergedAccount?.MergedInbox.Id == message.MergedInboxId)
{
EditingMergedAccount.MergedInbox.Name = message.NewName;
}
}
}
}