Files
Wino-Mail/Wino.Mail.ViewModels/AppPreferencesPageViewModel.cs
T

134 lines
4.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Wino.Core.Domain;
using Wino.Core.Domain.Enums;
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 AppPreferencesPageViewModel : MailBaseViewModel
{
2025-02-16 11:54:23 +01:00
public IPreferencesService PreferencesService { get; }
2025-02-22 00:22:00 +01:00
[ObservableProperty]
public partial List<string> SearchModes { get; set; }
2025-02-16 11:54:23 +01:00
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsStartupBehaviorDisabled))]
[NotifyPropertyChangedFor(nameof(IsStartupBehaviorEnabled))]
private StartupBehaviorResult startupBehaviorResult;
private int _emailSyncIntervalMinutes;
public int EmailSyncIntervalMinutes
{
get => _emailSyncIntervalMinutes;
set
{
SetProperty(ref _emailSyncIntervalMinutes, value);
PreferencesService.EmailSyncIntervalMinutes = value;
}
}
2025-02-16 11:54:23 +01:00
public bool IsStartupBehaviorDisabled => !IsStartupBehaviorEnabled;
public bool IsStartupBehaviorEnabled => StartupBehaviorResult == StartupBehaviorResult.Enabled;
2025-02-22 00:22:00 +01:00
private string _selectedDefaultSearchMode;
public string SelectedDefaultSearchMode
{
get => _selectedDefaultSearchMode;
set
{
SetProperty(ref _selectedDefaultSearchMode, value);
PreferencesService.DefaultSearchMode = (SearchMode)SearchModes.IndexOf(value);
}
}
2025-02-16 11:54:23 +01:00
private readonly IMailDialogService _dialogService;
private readonly IStartupBehaviorService _startupBehaviorService;
2025-02-16 11:54:23 +01:00
public AppPreferencesPageViewModel(IMailDialogService dialogService,
IPreferencesService preferencesService,
IStartupBehaviorService startupBehaviorService)
{
_dialogService = dialogService;
PreferencesService = preferencesService;
_startupBehaviorService = startupBehaviorService;
2025-02-16 11:35:43 +01:00
2025-02-22 00:22:00 +01:00
SearchModes =
[
Translator.SettingsAppPreferences_SearchMode_Local,
Translator.SettingsAppPreferences_SearchMode_Online
];
SelectedDefaultSearchMode = SearchModes[(int)PreferencesService.DefaultSearchMode];
EmailSyncIntervalMinutes = PreferencesService.EmailSyncIntervalMinutes;
2025-02-16 11:54:23 +01:00
}
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
[RelayCommand]
private async Task ToggleStartupBehaviorAsync()
{
if (IsStartupBehaviorEnabled)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
await DisableStartupAsync();
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
else
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
await EnableStartupAsync();
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
OnPropertyChanged(nameof(IsStartupBehaviorEnabled));
}
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
private async Task EnableStartupAsync()
{
StartupBehaviorResult = await _startupBehaviorService.ToggleStartupBehavior(true);
NotifyCurrentStartupState();
}
private async Task DisableStartupAsync()
{
StartupBehaviorResult = await _startupBehaviorService.ToggleStartupBehavior(false);
NotifyCurrentStartupState();
}
2025-02-16 11:43:30 +01:00
2025-02-16 11:54:23 +01:00
private void NotifyCurrentStartupState()
{
if (StartupBehaviorResult == StartupBehaviorResult.Enabled)
2025-02-16 11:35:43 +01:00
{
2025-02-16 11:54:23 +01:00
_dialogService.InfoBarMessage(Translator.GeneralTitle_Info, Translator.SettingsAppPreferences_StartupBehavior_Enabled, InfoBarMessageType.Success);
2025-02-16 11:35:43 +01:00
}
2025-02-16 11:54:23 +01:00
else if (StartupBehaviorResult == StartupBehaviorResult.Disabled)
{
_dialogService.InfoBarMessage(Translator.GeneralTitle_Info, Translator.SettingsAppPreferences_StartupBehavior_Disabled, InfoBarMessageType.Warning);
}
else if (StartupBehaviorResult == StartupBehaviorResult.DisabledByPolicy)
{
_dialogService.InfoBarMessage(Translator.GeneralTitle_Info, Translator.SettingsAppPreferences_StartupBehavior_DisabledByPolicy, InfoBarMessageType.Warning);
}
else if (StartupBehaviorResult == StartupBehaviorResult.DisabledByUser)
{
_dialogService.InfoBarMessage(Translator.GeneralTitle_Info, Translator.SettingsAppPreferences_StartupBehavior_DisabledByUser, InfoBarMessageType.Warning);
}
else
{
_dialogService.InfoBarMessage(Translator.GeneralTitle_Error, Translator.SettingsAppPreferences_StartupBehavior_FatalError, InfoBarMessageType.Error);
}
}
2025-02-16 11:54:23 +01:00
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:54:23 +01:00
StartupBehaviorResult = await _startupBehaviorService.GetCurrentStartupBehaviorAsync();
}
}