From 34e91c8474a53fdc9380359b62ffe17b994a80b5 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Wed, 30 Nov 2016 15:15:07 +0900 Subject: [PATCH] Add the concept of IOnlineComponents, registered tot he API for handling state changes. --- .../Tests/TestCaseChatDisplay.cs | 4 +- osu.Game/Online/API/APIAccess.cs | 60 ++++++++++++------- osu.Game/Online/API/IOnlineComponent.cs | 16 +++++ osu.Game/osu.Game.csproj | 1 + 4 files changed, 56 insertions(+), 25 deletions(-) create mode 100644 osu.Game/Online/API/IOnlineComponent.cs diff --git a/osu.Desktop.VisualTests/Tests/TestCaseChatDisplay.cs b/osu.Desktop.VisualTests/Tests/TestCaseChatDisplay.cs index cb412f8c97..83627a519a 100644 --- a/osu.Desktop.VisualTests/Tests/TestCaseChatDisplay.cs +++ b/osu.Desktop.VisualTests/Tests/TestCaseChatDisplay.cs @@ -45,7 +45,7 @@ namespace osu.Desktop.VisualTests.Tests { base.Reset(); - if (api.State != APIAccess.APIState.Online) + if (api.State != APIState.Online) api.OnStateChange += delegate { initializeChannels(); }; else initializeChannels(); @@ -65,7 +65,7 @@ namespace osu.Desktop.VisualTests.Tests { careChannels = new List(); - if (api.State != APIAccess.APIState.Online) + if (api.State != APIState.Online) return; Add(flow = new FlowContainer diff --git a/osu.Game/Online/API/APIAccess.cs b/osu.Game/Online/API/APIAccess.cs index a7b2239355..578386c4f8 100644 --- a/osu.Game/Online/API/APIAccess.cs +++ b/osu.Game/Online/API/APIAccess.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Net; using System.Threading; using osu.Framework; @@ -65,6 +66,18 @@ namespace osu.Game.Online.API thread.Start(); } + private List components = new List(); + + public void Register(IOnlineComponent component) + { + components.Add(component); + } + + public void Unregister(IOnlineComponent component) + { + components.Remove(component); + } + public string AccessToken => authentication.RequestAccessToken(); /// @@ -221,6 +234,7 @@ namespace osu.Game.Online.API log.Add($@"We just went {newState}!"); Scheduler.Add(delegate { + components.ForEach(c => c.APIStateChanged(this, newState)); OnStateChange?.Invoke(oldState, newState); }); } @@ -237,29 +251,6 @@ namespace osu.Game.Online.API public delegate void StateChangeDelegate(APIState oldState, APIState newState); - public enum APIState - { - /// - /// We cannot login (not enough credentials). - /// - Offline, - - /// - /// We are having connectivity issues. - /// - Failing, - - /// - /// We are in the process of (re-)connecting. - /// - Connecting, - - /// - /// We are online. - /// - Online - } - private void flushQueue(bool failOldRequests = true) { var oldQueue = queue; @@ -286,4 +277,27 @@ namespace osu.Game.Online.API Scheduler.Update(); } } + + public enum APIState + { + /// + /// We cannot login (not enough credentials). + /// + Offline, + + /// + /// We are having connectivity issues. + /// + Failing, + + /// + /// We are in the process of (re-)connecting. + /// + Connecting, + + /// + /// We are online. + /// + Online + } } diff --git a/osu.Game/Online/API/IOnlineComponent.cs b/osu.Game/Online/API/IOnlineComponent.cs new file mode 100644 index 0000000000..0208384921 --- /dev/null +++ b/osu.Game/Online/API/IOnlineComponent.cs @@ -0,0 +1,16 @@ +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace osu.Game.Online.API +{ + public interface IOnlineComponent + { + void APIStateChanged(APIAccess api, APIState state); + } +} diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index f4d0749080..2c28be06b1 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -69,6 +69,7 @@ +