From 1f67bfa32dacaf6dc85017b1dd5090e9fa51e224 Mon Sep 17 00:00:00 2001 From: Thaina Yu Date: Thu, 2 Nov 2023 14:10:29 +0700 Subject: [PATCH] safer null check --- Runtime/WebViewPanel.cs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/Runtime/WebViewPanel.cs b/Runtime/WebViewPanel.cs index f967749..9edab9e 100644 --- a/Runtime/WebViewPanel.cs +++ b/Runtime/WebViewPanel.cs @@ -48,8 +48,12 @@ public void LoadWebView(string loginToken = "") #else InitializeAndShowWebView(loginToken); #endif - messagePanel.SetMessage(messageType); - messagePanel.SetVisible(true); + if(messagePanel) + { + messagePanel.SetMessage(messageType); + messagePanel.SetVisible(true); + } + SetScreenPadding(); } @@ -93,7 +97,11 @@ public void ReloadWithLoginToken(string loginToken = "") /// public void SetVisible(bool visible) { - messagePanel.SetVisible(visible); + if(messagePanel) + { + messagePanel.SetVisible(visible); + } + if (webViewObject != null) { webViewObject.IsVisible = visible; @@ -124,7 +132,10 @@ private void SetScreenPadding() webViewObject.SetMargins(screenPadding.left, screenPadding.top, screenPadding.right, screenPadding.bottom); } - messagePanel.SetMargins(screenPadding.left, screenPadding.top, screenPadding.right, screenPadding.bottom); + if(messagePanel) + { + messagePanel.SetMargins(screenPadding.left, screenPadding.top, screenPadding.right, screenPadding.bottom); + } } // Receives message from RPM website, which contains avatar URL. @@ -133,7 +144,8 @@ private void OnWebMessageReceived(string message) SDKLogger.AvatarLoaderLogger.Log(TAG, $"--- WebView Message: {message}"); try { - HandleEvents(JsonConvert.DeserializeObject(message)); + var jsonMessage = JsonConvert.DeserializeObject(message); + HandleEvents(jsonMessage); } catch (Exception e) { @@ -146,18 +158,22 @@ private void HandleEvents(WebMessage webMessage) switch (webMessage.eventName) { case WebViewEvents.AVATAR_EXPORT: - OnAvatarCreated?.Invoke(webMessage.GetAvatarUrl()); + if(OnAvatarCreated != null) + OnAvatarCreated.Invoke(webMessage.GetAvatarUrl()); HideAndClearCache(); break; case WebViewEvents.USER_SET: - OnUserSet?.Invoke(webMessage.GetUserId()); + if(OnUserSet != null) + OnUserSet.Invoke(webMessage.GetUserId()); break; case WebViewEvents.ASSET_UNLOCK: - OnAssetUnlock?.Invoke(webMessage.GetAssetRecord()); + if(OnAssetUnlock != null) + OnAssetUnlock.Invoke(webMessage.GetAssetRecord()); break; case WebViewEvents.USER_AUTHORIZED: - OnUserAuthorized?.Invoke(webMessage.GetUserId()); + if(OnUserAuthorized != null) + OnUserAuthorized.Invoke(webMessage.GetUserId()); break; } }