Skip to content

Commit

Permalink
Finished SIWE UI
Browse files Browse the repository at this point in the history
  • Loading branch information
skibitsky committed Nov 12, 2024
1 parent 010d6c1 commit ee39a7b
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,12 @@

.snackbar__icon-container--color-success > .snackbar__icon {
--unity-image-tint-color: var(--ro-color-success-100);
}

.snackbar__icon-container--color-info {
background-color: var(--ro-accent-glass-005);
}

.snackbar__icon-container--color-info > .snackbar__icon {
--unity-image-tint-color: var(--ro-color-fg-100);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
justify-content: center
}

#siwe-view__logo-app-image {
width: 100%;
height: 100%;
position: absolute;
}

#siwe-view__logo-wallet-image {
width: 100%;
height: 100%;
position: absolute;
}

.siwe-view__logo {
width: 80px;
height: 80px;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" editor-extension-mode="False">
<Style src="SiweView.uss"/>
<ui:VisualElement name="siwe-view__logo-container">
<ui:Image name="siwe-view__logo-app" class="siwe-view__logo siwe-view__logo-app">
<ui:VisualElement name="siwe-view__logo-app-container" class="siwe-view__logo siwe-view__logo-app">
<ui:Image name="siwe-view__logo-app-placeholder" style="--unity-image: resource('Reown/AppKit/Icons/icon_regular_app');"
class="siwe-view__logo-placeholder"/>
</ui:Image>
<ui:Image name="siwe-view__logo-wallet" class="siwe-view__logo siwe-view__logo-wallet">
<ui:Image name="siwe-view__logo-app-placeholder" style="--unity-image: resource('Reown/AppKit/Icons/icon_regular_wallet');"
<ui:Image name="siwe-view__logo-app-image"/>
</ui:VisualElement>
<ui:VisualElement name="siwe-view__logo-wallet-container" class="siwe-view__logo siwe-view__logo-wallet">
<ui:Image name="siwe-view__logo-wallet-placeholder" style="--unity-image: resource('Reown/AppKit/Icons/icon_regular_wallet');"
class="siwe-view__logo-placeholder"/>
</ui:Image>
<ui:Image name="siwe-view__logo-wallet-image"/>
</ui:VisualElement>
</ui:VisualElement>
<ui:VisualElement name="siwe-view__instructions-container">
<ui:Label name="siwe-view__title" class="text-paragraph"
Expand Down
3 changes: 0 additions & 3 deletions src/Reown.AppKit.Unity/Runtime/AppKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ public static void OpenModal(ViewType viewType = ViewType.None)
if (!IsInitialized)
throw new Exception("AppKit not initialized"); // TODO: use custom ex type

if (IsModalOpen)
throw new Exception("AppKit already open"); // TODO: use custom ex type

Instance.OpenModalCore(viewType);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Reown.AppKit.Unity/Runtime/AppKitCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ private static void AccountConnectedHandler(object sender, Connector.AccountConn
if (WalletUtils.TryGetLastViewedWallet(out var lastViewedWallet))
WalletUtils.SetRecentWallet(lastViewedWallet);

CloseModal();
if (!SiweController.IsEnabled)
CloseModal();
}

private static void AccountDisconnectedHandler(object sender, Connector.AccountDisconnectedEventArgs e)
Expand Down
2 changes: 1 addition & 1 deletion src/Reown.AppKit.Unity/Runtime/Components/Modal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Modal()
body?.RegisterCallback<GeometryChangedEvent, Modal>(
(evt, modal) =>
{
var newHeight = Mathf.RoundToInt(evt.newRect.height + header.resolvedStyle.height
var newHeight = Mathf.CeilToInt(evt.newRect.height + header.resolvedStyle.height
#if UNITY_ANDROID || UNITY_IOS
// Bottom safe area
+ RuntimePanelUtils.ScreenToPanel(panel,
Expand Down
10 changes: 8 additions & 2 deletions src/Reown.AppKit.Unity/Runtime/Components/Snackbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ namespace Reown.AppKit.Unity.Components
{
public class Snackbar : VisualElement
{
public static readonly string ClassName = "snackbar";
public const string ClassName = "snackbar";
public static readonly string ClassNameIconContainer = $"{ClassName}__icon-container";
public static readonly string ClassNameIconContainerColorError = $"{ClassNameIconContainer}--color-error";
public static readonly string ClassNameIconContainerColorSuccess = $"{ClassNameIconContainer}--color-success";
public static readonly string ClassNameIconContainerColorInfo = $"{ClassNameIconContainer}--color-info";
public static readonly string ClassNameIcon = $"{ClassName}__icon";
public static readonly string ClassNameMessage = $"{ClassName}__message";

Expand All @@ -30,6 +31,10 @@ public IconColor CurrentIconColor
_iconContainer.AddToClassList(ClassNameIconContainerColorSuccess);
_currentColorClass = ClassNameIconContainerColorSuccess;
break;
case IconColor.Info:
_iconContainer.AddToClassList(ClassNameIconContainerColorInfo);
_currentColorClass = ClassNameIconContainerColorInfo;
break;
default:
throw new ArgumentOutOfRangeException(nameof(value), value, null);
}
Expand All @@ -56,7 +61,7 @@ public string Message
{
}

public Snackbar() : this(IconColor.Success, "Hello, friend!")
public Snackbar() : this(IconColor.Success, "")
{
}

Expand All @@ -77,6 +82,7 @@ public Snackbar(IconColor iconColor, string message)

public enum IconColor
{
Info,
Error,
Success
}
Expand Down
42 changes: 21 additions & 21 deletions src/Reown.AppKit.Unity/Runtime/Controllers/SiweController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using UnityEngine;
Expand Down Expand Up @@ -92,13 +91,8 @@ public async ValueTask<SiweSession> GetSessionAsync(GetSiweSessionArgs args)
session = new SiweSession(args);
}

Debug.Log($"[SiweController] Session is null: {session == null}");
var json = JsonConvert.SerializeObject(session);
Debug.Log($"[SiweController] Session JSON: {json}");
Debug.Log($"Thread id: {Thread.CurrentThread.ManagedThreadId}");
PlayerPrefs.SetString(SessionPlayerPrefsKey, json);
// PlayerPrefs.Save();
Debug.Log("[SiweController] Session saved to PlayerPrefs.");

Config.OnSignInSuccess(session);

Expand All @@ -107,18 +101,29 @@ public async ValueTask<SiweSession> GetSessionAsync(GetSiweSessionArgs args)

public async ValueTask DisconnectAsync()
{
Debug.Log("[SiweController] Delete session from PlayerPrefs.");
PlayerPrefs.DeleteKey(SessionPlayerPrefsKey);

if (Config.SignOut != null)
{
await Config.SignOut();
}

Debug.Log("[SiweController] Sign out success.");
Config.OnSignOutSuccess();
}

public bool TryLoadSiweSessionFromStorage(out SiweSession session)
{
var siweSessionJson = PlayerPrefs.GetString(SessionPlayerPrefsKey);
if (string.IsNullOrWhiteSpace(siweSessionJson))
{
session = null;
return false;
}

session = JsonConvert.DeserializeObject<SiweSession>(siweSessionJson);
return true;
}

private async void AccountDisconnectedHandler(object sender, Connector.AccountDisconnectedEventArgs e)
{
if (IsEnabled && Config.SignOutOnWalletDisconnect)
Expand All @@ -132,30 +137,25 @@ private async void ChainChangedHandler(object sender, NetworkController.ChainCha
if (!IsEnabled || !Config.SignOutOnChainChange)
return;

var siweSessionJson = PlayerPrefs.GetString(SessionPlayerPrefsKey);
if (string.IsNullOrWhiteSpace(siweSessionJson))
if (!TryLoadSiweSessionFromStorage(out var siweSession))
return;

var siweSession = JsonConvert.DeserializeObject<SiweSession>(siweSessionJson);
if (!siweSession.EthChainIds.Contains(e.NewChain.ChainReference))
{
await DisconnectAsync();
// TODO: request signature instead
await AppKit.DisconnectAsync();
}
if (siweSession.EthChainIds.Contains(e.NewChain.ChainReference))
return;

await DisconnectAsync();
// TODO: request signature instead
await AppKit.DisconnectAsync();
}

private async void AccountChangedHandler(object sender, Connector.AccountChangedEventArgs e)
{
if (!IsEnabled || !Config.SignOutOnAccountChange)
return;

var siweSessionJson = PlayerPrefs.GetString(SessionPlayerPrefsKey);
if (string.IsNullOrWhiteSpace(siweSessionJson))
if (!TryLoadSiweSessionFromStorage(out var siweSession))
return;

var siweSession = JsonConvert.DeserializeObject<SiweSession>(siweSessionJson);

if (!string.Equals(siweSession.EthAddress, e.Account.Address, StringComparison.InvariantCultureIgnoreCase))
{
await DisconnectAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected virtual void OnCopyAddressButtonClick()
{
var address = AppKit.AccountController.Address;
GUIUtility.systemCopyBuffer = address;
AppKit.NotificationController.Notify(NotificationType.Success, "EthAddress copied");
AppKit.NotificationController.Notify(NotificationType.Success, "Ethereum address copied");
}

private void ButtonsSetEnabled(bool value)
Expand Down
25 changes: 18 additions & 7 deletions src/Reown.AppKit.Unity/Runtime/Presenters/ModalHeaderPresenter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Reown.AppKit.Unity.Components;
using Reown.AppKit.Unity.Utils;
using Reown.Sign.Unity;
using UnityEngine;
using UnityEngine.UIElements;

namespace Reown.AppKit.Unity
{
Expand Down Expand Up @@ -82,13 +82,15 @@ private IEnumerator ShowSnackbarCoroutine(NotificationEventArgs notification)
{
NotificationType.Error => Snackbar.IconColor.Error,
NotificationType.Success => Snackbar.IconColor.Success,
_ => Snackbar.IconColor.Success // TODO: change to info
NotificationType.Info => Snackbar.IconColor.Info,
_ => Snackbar.IconColor.Info
};

var icon = notification.type switch
{
NotificationType.Error => Resources.Load<VectorImage>("Reown/AppKit/Icons/icon_bold_warningcircle"),
NotificationType.Success => Resources.Load<VectorImage>("Reown/AppKit/Icons/icon_bold_checkmark"),
NotificationType.Info => Resources.Load<VectorImage>("Reown/AppKit/Icons/icon_bold_info"),
_ => Resources.Load<VectorImage>("Reown/AppKit/Icons/icon_bold_warningcircle")
};

Expand All @@ -109,6 +111,15 @@ private void ViewChangedHandler(object _, ViewChangedEventArgs args)
if (_leftSlotItems.TryGetValue(args.oldViewType, out var oldItem))
oldItem.style.display = DisplayStyle.None;

if (args.newPresenter == null)
return;

// Right slot
View.rightSlot.style.visibility = !args.newPresenter.EnableCloseButton
? Visibility.Hidden
: Visibility.Visible;

// Left slot
if (_leftSlotItems.TryGetValue(args.newViewType, out var newItem))
{
newItem.style.display = DisplayStyle.Flex;
Expand All @@ -119,10 +130,10 @@ private void ViewChangedHandler(object _, ViewChangedEventArgs args)
View.leftSlot.style.visibility = Visibility.Hidden;
}

if (args.newPresenter != null)
View.style.borderBottomWidth = args.newPresenter.HeaderBorder
? 1
: 0;
// Header border
View.style.borderBottomWidth = args.newPresenter.HeaderBorder
? 1
: 0;
}

protected override void Dispose(bool disposing)
Expand Down
34 changes: 33 additions & 1 deletion src/Reown.AppKit.Unity/Runtime/Presenters/Presenter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using UnityEngine;
using UnityEngine.UIElements;

namespace Reown.AppKit.Unity
Expand All @@ -10,6 +9,11 @@ public abstract class PresenterBase : IDisposable

public virtual bool HeaderBorder { get; protected set; } = true;

/// <summary>
/// If true, the close button will be shown in the modal header.
/// </summary>
public virtual bool EnableCloseButton { get; protected set; } = true;

public RouterController Router { get; protected set; }

public virtual VisualElement ViewVisualElement { get; protected set; }
Expand All @@ -18,18 +22,27 @@ public abstract class PresenterBase : IDisposable

private bool _disposed;

/// <summary>
/// Called when the view becomes visible.
/// </summary>
public void OnVisible()
{
IsVisible = true;
OnVisibleCore();
}

/// <summary>
/// Called when the view becomes hidden, but still remains in the Router stack.
/// </summary>
public void OnHide()
{
IsVisible = false;
OnHideCore();
}

/// <summary>
/// Called when the view is completely removed from the Router stack.
/// </summary>
public void OnDisable()
{
IsVisible = false;
Expand Down Expand Up @@ -93,30 +106,49 @@ public Presenter(RouterController router, VisualElement parent, bool hideView =
BuildView(hideView);
}

/// <summary>
/// Builds the view and adds it to the parent view (usually <see cref="RouterController.RootVisualElement" /> of the Router).
/// </summary>
protected void BuildView(bool hideView)
{
View = CreateViewInstance();
View.style.display = hideView ? DisplayStyle.None : DisplayStyle.Flex;
Parent.Add(View);
}

/// <summary>
/// Implements the creation of the view instance.
/// </summary>
protected virtual TView CreateViewInstance()
{
return new TView();
}

/// <summary>
/// Called when the view becomes visible.
/// </summary>
protected override void OnVisibleCore()
{
}

/// <summary>
/// Called when the view becomes hidden, but still remains in the Router stack.
/// </summary>
protected override void OnHideCore()
{
}

/// <summary>
/// Called when the view is completely removed from the Router stack.
/// </summary>
protected override void OnDisableCore()
{
}

/// <summary>
/// Router will dispose the presenter if it's no longer needed.
/// This happens when another presenter of the same typed is registered in the Router via <see cref="RouterController.RegisterDefaultModalViews" />
/// </summary>
protected override void Dispose(bool disposing)
{
if (_disposed)
Expand Down
Loading

0 comments on commit ee39a7b

Please sign in to comment.