Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using ReactiveUI;
- using System;
- using System.Reactive;
- using MeksMathGame.Models;
- using MeksMathGame.Services;
- namespace MeksMathGame.ViewModels;
- public class SelectUserViewModel : ViewModelBase
- {
- public List<string?>? _currentUsers;
- public string? _selectedUsername;
- public string? _newUser;
- public bool? _isValid = true;
- public SelectUserViewModel()
- {
- CurrentUsers = Database.GetUsernames();
- SelectedUsername = CurrentUsers.Count > 0 ? CurrentUsers[0] : "Create New User";
- IObservable<bool> selectedUserOk = this.WhenAnyValue(
- x => x.SelectedUsername,
- x => x != "Select One");
- IObservable<bool> newUserOk = this.WhenAnyValue(
- x => x.NewUser,
- x => !string.IsNullOrEmpty(x));
- SendSelectUser = ReactiveCommand.Create(
- ReturnSelectedUser,
- selectedUserOk);
- SendNewUser = ReactiveCommand.Create(
- ReturnNewUser,
- newUserOk);
- }
- public User ReturnSelectedUser()
- {
- return new User { Username = SelectedUsername };
- }
- public User ReturnNewUser()
- {
- if (CurrentUsers.Contains(NewUser))
- {
- IsValid = false;
- return null;
- }
- User user = new() { Username = NewUser };
- Database.InsertNewUsername(user);
- return user;
- }
- #region Getters/Setters
- public List<string?>? CurrentUsers
- {
- get => _currentUsers;
- set => this.RaiseAndSetIfChanged(ref _currentUsers, value);
- }
- public string? SelectedUsername
- {
- get => _selectedUsername;
- set => this.RaiseAndSetIfChanged(ref _selectedUsername, value);
- }
- public string? NewUser
- {
- get => _newUser;
- set => this.RaiseAndSetIfChanged(ref _newUser, value);
- }
- public bool? IsValid
- {
- get => _isValid;
- set => this.RaiseAndSetIfChanged(ref _isValid, value);
- }
- #endregion
- public ReactiveCommand<Unit, User> SendSelectUser { get; }
- public ReactiveCommand<Unit, User> SendNewUser { get; }
- }
Advertisement
Add Comment
Please, Sign In to add comment