-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGiftingHeaderPresenter.cs
More file actions
151 lines (122 loc) · 4.87 KB
/
GiftingHeaderPresenter.cs
File metadata and controls
151 lines (122 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Threading;
using Cysharp.Threading.Tasks;
using DCL.Backpack.Gifting.Views;
using DCL.Input;
using DCL.Input.Component;
using DCL.Profiles;
using DCL.UI.ProfileElements;
using DCL.Utilities;
using UnityEngine;
using Utility;
namespace DCL.Backpack.Gifting.Presenters
{
public class GiftingHeaderPresenter : IDisposable
{
private static readonly InputMapComponent.Kind[] BLOCKED_INPUTS =
{
InputMapComponent.Kind.PLAYER,
InputMapComponent.Kind.SHORTCUTS,
InputMapComponent.Kind.CAMERA,
InputMapComponent.Kind.IN_WORLD_CAMERA,
};
private const string TITLE_FORMAT = "Send a Gift to <color=#{0}>{1}</color>";
private const int SEARCH_DEBOUNCE_MS = 500;
public event Action<string>? OnSearchChanged;
private readonly GiftingHeaderView view;
private readonly IProfileRepository profileRepository;
private readonly UserWalletAddressElementPresenter walletAddressController;
private readonly IInputBlock inputBlock;
private readonly ReactiveProperty<ProfileThumbnailViewModel> profileThumbnail =
new (ProfileThumbnailViewModel.Default());
private CancellationTokenSource? searchCts;
public Sprite? CurrentRecipientAvatarSprite
{
get;
private set;
}
public GiftingHeaderPresenter(GiftingHeaderView view,
IProfileRepository profileRepository,
IInputBlock inputBlock)
{
this.view = view;
this.profileRepository = profileRepository;
this.inputBlock = inputBlock;
walletAddressController = new UserWalletAddressElementPresenter(view.UserProfileWallet);
view.UserProfileImage.Bind(profileThumbnail);
view.SearchBar.inputField.onSelect.AddListener(OnSearchSelected);
view.SearchBar.inputField.onDeselect.AddListener(OnSearchDeselected);
view.SearchBar.inputField.onValueChanged.AddListener(DebounceSearch);
view.SearchBar.clearSearchButton.onClick.AddListener(ClearSearch);
}
private void OnSearchSelected(string text)
{
inputBlock.Disable(BLOCKED_INPUTS);
}
private void OnSearchDeselected(string text)
{
inputBlock.Enable(BLOCKED_INPUTS);
}
public async UniTask SetupAsync(string userId, string username, CancellationToken ct)
{
try
{
ct.ThrowIfCancellationRequested();
Profile.CompactInfo? profile = await profileRepository.GetCompactAsync(userId, ct: ct);
if (profile == null || ct.IsCancellationRequested)
return;
walletAddressController.Setup(profile.Value);
Color userNameColor = profile.Value.UserNameColor;
string userNameColorHex = ColorUtility.ToHtmlStringRGB(userNameColor);
if (ct.IsCancellationRequested)
return;
view.Title.text = string.Format(TITLE_FORMAT, userNameColorHex, profile.Value.Name);
profileThumbnail.SetLoading(userNameColor);
string faceUrl = profile.Value.FaceSnapshotUrl;
if (!string.IsNullOrEmpty(faceUrl))
{
await GetProfileThumbnailCommand.Instance.ExecuteAsync(
profileThumbnail,
null,
userId,
faceUrl,
ct);
}
if (ct.IsCancellationRequested)
return;
if (profileThumbnail.Value.Sprite != null)
CurrentRecipientAvatarSprite = profileThumbnail.Value.Sprite;
walletAddressController.Setup(profile.Value);
}
catch (OperationCanceledException)
{
//Ignore
}
}
private void DebounceSearch(string newText)
{
searchCts = searchCts.SafeRestart();
DebounceSearchAsync(newText, searchCts.Token).Forget();
}
private async UniTask DebounceSearchAsync(string newText, CancellationToken ct)
{
await UniTask.Delay(SEARCH_DEBOUNCE_MS, cancellationToken: ct);
OnSearchChanged?.Invoke(newText);
}
private void ClearSearch()
{
view.SearchBar.inputField.SetTextWithoutNotify(string.Empty);
OnSearchChanged?.Invoke(string.Empty);
}
public void Dispose()
{
walletAddressController.Dispose();
searchCts.SafeCancelAndDispose();
}
public void ClearSearchImmediate()
{
view.SearchBar.inputField.SetTextWithoutNotify("");
OnSearchChanged?.Invoke(string.Empty);
}
}
}