From 11f726ad45822836de9556056bd5b8eeccb43754 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 30 Nov 2016 16:54:15 +0900 Subject: [PATCH] Add basic login flow. --- osu.Game/Online/API/APIAccess.cs | 13 ++++ .../Overlays/Options/General/LoginOptions.cs | 73 ++++++++++++++++--- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index 578386c4f8..0f16d103e2 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Diagnostics; using System.Net; using System.Threading; using osu.Framework; @@ -71,6 +72,7 @@ namespace osu.Game.Online.API public void Register(IOnlineComponent component) { components.Add(component); + component.APIStateChanged(this, state); } public void Unregister(IOnlineComponent component) @@ -158,6 +160,16 @@ namespace osu.Game.Online.API password = null; } + public void Login(string username, string password) + { + Debug.Assert(State == APIState.Offline); + + Username = username; + Password = password; + + State = APIState.Connecting; + } + /// /// Handle a single API request. /// @@ -167,6 +179,7 @@ namespace osu.Game.Online.API { try { + Logger.Log($@"Performing request {req}", LoggingTarget.Network); req.Perform(this); State = APIState.Online; diff --git a/osu.Game/Overlays/Options/General/LoginOptions.cs b/osu.Game/Overlays/Options/General/LoginOptions.cs index 2dc8bb5976..c978a8c7ba 100644 --- a/osu.Game/Overlays/Options/General/LoginOptions.cs +++ b/osu.Game/Overlays/Options/General/LoginOptions.cs @@ -1,4 +1,5 @@ -using OpenTK; +using System; +using OpenTK; using osu.Framework; using osu.Framework.Allocation; using osu.Framework.Graphics; @@ -10,7 +11,7 @@ using osu.Game.Online.API; namespace osu.Game.Overlays.Options.General { - public class LoginOptions : OptionsSubsection + public class LoginOptions : OptionsSubsection, IOnlineComponent { private Container loginForm; protected override string Header => "Sign In"; @@ -31,16 +32,56 @@ namespace osu.Game.Overlays.Options.General [BackgroundDependencyLoader(permitNulls: true)] private void load(APIAccess api) { - if (api == null) - return; - loginForm.Children = new Drawable[] - { - new LoginForm(api) - }; + api?.Register(this); } + public void APIStateChanged(APIAccess api, APIState state) + { + switch (state) + { + case APIState.Offline: + loginForm.Children = new Drawable[] + { + new LoginForm(api) + }; + break; + case APIState.Failing: + loginForm.Children = new Drawable[] + { + new SpriteText + { + Text = @"Connection failing :(", + }, + }; + break; + case APIState.Connecting: + loginForm.Children = new Drawable[] + { + new SpriteText + { + Text = @"Connecting...", + }, + }; + break; + case APIState.Online: + loginForm.Children = new Drawable[] + { + new SpriteText + { + Text = $@"Connected as {api.Username}!", + }, + }; + break; + } + } + class LoginForm : FlowContainer { + private APIAccess api; + + private TextBox username; + private TextBox password; + public LoginForm(APIAccess api) { Direction = FlowDirection.VerticalOnly; @@ -51,16 +92,28 @@ namespace osu.Game.Overlays.Options.General Children = new Drawable[] { new SpriteText { Text = "Username" }, - new TextBox { Height = 20, RelativeSizeAxes = Axes.X, Text = api?.Username ?? string.Empty }, + username = new TextBox { Height = 20, RelativeSizeAxes = Axes.X, Text = api?.Username ?? string.Empty }, new SpriteText { Text = "Password" }, - new TextBox { Height = 20, RelativeSizeAxes = Axes.X }, + password = new PasswordTextBox { Height = 20, RelativeSizeAxes = Axes.X }, new OsuButton { RelativeSizeAxes = Axes.X, Text = "Log in", + Action = performLogin } }; } + + private void performLogin() + { + api.Login(username.Text, password.Text); + } + + [BackgroundDependencyLoader(permitNulls: true)] + private void load(APIAccess api) + { + this.api = api; + } } } }