|
| 1 | +using MsBox.Avalonia; |
| 2 | +using MsBox.Avalonia.Enums; |
| 3 | +using OpenLoco.Common.Logging; |
| 4 | +using OpenLoco.Gui.Models; |
| 5 | +using ReactiveUI; |
| 6 | +using ReactiveUI.Fody.Helpers; |
| 7 | +using System.Reactive; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace OpenLoco.Gui.ViewModels |
| 11 | +{ |
| 12 | + public abstract class BaseLocoFileViewModel : ReactiveObject, ILocoFileViewModel |
| 13 | + { |
| 14 | + protected BaseLocoFileViewModel(FileSystemItem currentFile, ObjectEditorModel model) |
| 15 | + { |
| 16 | + CurrentFile = currentFile; |
| 17 | + Model = model; |
| 18 | + |
| 19 | + ReloadCommand = ReactiveCommand.Create(Load); |
| 20 | + SaveCommand = ReactiveCommand.Create(Save); |
| 21 | + SaveAsCommand = ReactiveCommand.Create(SaveAs); |
| 22 | + DeleteLocalFileCommand = ReactiveCommand.CreateFromTask(DeleteWrapper); |
| 23 | + } |
| 24 | + |
| 25 | + [Reactive] |
| 26 | + public FileSystemItem CurrentFile { get; init; } |
| 27 | + public ObjectEditorModel Model { get; init; } |
| 28 | + |
| 29 | + protected ILogger logger => Model.Logger; |
| 30 | + |
| 31 | + public ReactiveCommand<Unit, Unit> ReloadCommand { get; init; } |
| 32 | + public ReactiveCommand<Unit, Unit> SaveCommand { get; init; } |
| 33 | + public ReactiveCommand<Unit, Unit> SaveAsCommand { get; init; } |
| 34 | + public ReactiveCommand<Unit, Unit> DeleteLocalFileCommand { get; init; } |
| 35 | + |
| 36 | + public abstract void Load(); |
| 37 | + public abstract void Save(); |
| 38 | + public abstract void SaveAs(); |
| 39 | + |
| 40 | + async Task DeleteWrapper() |
| 41 | + { |
| 42 | + var box = MessageBoxManager |
| 43 | + .GetMessageBoxStandard("Confirm Delete", "Are you sure you would like to delete?", ButtonEnum.YesNo); |
| 44 | + var result = await box.ShowAsync(); |
| 45 | + |
| 46 | + if (result == ButtonResult.Yes) |
| 47 | + { |
| 48 | + Delete(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public virtual void Delete() { } |
| 53 | + |
| 54 | + public string ReloadText => CurrentFile.FileLocation == FileLocation.Local ? "Reload" : "Redownload"; |
| 55 | + public string SaveText => CurrentFile.FileLocation == FileLocation.Local ? "Save" : "Download"; |
| 56 | + public string SaveAsText => $"{SaveText} As"; |
| 57 | + public string DeleteLocalFileText => "Delete File"; |
| 58 | + |
| 59 | + public string ReloadIcon => CurrentFile.FileLocation == FileLocation.Local ? "DatabaseRefresh" : "FileSync"; |
| 60 | + public string SaveIcon => CurrentFile.FileLocation == FileLocation.Local ? "ContentSave" : "FileDownload"; |
| 61 | + public string SaveAsIcon => CurrentFile.FileLocation == FileLocation.Local ? "ContentSavePlus" : "FileDownloadOutline"; |
| 62 | + public string DeleteLocalFileIcon => "DeleteForever"; |
| 63 | + } |
| 64 | +} |
0 commit comments