2024-04-18 01:44:37 +02:00
|
|
|
using System;
|
2024-06-13 00:51:59 +02:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
2024-04-18 01:44:37 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2024-06-13 00:51:59 +02:00
|
|
|
using MoreLinq;
|
|
|
|
|
using MoreLinq.Extensions;
|
2024-04-18 01:44:37 +02:00
|
|
|
using Wino.Core.Domain;
|
2024-11-10 23:28:25 +01:00
|
|
|
using Wino.Core.Domain.Entities.Mail;
|
|
|
|
|
using Wino.Core.Domain.Entities.Shared;
|
2024-04-18 01:44:37 +02:00
|
|
|
using Wino.Core.Domain.Interfaces;
|
|
|
|
|
using Wino.Core.Domain.Models.Navigation;
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
namespace Wino.Mail.ViewModels;
|
|
|
|
|
|
|
|
|
|
public partial class SignatureManagementPageViewModel(IMailDialogService dialogService,
|
|
|
|
|
ISignatureService signatureService,
|
|
|
|
|
IAccountService accountService) : MailBaseViewModel
|
2024-04-18 01:44:37 +02:00
|
|
|
{
|
2025-02-16 11:54:23 +01:00
|
|
|
public ObservableCollection<AccountSignature> Signatures { get; set; } = [];
|
2026-04-22 13:25:07 +02:00
|
|
|
private bool isLoaded;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[ObservableProperty]
|
2026-04-22 13:25:07 +02:00
|
|
|
public partial bool IsSignatureEnabled { get; set; }
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public Guid EmptyGuid { get; } = Guid.Empty;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-04-22 13:25:07 +02:00
|
|
|
[ObservableProperty]
|
|
|
|
|
public partial AccountSignature SelectedSignatureForNewMessages { get; set; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-04-22 13:25:07 +02:00
|
|
|
[ObservableProperty]
|
|
|
|
|
public partial AccountSignature SelectedSignatureForFollowingMessages { get; set; }
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private MailAccount Account { get; set; }
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
private readonly IMailDialogService _dialogService = dialogService;
|
|
|
|
|
private readonly ISignatureService _signatureService = signatureService;
|
|
|
|
|
private readonly IAccountService _accountService = accountService;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
public override async void OnNavigatedTo(NavigationMode mode, object parameters)
|
|
|
|
|
{
|
|
|
|
|
base.OnNavigatedTo(mode, parameters);
|
2026-04-22 13:25:07 +02:00
|
|
|
isLoaded = false;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (parameters is Guid accountId)
|
|
|
|
|
Account = await _accountService.GetAccountAsync(accountId);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (Account == null) return;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
var dbSignatures = await _signatureService.GetSignaturesAsync(Account.Id);
|
2026-04-22 13:25:07 +02:00
|
|
|
var noneSignature = new AccountSignature { Id = EmptyGuid, Name = Translator.SettingsSignature_NoneSignatureName };
|
|
|
|
|
var signatureForNewMessages = dbSignatures.FirstOrDefault(x => x.Id == Account.Preferences.SignatureIdForNewMessages) ?? noneSignature;
|
|
|
|
|
var signatureForFollowingMessages = dbSignatures.FirstOrDefault(x => x.Id == Account.Preferences.SignatureIdForFollowingMessages) ?? noneSignature;
|
|
|
|
|
|
|
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
|
|
|
|
IsSignatureEnabled = Account.Preferences.IsSignatureEnabled;
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2026-04-22 13:25:07 +02:00
|
|
|
Signatures.Clear();
|
|
|
|
|
Signatures.Add(noneSignature);
|
|
|
|
|
dbSignatures.ForEach(Signatures.Add);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2026-04-22 13:25:07 +02:00
|
|
|
SelectedSignatureForNewMessages = signatureForNewMessages;
|
|
|
|
|
SelectedSignatureForFollowingMessages = signatureForFollowingMessages;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
isLoaded = true;
|
2025-02-16 11:54:23 +01:00
|
|
|
}
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
protected override async void OnPropertyChanged(PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
base.OnPropertyChanged(e);
|
2025-02-16 11:43:30 +01:00
|
|
|
|
2026-04-22 13:25:07 +02:00
|
|
|
if (!isLoaded || Account?.Preferences == null) return;
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
switch (e.PropertyName)
|
|
|
|
|
{
|
|
|
|
|
case nameof(IsSignatureEnabled):
|
|
|
|
|
Account.Preferences.IsSignatureEnabled = IsSignatureEnabled;
|
|
|
|
|
await _accountService.UpdateAccountAsync(Account);
|
|
|
|
|
break;
|
2026-04-22 13:25:07 +02:00
|
|
|
case nameof(SelectedSignatureForNewMessages):
|
|
|
|
|
Account.Preferences.SignatureIdForNewMessages = GetPersistedSignatureId(SelectedSignatureForNewMessages);
|
2025-02-16 11:54:23 +01:00
|
|
|
await _accountService.UpdateAccountAsync(Account);
|
|
|
|
|
break;
|
2026-04-22 13:25:07 +02:00
|
|
|
case nameof(SelectedSignatureForFollowingMessages):
|
|
|
|
|
Account.Preferences.SignatureIdForFollowingMessages = GetPersistedSignatureId(SelectedSignatureForFollowingMessages);
|
2025-02-16 11:54:23 +01:00
|
|
|
await _accountService.UpdateAccountAsync(Account);
|
|
|
|
|
break;
|
2024-06-13 00:51:59 +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 async Task OpenSignatureEditorCreateAsync()
|
|
|
|
|
{
|
|
|
|
|
var dialogResult = await _dialogService.ShowSignatureEditorDialog();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (dialogResult == null) return;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
dialogResult.MailAccountId = Account.Id;
|
2026-04-22 13:25:07 +02:00
|
|
|
await ExecuteUIThread(() => Signatures.Add(dialogResult));
|
2025-02-16 11:54:23 +01:00
|
|
|
await _signatureService.CreateSignatureAsync(dialogResult);
|
|
|
|
|
}
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[RelayCommand]
|
|
|
|
|
private async Task OpenSignatureEditorEditAsync(AccountSignature signatureModel)
|
|
|
|
|
{
|
|
|
|
|
var dialogResult = await _dialogService.ShowSignatureEditorDialog(signatureModel);
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (dialogResult == null) return;
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
var indexOfCurrentSignature = Signatures.IndexOf(signatureModel);
|
2026-04-22 13:25:07 +02:00
|
|
|
if (indexOfCurrentSignature < 0) return;
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2026-04-22 13:25:07 +02:00
|
|
|
var wasSelectedForNewMessages = SelectedSignatureForNewMessages?.Id == signatureModel.Id;
|
|
|
|
|
var wasSelectedForFollowingMessages = SelectedSignatureForFollowingMessages?.Id == signatureModel.Id;
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2026-04-22 13:25:07 +02:00
|
|
|
dialogResult.MailAccountId = signatureModel.MailAccountId;
|
|
|
|
|
|
|
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
|
|
|
|
Signatures[indexOfCurrentSignature] = dialogResult;
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2026-04-22 13:25:07 +02:00
|
|
|
if (wasSelectedForNewMessages)
|
|
|
|
|
SelectedSignatureForNewMessages = dialogResult;
|
|
|
|
|
|
|
|
|
|
if (wasSelectedForFollowingMessages)
|
|
|
|
|
SelectedSignatureForFollowingMessages = dialogResult;
|
|
|
|
|
});
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await _signatureService.UpdateSignatureAsync(dialogResult);
|
|
|
|
|
}
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
[RelayCommand]
|
|
|
|
|
private async Task DeleteSignatureAsync(AccountSignature signatureModel)
|
|
|
|
|
{
|
|
|
|
|
var shouldRemove = await _dialogService.ShowConfirmationDialogAsync(string.Format(Translator.SignatureDeleteDialog_Message, signatureModel.Name), Translator.SignatureDeleteDialog_Title, Translator.Buttons_Delete);
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
if (!shouldRemove) return;
|
2024-06-13 00:51:59 +02:00
|
|
|
|
2026-04-22 13:25:07 +02:00
|
|
|
var shouldResetNewMessagesSignature = SelectedSignatureForNewMessages?.Id == signatureModel.Id;
|
|
|
|
|
var shouldResetFollowingMessagesSignature = SelectedSignatureForFollowingMessages?.Id == signatureModel.Id;
|
|
|
|
|
|
|
|
|
|
await ExecuteUIThread(() =>
|
|
|
|
|
{
|
|
|
|
|
Signatures.Remove(signatureModel);
|
|
|
|
|
|
|
|
|
|
var noneSignature = GetNoneSignature();
|
|
|
|
|
|
|
|
|
|
if (shouldResetNewMessagesSignature)
|
|
|
|
|
SelectedSignatureForNewMessages = noneSignature;
|
|
|
|
|
|
|
|
|
|
if (shouldResetFollowingMessagesSignature)
|
|
|
|
|
SelectedSignatureForFollowingMessages = noneSignature;
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-16 11:54:23 +01:00
|
|
|
await _signatureService.DeleteSignatureAsync(signatureModel);
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|
2026-04-22 13:25:07 +02:00
|
|
|
|
|
|
|
|
private Guid? GetPersistedSignatureId(AccountSignature signature)
|
|
|
|
|
=> signature?.Id is Guid signatureId && signatureId != EmptyGuid
|
|
|
|
|
? signatureId
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
|
|
private AccountSignature GetNoneSignature()
|
|
|
|
|
=> Signatures.FirstOrDefault(x => x.Id == EmptyGuid)
|
|
|
|
|
?? new AccountSignature { Id = EmptyGuid, Name = Translator.SettingsSignature_NoneSignatureName };
|
2024-04-18 01:44:37 +02:00
|
|
|
}
|