Browse Source

update validation in settings window

Veloe 2 years ago
parent
commit
c4265cef6f
1 changed files with 46 additions and 13 deletions
  1. 46 13
      VeloeMinecraftLauncher/ViewModels/SettingsWindowViewModel.cs

+ 46 - 13
VeloeMinecraftLauncher/ViewModels/SettingsWindowViewModel.cs

@@ -8,6 +8,8 @@ using Avalonia.Controls;
 using System.Threading.Tasks;
 using System.Collections.ObjectModel;
 using Serilog.Events;
+using System.Linq;
+using ReactiveUI.Validation.Components;
 
 namespace VeloeMinecraftLauncher.ViewModels;
 
@@ -20,15 +22,19 @@ public class SettingsWindowViewModel : ViewModelBase
         var _logger = Settings.logger;
 
         this.ValidationRule(
-        viewModel => viewModel.MaxRam,
-        value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); /*logger.Debug("Validator result: {0} {1}", value, resultb);*/ IsValid = resultb; return resultb; },
-        "Not a number.");
+            viewModel => viewModel.MaxRam,
+            value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); return resultb; },
+            "Not a number.");
+
+        var helper = this.ValidationRule(
+            viewModel => viewModel.JavaPath,
+            value => { string path = Path.Combine(value, "bin/java"); if (OperatingSystem.IsWindows()) path += ".exe"; bool resultb = File.Exists(path); return resultb; },
+            "Can't find java executable.");
 
         this.ValidationRule(
-        viewModel => viewModel.MaxLog,
-        value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); /*logger.Debug("Validator result: {0} {1}", value, resultb);*/ IsValid = resultb; return resultb; },
-        "Not a number.");
-   
+            viewModel => viewModel.MaxLog,
+            value => { UInt32 result; bool resultb = UInt32.TryParse(value, out result); return resultb; },
+            "Not a number.");   
 }
 
     private string _launcherVersion;
@@ -45,7 +51,11 @@ public class SettingsWindowViewModel : ViewModelBase
     public bool UseCustomJava
     {
         get => Settings.useCustomJava;
-        set => this.RaiseAndSetIfChanged(ref Settings.useCustomJava, value);
+        set 
+        {
+            this.RaiseAndSetIfChanged(ref Settings.useCustomJava, value);
+            this.RaisePropertyChanged(nameof(IsValid));
+        }
     }
 
     public bool SetMaxRam
@@ -57,7 +67,11 @@ public class SettingsWindowViewModel : ViewModelBase
     public bool SetMinecraftFolder
     {
         get => Settings.setPath;
-        set => this.RaiseAndSetIfChanged(ref Settings.setPath, value);
+        set 
+        { 
+            this.RaiseAndSetIfChanged(ref Settings.setPath, value); 
+            this.RaisePropertyChanged(nameof(IsValid)); 
+        }
     }
 
     public bool CheckAssets
@@ -69,19 +83,38 @@ public class SettingsWindowViewModel : ViewModelBase
     public string JavaPath
     {
         get => _javaPath;
-        set => this.RaiseAndSetIfChanged(ref _javaPath, value);
+        set 
+        { 
+            this.RaiseAndSetIfChanged(ref _javaPath, value); 
+            this.RaisePropertyChanged(nameof(IsValid)); 
+        }
     }
 
     public string MaxRam
     {
         get => _maxRam;
-        set => this.RaiseAndSetIfChanged(ref _maxRam, value);
+        set 
+        { 
+            this.RaiseAndSetIfChanged(ref _maxRam, value); 
+            this.RaisePropertyChanged(nameof(IsValid)); 
+        }
     }
 
     public bool IsValid
     {
-        get => _isValid;
-        set => this.RaiseAndSetIfChanged(ref _isValid, value);
+        get
+        {
+            _isValid = true;
+            if (!UseCustomJava)
+            {
+                var rules = ValidationContext.Validations.Where(r => !(r as BasePropertyValidation<SettingsWindowViewModel>).Properties.Contains(nameof(JavaPath)));
+                foreach (var rule in rules)
+                    _isValid &= rule.IsValid;  
+            }
+            else
+                 _isValid = ValidationContext.GetIsValid();
+            return _isValid;
+        }
     }
 
     public string MinecraftFolderPath