Skip to content

Commit

Permalink
GD-68 added disconnect feature (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: Eugene Alekseev <alekseev@ankr.com>
  • Loading branch information
Vukiz and Eugene Alekseev authored Apr 6, 2022
1 parent 638c669 commit 0a76351
Show file tree
Hide file tree
Showing 29 changed files with 622 additions and 1,283 deletions.
60 changes: 44 additions & 16 deletions Assets/AnkrSDK/Examples/Scripts/ConnectionController.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
using System;
using AnkrSDK.Core.Utils.UI;
using AnkrSDK.WalletConnectSharp.Core;
using AnkrSDK.WalletConnectSharp.Core.Models;
using AnkrSDK.WalletConnectSharp.Unity;
using Cysharp.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace AnkrSDK.Examples
namespace AnkrSDK
{
public class ConnectionController : MonoBehaviour
{
private const string LoginText = "Login";
private const string ConnectingText = "Connecting...";
private const string DisconnectedText = "Disconnected";

[SerializeField] private QRCodeImage _qrCodeImage;
[SerializeField] private TMP_Text _connectionText;
Expand All @@ -27,9 +30,10 @@ private void OnEnable()
private void OnDisable()
{
UnsubscribeFromTransportEvents();
WalletConnect.Instance.ConnectionStarted -= OnConnectionStarted;
}

private void UpdateSceneState()
private void UpdateSceneState(WCSessionData _ = null)
{
var walletConnectUnitySession = WalletConnect.ActiveSession;
if (walletConnectUnitySession == null)
Expand All @@ -51,20 +55,13 @@ private async UniTaskVoid TrySubscribeToWalletEvents()
{
if (WalletConnect.Instance == null)
{
Debug.Log("Wallet Connect Instance is null waiting.");
await UniTask.WaitWhile(() => WalletConnect.Instance == null);
}

_loginButton.onClick.AddListener(GetLoginAction());

WalletConnect.Instance.ConnectedEvent.AddListener(UpdateSceneState);
UpdateSceneState();

SubscribeOnTransportEvents();

if (WalletConnect.ActiveSession != null)
{
UpdateLoginButtonState(this, WalletConnect.ActiveSession);
}
}

#if !UNITY_EDITOR && (UNITY_ANDROID || UNITY_IOS)
Expand All @@ -91,35 +88,66 @@ private UnityAction GetLoginAction()

private void SubscribeOnTransportEvents()
{
if (WalletConnect.ActiveSession == null)
{
return;
}
UpdateSceneState();
UpdateLoginButtonState(this, WalletConnect.ActiveSession);

WalletConnect.Instance.ConnectedEvent.AddListener(UpdateSceneState);

WalletConnect.ActiveSession.OnTransportConnect += UpdateLoginButtonState;
WalletConnect.ActiveSession.OnTransportDisconnect += UpdateLoginButtonState;
WalletConnect.ActiveSession.OnTransportOpen += UpdateLoginButtonState;

WalletConnect.ActiveSession.OnSessionDisconnect += OnSessionDisconnect;
Debug.Log("[Connection Controller] Subscribed");
}

private void OnSessionDisconnect(object sender, EventArgs e)
{
Debug.Log("Session Disconnected");
UnsubscribeFromTransportEvents();
UpdateLoginButtonState(this, WalletConnect.ActiveSession);
WalletConnect.Instance.ConnectionStarted += OnConnectionStarted;
}

private void OnConnectionStarted()
{
WalletConnect.Instance.ConnectionStarted -= OnConnectionStarted;

Debug.Log("Connection Started");
TrySubscribeToWalletEvents().Forget();
}

private void UnsubscribeFromTransportEvents()
{
_loginButton.onClick.RemoveAllListeners();
WalletConnect.Instance.ConnectedEvent.RemoveListener(UpdateSceneState);

if (WalletConnect.ActiveSession == null)
{
return;
}

WalletConnect.Instance.ConnectedEvent.RemoveListener(UpdateSceneState);
WalletConnect.ActiveSession.OnTransportConnect -= UpdateLoginButtonState;
WalletConnect.ActiveSession.OnTransportDisconnect -= UpdateLoginButtonState;
WalletConnect.ActiveSession.OnTransportOpen -= UpdateLoginButtonState;

WalletConnect.ActiveSession.OnSessionDisconnect -= OnSessionDisconnect;

Debug.Log("[Connection Controller] Unsubscribed");
}

private void UpdateLoginButtonState(object sender, WalletConnectProtocol e)
{
UpdateSceneState();
_connectionText.text = e.TransportConnected ? LoginText : ConnectingText;
if (!e.Connected && !e.Connecting && e.Disconnected)
{
_connectionText.text = DisconnectedText;
}
else
{
_connectionText.text = e.TransportConnected ? LoginText : ConnectingText;
}

_loginButton.interactable = e.TransportConnected;
}
}
Expand Down
65 changes: 65 additions & 0 deletions Assets/AnkrSDK/Examples/Scripts/DisconnectButtonController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using AnkrSDK.Core.Implementation;
using AnkrSDK.WalletConnectSharp.Core;
using AnkrSDK.WalletConnectSharp.Unity;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

namespace AnkrSDK
{
public class DisconnectButtonController : MonoBehaviour
{
[SerializeField] private Button _button;

private void OnEnable()
{
SubscribeOnTransportEvents().Forget();
_button.onClick.AddListener(OnButtonClick);
}

private void OnDisable()
{
UnsubscribeFromTransportEvents();
_button.onClick.RemoveAllListeners();
}

private async UniTaskVoid SubscribeOnTransportEvents()
{
if (WalletConnect.Instance == null)
{
await UniTask.WaitWhile(() => WalletConnect.Instance == null);
}

if (WalletConnect.ActiveSession == null)
{
return;
}

WalletConnect.ActiveSession.OnTransportConnect += UpdateDisconnectButtonState;
WalletConnect.ActiveSession.OnTransportDisconnect += UpdateDisconnectButtonState;
WalletConnect.ActiveSession.OnTransportOpen += UpdateDisconnectButtonState;
}

private void UnsubscribeFromTransportEvents()
{
if (WalletConnect.ActiveSession == null)
{
return;
}

WalletConnect.ActiveSession.OnTransportConnect -= UpdateDisconnectButtonState;
WalletConnect.ActiveSession.OnTransportDisconnect -= UpdateDisconnectButtonState;
WalletConnect.ActiveSession.OnTransportOpen -= UpdateDisconnectButtonState;
}

private void UpdateDisconnectButtonState(object sender, WalletConnectProtocol e)
{
_button.gameObject.SetActive(!e.Disconnected);
}

private static void OnButtonClick()
{
EthHandler.Disconnect().Forget();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0a76351

Please sign in to comment.