Skip to content

Commit

Permalink
safer null check
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaina committed Jun 17, 2024
1 parent 935f360 commit 7298546
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions Runtime/WebViewPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -92,8 +96,12 @@ public void ReloadWithLoginToken(string loginToken = "")
/// </summary>
public void SetVisible(bool visible)
{
messagePanel.SetVisible(visible);
if (webViewObject != null)
if (messagePanel)
{
messagePanel.SetVisible(visible);
}

if (webViewObject)
{
webViewObject.IsVisible = visible;
}
Expand All @@ -102,7 +110,7 @@ public void SetVisible(bool visible)
private void OnDrawGizmos()
{
var rectTransform = transform as RectTransform;
if (rectTransform != null)
if (rectTransform)
{
Gizmos.matrix = rectTransform.localToWorldMatrix;
Gizmos.color = Color.green;
Expand All @@ -123,7 +131,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.
Expand All @@ -132,7 +143,8 @@ private void OnWebMessageReceived(string message)
SDKLogger.AvatarLoaderLogger.Log(TAG, $"--- WebView Message: {message}");
try
{
HandleEvents(JsonConvert.DeserializeObject<WebMessage>(message));
var jsonMessage = JsonConvert.DeserializeObject<WebMessage>(message);
HandleEvents(jsonMessage);
}
catch (Exception e)
{
Expand All @@ -145,24 +157,30 @@ private void HandleEvents(WebMessage webMessage)
switch (webMessage.eventName)
{
case WebViewEvents.AVATAR_EXPORT:
OnAvatarCreated?.Invoke(webMessage.GetAvatarUrl());
if(OnAvatarCreated)
OnAvatarCreated.Invoke(webMessage.GetAvatarUrl());
HideAndClearCache();
break;
case WebViewEvents.USER_SET:
OnUserSet?.Invoke(webMessage.GetUserId());
if(OnUserSet)
OnUserSet.Invoke(webMessage.GetUserId());

break;
case WebViewEvents.ASSET_UNLOCK:
OnAssetUnlock?.Invoke(webMessage.GetAssetRecord());
if(OnAssetUnlock)
OnAssetUnlock.Invoke(webMessage.GetAssetRecord());
break;
case WebViewEvents.USER_AUTHORIZED:
OnUserAuthorized?.Invoke(webMessage.GetUserId());
if(OnUserAuthorized)
OnUserAuthorized.Invoke(webMessage.GetUserId());
break;
case WebViewEvents.USER_LOGOUT:
OnUserLogout?.Invoke();
if(OnUserLogout)
OnUserLogout.Invoke();
break;
case WebViewEvents.USER_UPDATED:
OnUserUpdate?.Invoke(webMessage.GetUserId());
if(OnUserUpdate)
OnUserUpdate.Invoke(webMessage.GetUserId());
break;
}
}
Expand Down

0 comments on commit 7298546

Please sign in to comment.