diff --git a/Assets/connectivityUI.cs b/Assets/connectivityUI.cs index 59db960..636f542 100644 --- a/Assets/connectivityUI.cs +++ b/Assets/connectivityUI.cs @@ -6,46 +6,68 @@ using Raindrop; using UnityEngine; using Raindrop.Netcom; using Raindrop.ServiceLocator; +using UniRx; using UnityEngine.UI; +using Logger = OpenMetaverse.Logger; // change the color of the image to red or green based on connection to server. [RequireComponent(typeof(Image))] public class connectivityUI : MonoBehaviour { + private ReactiveProperty isConnected = new ReactiveProperty(); + private RaindropInstance instance => ServiceLocator.Instance.Get(); void Start() { - notConnected(); - - var _instance = ServiceLocator.Instance.Get(); - _instance.Netcom.ClientLoginStatus += NetcomOnClientLoginStatus; - _instance.Netcom.ClientLoggedOut += NetcomOnClientLoggedOut; + if (instance == null || instance.Netcom == null) + Logger.Log("raindrop instance/netcom not available", Helpers.LogLevel.Error); + isConnected.AsObservable().Subscribe(_ => updateConnectivityUI(_)); + isConnected.Value = instance.Netcom.IsLoggedIn; + + instance.Netcom.ClientLoginStatus += NetcomOnClientLoginStatus; + instance.Netcom.ClientLoggedOut += NetcomOnClientLoggedOut; } + private void updateConnectivityUI(bool isConnected) + { + if (! isConnected) + { + show_notConnected(); + } + else + { + show_isConnected(); + } + } + + #region Subscribe to backend connectivity events private void NetcomOnClientLoggedOut(object sender, EventArgs e) { - notConnected(); + isConnected.Value = false; } private void NetcomOnClientLoginStatus(object sender, LoginProgressEventArgs e) { if (e.Status == LoginStatus.Success) - { - yesConnected(); + { + isConnected.Value = true; } else if (e.Status == LoginStatus.Failed) { - notConnected(); + isConnected.Value = false; } } + #endregion - private void notConnected() + #region view + private void show_notConnected() { this.GetComponent().color = Color.red; } - private void yesConnected() + private void show_isConnected() { this.GetComponent().color = Color.green; } + #endregion }