Files @ ccf67c142d65
Branch filter:

Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/ViewModelBase.cs

Jason Maltzen
Update assembly to version 1.8.0
using System;
using System.Diagnostics;
using System.Reactive.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Avalonia.Platform;
using DesertPaintCodex.Views;
using ReactiveUI;

namespace DesertPaintCodex.ViewModels
{
    public class ViewModelBase : ReactiveObject
    {
        public ViewModelBase()
        {
            ShowMessageBoxDialog = new Interaction<MessageBoxViewModel, int>();
        }

        public void OpenBrowser(string url)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                // If no associated application/json MimeType is found xdg-open opens retun error
                // but it tries to open it anyway using the console editor (nano, vim, other..)
                ShellExec($"xdg-open {url}", waitForExit: false);
            }
            else
            {
                using (Process.Start(new ProcessStartInfo
                {
                    FileName        = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? url : "open",
                    Arguments       = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"-e {url}" : "",
                    CreateNoWindow  = true,
                    UseShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                })){}
            }
        }

        private static void ShellExec(string cmd, bool waitForExit = true)
        {
            var escapedArgs = cmd.Replace("\"", "\\\"");

            using var process = Process.Start(
                new ProcessStartInfo
                {
                    FileName               = "/bin/sh",
                    Arguments              = $"-c \"{escapedArgs}\"",
                    RedirectStandardOutput = true,
                    UseShellExecute        = false,
                    CreateNoWindow         = true,
                    WindowStyle            = ProcessWindowStyle.Hidden
                }
            );
            if (waitForExit)
            {
                process?.WaitForExit();
            }
        }
        
        public async Task<int> ShowMessageBox(string title, string message, string? optionA = null, string? optionB = null, string? optionC = null)
        {
            if (string.IsNullOrEmpty(optionA))
            {
                optionA = "Ok";
            }

            return await ShowMessageBoxDialog.Handle(new MessageBoxViewModel(title, message, optionA, optionB, optionC));
        }

        public async Task<bool> ShowYesNoBox(string title, string message)
        {
            int rawVal = await ShowMessageBox(title, message, "No", "Yes");
            return rawVal == 1;
        }
        
        public async Task<bool> ShowOkCancelBox(string title, string message)
        {
            int rawVal = await ShowMessageBox(title, message, "Cancel", "Ok");
            return rawVal == 1;
        }

        public async Task ShowInfoBox(string title, string message)
        {
            await ShowMessageBox(title, message, "Close");
        }
        
        public Interaction<MessageBoxViewModel, int> ShowMessageBoxDialog { get; }
    }
}