Files
@ 35683a714f6b
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/MainWindowViewModel.cs
35683a714f6b
6.3 KiB
text/x-csharp
Added tag R_T10_1.0 for changeset f419334a476f
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input.Platform;
using DesertPaintCodex.Models;
using DesertPaintCodex.Services;
using ReactiveUI;
namespace DesertPaintCodex.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private string _statusText = string.Empty;
public string StatusText { get => _statusText; private set => this.RaiseAndSetIfChanged(ref _statusText, value); }
private static readonly List<string> ZipFileExtensions = new() { $"*.zip;" };
private static readonly FileDialogFilter ZipDialogFilter = new() {Extensions = ZipFileExtensions};
private static readonly List<FileDialogFilter> ZipDialogFilters = new() { ZipDialogFilter };
private static readonly List<FileDialogFilter> NoFilters = new();
public MainWindowViewModel()
{
if (!ProfileManager.HasProfileLoaded && ProfileManager.HasProfiles())
{
ProfileManager.LoadProfile(ProfileManager.GetProfileList()[0]);
}
Debug.Assert(ProfileManager.HasProfileLoaded);
PaletteService.Initialize();
ReactionTestService.Initialize();
ShowAboutDialog = new Interaction<AboutViewModel, Unit>();
ShowScreenSettingsDialog = new Interaction<ScreenSettingsViewModel, Unit>();
StatusText = "USER PROFILE: " + ProfileManager.CurrentProfile?.Name;
Exit = ReactiveCommand.Create(() => { });
}
public async void ManageProfiles()
{
if (Application.Current is not App app) return;
if (await ValidateSafeExit())
{
ProfileManager.UnloadProfile();
app.ReturnToWelcome();
}
}
public static async void ImportProfile()
{
string? fileName = await GetLoadFileName("Open Zipped Profile", ZipDialogFilters);
if (!string.IsNullOrEmpty(fileName))
{
ProfileManager.CurrentProfile?.Import(fileName);
}
}
public static async void ExportProfile()
{
string? fileName = await GetSaveFileName("Save Zipped Profile", ZipDialogFilters);
if (!string.IsNullOrEmpty(fileName))
{
ProfileManager.CurrentProfile?.Import(fileName);
}
}
public static async void ExportForPP()
{
string? fileName = await GetSaveFileName("Save Practical Paint File", NoFilters);
if (!string.IsNullOrEmpty(fileName))
{
ProfileManager.CurrentProfile?.SaveToPP(fileName);
}
}
public static async void ExportPaintRecipes()
{
string? fileName = await GetSaveFileName("Export Paint Recipes", NoFilters);
if (!string.IsNullOrEmpty(fileName))
{
ProfileManager.CurrentProfile?.ExportWikiRecipes(fileName);
}
}
public static async void ExportRibbonRecipes()
{
string? fileName = await GetSaveFileName("Export Ribbon Recipes", NoFilters);
if (!string.IsNullOrEmpty(fileName))
{
ProfileManager.CurrentProfile?.ExportWikiRibbons(fileName);
}
}
public static async void CopyPaintRecipes()
{
StringWriter writer = new();
ProfileManager.CurrentProfile?.ExportWikiRecipes(writer);
IClipboard clipboard = Application.Current.Clipboard;
await writer.FlushAsync();
await clipboard.SetTextAsync(writer.ToString());
writer.Close();
}
public static async void CopyRibbonRecipes()
{
StringWriter writer = new();
ProfileManager.CurrentProfile?.ExportWikiRibbons(writer);
IClipboard clipboard = Application.Current.Clipboard;
await writer.FlushAsync();
await clipboard.SetTextAsync(writer.ToString());
writer.Close();
}
public async Task ShowScreenSettings()
{
await ShowScreenSettingsDialog.Handle(new ScreenSettingsViewModel());
}
public async Task ShowAbout()
{
await ShowAboutDialog.Handle(new AboutViewModel());
}
public async Task<bool> ValidateSafeExit()
{
// TODO: Determine if there's unsaved stuff we need to deal with.
// return await ShowYesNoBox("Leaving so Soon?", "[A potential reason not to quit goes here]");
await Task.Delay(1); // Stub to prevent warnings.
return true;
}
private static async Task<string?> GetLoadFileName(string title, List<FileDialogFilter> filters)
{
if (Application.Current.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) return null;
// TODO: Figure out why the file filters aren't working.
OpenFileDialog dialog = new()
{
Title = title,
Filters = NoFilters, // filters,
AllowMultiple = false
};
string[] files = await dialog.ShowAsync(desktop.MainWindow);
return files.Length > 0 ? files[0] : null;
}
private static async Task<string?> GetSaveFileName(string title, List<FileDialogFilter> filters)
{
if (Application.Current.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop) return null;
// TODO: Figure out why the file filters aren't working.
SaveFileDialog dialog = new()
{
Title = title,
Filters = NoFilters, // filters
};
return await dialog.ShowAsync(desktop.MainWindow);
}
public Interaction<AboutViewModel, Unit> ShowAboutDialog { get; }
public Interaction<ScreenSettingsViewModel, Unit> ShowScreenSettingsDialog { get; }
public ReactiveCommand<Unit, Unit> Exit { get; }
}
}
|