diff --git a/ColourMeOKGame/Assets/Scripts/Backend.cs b/ColourMeOKGame/Assets/Scripts/Backend.cs index 7995ff5..0f2431d 100644 --- a/ColourMeOKGame/Assets/Scripts/Backend.cs +++ b/ColourMeOKGame/Assets/Scripts/Backend.cs @@ -58,7 +58,7 @@ public enum UserAccountDetailTargetEnum } /// - /// callback functions to be invoked when the user signs in + /// callback functions to be invoked when the connection status changes /// /// private readonly List> _onConnectionStatusChangedCallbacks = new(); @@ -119,6 +119,7 @@ public void Initialise(Action callback) _db = FirebaseDatabase.DefaultInstance.RootReference; Status = FirebaseConnectionStatus.Connected; callback(Status); + FireOnConnectionStatusChangedCallbacks(); break; case DependencyStatus.UnavailableDisabled: @@ -127,16 +128,19 @@ public void Initialise(Action callback) case DependencyStatus.UnavailablePermission: Status = FirebaseConnectionStatus.ExternalError; callback(Status); + FireOnConnectionStatusChangedCallbacks(); break; case DependencyStatus.UnavailableUpdating: Status = FirebaseConnectionStatus.Updating; callback(Status); + FireOnConnectionStatusChangedCallbacks(); RetryInitialiseAfterDelay(callback); break; case DependencyStatus.UnavailableUpdaterequired: Status = FirebaseConnectionStatus.UpdateRequired; + FireOnConnectionStatusChangedCallbacks(); callback(Status); break; @@ -144,6 +148,7 @@ public void Initialise(Action callback) default: Status = FirebaseConnectionStatus.InternalError; Debug.LogError("firebase ??? blew up or something," + task.Result); + FireOnConnectionStatusChangedCallbacks(); callback(Status); break; } @@ -152,6 +157,55 @@ public void Initialise(Action callback) }); } + /// + /// async function to retry initialisation after a delay + /// + private async void RetryInitialiseAfterDelay(Action callback) + { + await Task.Delay(TimeSpan.FromSeconds(10)); + Initialise(callback); + } + + /// + /// cleanup function + /// + public void Cleanup() + { + SignOutUser(); + _auth.StateChanged -= AuthStateChanged; + _auth = null; + } + + /// + /// function to register a callback for when the user signs in + /// + /// callback function that takes in a FirebaseUser object + public void RegisterOnSignInCallback(Action callback) + { + _onSignInCallbacks.Add(callback); + Debug.Log($"registering OnSignInCallback ({_onSignInCallbacks.Count})"); + } + + /// + /// function to register a callback for when the user signs out + /// + /// callback function + public void RegisterOnSignOutCallback(Action callback) + { + _onSignOutCallbacks.Add(callback); + Debug.Log($"registering OnSignOutCallback ({_onSignOutCallbacks.Count})"); + } + + /// + /// function to register a callback for when the connection status changes + /// + /// callback function that takes in a FirebaseConnectionStatus enum + public void RegisterOnConnectionStatusChangedCallback(Action callback) + { + _onConnectionStatusChangedCallbacks.Add(callback); + Debug.Log($"registering ConnectionStatusChangedCallback ({_onConnectionStatusChangedCallbacks.Count})"); + } + /// /// function to fire all on sign in callbacks /// @@ -185,24 +239,22 @@ private void FireOnSignOutCallbacks() Debug.LogError($"error invoking OnSignOutCallback: {e.Message}"); } } - + /// - /// async function to retry initialisation after a delay + /// function to fire all on connection status changed callbacks /// - private async void RetryInitialiseAfterDelay(Action callback) + private void FireOnConnectionStatusChangedCallbacks() { - await Task.Delay(TimeSpan.FromSeconds(10)); - Initialise(callback); - } - - /// - /// cleanup function - /// - public void Cleanup() - { - SignOutUser(); - _auth.StateChanged -= AuthStateChanged; - _auth = null; + Debug.Log($"firing OnConnectionStatusChangedCallbacks ({_onConnectionStatusChangedCallbacks.Count})"); + foreach (var callback in _onConnectionStatusChangedCallbacks) + try + { + callback.Invoke(Status); + } + catch (Exception e) + { + Debug.LogError($"error invoking OnConnectionStatusChangedCallback: {e.Message}"); + } } /// @@ -233,24 +285,6 @@ private void AuthStateChanged(object sender, EventArgs eventArgs) RetrieveUsernameWithCallback((_, _) => { FireOnSignInCallbacks(); }); } - /// - /// function to register a callback for when the user signs in - /// - /// callback function that takes in a FirebaseUser object - public void RegisterOnSignInCallback(Action callback) - { - _onSignInCallbacks.Add(callback); - } - - /// - /// function to register a callback for when the user signs out - /// - /// callback function - public void RegisterOnSignOutCallback(Action callback) - { - _onSignOutCallbacks.Add(callback); - } - /// /// abstraction function to authenticate the user /// diff --git a/ColourMeOKGame/Assets/Scripts/GameManager.cs b/ColourMeOKGame/Assets/Scripts/GameManager.cs index e6611ef..5518b5c 100644 --- a/ColourMeOKGame/Assets/Scripts/GameManager.cs +++ b/ColourMeOKGame/Assets/Scripts/GameManager.cs @@ -107,6 +107,19 @@ private void OnEnable() Debug.Log("sign in callback, refreshing GameManager-controlled SideView UI"); _data.LoadFromTheWorld(FireLocalPlayerDataChangeCallbacks); }); + + Backend.RegisterOnConnectionStatusChangedCallback(status => + { + Debug.Log($"post-fcStatus change, deciding to show/hide buttons based on new status: {status}"); + ui.UI.Q