70 lines
2.0 KiB
C#
70 lines
2.0 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";
|
|
|
|
[Reactive(SetModifier = AccessModifier.Protected)] private string _issue = String.Empty;
|
|
|
|
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}");
|
|
Issue = ex.Content ?? String.Empty;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await Console.Error.WriteLineAsync($"Login as '{Username}' Failed: {ex}");
|
|
Issue = ex.Message;
|
|
}
|
|
|
|
Console.WriteLine($"Login as '{Username}' success!");
|
|
}
|
|
|
|
} |