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

110 lines
3.4 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;
2020-04-16 15:16:49 +00:00
using Wabbajack.Common;
using Wabbajack.Lib;
namespace Wabbajack
{
public class ModlistSettingsEditorVM : ViewModel
{
2020-04-15 17:40:41 +00:00
private readonly CompilationModlistSettings _settings;
[Reactive]
public string ModListName { get; set; }
2020-04-16 15:16:49 +00:00
[Reactive]
public string VersionText { get; set; }
2020-04-27 09:58:33 +00:00
private readonly ObservableAsPropertyHelper<Version> _version;
public Version Version => _version.Value;
[Reactive]
public string AuthorText { get; set; }
[Reactive]
public string Description { get; set; }
public FilePickerVM ImagePath { get; }
2019-12-20 07:14:43 +00:00
[Reactive]
2020-04-15 17:40:41 +00:00
public string Readme { get; set; }
2021-07-20 05:08:12 +00:00
[Reactive] public string MachineUrl { get; set; } = "";
[Reactive] public bool Publish { get; set; } = false;
[Reactive]
public string Website { get; set; }
2020-04-27 09:58:33 +00:00
[Reactive]
public bool IsNSFW { get; set; }
public IObservable<bool> InError { get; }
public ModlistSettingsEditorVM(CompilationModlistSettings settings)
{
2020-04-15 17:40:41 +00:00
_settings = settings;
2020-04-16 15:16:49 +00:00
ImagePath = new FilePickerVM
{
ExistCheckOption = FilePickerVM.CheckOptions.IfPathNotEmpty,
PathType = FilePickerVM.PathTypeOptions.File,
};
ImagePath.Filters.Add(new CommonFileDialogFilter("Banner image", "*.png"));
_version = this.WhenAny(x => x.VersionText)
.Select(x =>
{
if (string.IsNullOrWhiteSpace(x))
return new Version(0, 0);
return !Version.TryParse(x, out var version) ? new Version(0, 0) : version;
}).ObserveOnGuiThread()
.ToProperty(this, x => x.Version);
2020-04-15 17:40:41 +00:00
InError = this.WhenAny(x => x.ImagePath.ErrorState)
.Select(err => err.Failed)
2020-04-16 15:16:49 +00:00
.CombineLatest(
this.WhenAny(x => x.VersionText)
.Select(x => Version.TryParse(x, out _)),
2020-04-16 15:16:49 +00:00
(image, version) => !image && !version)
.Publish()
.RefCount();
}
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;
2020-04-15 17:40:41 +00:00
Readme = _settings.Readme;
2019-11-21 15:45:00 +00:00
ImagePath.TargetPath = _settings.SplashScreen;
Website = _settings.Website;
2020-04-16 15:16:49 +00:00
VersionText = _settings.Version;
2020-04-27 09:58:33 +00:00
IsNSFW = _settings.IsNSFW;
2021-07-20 05:08:12 +00:00
MachineUrl = _settings.MachineUrl;
Publish = _settings.Publish;
}
public void Save()
{
2020-04-16 15:16:49 +00:00
_settings.Version = VersionText;
2019-11-21 15:45:00 +00:00
_settings.Author = AuthorText;
_settings.ModListName = ModListName;
_settings.Description = Description;
2020-04-15 17:40:41 +00:00
_settings.Readme = Readme;
2019-11-21 15:45:00 +00:00
_settings.SplashScreen = ImagePath.TargetPath;
_settings.Website = Website;
2020-04-27 09:58:33 +00:00
_settings.IsNSFW = IsNSFW;
2021-07-20 05:08:12 +00:00
_settings.MachineUrl = MachineUrl;
_settings.Publish = Publish;
}
}
}