Skip to content

Commit 0147d96

Browse files
authored
removed boilerplates dependency from SequenceFrontend (#267)
* removed boilerplates dependency from SequenceFrontend * version bump
1 parent 388c1d6 commit 0147d96

File tree

11 files changed

+199
-25
lines changed

11 files changed

+199
-25
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using UnityEngine;
2+
3+
namespace Sequence.Boilerplates
4+
{
5+
public interface ITween
6+
{
7+
public void Initialize(RectTransform rectTransform);
8+
public void AnimateIn(float durationInSeconds);
9+
public void AnimateOut(float durationInSeconds);
10+
}
11+
}

Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/Tweening/ITween.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Text;
3+
using System.Threading.Tasks;
4+
using UnityEngine;
5+
using UnityEngine.UI;
6+
7+
namespace Sequence.Boilerplates
8+
{
9+
public class QrCodeView : MonoBehaviour
10+
{
11+
const string ApiEndpoint = "https://api.sequence.app/qr/";
12+
13+
[SerializeField] private string _format;
14+
[SerializeField] private int _size;
15+
[SerializeField] private RawImage _qrImage;
16+
17+
public async Task Show(string paymentToken, string destinationAddress, string amount)
18+
{
19+
var deeplink = string.Format(_format, paymentToken, destinationAddress, amount);
20+
await Show(deeplink);
21+
}
22+
23+
public async Task Show(string deeplink)
24+
{
25+
_qrImage.texture = null;
26+
_qrImage.texture = await GenerateQrCodeAsync(deeplink);
27+
}
28+
29+
private async Task<Texture2D> GenerateQrCodeAsync(string deeplink)
30+
{
31+
var encodedLink = Convert.ToBase64String(Encoding.UTF8.GetBytes(deeplink));
32+
var qrLink = ApiEndpoint + encodedLink + $"/{_size}";
33+
return await AssetHandler.GetTexture2DAsync(qrLink);
34+
}
35+
}
36+
}

Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/QrCodeView.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/UI/SequenceSampleUI.cs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
21
using Sequence.Authentication;
3-
using Sequence.Boilerplates.DailyRewards;
4-
using Sequence.Boilerplates.InGameShop;
5-
using Sequence.Boilerplates.Inventory;
6-
using Sequence.Boilerplates.Login;
7-
using Sequence.Boilerplates.PlayerProfile;
82
using Sequence.Config;
93
using Sequence.EmbeddedWallet;
104
using Sequence.Utils.SecureStorage;
@@ -23,36 +17,28 @@ public class SequenceSampleUI : MonoBehaviour
2317
public static SequenceSampleUI instance;
2418

2519
private ILogin _loginHandler;
26-
private SequenceLoginWindow _loginWindow;
20+
private LoginPanel _loginWindow;
2721
private TransitionPanel _featureSelection;
2822
private WalletPanel _walletPanel;
2923
private SignMessagePanel _signMessagePanel;
3024
private SendTransactionPanel _sendTransactionPanel;
3125
private SendTransactionWithFeeOptionsPanel _sendTransactionWithFeeOptionsPanel;
3226
private SeeMarketplaceListingsPanel _seeMarketplaceListingsPanel;
3327
private MarketplaceItemDetailsPanel _marketplaceItemDetailsPanel;
34-
private SequencePlayerProfile _playerProfile;
35-
private SequenceDailyRewards _dailyRewards;
36-
private SequenceInventory _inventory;
37-
private SequenceInGameShop _inGameShop;
3828

3929
private void Awake()
4030
{
4131
if (instance == null) instance = this;
4232
else Destroy(gameObject);
4333

44-
_loginWindow = GetComponentInChildren<SequenceLoginWindow>();
34+
_loginWindow = GetComponentInChildren<LoginPanel>();
4535
_featureSelection = GetComponentInChildren<TransitionPanel>();
4636
_walletPanel = GetComponentInChildren<WalletPanel>();
4737
_signMessagePanel = GetComponentInChildren<SignMessagePanel>();
4838
_sendTransactionPanel = GetComponentInChildren<SendTransactionPanel>();
4939
_sendTransactionWithFeeOptionsPanel = GetComponentInChildren<SendTransactionWithFeeOptionsPanel>();
5040
_seeMarketplaceListingsPanel = GetComponentInChildren<SeeMarketplaceListingsPanel>();
5141
_marketplaceItemDetailsPanel = GetComponentInChildren<MarketplaceItemDetailsPanel>();
52-
_playerProfile = GetComponentInChildren<SequencePlayerProfile>();
53-
_dailyRewards = GetComponentInChildren<SequenceDailyRewards>();
54-
_inventory = GetComponentInChildren<SequenceInventory>();
55-
_inGameShop = GetComponentInChildren<SequenceInGameShop>();
5642

5743
if (!IsTesting)
5844
{
@@ -91,11 +77,6 @@ private void DisableAllUIPages()
9177
{
9278
pages[i].gameObject.SetActive(false);
9379
}
94-
95-
_playerProfile.Hide();
96-
_inventory.Hide();
97-
_inGameShop.Hide();
98-
_dailyRewards.Hide();
9980
}
10081

10182
public void OpenPlayerProfile(IWallet wallet)
@@ -169,7 +150,7 @@ private void OnFailedToRecoverSession(string error)
169150
Debug.LogError($"Error attempting to recover Sequence session: {error}");
170151

171152
DisableAllUIPages();
172-
_loginWindow.Show();
153+
_loginWindow.Close();
173154
}
174155
}
175156
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Sequence.Utils;
8+
using UnityEngine;
9+
using UnityEngine.Networking;
10+
11+
namespace Sequence.Boilerplates
12+
{
13+
public static class AssetHandler
14+
{
15+
public static readonly Texture2D DefaultTexture = new Texture2D(100, 100); // Default if we fail to fetch the texture
16+
private static readonly string Directory = Path.Combine(Application.persistentDataPath, "assets");
17+
18+
public static async Task<Sprite> GetSpriteAsync(string url)
19+
{
20+
var texture = await GetTexture2DAsync(url);
21+
var sprite = Sprite.Create(texture,
22+
new Rect(0, 0, texture.width, texture.height),
23+
new Vector2(.5f, .5f));
24+
25+
return sprite;
26+
}
27+
28+
public static async Task<Texture2D> GetTexture2DAsync(string url)
29+
{
30+
var texture = DefaultTexture;
31+
var cacheKey = Convert.ToBase64String(Encoding.UTF8.GetBytes(url));
32+
if (url == null || url.Length <= 0 || url.EndsWith(".gif"))
33+
return texture;
34+
35+
if (TryGetTexture(cacheKey, out var cachedTexture))
36+
return cachedTexture;
37+
38+
try
39+
{
40+
using UnityWebRequest imageRequest = UnityWebRequestTexture.GetTexture(url);
41+
await imageRequest.SendWebRequest();
42+
43+
if (imageRequest.result != UnityWebRequest.Result.Success)
44+
{
45+
Debug.LogWarning($"Error fetching image at url: {url}\nError: {imageRequest.error}\nDownload Handler error: {imageRequest.downloadHandler.error}\nReturning default");
46+
}
47+
else
48+
{
49+
texture = ((DownloadHandlerTexture) imageRequest.downloadHandler).texture;
50+
var data = texture.EncodeToPNG();
51+
FileStorage.Save(data, cacheKey, Directory);
52+
}
53+
} catch (HttpRequestException e) {
54+
Debug.LogWarning("HTTP Request failed: " + e.Message);
55+
} catch (FormatException e) {
56+
Debug.LogWarning("Invalid URL format: " + e.Message);
57+
} catch (Exception e) {
58+
if (e.Message.Contains($"{(int)HttpStatusCode.Gone}"))
59+
{
60+
Debug.LogWarning($"Error fetching image at url: {url}\nError: {e.Message}\nReturning default");
61+
}
62+
else
63+
{
64+
Debug.LogWarning("An unexpected error occurred: " + e.Message + $"\nUrl: {url}\nReturning default");
65+
}
66+
}
67+
68+
return texture;
69+
}
70+
71+
public static bool TryGetTexture(string key, out Texture2D texture)
72+
{
73+
var data = FileStorage.Read(Path.Combine(Directory, key));
74+
if (data == null)
75+
{
76+
texture = null;
77+
return false;
78+
}
79+
80+
texture = new Texture2D(1, 1);
81+
texture.LoadImage(data);
82+
return true;
83+
}
84+
}
85+
}

Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/Utils/AssetHandler.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.IO;
2+
3+
namespace Sequence.Boilerplates
4+
{
5+
public static class FileStorage
6+
{
7+
public static void RemoveAllInDirectory(params string[] pathParts)
8+
{
9+
var path = Path.Combine(pathParts);
10+
var dir = new DirectoryInfo(path);
11+
12+
foreach (var fi in dir.GetFiles())
13+
fi.Delete();
14+
15+
foreach (var di in dir.GetDirectories())
16+
{
17+
RemoveAllInDirectory(di.FullName);
18+
di.Delete();
19+
}
20+
}
21+
22+
public static void Save(byte[] content, string fileName, string path)
23+
{
24+
CheckForDirectory(path);
25+
var filePath = Path.Combine(path, fileName);
26+
27+
File.WriteAllBytes(filePath, content);
28+
}
29+
30+
public static byte[] Read(string path)
31+
{
32+
var exists = File.Exists(path);
33+
return exists ? File.ReadAllBytes(path) : null;
34+
}
35+
36+
private static void CheckForDirectory(string path)
37+
{
38+
if (!Directory.Exists(path))
39+
Directory.CreateDirectory(path);
40+
}
41+
}
42+
}

Packages/Sequence-Unity/Sequence/SequenceFrontend/Scripts/Utils/FileStorage.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/Sequence-Unity/Sequence/SequenceFrontend/SequenceExamples.asmdef

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"GUID:a35e3a53d4439435f8b36ed2c6158cd8",
1010
"GUID:b4f9c0f8f363f439b9e337f79050f189",
1111
"GUID:403077141e1554429a890cbc129df651",
12-
"GUID:19b9eb7db56cc47349571a4fbb0dd677",
13-
"GUID:c1c11ccc5362841f39d01cb3a3316c5f"
12+
"GUID:19b9eb7db56cc47349571a4fbb0dd677"
1413
],
1514
"includePlatforms": [],
1615
"excludePlatforms": [],

0 commit comments

Comments
 (0)