-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLoginViewModel.cs
More file actions
73 lines (62 loc) · 2.25 KB
/
LoginViewModel.cs
File metadata and controls
73 lines (62 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2025 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Reactive.Concurrency;
using System.Reactive.Subjects;
namespace ReactiveUI.Samples.Winforms;
/// <summary>
/// A view model that handles user login with reactive validation and async execution.
/// </summary>
public class LoginViewModel : ReactiveObject
{
private string? _userName;
private string? _password;
/// <summary>
/// Initializes a new instance of the <see cref="LoginViewModel"/> class.
/// </summary>
/// <param name="scheduler">The scheduler to use for command execution.</param>
public LoginViewModel(IScheduler scheduler)
{
var canLogin = this.WhenAnyValue(
vm => vm.UserName,
vm => vm.Password,
(user, pass) => !string.IsNullOrWhiteSpace(user) && !string.IsNullOrWhiteSpace(pass));
var cancelSubject = new Subject<Unit>();
Login = ReactiveCommand.CreateFromObservable(
() => Observable
.Return(Password is "secret")
.Delay(TimeSpan.FromSeconds(1), scheduler)
.TakeUntil(cancelSubject),
canLogin,
scheduler);
Cancel = ReactiveCommand.Create(
() => cancelSubject.OnNext(Unit.Default),
Login.IsExecuting,
scheduler);
}
/// <summary>
/// Gets or sets the user name.
/// </summary>
public string? UserName
{
get => _userName;
set => this.RaiseAndSetIfChanged(ref _userName, value);
}
/// <summary>
/// Gets or sets the password.
/// </summary>
public string? Password
{
get => _password;
set => this.RaiseAndSetIfChanged(ref _password, value);
}
/// <summary>
/// Gets the login command. Returns true on success, false on failure.
/// </summary>
public ReactiveCommand<Unit, bool> Login { get; }
/// <summary>
/// Gets the cancel command. Only available while login is executing.
/// </summary>
public ReactiveCommand<Unit, Unit> Cancel { get; }
}