-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathGiftTransferController.cs
More file actions
301 lines (250 loc) · 10.1 KB
/
GiftTransferController.cs
File metadata and controls
301 lines (250 loc) · 10.1 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using DCL.Backpack.Gifting.Presenters.GiftTransfer.Commands;
using DCL.Backpack.Gifting.Views;
using DCL.Browser;
using DCL.Diagnostics;
using DCL.Multiplayer.Connections.DecentralandUrls;
using DCL.UI.ConfirmationDialog.Opener;
using DCL.Utility;
using Global.AppArgs;
using MVC;
using UnityEngine;
using Utility;
using static DCL.Backpack.Gifting.Events.GiftingEvents;
namespace DCL.Backpack.Gifting.Presenters
{
public sealed class GiftTransferController
: ControllerBase<GiftTransferStatusView, GiftTransferParams>
{
private static readonly TimeSpan LONG_RUNNING_HINT_DELAY = TimeSpan.FromSeconds(10);
public override CanvasOrdering.SortingLayer Layer => CanvasOrdering.SortingLayer.POPUP;
private enum State { Waiting, Success, Failed }
private State currentState;
private readonly IEventBus eventBus;
private readonly IWebBrowser webBrowser;
private readonly IMVCManager mvcManager;
private readonly IDecentralandUrlsSource decentralandUrlsSource;
private readonly GiftTransferRequestCommand giftTransferRequestCommand;
private readonly IScreenModeController screenModeController;
private IDisposable? subProgress;
private CancellationTokenSource? lifeCts;
private CancellationTokenSource? delayCts;
public GiftTransferController(ViewFactoryMethod viewFactory,
IWebBrowser webBrowser,
IEventBus eventBus,
IMVCManager mvcManager,
IDecentralandUrlsSource decentralandUrlsSource,
GiftTransferRequestCommand giftTransferRequestCommand,
IScreenModeController screenModeController
)
: base(viewFactory)
{
this.webBrowser = webBrowser;
this.eventBus = eventBus;
this.mvcManager = mvcManager;
this.decentralandUrlsSource = decentralandUrlsSource;
this.giftTransferRequestCommand = giftTransferRequestCommand;
this.screenModeController = screenModeController;
}
protected override void OnViewShow()
{
lifeCts = new CancellationTokenSource();
if (viewInstance != null)
{
viewInstance.MarketplaceLink.OnLinkClicked += OnMarketplaceActivityLinkClicked;
viewInstance.RecipientName.text = string.Format(
GiftingTextIds.ColoredTextFormat,
inputData.userNameColorHex,
inputData.recipientName
);
if (inputData.userThumbnail != null)
viewInstance.RecipientAvatar.sprite = inputData.userThumbnail;
viewInstance.ItemName.text = inputData.giftDisplayName;
if (inputData.giftThumbnail != null)
viewInstance.ItemThumbnail.sprite = inputData.giftThumbnail;
viewInstance.ItemCategory.sprite = inputData.style.categoryIcon;
viewInstance.ItemCategoryBackground.color = inputData.style.flapColor;
viewInstance.ItemBackground.sprite = inputData.style.rarityBackground;
SetViewState(State.Waiting);
SetPhase(GiftTransferPhase.WaitingForWallet, GiftingTextIds.WaitingForWalletMessage);
}
subProgress = eventBus.Subscribe<GiftTransferProgress>(OnProgress);
ProcessTransferFlowAsync(lifeCts.Token)
.Forget();
}
protected override void OnViewClose()
{
if (viewInstance != null)
viewInstance.MarketplaceLink.OnLinkClicked -= OnMarketplaceActivityLinkClicked;
subProgress?.Dispose();
delayCts.SafeCancelAndDispose();
lifeCts.SafeCancelAndDispose();
}
protected override async UniTask WaitForCloseIntentAsync(CancellationToken ct)
{
await UniTask.WhenAny(viewInstance.BackButton.OnClickAsync(ct),
viewInstance.CloseButton.OnClickAsync(ct));
}
private async UniTask ProcessTransferFlowAsync(CancellationToken ct)
{
screenModeController.ApplyWindowedMode();
try
{
var result = await giftTransferRequestCommand.ExecuteAsync(inputData, ct);
ct.ThrowIfCancellationRequested();
if (result.IsSuccess)
OnSuccess();
else
OnFailure();
}
catch (OperationCanceledException)
{
// expected, ignore
}
catch (Exception e)
{
ReportHub.LogException(e, new ReportData(ReportCategory.GIFTING));
OnFailure();
}
finally
{
screenModeController.RestoreResolutionAndScreenMode();
}
}
private void OnMarketplaceActivityLinkClicked(string _)
{
LinkCallback(decentralandUrlsSource.Url(DecentralandUrl.MarketplaceLink));
}
private void OnProgress(GiftTransferProgress e)
{
SetPhase(e.Phase, e.Message);
if (e.Phase != GiftTransferPhase.Authorizing)
return;
if (lifeCts == null || lifeCts.IsCancellationRequested)
return;
StartDelayedStateTimer(lifeCts.Token);
}
private void StartDelayedStateTimer(CancellationToken ct)
{
delayCts = delayCts.SafeRestartLinked(ct);
ShowAfterDelayAsync(delayCts.Token)
.Forget();
}
private async UniTask ShowAfterDelayAsync(CancellationToken token)
{
try
{
// If the Web3 signature prompt takes too long,
// show a hint so users don't think the UI is frozen.
await UniTask.Delay(LONG_RUNNING_HINT_DELAY, cancellationToken: token);
if (viewInstance != null && viewInstance.LongRunningHint != null)
viewInstance.LongRunningHint.gameObject.SetActive(true);
}
catch (OperationCanceledException)
{
/* Expected */
}
}
private void OnSuccess()
{
currentState = State.Success;
delayCts.SafeCancelAndDispose();
RequestClose();
OpenSuccessScreenAfterCloseAsync(CancellationToken.None)
.Forget();
}
private void OnFailure()
{
currentState = State.Failed;
delayCts.SafeCancelAndDispose();
RequestClose();
ShowErrorPopupAsync(CancellationToken.None)
.Forget();
}
private async UniTask OpenSuccessScreenAfterCloseAsync(CancellationToken ct)
{
try
{
var successParams = new GiftTransferSuccessParams(inputData.recipientName,
inputData.userThumbnail,
inputData.userNameColorHex);
await mvcManager.ShowAsync(GiftTransferSuccessController.IssueCommand(successParams), ct);
}
catch (OperationCanceledException)
{
// ignore, user navigated away
}
catch (Exception e)
{
ReportHub.LogException(e, new ReportData(ReportCategory.GIFTING));
}
}
private void SetViewState(State newState)
{
currentState = newState;
if (viewInstance == null) return;
switch (newState)
{
case State.Waiting:
viewInstance.TitleLabel.text = GiftingTextIds.PreparingGiftTitle;
viewInstance.StatusContainer.SetActive(true);
if (viewInstance.LongRunningHint != null)
viewInstance.LongRunningHint.gameObject.SetActive(false);
break;
}
}
private void SetPhase(GiftTransferPhase phase, string? msg)
{
if (viewInstance == null || currentState != State.Waiting) return;
viewInstance.StatusText.text = msg ?? GiftingTextIds.DefaultStatusMessage;
}
private void RequestClose()
{
viewInstance?.CloseButton.onClick.Invoke();
}
private async UniTask ShowErrorPopupAsync(CancellationToken ct)
{
try
{
string supportUrl = decentralandUrlsSource.Url(DecentralandUrl.Support);
string supportLink = string.Format(GiftingTextIds.ErrorDialogSupportLinkFormat, supportUrl);
var dialogParams = new ConfirmationDialogParameter(
GiftingTextIds.ErrorDialogTitle,
GiftingTextIds.ErrorDialogCancelText,
GiftingTextIds.ErrorDialogConfirmText,
viewInstance?.WarningIcon,
false,
false,
null,
GiftingTextIds.ErrorDialogDescription,
linkText: supportLink,
onLinkClickCallback: LinkCallback,
preserveAspect: true
);
var result = await ViewDependencies
.ConfirmationDialogOpener
.OpenConfirmationDialogAsync(dialogParams, ct);
if (result == ConfirmationResult.CONFIRM)
{
ReportHub.Log(ReportCategory.GIFTING, GiftingTextIds.RetryLogMessage);
await mvcManager.ShowAsync(IssueCommand(inputData), ct);
}
}
catch (OperationCanceledException)
{
// dialog closed or navigation away
}
catch (Exception e)
{
ReportHub.LogException(e, new ReportData(ReportCategory.GIFTING));
}
}
private void LinkCallback(string url)
{
webBrowser.OpenUrl(url);
}
}
}