diff --git a/Wabbajack/View Models/Settings/CredentialsLoginVM.cs b/Wabbajack/View Models/Settings/CredentialsLoginVM.cs new file mode 100644 index 00000000..dca0436a --- /dev/null +++ b/Wabbajack/View Models/Settings/CredentialsLoginVM.cs @@ -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 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))); + } + } +} diff --git a/Wabbajack/View Models/Settings/LoginManagerVM.cs b/Wabbajack/View Models/Settings/LoginManagerVM.cs index 1487adf7..e7ad9745 100644 --- a/Wabbajack/View Models/Settings/LoginManagerVM.cs +++ b/Wabbajack/View Models/Settings/LoginManagerVM.cs @@ -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 _MetaInfo; - public string MetaInfo => _MetaInfo.Value; + private readonly ObservableAsPropertyHelper _metaInfo; + public string MetaInfo => _metaInfo.Value; public INeedsLogin Login { get; } + public INeedsLoginCredentials LoginWithCredentials { get; } + public bool UsesCredentials { get; } + + public ReactiveCommand 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()); } } } diff --git a/Wabbajack/Views/Settings/CredentialsLoginView.xaml b/Wabbajack/Views/Settings/CredentialsLoginView.xaml new file mode 100644 index 00000000..89539bed --- /dev/null +++ b/Wabbajack/Views/Settings/CredentialsLoginView.xaml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/Wabbajack/Views/Settings/CredentialsLoginView.xaml.cs b/Wabbajack/Views/Settings/CredentialsLoginView.xaml.cs new file mode 100644 index 00000000..36a2ac1e --- /dev/null +++ b/Wabbajack/Views/Settings/CredentialsLoginView.xaml.cs @@ -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); + }); + } + } +} diff --git a/Wabbajack/Views/Settings/LoginItemView.xaml.cs b/Wabbajack/Views/Settings/LoginItemView.xaml.cs index fdcc7aaa..49102a7f 100644 --- a/Wabbajack/Views/Settings/LoginItemView.xaml.cs +++ b/Wabbajack/Views/Settings/LoginItemView.xaml.cs @@ -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 { - /// - /// Interaction logic for LoginItemView.xaml - /// - public partial class LoginItemView : ReactiveUserControl + 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"; }); }); } diff --git a/Wabbajack/Views/Settings/LoginWindowView.xaml b/Wabbajack/Views/Settings/LoginWindowView.xaml new file mode 100644 index 00000000..857c7be4 --- /dev/null +++ b/Wabbajack/Views/Settings/LoginWindowView.xaml @@ -0,0 +1,16 @@ + + + + + diff --git a/Wabbajack/Views/Settings/LoginWindowView.xaml.cs b/Wabbajack/Views/Settings/LoginWindowView.xaml.cs new file mode 100644 index 00000000..04ad66f7 --- /dev/null +++ b/Wabbajack/Views/Settings/LoginWindowView.xaml.cs @@ -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); + } + } +}