using System; using System.Reactive; using System.Text.RegularExpressions; using System.Threading.Tasks; using ReactiveUI; using ReactiveUI.SourceGenerators; using Refit; namespace Flawless.Client.ViewModels; public partial class ServerSetupViewModel : UserControlViewModelBase { [Reactive] private string _host = "http://localhost:5256/"; [Reactive] private string? _issue; private MainWindowViewModel _main; public ReactiveCommand SetHostCommand { get; } private static readonly Regex httpRegex = new Regex( @"^(?i)https?://(([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{1,5})?(/[\w\-\.~%!$&'()*+,;=:@/?#]*)?(?-i)$", RegexOptions.Compiled | RegexOptions.CultureInvariant); public ServerSetupViewModel(MainWindowViewModel main) { _main = main; Title = "Connect to a Flawless Server"; // Must clear this gateway if (ApiHelper.IsGatewayReady) ApiHelper.ClearGateway(); SetHostCommand = ReactiveCommand.CreateFromTask( FetchServerDataAsync, this.WhenAnyValue(x => x.Host, s => !string.IsNullOrWhiteSpace(s))); } private async Task FetchServerDataAsync() { try { Issue = string.Empty; await ApiHelper.SetGatewayAsync(Host); _main.RedirectToLogin(); } catch (ApiException ex) { await Console.Error.WriteLineAsync("Can not connect to server: " + ex.ToString()); Issue = ex.Content; } catch (Exception ex) { await Console.Error.WriteLineAsync("Can not connect to server: " + ex.ToString()); Issue = ex.Message; } } }