-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GD-68 added disconnect feature (#13)
Co-authored-by: Eugene Alekseev <alekseev@ankr.com>
- Loading branch information
Showing
29 changed files
with
622 additions
and
1,283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
Assets/AnkrSDK/Examples/Scripts/DisconnectButtonController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/AnkrSDK/Examples/Scripts/DisconnectButtonController.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.