-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCommunityCardController.cs
More file actions
441 lines (378 loc) · 20 KB
/
Copy pathCommunityCardController.cs
File metadata and controls
441 lines (378 loc) · 20 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
using Cysharp.Threading.Tasks;
using DCL.Browser;
using DCL.Chat.ControllerShowParams;
using DCL.Chat.EventBus;
using DCL.Clipboard;
using DCL.Communities.CommunitiesCard.Events;
using DCL.Communities.CommunitiesCard.Members;
using DCL.Communities.CommunityCreation;
using DCL.Communities.CommunitiesCard.Places;
using DCL.Diagnostics;
using DCL.EventsApi;
using DCL.Friends;
using DCL.InWorldCamera;
using DCL.InWorldCamera.CameraReelGallery;
using DCL.InWorldCamera.CameraReelStorageService;
using DCL.InWorldCamera.CameraReelStorageService.Schemas;
using DCL.InWorldCamera.PhotoDetail;
using DCL.Multiplayer.Connections.DecentralandUrls;
using DCL.PlacesAPIService;
using DCL.Profiles;
using DCL.UI;
using DCL.UI.Profiles.Helpers;
using DCL.UI.SharedSpaceManager;
using DCL.Utilities;
using DCL.Utilities.Extensions;
using DCL.Web3.Identities;
using DCL.WebRequests;
using ECS.SceneLifeCycle.Realm;
using MVC;
using System;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using Utility;
using Utility.Types;
namespace DCL.Communities.CommunitiesCard
{
public class CommunityCardController : ControllerBase<CommunityCardView, CommunityCardParameter>
{
private static readonly int BG_SHADER_COLOR_1 = Shader.PropertyToID("_Color1");
private const string JOIN_COMMUNITY_ERROR_TEXT = "There was an error joining the community. Please try again.";
private const string DELETE_COMMUNITY_ERROR_TEXT = "There was an error deleting the community. Please try again.";
private const string LEAVE_COMMUNITY_ERROR_TEXT = "There was an error leaving the community. Please try again.";
private const int WARNING_NOTIFICATION_DURATION_MS = 3000;
public override CanvasOrdering.SortingLayer Layer => CanvasOrdering.SortingLayer.Popup;
private readonly IMVCManager mvcManager;
private readonly ICameraReelStorageService cameraReelStorageService;
private readonly ICameraReelScreenshotsStorage cameraReelScreenshotsStorage;
private readonly ObjectProxy<IFriendsService> friendServiceProxy;
private readonly CommunitiesDataProvider communitiesDataProvider;
private readonly IWebRequestController webRequestController;
private readonly ProfileRepositoryWrapper profileRepositoryWrapper;
private readonly IPlacesAPIService placesAPIService;
private readonly IRealmNavigator realmNavigator;
private readonly ISystemClipboard clipboard;
private readonly IWebBrowser webBrowser;
private readonly IEventsApiService eventsApiService;
private readonly ISharedSpaceManager sharedSpaceManager;
private readonly IChatEventBus chatEventBus;
private readonly IDecentralandUrlsSource decentralandUrlsSource;
private readonly IWeb3IdentityCache web3IdentityCache;
private readonly LambdasProfilesProvider lambdasProfilesProvider;
private readonly GalleryEventBus galleryEventBus;
private CameraReelGalleryController? cameraReelGalleryController;
private MembersListController? membersListController;
private PlacesSectionController? placesSectionController;
private EventListController? eventListController;
private CancellationTokenSource sectionCancellationTokenSource = new ();
private CancellationTokenSource panelCancellationTokenSource = new ();
private CancellationTokenSource communityOperationsCancellationTokenSource = new ();
private UniTaskCompletionSource closeIntentCompletionSource = new ();
private ISpriteCache? spriteCache;
private bool isSpriteCacheExternal;
private readonly ThumbnailLoader thumbnailLoader;
private GetCommunityResponse.CommunityData communityData;
private string[] communityPlaceIds;
public CommunityCardController(ViewFactoryMethod viewFactory,
IMVCManager mvcManager,
ICameraReelStorageService cameraReelStorageService,
ICameraReelScreenshotsStorage cameraReelScreenshotsStorage,
ObjectProxy<IFriendsService> friendServiceProxy,
CommunitiesDataProvider communitiesDataProvider,
IWebRequestController webRequestController,
ProfileRepositoryWrapper profileDataProvider,
IPlacesAPIService placesAPIService,
IRealmNavigator realmNavigator,
ISystemClipboard clipboard,
IWebBrowser webBrowser,
IEventsApiService eventsApiService,
ISharedSpaceManager sharedSpaceManager,
IChatEventBus chatEventBus,
IDecentralandUrlsSource decentralandUrlsSource,
IWeb3IdentityCache web3IdentityCache,
LambdasProfilesProvider lambdasProfilesProvider,
GalleryEventBus galleryEventBus)
: base(viewFactory)
{
this.mvcManager = mvcManager;
this.cameraReelStorageService = cameraReelStorageService;
this.cameraReelScreenshotsStorage = cameraReelScreenshotsStorage;
this.friendServiceProxy = friendServiceProxy;
this.communitiesDataProvider = communitiesDataProvider;
this.webRequestController = webRequestController;
this.profileRepositoryWrapper = profileDataProvider;
this.placesAPIService = placesAPIService;
this.realmNavigator = realmNavigator;
this.clipboard = clipboard;
this.webBrowser = webBrowser;
this.eventsApiService = eventsApiService;
this.sharedSpaceManager = sharedSpaceManager;
this.chatEventBus = chatEventBus;
this.decentralandUrlsSource = decentralandUrlsSource;
this.web3IdentityCache = web3IdentityCache;
this.lambdasProfilesProvider = lambdasProfilesProvider;
this.galleryEventBus = galleryEventBus;
this.thumbnailLoader = new ThumbnailLoader(null);
chatEventBus.OpenPrivateConversationRequested += CloseCardOnConversationRequested;
communitiesDataProvider.CommunityUpdated += OnCommunityUpdated;
communitiesDataProvider.CommunityUserRemoved += OnCommunityUserRemoved;
communitiesDataProvider.CommunityLeft += OnCommunityLeft;
communitiesDataProvider.CommunityUserBanned += OnUserBannedFromCommunity;
}
public override void Dispose()
{
if (viewInstance != null)
{
viewInstance.SectionChanged -= OnSectionChanged;
viewInstance.OpenWizardRequested -= OnOpenCommunityWizard;
viewInstance.OpenChatRequested -= OnOpenCommunityChatAsync;
viewInstance.JoinCommunity -= JoinCommunity;
viewInstance.LeaveCommunityRequested -= LeaveCommunityRequested;
viewInstance.DeleteCommunityRequested -= OnDeleteCommunityRequested;
viewInstance.CameraReelGalleryConfigs.PhotosView.OpenWizardButtonClicked -= OnOpenCommunityWizard;
}
chatEventBus.OpenPrivateConversationRequested -= CloseCardOnConversationRequested;
communitiesDataProvider.CommunityUpdated -= OnCommunityUpdated;
communitiesDataProvider.CommunityUserRemoved -= OnCommunityUserRemoved;
communitiesDataProvider.CommunityLeft -= OnCommunityLeft;
communitiesDataProvider.CommunityUserBanned -= OnUserBannedFromCommunity;
sectionCancellationTokenSource.SafeCancelAndDispose();
panelCancellationTokenSource.SafeCancelAndDispose();
communityOperationsCancellationTokenSource.SafeCancelAndDispose();
if (cameraReelGalleryController != null)
cameraReelGalleryController.ThumbnailClicked -= OnThumbnailClicked;
cameraReelGalleryController?.Dispose();
membersListController?.Dispose();
placesSectionController?.Dispose();
eventListController?.Dispose();
}
private void OnUserBannedFromCommunity(string communityId, string userAddress) =>
OnCommunityUserRemoved(communityId);
private void OnCommunityLeft(string communityId, bool success)
{
if (success)
OnCommunityUserRemoved(communityId);
}
private void OnCommunityUserRemoved(string communityId)
{
if (communityData.id != communityId) return;
communityData.DecreaseMembersCount();
viewInstance?.UpdateMemberCount(communityData);
}
private void OnCommunityUpdated(string communityId)
{
if (!communityId.Equals(communityData.id)) return;
ResetSubControllers();
SetDefaultsAndLoadData();
}
private void CloseCardOnConversationRequested(string _) =>
CloseController();
private void OnDeleteCommunityRequested()
{
communityOperationsCancellationTokenSource = communityOperationsCancellationTokenSource.SafeRestart();
DeleteCommunityAsync(communityOperationsCancellationTokenSource.Token).Forget();
return;
async UniTaskVoid DeleteCommunityAsync(CancellationToken ct)
{
Result<bool> result = await communitiesDataProvider.DeleteCommunityAsync(communityData.id, ct)
.SuppressToResultAsync(ReportCategory.COMMUNITIES);
if (ct.IsCancellationRequested)
return;
if (!result.Success || !result.Value)
{
await viewInstance!.warningNotificationView.AnimatedShowAsync(DELETE_COMMUNITY_ERROR_TEXT, WARNING_NOTIFICATION_DURATION_MS, ct)
.SuppressToResultAsync(ReportCategory.COMMUNITIES);
return;
}
CloseController();
}
}
private void CloseController() =>
closeIntentCompletionSource.TrySetResult();
private async void OnOpenCommunityChatAsync()
{
try
{
await sharedSpaceManager.ShowAsync(PanelsSharingSpace.Chat, new ChatControllerShowParams(true, true));
chatEventBus.OpenCommunityConversationUsingUserId(communityData.id);
CloseController();
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
ReportHub.LogException(ex, ReportCategory.COMMUNITIES);
}
}
protected override void OnViewInstantiated()
{
viewInstance!.SectionChanged += OnSectionChanged;
viewInstance.OpenWizardRequested += OnOpenCommunityWizard;
viewInstance.OpenChatRequested += OnOpenCommunityChatAsync;
viewInstance.JoinCommunity += JoinCommunity;
viewInstance.LeaveCommunityRequested += LeaveCommunityRequested;
viewInstance.DeleteCommunityRequested += OnDeleteCommunityRequested;
viewInstance.CameraReelGalleryConfigs.PhotosView.OpenWizardButtonClicked += OnOpenCommunityWizard;
cameraReelGalleryController = new CameraReelGalleryController(viewInstance.CameraReelGalleryConfigs.PhotosView.GalleryView, cameraReelStorageService, cameraReelScreenshotsStorage,
new ReelGalleryConfigParams(viewInstance.CameraReelGalleryConfigs.GridLayoutFixedColumnCount, viewInstance.CameraReelGalleryConfigs.ThumbnailHeight,
viewInstance.CameraReelGalleryConfigs.ThumbnailWidth, false, false), false);
cameraReelGalleryController.ThumbnailClicked += OnThumbnailClicked;
membersListController = new MembersListController(viewInstance.MembersListView,
profileRepositoryWrapper,
mvcManager,
friendServiceProxy,
communitiesDataProvider,
viewInstance.warningNotificationView,
sharedSpaceManager,
chatEventBus,
web3IdentityCache);
placesSectionController = new PlacesSectionController(viewInstance.PlacesSectionView,
thumbnailLoader,
communitiesDataProvider,
placesAPIService,
viewInstance.warningNotificationView,
viewInstance.successNotificationView,
realmNavigator,
mvcManager,
clipboard,
webBrowser,
lambdasProfilesProvider);
eventListController = new EventListController(viewInstance.EventListView,
eventsApiService,
placesAPIService,
thumbnailLoader,
mvcManager,
viewInstance.warningNotificationView,
viewInstance.successNotificationView,
clipboard,
webBrowser,
realmNavigator,
decentralandUrlsSource);
viewInstance.SetCardBackgroundColor(viewInstance.BackgroundColor, BG_SHADER_COLOR_1);
}
protected override void OnViewShow() =>
SetDefaultsAndLoadData();
private void SetDefaultsAndLoadData()
{
panelCancellationTokenSource = panelCancellationTokenSource.SafeRestart();
closeIntentCompletionSource = new UniTaskCompletionSource();
viewInstance!.SetDefaults();
viewInstance.MembersListView.SetSectionButtonsActive(false);
LoadCommunityDataAsync(panelCancellationTokenSource.Token).Forget();
return;
async UniTaskVoid LoadCommunityDataAsync(CancellationToken ct)
{
viewInstance!.SetLoadingState(true);
//Since it's the tab that is automatically selected when the community card is opened, we set it to loading.
viewInstance.MembersListView.SetLoadingStateActive(true);
if (spriteCache == null)
{
isSpriteCacheExternal = inputData.ThumbnailSpriteCache != null;
spriteCache = isSpriteCacheExternal ? inputData.ThumbnailSpriteCache! : new SpriteCache(webRequestController);
thumbnailLoader.Cache = spriteCache;
}
GetCommunityResponse response = await communitiesDataProvider.GetCommunityAsync(inputData.CommunityId, ct);
communityPlaceIds = (await communitiesDataProvider.GetCommunityPlacesAsync(inputData.CommunityId, ct)).ToArray();
communityData = response.data;
viewInstance.SetLoadingState(false);
viewInstance.SetPanelCancellationToken(ct);
viewInstance.ConfigureCommunity(communityData, thumbnailLoader);
viewInstance.ResetToggle(true);
eventListController?.ShowEvents(communityData, communityPlaceIds, ct);
}
}
protected override void OnViewClose()
{
sectionCancellationTokenSource.SafeCancelAndDispose();
panelCancellationTokenSource.SafeCancelAndDispose();
communityOperationsCancellationTokenSource.SafeCancelAndDispose();
spriteCache = null;
ResetSubControllers();
viewInstance!.ResetToggle(false);
}
private void ResetSubControllers()
{
membersListController?.Reset();
placesSectionController?.Reset();
eventListController?.Reset();
}
private void OnThumbnailClicked(List<CameraReelResponseCompact> reels, int index,
Action<CameraReelResponseCompact> reelDeleteIntention, Action<CameraReelResponseCompact> reelListRefreshIntention) =>
mvcManager.ShowAsync(PhotoDetailController.IssueCommand(new PhotoDetailParameter(reels, index,
true, PhotoDetailParameter.CallerContext.CommunityCard, reelDeleteIntention,
reelListRefreshIntention, galleryEventBus)));
private void OnSectionChanged(CommunityCardView.Sections section)
{
sectionCancellationTokenSource = sectionCancellationTokenSource.SafeRestart();
switch (section)
{
case CommunityCardView.Sections.PHOTOS:
viewInstance!.CameraReelGalleryConfigs.PhotosView.SetAdminEmptyTextActive(communityData.role is CommunityMemberRole.moderator or CommunityMemberRole.owner);
cameraReelGalleryController!.ShowPlacesGalleryAsync(communityPlaceIds, sectionCancellationTokenSource.Token).Forget();
break;
case CommunityCardView.Sections.MEMBERS:
membersListController!.ShowMembersList(communityData, sectionCancellationTokenSource.Token);
break;
case CommunityCardView.Sections.PLACES:
placesSectionController!.ShowPlaces(communityData, communityPlaceIds, sectionCancellationTokenSource.Token);
break;
}
}
private void OnOpenCommunityWizard()
{
mvcManager.ShowAsync(
CommunityCreationEditionController.IssueCommand(new CommunityCreationEditionParameter(
canCreateCommunities: true,
communityId: communityData.id,
spriteCache!)));
}
private void JoinCommunity()
{
communityOperationsCancellationTokenSource = communityOperationsCancellationTokenSource.SafeRestart();
JoinCommunityAsync(communityOperationsCancellationTokenSource.Token).Forget();
return;
async UniTaskVoid JoinCommunityAsync(CancellationToken ct)
{
Result<bool> result = await communitiesDataProvider.JoinCommunityAsync(communityData.id, ct)
.SuppressToResultAsync(ReportCategory.COMMUNITIES);
if (ct.IsCancellationRequested)
return;
if (!result.Success || !result.Value)
{
await viewInstance!.warningNotificationView.AnimatedShowAsync(JOIN_COMMUNITY_ERROR_TEXT, WARNING_NOTIFICATION_DURATION_MS, ct)
.SuppressToResultAsync(ReportCategory.COMMUNITIES);
return;
}
communityData.IncreaseMembersCount();
viewInstance!.UpdateMemberCount(communityData);
//Reset member list and fetch the data again so that we pop inside the member list
membersListController?.Reset();
membersListController?.ShowMembersList(communityData, sectionCancellationTokenSource.Token);
viewInstance.ConfigureInteractionButtons(CommunityMemberRole.member);
}
}
private void LeaveCommunityRequested()
{
communityOperationsCancellationTokenSource = communityOperationsCancellationTokenSource.SafeRestart();
LeaveCommunityAsync(communityOperationsCancellationTokenSource.Token).Forget();
return;
async UniTaskVoid LeaveCommunityAsync(CancellationToken ct)
{
Result<bool> result = await communitiesDataProvider.LeaveCommunityAsync(communityData.id, ct)
.SuppressToResultAsync(ReportCategory.COMMUNITIES);
if (ct.IsCancellationRequested)
return;
if (!result.Success || !result.Value)
{
await viewInstance!.warningNotificationView.AnimatedShowAsync(LEAVE_COMMUNITY_ERROR_TEXT, WARNING_NOTIFICATION_DURATION_MS, ct)
.SuppressToResultAsync(ReportCategory.COMMUNITIES);
return;
}
membersListController?.TryRemoveLocalUser();
viewInstance!.ConfigureInteractionButtons(CommunityMemberRole.none);
}
}
protected override UniTask WaitForCloseIntentAsync(CancellationToken ct) =>
UniTask.WhenAny(viewInstance!.GetClosingTasks(closeIntentCompletionSource.Task, ct));
}
}