1
0

68 lines
1.9 KiB
C#

using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Flawless.Client.Remote;
using Flawless.Client.Service;
using ReactiveUI;
using ReactiveUI.SourceGenerators;
using Refit;
namespace Flawless.Client.ViewModels;
public partial class LoginPageViewModel : ViewModelBase, IRoutableViewModel
{
public string? UrlPathSegment { get; } = Guid.NewGuid().ToString();
public IScreen HostScreen { get; }
[Reactive] private string _username = "cardidi";
[Reactive] private string _password = "4453A2b33";
public IObservable<bool> CanLogin;
public IObservable<bool> CanRegister => Api.C.Status.Select(s => s != null && s.AllowPublicRegister);
public LoginPageViewModel(IScreen hostScreen)
{
HostScreen = hostScreen;
CanLogin = this.WhenAnyValue(
x => x.Username,
x => x.Password,
(user, pass) => !string.IsNullOrEmpty(user) && user.Length > 3 && !string.IsNullOrEmpty(pass) && pass.Length >= 6
);
}
[ReactiveCommand(CanExecute = nameof(CanRegister))]
private void Register()
{
HostScreen.Router.Navigate.Execute(new RegisterPageViewModel(HostScreen));
}
[ReactiveCommand(CanExecute = nameof(CanLogin))]
private async Task LoginAsync()
{
try
{
await Api.C.LoginAsync(Username, Password);
await RepositoryService.C.UpdateRepositoriesFromServerAsync();
}
catch (ApiException ex)
{
await Console.Error.WriteLineAsync($"Login as '{Username}' Failed: {ex.Content}");
UIHelper.NotifyError(ex);
}
catch (Exception ex)
{
await Console.Error.WriteLineAsync($"Login as '{Username}' Failed: {ex}");
UIHelper.NotifyError(ex);
}
Console.WriteLine($"Login as '{Username}' success!");
}
}