1
0

52 lines
1.4 KiB
C#

using System;
using System.Reactive;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Flawless.Client.Service;
using ReactiveUI;
using ReactiveUI.SourceGenerators;
using Refit;
namespace Flawless.Client.ViewModels;
public partial class ServerSetupPageViewModel : RoutableViewModelBase
{
[Reactive] private string _host = "http://localhost:5256/";
[Reactive(SetModifier = AccessModifier.Protected)] private string? _issue;
public IObservable<bool> CanSetHost { get; }
public ServerSetupPageViewModel(IScreen hostScreen) : base(hostScreen)
{
// Must clear this gateway
if (Api.C.IsGatewayReady) Api.C.ClearGateway();
CanSetHost = this.WhenAnyValue(x => x.Host, s => !string.IsNullOrWhiteSpace(s));
}
[ReactiveCommand]
private async Task SetHostAsync()
{
try
{
Issue = string.Empty;
await Api.C.SetGatewayAsync(Host);
HostScreen.Router.Navigate.Execute(new LoginPageViewModel(HostScreen));
}
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;
}
}
}