Created Login Window

This commit is contained in:
erri120 2020-04-04 15:54:59 +02:00
parent 21b78931a8
commit cf79e82276
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
7 changed files with 184 additions and 26 deletions

View File

@ -0,0 +1,34 @@
using System.Reactive;
using System.Reactive.Linq;
using System.Windows.Controls;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Lib;
using Wabbajack.Lib.Downloaders;
namespace Wabbajack
{
public class CredentialsLoginVM : ViewModel
{
[Reactive]
public string Username { get; set; }
[Reactive]
public string Password { get; set; }
[Reactive]
public LoginReturnMessage ReturnMessage { get; set; }
public ReactiveCommand<Unit, Unit> LoginCommand;
public CredentialsLoginVM(INeedsLoginCredentials downloader)
{
LoginCommand = ReactiveCommand.Create(() =>
{
ReturnMessage = downloader.LoginWithCredentials(Username, Password);
Password = "";
}, this.WhenAny(x => x.Username).CombineLatest(this.WhenAny(x => x.Password),
(username, password) => !string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(password)));
}
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
@ -27,16 +28,39 @@ namespace Wabbajack
public class LoginTargetVM : ViewModel
{
private readonly ObservableAsPropertyHelper<string> _MetaInfo;
public string MetaInfo => _MetaInfo.Value;
private readonly ObservableAsPropertyHelper<string> _metaInfo;
public string MetaInfo => _metaInfo.Value;
public INeedsLogin Login { get; }
public INeedsLoginCredentials LoginWithCredentials { get; }
public bool UsesCredentials { get; }
public ReactiveCommand<Unit, Unit> TriggerCredentialsLogin;
public LoginTargetVM(INeedsLogin login)
{
Login = login;
_MetaInfo = (login.MetaInfo ?? Observable.Return(""))
if (login is INeedsLoginCredentials loginWithCredentials)
{
UsesCredentials = true;
LoginWithCredentials = loginWithCredentials;
}
_metaInfo = (login.MetaInfo ?? Observable.Return(""))
.ToGuiProperty(this, nameof(MetaInfo));
if (!UsesCredentials)
return;
TriggerCredentialsLogin = ReactiveCommand.Create(() =>
{
if (!(login is INeedsLoginCredentials))
return;
var loginWindow = new LoginWindowView(LoginWithCredentials);
loginWindow.Show();
}, LoginWithCredentials.IsLoggedIn.Select(b => !b).ObserveOnGuiThread());
}
}
}

View File

@ -0,0 +1,34 @@
<rxui:ReactiveUserControl
x:Class="Wabbajack.CredentialsLoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Wabbajack"
xmlns:rxui="http://reactiveui.net"
x:TypeArguments="local:CredentialsLoginVM"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Margin="16">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="60"/>
<RowDefinition Height="40"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="120"/>
</Grid.ColumnDefinitions>
<TextBox FontSize="20" x:Name="Username" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"/>
<TextBox Margin="0 16 0 -8" FontSize="20" x:Name="Password" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"/>
<Button Margin="8 8 0 0" x:Name="LoginButton" Grid.Row="2" Grid.Column="2">
<TextBlock FontSize="14">Login</TextBlock>
</Button>
<TextBox x:Name="Message" FontSize="20" Margin="0 16 0 0" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3"/>
</Grid>
</rxui:ReactiveUserControl>

View File

@ -0,0 +1,35 @@
using System.Reactive.Disposables;
using System.Windows;
using System.Windows.Controls;
using ReactiveUI;
using Wabbajack.Lib.Downloaders;
namespace Wabbajack
{
public partial class CredentialsLoginView
{
public INeedsLoginCredentials Downloader { get; set; }
public CredentialsLoginView(INeedsLoginCredentials downloader)
{
Downloader = downloader;
if (ViewModel == null)
ViewModel = new CredentialsLoginVM(downloader);
InitializeComponent();
this.WhenActivated(disposable =>
{
this.Bind(ViewModel, x => x.Username, x => x.Username.Text)
.DisposeWith(disposable);
this.Bind(ViewModel, x => x.Password, x => x.Password.Text)
.DisposeWith(disposable);
this.OneWayBindStrict(ViewModel, x => x.LoginCommand, x => x.LoginButton.Command)
.DisposeWith(disposable);
this.OneWayBind(ViewModel, x => x.ReturnMessage.Message, x => x.Message.Text)
.DisposeWith(disposable);
});
}
}
}

View File

@ -1,40 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ReactiveUI;
namespace Wabbajack
{
/// <summary>
/// Interaction logic for LoginItemView.xaml
/// </summary>
public partial class LoginItemView : ReactiveUserControl<LoginTargetVM>
public partial class LoginItemView
{
public LoginItemView()
{
InitializeComponent();
this.WhenActivated(disposable =>
{
this.OneWayBindStrict(this.ViewModel, x => x.Login.SiteName, x => x.SiteNameText.Text)
this.OneWayBindStrict(ViewModel, x => x.Login.SiteName, x => x.SiteNameText.Text)
.DisposeWith(disposable);
this.OneWayBindStrict(this.ViewModel, x => x.Login.TriggerLogin, x => x.LoginButton.Command)
if (!ViewModel.UsesCredentials)
{
this.OneWayBindStrict(ViewModel, x => x.Login.TriggerLogin, x => x.LoginButton.Command)
.DisposeWith(disposable);
}
else
{
this.OneWayBindStrict(ViewModel, x => x.TriggerCredentialsLogin, x => x.LoginButton.Command)
.DisposeWith(disposable);
this.OneWayBindStrict(this.ViewModel, x => x.Login.ClearLogin, x => x.LogoutButton.Command)
}
this.OneWayBindStrict(ViewModel, x => x.Login.ClearLogin, x => x.LogoutButton.Command)
.DisposeWith(disposable);
this.OneWayBindStrict(this.ViewModel, x => x.MetaInfo, x => x.MetaText.Text)
this.OneWayBindStrict(ViewModel, x => x.MetaInfo, x => x.MetaText.Text)
.DisposeWith(disposable);
// Modify label state
@ -42,13 +36,14 @@ namespace Wabbajack
.Switch()
.Subscribe(x =>
{
this.LoginButton.Content = x ? "Login" : "Logged In";
LoginButton.Content = x ? "Login" : "Logged In";
});
this.WhenAny(x => x.ViewModel.Login.ClearLogin.CanExecute)
.Switch()
.Subscribe(x =>
{
this.LogoutButton.Content = x ? "Logout" : "Logged Out";
LogoutButton.Content = x ? "Logout" : "Logged Out";
});
});
}

View File

@ -0,0 +1,16 @@
<mah:MetroWindow x:Class="Wabbajack.LoginWindowView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wabbajack"
mc:Ignorable="d"
WindowTitleBrush="{StaticResource MahApps.Brushes.Accent}"
Style="{StaticResource {x:Type Window}}"
d:DataContext="{d:DesignInstance local:LoginWindowView }"
Title="Login" Height="450" Width="800">
<Grid x:Name="Grid">
</Grid>
</mah:MetroWindow>

View File

@ -0,0 +1,20 @@
using Wabbajack.Lib.Downloaders;
namespace Wabbajack
{
public partial class LoginWindowView
{
public INeedsLoginCredentials Downloader { get; set; }
public LoginWindowView(INeedsLoginCredentials downloader)
{
Downloader = downloader;
InitializeComponent();
var loginView = new CredentialsLoginView(downloader);
Grid.Children.Add(loginView);
}
}
}