From f358fce9abe950fa0f60e69bea2776196ef64ccc Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 12 Jun 2019 18:04:57 +0900 Subject: [PATCH] Move activity (writable) bindable to APIAccess so it correctly transfers between users --- .../Visual/Online/TestSceneUserPanel.cs | 17 +++++++++++------ osu.Game/Online/API/APIAccess.cs | 8 ++++++++ osu.Game/Online/API/DummyAPIAccess.cs | 11 +++++++++++ osu.Game/Online/API/IAPIProvider.cs | 5 +++++ .../Settings/Sections/General/LoginSettings.cs | 6 ++---- osu.Game/Screens/OsuScreen.cs | 2 +- osu.Game/Users/User.cs | 2 +- osu.Game/Users/UserPanel.cs | 2 +- 8 files changed, 40 insertions(+), 13 deletions(-) diff --git a/osu.Game.Tests/Visual/Online/TestSceneUserPanel.cs b/osu.Game.Tests/Visual/Online/TestSceneUserPanel.cs index 30814ad9c7..54f06d6ad2 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneUserPanel.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneUserPanel.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using NUnit.Framework; +using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Users; @@ -61,12 +62,16 @@ namespace osu.Game.Tests.Visual.Online [Test] public void UserActivitiesTests() { - AddStep("idle", () => { peppy.Activity.Value = null; }); - AddStep("spectating", () => { peppy.Activity.Value = new UserActivity.Spectating(); }); - AddStep("solo", () => { peppy.Activity.Value = new UserActivity.SoloGame(null, null); }); - AddStep("choosing", () => { peppy.Activity.Value = new UserActivity.ChoosingBeatmap(); }); - AddStep("editing", () => { peppy.Activity.Value = new UserActivity.Editing(null); }); - AddStep("modding", () => { peppy.Activity.Value = new UserActivity.Modding(); }); + Bindable activity = new Bindable(); + + peppy.Activity.BindTo(activity); + + AddStep("idle", () => { activity.Value = null; }); + AddStep("spectating", () => { activity.Value = new UserActivity.Spectating(); }); + AddStep("solo", () => { activity.Value = new UserActivity.SoloGame(null, null); }); + AddStep("choosing", () => { activity.Value = new UserActivity.ChoosingBeatmap(); }); + AddStep("editing", () => { activity.Value = new UserActivity.Editing(null); }); + AddStep("modding", () => { activity.Value = new UserActivity.Modding(); }); } } } diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index 343d6a67b7..12b38fab1e 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -37,6 +37,8 @@ namespace osu.Game.Online.API public Bindable LocalUser { get; } = new Bindable(createGuestUser()); + public Bindable Activity { get; } = new Bindable(); + protected bool HasLogin => authentication.Token.Value != null || (!string.IsNullOrEmpty(ProvidedUsername) && !string.IsNullOrEmpty(password)); private readonly CancellationTokenSource cancellationToken = new CancellationTokenSource(); @@ -55,6 +57,12 @@ namespace osu.Game.Online.API authentication.TokenString = config.Get(OsuSetting.Token); authentication.Token.ValueChanged += onTokenChanged; + LocalUser.BindValueChanged(u => + { + u.OldValue?.Activity.UnbindFrom(Activity); + u.NewValue.Activity.BindTo(Activity); + }, true); + var thread = new Thread(run) { Name = "APIAccess", diff --git a/osu.Game/Online/API/DummyAPIAccess.cs b/osu.Game/Online/API/DummyAPIAccess.cs index 99fde10309..6c04c77dc0 100644 --- a/osu.Game/Online/API/DummyAPIAccess.cs +++ b/osu.Game/Online/API/DummyAPIAccess.cs @@ -17,6 +17,8 @@ namespace osu.Game.Online.API Id = 1001, }); + public Bindable Activity { get; } = new Bindable(); + public bool IsLoggedIn => true; public string ProvidedUsername => LocalUser.Value.Username; @@ -41,6 +43,15 @@ namespace osu.Game.Online.API } } + public DummyAPIAccess() + { + LocalUser.BindValueChanged(u => + { + u.OldValue?.Activity.UnbindFrom(Activity); + u.NewValue.Activity.BindTo(Activity); + }, true); + } + public virtual void Queue(APIRequest request) { } diff --git a/osu.Game/Online/API/IAPIProvider.cs b/osu.Game/Online/API/IAPIProvider.cs index 7c1f850943..0cd41aee26 100644 --- a/osu.Game/Online/API/IAPIProvider.cs +++ b/osu.Game/Online/API/IAPIProvider.cs @@ -13,6 +13,11 @@ namespace osu.Game.Online.API /// Bindable LocalUser { get; } + /// + /// The current user's activity. + /// + Bindable Activity { get; } + /// /// Returns whether the local user is logged in. /// diff --git a/osu.Game/Overlays/Settings/Sections/General/LoginSettings.cs b/osu.Game/Overlays/Settings/Sections/General/LoginSettings.cs index 223c2aaf13..1454b6592d 100644 --- a/osu.Game/Overlays/Settings/Sections/General/LoginSettings.cs +++ b/osu.Game/Overlays/Settings/Sections/General/LoginSettings.cs @@ -156,7 +156,7 @@ namespace osu.Game.Overlays.Settings.Sections.General panel.Status.BindTo(api.LocalUser.Value.Status); panel.Activity.BindTo(api.LocalUser.Value.Activity); - dropdown.Current.ValueChanged += action => + dropdown.Current.BindValueChanged(action => { switch (action.NewValue) { @@ -179,9 +179,7 @@ namespace osu.Game.Overlays.Settings.Sections.General api.Logout(); break; } - }; - dropdown.Current.TriggerChange(); - + }, true); break; } diff --git a/osu.Game/Screens/OsuScreen.cs b/osu.Game/Screens/OsuScreen.cs index fe53ad17c3..e2aeb41de1 100644 --- a/osu.Game/Screens/OsuScreen.cs +++ b/osu.Game/Screens/OsuScreen.cs @@ -193,7 +193,7 @@ namespace osu.Game.Screens private void updateActivity() { if (api != null) - api.LocalUser.Value.Activity.Value = activity; + api.Activity.Value = activity; } /// diff --git a/osu.Game/Users/User.cs b/osu.Game/Users/User.cs index cf67af7bb8..c3ecd62e10 100644 --- a/osu.Game/Users/User.cs +++ b/osu.Game/Users/User.cs @@ -25,7 +25,7 @@ namespace osu.Game.Users public Bindable Status = new Bindable(); - public Bindable Activity = new Bindable(); + public IBindable Activity = new Bindable(); //public Team Team; diff --git a/osu.Game/Users/UserPanel.cs b/osu.Game/Users/UserPanel.cs index 04f5a0a3fb..833c62013b 100644 --- a/osu.Game/Users/UserPanel.cs +++ b/osu.Game/Users/UserPanel.cs @@ -42,7 +42,7 @@ namespace osu.Game.Users public readonly Bindable Status = new Bindable(); - public readonly Bindable Activity = new Bindable(); + public readonly IBindable Activity = new Bindable(); public new Action Action;