Files
@ 8ee5f618b07c
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/MainWindowViewModel.cs
8ee5f618b07c
8.8 KiB
text/x-csharp
Merge
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | using System;
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 ZipFileFilter = new() {Extensions = ZipFileExtensions, Name = "Zip"};
private static readonly List<FileDialogFilter> ZipFileFilters = new() {ZipFileFilter};
private static readonly List<string> TxtFileExtensions = new() {"txt"};
private static readonly FileDialogFilter TxtFileFilter = new() {Extensions = TxtFileExtensions, Name = "Text"};
private static readonly List<FileDialogFilter> TxtFileFilters = new() {TxtFileFilter};
private static readonly List<FileDialogFilter> NoFileFilters = 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 async void ImportProfile()
{
string? fileName = await GetLoadFileName("Open Zipped Profile", ZipFileFilters);
if (string.IsNullOrEmpty(fileName)) return;
try
{
ProfileManager.CurrentProfile?.Import(fileName);
}
catch (Exception e)
{
Debug.WriteLine("ImportProfile threw exception " + e);
await ShowMessageBox("Import Failed",
"Your file could not be imported. It must be a zip file containing a Desert Paint Codex profile.", "OK");
}
ProfileManager.ReloadProfile();
if (Application.Current is not App app) return;
app.RefreshMainWindow();
}
public async void ExportProfile()
{
string? fileName = await GetSaveFileName("Save Zipped Profile", ZipFileFilters);
if (string.IsNullOrEmpty(fileName)) return;
try
{
ProfileManager.CurrentProfile?.Export(fileName);
}
catch (Exception e)
{
Debug.WriteLine("ExportProfile threw exception " + e);
await ShowMessageBox("Export Failed",
"Your profile could not be exported. Please ensure that you are providing a valid filename for the zip file that we are creating.", "OK");
}
}
public async void ExportForPP(object f)
{
string? fileName = await GetSaveFileName("Save Practical Paint File", TxtFileFilters, "reactions.txt");
if (string.IsNullOrEmpty(fileName)) return;
try
{
ProfileManager.CurrentProfile?.SaveToPP(fileName);
}
catch (Exception e)
{
Debug.WriteLine("ExportForPP threw exception " + e);
await ShowMessageBox("Export Failed",
"Please ensure that you have provided a valid file path for your reactions file.", "OK");
}
}
public async void ImportFromPP()
{
string? fileName = await GetLoadFileName("Import Reactions File", TxtFileFilters, "reactions.txt");
if (string.IsNullOrEmpty(fileName)) return;
try
{
ProfileManager.CurrentProfile?.ImportFromPP(fileName);
}
catch (Exception e)
{
Debug.WriteLine("ImportFromPP threw exception " + e);
await ShowMessageBox("Import Failed",
"Your file could not be imported. It must be a valid Practical Paint reactions.txt file.", "OK");
}
ProfileManager.ReloadProfile();
if (Application.Current is not App app) return;
app.RefreshMainWindow();
}
public static async void ExportPaintRecipes()
{
string? fileName = await GetSaveFileName("Export Paint Recipes", NoFileFilters);
if (!string.IsNullOrEmpty(fileName))
{
ProfileManager.CurrentProfile?.ExportWikiRecipes(fileName);
}
}
public static async void ExportRibbonRecipes()
{
string? fileName = await GetSaveFileName("Export Ribbon Recipes", NoFileFilters);
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, string? fileName = null)
{
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 = filters,
InitialFileName = fileName,
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, string? fileName = null)
{
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 = filters,
InitialFileName = fileName
};
return await dialog.ShowAsync(desktop.MainWindow);
}
public Interaction<AboutViewModel, Unit> ShowAboutDialog { get; }
public Interaction<ScreenSettingsViewModel, Unit> ShowScreenSettingsDialog { get; }
public ReactiveCommand<Unit, Unit> Exit { get; }
}
}
|