-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGiftSelectionController.cs
More file actions
286 lines (235 loc) · 10.6 KB
/
GiftSelectionController.cs
File metadata and controls
286 lines (235 loc) · 10.6 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Threading;
using CommunicationData.URLHelpers;
using Cysharp.Threading.Tasks;
using DCL.Backpack.Gifting.Factory;
using DCL.Backpack.Gifting.Services.GiftingInventory;
using DCL.Backpack.Gifting.Services.SnapshotEquipped;
using DCL.Backpack.Gifting.Views;
using DCL.Diagnostics;
using DCL.Passport;
using DCL.Profiles;
using MVC;
using UnityEngine;
using Utility;
namespace DCL.Backpack.Gifting.Presenters
{
public class GiftSelectionController : ControllerBase<GiftingView, GiftSelectionParams>
{
private const string MsgErrorNonTransferableNft = "Cannot send this item as a gift because it's not a transferable NFT.";
private const string CouldNotFindTokenForUrnLog = "Could not find a valid tokenId for URN {0}. Aborting gift transfer.";
private const string DefaultGiftItemName = "Item";
public override CanvasOrdering.SortingLayer Layer => CanvasOrdering.SortingLayer.POPUP;
private readonly IProfileRepository profileRepository;
private readonly GiftSelectionComponentFactory componentFactory;
private readonly GiftInventoryService giftInventoryService;
private readonly IAvatarEquippedStatusProvider equippedStatusProvider;
private readonly IMVCManager mvcManager;
private GiftingHeaderPresenter? headerPresenter;
private GiftingFooterPresenter? footerPresenter;
private GiftingTabsManager? tabsManager;
private IGiftingGridPresenter? wearablesGridPresenter;
private IGiftingGridPresenter? emotesGridPresenter;
private GiftingErrorsController? giftingErrorsController;
private CancellationTokenSource? lifeCts;
public GiftSelectionController(ViewFactoryMethod viewFactory,
GiftSelectionComponentFactory componentFactory,
GiftInventoryService giftInventoryService,
IAvatarEquippedStatusProvider equippedStatusProvider,
IProfileRepository profileRepository,
IMVCManager mvcManager) : base(viewFactory)
{
this.componentFactory = componentFactory;
this.giftInventoryService = giftInventoryService;
this.equippedStatusProvider = equippedStatusProvider;
this.profileRepository = profileRepository;
this.mvcManager = mvcManager;
}
#region MVC
protected override void OnViewInstantiated()
{
if (viewInstance == null) return;
headerPresenter = componentFactory.CreateHeader(viewInstance.HeaderView);
footerPresenter = componentFactory.CreateFooter(viewInstance.FooterView);
giftingErrorsController = componentFactory.CreateErrorController(viewInstance);
tabsManager = componentFactory.CreateTabs(viewInstance,
out var wPresenter,
out var ePresenter);
wearablesGridPresenter = wPresenter;
emotesGridPresenter = ePresenter;
}
protected override void OnViewShow()
{
InitializeViewAsync()
.Forget();
}
private async UniTask InitializeViewAsync()
{
lifeCts = new CancellationTokenSource();
giftingErrorsController?.Hide(true);
try
{
await equippedStatusProvider.InitializeAsync(lifeCts.Token);
if (lifeCts.IsCancellationRequested) return;
headerPresenter?.ClearSearchImmediate();
wearablesGridPresenter?.Deactivate();
emotesGridPresenter?.Deactivate();
footerPresenter?.SetInitialState();
headerPresenter?.SetupAsync(inputData.userAddress, inputData.userName, lifeCts.Token).Forget();
if (headerPresenter != null) headerPresenter.OnSearchChanged += HandleSearchChanged;
if (footerPresenter != null) footerPresenter.OnSendGift += HandleSendGift;
if (wearablesGridPresenter != null)
{
wearablesGridPresenter.OnSelectionChanged += OnSelectionChanged;
wearablesGridPresenter.OnLoadingStateChanged += OnLoadingStateChanged;
}
if (emotesGridPresenter != null)
{
emotesGridPresenter.OnSelectionChanged += OnSelectionChanged;
emotesGridPresenter.OnLoadingStateChanged += OnLoadingStateChanged;
}
if (tabsManager != null)
{
tabsManager.OnSectionDeactivated += HandleSectionDeactivated;
tabsManager.Initialize();
}
wearablesGridPresenter?.SetSearchText(string.Empty);
emotesGridPresenter?.SetSearchText(string.Empty);
}
catch (Exception e)
{
}
}
private void HandleSectionDeactivated(GiftingSection section, IGiftingGridPresenter _)
{
headerPresenter?.ClearSearchImmediate();
}
protected override void OnViewClose()
{
if (headerPresenter != null) headerPresenter.OnSearchChanged -= HandleSearchChanged;
if (footerPresenter != null) footerPresenter.OnSendGift -= HandleSendGift;
if (wearablesGridPresenter != null)
{
wearablesGridPresenter.OnSelectionChanged -= OnSelectionChanged;
wearablesGridPresenter.OnLoadingStateChanged -= OnLoadingStateChanged;
}
if (emotesGridPresenter != null)
{
emotesGridPresenter.OnSelectionChanged -= OnSelectionChanged;
emotesGridPresenter.OnLoadingStateChanged -= OnLoadingStateChanged;
}
if (tabsManager != null)
tabsManager.OnSectionDeactivated -= HandleSectionDeactivated;
wearablesGridPresenter?.Deactivate();
emotesGridPresenter?.Deactivate();
giftingErrorsController?.Hide(true);
lifeCts.SafeCancelAndDispose();
}
private void OnSelectionChanged(string? selectedUrn)
{
if (string.IsNullOrEmpty(selectedUrn))
{
footerPresenter?.UpdateState(null);
return;
}
var active = tabsManager?.ActivePresenter;
string? itemName = active?.GetItemNameByUrn(selectedUrn);
footerPresenter?.UpdateState(itemName, inputData.userName);
}
private void OnLoadingStateChanged(bool isLoading)
{
if (viewInstance == null || tabsManager == null)
return;
var activePresenter = tabsManager.ActivePresenter;
if (activePresenter == null)
return;
if (isLoading)
{
if (activePresenter.CurrentItemCount == 0)
viewInstance.ProgressContainer.SetActive(true);
}
else
{
viewInstance.ProgressContainer.SetActive(false);
}
}
private void HandleSendGift()
{
OpenTransferPopupAsync()
.Forget();
}
private async UniTask OpenTransferPopupAsync()
{
var activePresenter = tabsManager?.ActivePresenter;
string? selectedUrn = activePresenter?.SelectedUrn;
if (activePresenter == null || string.IsNullOrEmpty(selectedUrn))
return;
try
{
string itemType = activePresenter is WearableGridPresenter
? GiftingItemTypes.Wearable
: GiftingItemTypes.Emote;
if (!giftInventoryService.TryGetBestTransferableToken(new URN(selectedUrn), itemType, out string tokenId, out string instanceUrn))
{
ReportHub.LogError( ReportCategory.GIFTING, string.Format(CouldNotFindTokenForUrnLog, selectedUrn));
giftingErrorsController?.Show(MsgErrorNonTransferableNft);
return;
}
string giftDisplayName = activePresenter.GetItemNameByUrn(selectedUrn) ?? DefaultGiftItemName;
var giftThumb = activePresenter.GetThumbnailByUrn(selectedUrn);
if (!activePresenter.TryBuildStyleSnapshot(selectedUrn, out var style))
style = new GiftItemStyleSnapshot(null, null, Color.white);
var ct = lifeCts?.Token ?? CancellationToken.None;
var recipientProfile = await profileRepository.GetAsync(inputData.userAddress, ct);
if (ct.IsCancellationRequested) return;
var userNameColor = recipientProfile?.UserNameColor ?? Color.black;
string userNameColorHex = ColorUtility.ToHtmlStringRGB(userNameColor);
var transferParams = new GiftTransferParams(
inputData.userAddress,
inputData.userName,
headerPresenter?.CurrentRecipientAvatarSprite,
selectedUrn,
giftDisplayName,
giftThumb,
style,
itemType,
tokenId,
instanceUrn,
userNameColorHex
);
await mvcManager.ShowAsync(GiftTransferController.IssueCommand(transferParams), CancellationToken.None);
}
catch (OperationCanceledException)
{
// expected path when the popup / controller is disposed; no-op
}
catch (Exception e)
{
ReportHub.LogException(e, new ReportData(ReportCategory.GIFTING));
}
}
private void HandleSearchChanged(string searchText)
{
tabsManager?.ActivePresenter?.SetSearchText(searchText);
}
protected override UniTask WaitForCloseIntentAsync(CancellationToken ct)
{
if (viewInstance == null)
return UniTask.Never(ct);
return UniTask.WhenAny(
viewInstance!.CloseButton.OnClickAsync(ct),
viewInstance!.BackgroundButton.OnClickAsync(ct),
viewInstance!.FooterView.SendGiftButton.OnClickAsync(ct),
viewInstance!.FooterView.CancelButton.OnClickAsync(ct)
);
}
#endregion
public override void Dispose()
{
base.Dispose();
headerPresenter?.Dispose();
footerPresenter?.Dispose();
tabsManager?.Dispose();
}
}
}