Fix signature settings persistence and editing
This commit is contained in:
@@ -20,47 +20,18 @@ public partial class SignatureManagementPageViewModel(IMailDialogService dialogS
|
|||||||
IAccountService accountService) : MailBaseViewModel
|
IAccountService accountService) : MailBaseViewModel
|
||||||
{
|
{
|
||||||
public ObservableCollection<AccountSignature> Signatures { get; set; } = [];
|
public ObservableCollection<AccountSignature> Signatures { get; set; } = [];
|
||||||
|
private bool isLoaded;
|
||||||
|
|
||||||
[ObservableProperty]
|
[ObservableProperty]
|
||||||
private bool isSignatureEnabled;
|
public partial bool IsSignatureEnabled { get; set; }
|
||||||
|
|
||||||
private int signatureForNewMessagesIndex;
|
|
||||||
|
|
||||||
public Guid EmptyGuid { get; } = Guid.Empty;
|
public Guid EmptyGuid { get; } = Guid.Empty;
|
||||||
|
|
||||||
public int SignatureForNewMessagesIndex
|
[ObservableProperty]
|
||||||
{
|
public partial AccountSignature SelectedSignatureForNewMessages { get; set; }
|
||||||
get => signatureForNewMessagesIndex;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (value == -1)
|
|
||||||
{
|
|
||||||
SetProperty(ref signatureForNewMessagesIndex, 0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SetProperty(ref signatureForNewMessagesIndex, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private int signatureForFollowingMessagesIndex;
|
[ObservableProperty]
|
||||||
|
public partial AccountSignature SelectedSignatureForFollowingMessages { get; set; }
|
||||||
public int SignatureForFollowingMessagesIndex
|
|
||||||
{
|
|
||||||
get => signatureForFollowingMessagesIndex;
|
|
||||||
set
|
|
||||||
{
|
|
||||||
if (value == -1)
|
|
||||||
{
|
|
||||||
SetProperty(ref signatureForFollowingMessagesIndex, 0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
SetProperty(ref signatureForFollowingMessagesIndex, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private MailAccount Account { get; set; }
|
private MailAccount Account { get; set; }
|
||||||
|
|
||||||
@@ -71,6 +42,7 @@ public partial class SignatureManagementPageViewModel(IMailDialogService dialogS
|
|||||||
public override async void OnNavigatedTo(NavigationMode mode, object parameters)
|
public override async void OnNavigatedTo(NavigationMode mode, object parameters)
|
||||||
{
|
{
|
||||||
base.OnNavigatedTo(mode, parameters);
|
base.OnNavigatedTo(mode, parameters);
|
||||||
|
isLoaded = false;
|
||||||
|
|
||||||
if (parameters is Guid accountId)
|
if (parameters is Guid accountId)
|
||||||
Account = await _accountService.GetAccountAsync(accountId);
|
Account = await _accountService.GetAccountAsync(accountId);
|
||||||
@@ -78,36 +50,43 @@ public partial class SignatureManagementPageViewModel(IMailDialogService dialogS
|
|||||||
if (Account == null) return;
|
if (Account == null) return;
|
||||||
|
|
||||||
var dbSignatures = await _signatureService.GetSignaturesAsync(Account.Id);
|
var dbSignatures = await _signatureService.GetSignaturesAsync(Account.Id);
|
||||||
|
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;
|
IsSignatureEnabled = Account.Preferences.IsSignatureEnabled;
|
||||||
|
|
||||||
Signatures.Clear();
|
Signatures.Clear();
|
||||||
Signatures.Add(new AccountSignature { Id = EmptyGuid, Name = Translator.SettingsSignature_NoneSignatureName });
|
Signatures.Add(noneSignature);
|
||||||
dbSignatures.ForEach(Signatures.Add);
|
dbSignatures.ForEach(Signatures.Add);
|
||||||
|
|
||||||
SignatureForNewMessagesIndex = Signatures.IndexOf(Signatures.FirstOrDefault(x => x.Id == Account.Preferences.SignatureIdForNewMessages));
|
SelectedSignatureForNewMessages = signatureForNewMessages;
|
||||||
SignatureForFollowingMessagesIndex = Signatures.IndexOf(Signatures.FirstOrDefault(x => x.Id == Account.Preferences.SignatureIdForFollowingMessages));
|
SelectedSignatureForFollowingMessages = signatureForFollowingMessages;
|
||||||
|
});
|
||||||
|
|
||||||
|
isLoaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async void OnPropertyChanged(PropertyChangedEventArgs e)
|
protected override async void OnPropertyChanged(PropertyChangedEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnPropertyChanged(e);
|
base.OnPropertyChanged(e);
|
||||||
|
|
||||||
|
if (!isLoaded || Account?.Preferences == null) return;
|
||||||
|
|
||||||
switch (e.PropertyName)
|
switch (e.PropertyName)
|
||||||
{
|
{
|
||||||
case nameof(IsSignatureEnabled):
|
case nameof(IsSignatureEnabled):
|
||||||
Account.Preferences.IsSignatureEnabled = IsSignatureEnabled;
|
Account.Preferences.IsSignatureEnabled = IsSignatureEnabled;
|
||||||
await _accountService.UpdateAccountAsync(Account);
|
await _accountService.UpdateAccountAsync(Account);
|
||||||
break;
|
break;
|
||||||
case nameof(SignatureForNewMessagesIndex):
|
case nameof(SelectedSignatureForNewMessages):
|
||||||
Account.Preferences.SignatureIdForNewMessages = SignatureForNewMessagesIndex > -1
|
Account.Preferences.SignatureIdForNewMessages = GetPersistedSignatureId(SelectedSignatureForNewMessages);
|
||||||
&& Signatures[SignatureForNewMessagesIndex].Id != EmptyGuid
|
|
||||||
? Signatures[SignatureForNewMessagesIndex].Id : null;
|
|
||||||
await _accountService.UpdateAccountAsync(Account);
|
await _accountService.UpdateAccountAsync(Account);
|
||||||
break;
|
break;
|
||||||
case nameof(SignatureForFollowingMessagesIndex):
|
case nameof(SelectedSignatureForFollowingMessages):
|
||||||
Account.Preferences.SignatureIdForFollowingMessages = SignatureForFollowingMessagesIndex > -1
|
Account.Preferences.SignatureIdForFollowingMessages = GetPersistedSignatureId(SelectedSignatureForFollowingMessages);
|
||||||
&& Signatures[SignatureForFollowingMessagesIndex].Id != EmptyGuid
|
|
||||||
? Signatures[SignatureForFollowingMessagesIndex].Id : null;
|
|
||||||
await _accountService.UpdateAccountAsync(Account);
|
await _accountService.UpdateAccountAsync(Account);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -121,7 +100,7 @@ public partial class SignatureManagementPageViewModel(IMailDialogService dialogS
|
|||||||
if (dialogResult == null) return;
|
if (dialogResult == null) return;
|
||||||
|
|
||||||
dialogResult.MailAccountId = Account.Id;
|
dialogResult.MailAccountId = Account.Id;
|
||||||
Signatures.Add(dialogResult);
|
await ExecuteUIThread(() => Signatures.Add(dialogResult));
|
||||||
await _signatureService.CreateSignatureAsync(dialogResult);
|
await _signatureService.CreateSignatureAsync(dialogResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,18 +112,23 @@ public partial class SignatureManagementPageViewModel(IMailDialogService dialogS
|
|||||||
if (dialogResult == null) return;
|
if (dialogResult == null) return;
|
||||||
|
|
||||||
var indexOfCurrentSignature = Signatures.IndexOf(signatureModel);
|
var indexOfCurrentSignature = Signatures.IndexOf(signatureModel);
|
||||||
var signatureNewMessagesIndex = SignatureForNewMessagesIndex;
|
if (indexOfCurrentSignature < 0) return;
|
||||||
var signatureFollowingMessagesIndex = SignatureForFollowingMessagesIndex;
|
|
||||||
|
|
||||||
|
var wasSelectedForNewMessages = SelectedSignatureForNewMessages?.Id == signatureModel.Id;
|
||||||
|
var wasSelectedForFollowingMessages = SelectedSignatureForFollowingMessages?.Id == signatureModel.Id;
|
||||||
|
|
||||||
|
dialogResult.MailAccountId = signatureModel.MailAccountId;
|
||||||
|
|
||||||
|
await ExecuteUIThread(() =>
|
||||||
|
{
|
||||||
Signatures[indexOfCurrentSignature] = dialogResult;
|
Signatures[indexOfCurrentSignature] = dialogResult;
|
||||||
|
|
||||||
// Reset selection to point updated signature.
|
if (wasSelectedForNewMessages)
|
||||||
// When Item updated/removed index switches to -1. We save index that was used before and update -1 to it.
|
SelectedSignatureForNewMessages = dialogResult;
|
||||||
if (signatureNewMessagesIndex == indexOfCurrentSignature)
|
|
||||||
SignatureForNewMessagesIndex = indexOfCurrentSignature;
|
|
||||||
|
|
||||||
if (signatureFollowingMessagesIndex == indexOfCurrentSignature)
|
if (wasSelectedForFollowingMessages)
|
||||||
SignatureForFollowingMessagesIndex = indexOfCurrentSignature;
|
SelectedSignatureForFollowingMessages = dialogResult;
|
||||||
|
});
|
||||||
|
|
||||||
await _signatureService.UpdateSignatureAsync(dialogResult);
|
await _signatureService.UpdateSignatureAsync(dialogResult);
|
||||||
}
|
}
|
||||||
@@ -156,7 +140,31 @@ public partial class SignatureManagementPageViewModel(IMailDialogService dialogS
|
|||||||
|
|
||||||
if (!shouldRemove) return;
|
if (!shouldRemove) return;
|
||||||
|
|
||||||
|
var shouldResetNewMessagesSignature = SelectedSignatureForNewMessages?.Id == signatureModel.Id;
|
||||||
|
var shouldResetFollowingMessagesSignature = SelectedSignatureForFollowingMessages?.Id == signatureModel.Id;
|
||||||
|
|
||||||
|
await ExecuteUIThread(() =>
|
||||||
|
{
|
||||||
Signatures.Remove(signatureModel);
|
Signatures.Remove(signatureModel);
|
||||||
|
|
||||||
|
var noneSignature = GetNoneSignature();
|
||||||
|
|
||||||
|
if (shouldResetNewMessagesSignature)
|
||||||
|
SelectedSignatureForNewMessages = noneSignature;
|
||||||
|
|
||||||
|
if (shouldResetFollowingMessagesSignature)
|
||||||
|
SelectedSignatureForFollowingMessages = noneSignature;
|
||||||
|
});
|
||||||
|
|
||||||
await _signatureService.DeleteSignatureAsync(signatureModel);
|
await _signatureService.DeleteSignatureAsync(signatureModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
using Wino.Core.Domain;
|
using Wino.Core.Domain;
|
||||||
using Wino.Core.Domain.Entities.Mail;
|
using Wino.Core.Domain.Entities.Mail;
|
||||||
@@ -24,9 +23,6 @@ public sealed partial class SignatureEditorDialog : ContentDialog
|
|||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
SignatureNameTextBox.Text = signatureModel.Name.Trim();
|
|
||||||
SignatureNameTextBox.Header = string.Format(Translator.SignatureEditorDialog_SignatureName_TitleEdit, signatureModel.Name);
|
|
||||||
|
|
||||||
Result = new AccountSignature
|
Result = new AccountSignature
|
||||||
{
|
{
|
||||||
Id = signatureModel.Id,
|
Id = signatureModel.Id,
|
||||||
@@ -35,6 +31,9 @@ public sealed partial class SignatureEditorDialog : ContentDialog
|
|||||||
HtmlBody = signatureModel.HtmlBody
|
HtmlBody = signatureModel.HtmlBody
|
||||||
};
|
};
|
||||||
|
|
||||||
|
SignatureNameTextBox.Text = Result.Name.Trim();
|
||||||
|
SignatureNameTextBox.Header = string.Format(Translator.SignatureEditorDialog_SignatureName_TitleEdit, Result.Name);
|
||||||
|
|
||||||
// TODO: Should be added additional logic to enable/disable primary button when webview content changed.
|
// TODO: Should be added additional logic to enable/disable primary button when webview content changed.
|
||||||
IsPrimaryButtonEnabled = true;
|
IsPrimaryButtonEnabled = true;
|
||||||
}
|
}
|
||||||
@@ -51,7 +50,7 @@ public sealed partial class SignatureEditorDialog : ContentDialog
|
|||||||
|
|
||||||
private async void SaveClicked(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
private async void SaveClicked(ContentDialog sender, ContentDialogButtonClickEventArgs args)
|
||||||
{
|
{
|
||||||
var newSignature = Regex.Unescape((await WebViewEditor.GetHtmlBodyAsync())!);
|
var newSignature = await WebViewEditor.GetHtmlBodyAsync() ?? string.Empty;
|
||||||
|
|
||||||
if (Result == null)
|
if (Result == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -77,7 +77,7 @@
|
|||||||
IsActionIconVisible="False"
|
IsActionIconVisible="False"
|
||||||
IsClickEnabled="False"
|
IsClickEnabled="False"
|
||||||
IsEnabled="{x:Bind ViewModel.IsSignatureEnabled, Mode=OneWay}">
|
IsEnabled="{x:Bind ViewModel.IsSignatureEnabled, Mode=OneWay}">
|
||||||
<ComboBox ItemsSource="{x:Bind ViewModel.Signatures}" SelectedIndex="{x:Bind ViewModel.SignatureForNewMessagesIndex, Mode=TwoWay}">
|
<ComboBox ItemsSource="{x:Bind ViewModel.Signatures}" SelectedItem="{x:Bind ViewModel.SelectedSignatureForNewMessages, Mode=TwoWay}">
|
||||||
<ComboBox.ItemTemplate>
|
<ComboBox.ItemTemplate>
|
||||||
<DataTemplate x:DataType="entities:AccountSignature">
|
<DataTemplate x:DataType="entities:AccountSignature">
|
||||||
<TextBlock Text="{x:Bind Name}" />
|
<TextBlock Text="{x:Bind Name}" />
|
||||||
@@ -91,7 +91,7 @@
|
|||||||
IsActionIconVisible="False"
|
IsActionIconVisible="False"
|
||||||
IsClickEnabled="False"
|
IsClickEnabled="False"
|
||||||
IsEnabled="{x:Bind ViewModel.IsSignatureEnabled, Mode=OneWay}">
|
IsEnabled="{x:Bind ViewModel.IsSignatureEnabled, Mode=OneWay}">
|
||||||
<ComboBox ItemsSource="{x:Bind ViewModel.Signatures}" SelectedIndex="{x:Bind ViewModel.SignatureForFollowingMessagesIndex, Mode=TwoWay}">
|
<ComboBox ItemsSource="{x:Bind ViewModel.Signatures}" SelectedItem="{x:Bind ViewModel.SelectedSignatureForFollowingMessages, Mode=TwoWay}">
|
||||||
<ComboBox.ItemTemplate>
|
<ComboBox.ItemTemplate>
|
||||||
<DataTemplate x:DataType="entities:AccountSignature">
|
<DataTemplate x:DataType="entities:AccountSignature">
|
||||||
<TextBlock Text="{x:Bind Name}" />
|
<TextBlock Text="{x:Bind Name}" />
|
||||||
|
|||||||
Reference in New Issue
Block a user