Files
DiscordChatExporter/DiscordChatExporter.Gui/ViewModels/MainViewModel.cs
Copilot 12d98e9ab0 Add localization (#1482)
Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-02-24 20:49:32 +02:00

125 lines
3.8 KiB
C#

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Avalonia;
using CommunityToolkit.Mvvm.Input;
using DiscordChatExporter.Gui.Framework;
using DiscordChatExporter.Gui.Localization;
using DiscordChatExporter.Gui.Services;
using DiscordChatExporter.Gui.Utils.Extensions;
using DiscordChatExporter.Gui.ViewModels.Components;
namespace DiscordChatExporter.Gui.ViewModels;
public partial class MainViewModel(
ViewModelManager viewModelManager,
DialogManager dialogManager,
SnackbarManager snackbarManager,
SettingsService settingsService,
UpdateService updateService,
LocalizationManager localizationManager
) : ViewModelBase
{
public string Title { get; } = $"{Program.Name} v{Program.VersionString}";
public DashboardViewModel Dashboard { get; } = viewModelManager.CreateDashboardViewModel();
private async Task ShowUkraineSupportMessageAsync()
{
if (!settingsService.IsUkraineSupportMessageEnabled)
return;
var dialog = viewModelManager.CreateMessageBoxViewModel(
localizationManager.UkraineSupportTitle,
localizationManager.UkraineSupportMessage,
localizationManager.LearnMoreButton,
localizationManager.CloseButton
);
// Disable this message in the future
settingsService.IsUkraineSupportMessageEnabled = false;
settingsService.Save();
if (await dialogManager.ShowDialogAsync(dialog) == true)
Process.StartShellExecute("https://tyrrrz.me/ukraine?source=discordchatexporter");
}
private async Task ShowDevelopmentBuildMessageAsync()
{
if (!Program.IsDevelopmentBuild)
return;
// If debugging, the user is likely a developer
if (Debugger.IsAttached)
return;
var dialog = viewModelManager.CreateMessageBoxViewModel(
localizationManager.UnstableBuildTitle,
string.Format(localizationManager.UnstableBuildMessage, Program.Name),
localizationManager.SeeReleasesButton,
localizationManager.CloseButton
);
if (await dialogManager.ShowDialogAsync(dialog) == true)
Process.StartShellExecute(Program.ProjectReleasesUrl);
}
private async Task CheckForUpdatesAsync()
{
try
{
var updateVersion = await updateService.CheckForUpdatesAsync();
if (updateVersion is null)
return;
snackbarManager.Notify(
string.Format(
localizationManager.UpdateDownloadingMessage,
Program.Name,
updateVersion
)
);
await updateService.PrepareUpdateAsync(updateVersion);
snackbarManager.Notify(
localizationManager.UpdateReadyMessage,
localizationManager.UpdateInstallNowButton,
() =>
{
updateService.FinalizeUpdate(true);
if (Application.Current?.ApplicationLifetime?.TryShutdown(2) != true)
Environment.Exit(2);
}
);
}
catch
{
// Failure to update shouldn't crash the application
snackbarManager.Notify(localizationManager.UpdateFailedMessage);
}
}
[RelayCommand]
private async Task InitializeAsync()
{
await ShowUkraineSupportMessageAsync();
await ShowDevelopmentBuildMessageAsync();
await CheckForUpdatesAsync();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
// Save settings
settingsService.Save();
// Finalize pending updates
updateService.FinalizeUpdate(false);
}
base.Dispose(disposing);
}
}