wabbajack/Wabbajack/View Models/Compilers/ModlistSettingsEditorVM.cs

112 lines
3.6 KiB
C#
Raw Normal View History

using System;
using System.Reactive.Linq;
2019-12-20 07:14:43 +00:00
using System.Windows.Input;
using DynamicData;
using Microsoft.WindowsAPICodePack.Dialogs;
2019-12-20 07:14:43 +00:00
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Lib;
namespace Wabbajack
{
public class ModlistSettingsEditorVM : ViewModel
{
2019-11-21 15:45:00 +00:00
private CompilationModlistSettings _settings;
[Reactive]
public string ModListName { get; set; }
[Reactive]
public string AuthorText { get; set; }
[Reactive]
public string Description { get; set; }
public FilePickerVM ImagePath { get; }
2019-12-20 07:14:43 +00:00
public FilePickerVM ReadmeFilePath { get; }
[Reactive]
public string ReadmeWebsite { get; set; }
[Reactive]
public string Website { get; set; }
2019-12-20 07:14:43 +00:00
[Reactive]
public bool ReadmeIsWebsite { get; set; }
public IObservable<bool> InError { get; }
2019-12-20 07:14:43 +00:00
public ICommand SwapToTextReadmeCommand { get; }
public ICommand SwapToWebsiteReadmeCommand { get; }
public ModlistSettingsEditorVM(CompilationModlistSettings settings)
{
2019-11-21 15:45:00 +00:00
this._settings = settings;
2019-11-21 15:04:33 +00:00
ImagePath = new FilePickerVM()
{
ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty,
PathType = FilePickerVM.PathTypeOptions.File,
};
ImagePath.Filters.Add(new CommonFileDialogFilter("Banner image", "*.png"));
2019-12-20 07:14:43 +00:00
ReadmeFilePath = new FilePickerVM()
{
PathType = FilePickerVM.PathTypeOptions.File,
ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty,
};
2019-12-20 07:14:43 +00:00
ReadmeFilePath.Filters.Add(new CommonFileDialogFilter("Text", "*.txt"));
ReadmeFilePath.Filters.Add(new CommonFileDialogFilter("HTML File", "*.html"));
InError = Observable.CombineLatest(
this.WhenAny(x => x.ImagePath.ErrorState).Select(err => err.Failed),
2019-12-20 07:14:43 +00:00
this.WhenAny(x => x.ReadmeFilePath.ErrorState).Select(err => err.Failed),
this.WhenAny(x => x.ReadmeIsWebsite),
2019-12-20 20:12:01 +00:00
resultSelector: (img, readme, isWebsite) => img || (readme && !isWebsite))
.Publish()
.RefCount();
2019-12-20 07:14:43 +00:00
SwapToTextReadmeCommand = ReactiveCommand.Create(() => ReadmeIsWebsite = false);
SwapToWebsiteReadmeCommand = ReactiveCommand.Create(() => ReadmeIsWebsite = true);
}
public void Init()
{
2019-11-21 15:45:00 +00:00
AuthorText = _settings.Author;
if (!string.IsNullOrWhiteSpace(_settings.ModListName))
{
2019-11-21 15:45:00 +00:00
ModListName = _settings.ModListName;
}
2019-11-21 15:45:00 +00:00
Description = _settings.Description;
2019-12-20 07:14:43 +00:00
ReadmeIsWebsite = _settings.ReadmeIsWebsite;
if (ReadmeIsWebsite)
{
ReadmeWebsite = _settings.Readme;
}
else
{
ReadmeFilePath.TargetPath = _settings.Readme;
}
2019-11-21 15:45:00 +00:00
ImagePath.TargetPath = _settings.SplashScreen;
Website = _settings.Website;
}
public void Save()
{
2019-11-21 15:45:00 +00:00
_settings.Author = AuthorText;
_settings.ModListName = ModListName;
_settings.Description = Description;
2019-12-20 07:14:43 +00:00
_settings.ReadmeIsWebsite = ReadmeIsWebsite;
if (ReadmeIsWebsite)
{
_settings.Readme = ReadmeWebsite;
}
else
{
_settings.Readme = ReadmeFilePath.TargetPath;
}
2019-11-21 15:45:00 +00:00
_settings.SplashScreen = ImagePath.TargetPath;
_settings.Website = Website;
}
}
}