| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using DCL.World.PortableExperiences; |
| | 4 | | using DCLServices.PortableExperiences.Analytics; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.PortableExperiences.Confirmation |
| | 11 | | { |
| | 12 | | public class ExperiencesConfirmationPopupController |
| | 13 | | { |
| | 14 | | private enum InputType |
| | 15 | | { |
| | 16 | | Accept, |
| | 17 | | Reject, |
| | 18 | | Cancel, |
| | 19 | | } |
| | 20 | |
|
| | 21 | | private readonly IExperiencesConfirmationPopupView view; |
| | 22 | | private readonly DataStore dataStore; |
| | 23 | | private readonly IConfirmedExperiencesRepository confirmedExperiencesRepository; |
| | 24 | | private readonly IUserProfileBridge userProfileBridge; |
| | 25 | | private readonly IPortableExperiencesAnalyticsService analytics; |
| 12 | 26 | | private readonly List<string> descriptionBuffer = new (); |
| | 27 | |
|
| | 28 | | private string experienceId; |
| | 29 | | private bool dontShowAnymore; |
| 12 | 30 | | private CancellationTokenSource openProcessCancellationToken = new (); |
| 12 | 31 | | private UniTaskCompletionSource<InputType> userInputTask = new (); |
| | 32 | |
|
| 12 | 33 | | public ExperiencesConfirmationPopupController(IExperiencesConfirmationPopupView view, |
| | 34 | | DataStore dataStore, |
| | 35 | | IConfirmedExperiencesRepository confirmedExperiencesRepository, |
| | 36 | | IUserProfileBridge userProfileBridge, |
| | 37 | | IPortableExperiencesAnalyticsService analytics) |
| | 38 | | { |
| 12 | 39 | | this.view = view; |
| 12 | 40 | | this.dataStore = dataStore; |
| 12 | 41 | | this.confirmedExperiencesRepository = confirmedExperiencesRepository; |
| 12 | 42 | | this.userProfileBridge = userProfileBridge; |
| 12 | 43 | | this.analytics = analytics; |
| | 44 | |
|
| 12 | 45 | | view.Hide(true); |
| | 46 | |
|
| 14 | 47 | | view.OnAccepted += () => userInputTask.TrySetResult(InputType.Accept); |
| 14 | 48 | | view.OnRejected += () => userInputTask.TrySetResult(InputType.Reject); |
| 13 | 49 | | view.OnCancelled += () => userInputTask.TrySetResult(InputType.Cancel); |
| 15 | 50 | | view.OnDontShowAnymore += () => dontShowAnymore = true; |
| 14 | 51 | | view.OnKeepShowing += () => dontShowAnymore = false; |
| | 52 | |
|
| 12 | 53 | | dataStore.world.portableExperiencePendingToConfirm.OnChange += OnConfirmRequested; |
| 12 | 54 | | } |
| | 55 | |
|
| | 56 | | public void Dispose() |
| | 57 | | { |
| 0 | 58 | | view.Dispose(); |
| 0 | 59 | | openProcessCancellationToken.SafeCancelAndDispose(); |
| | 60 | |
|
| 0 | 61 | | dataStore.world.portableExperiencePendingToConfirm.OnChange -= OnConfirmRequested; |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | private void OnConfirmRequested(ExperiencesConfirmationData current, ExperiencesConfirmationData previous) |
| | 65 | | { |
| | 66 | | async UniTaskVoid ShowAndWaitForUserInputThenHide( |
| | 67 | | ExperiencesConfirmationData confirmationData, |
| | 68 | | CancellationToken cancellationToken) |
| | 69 | | { |
| 78 | 70 | | string pxId = confirmationData.Experience.ExperienceId; |
| | 71 | |
|
| 78 | 72 | | ExperiencesConfirmationData.ExperienceMetadata metadata = confirmationData.Experience; |
| | 73 | |
|
| 78 | 74 | | experienceId = pxId; |
| | 75 | |
|
| 78 | 76 | | descriptionBuffer.Clear(); |
| | 77 | |
|
| 78 | 78 | | if (metadata.Permissions != null) |
| | 79 | | { |
| 272 | 80 | | foreach (string permission in metadata.Permissions) |
| 77 | 81 | | descriptionBuffer.Add(permission); |
| | 82 | | } |
| | 83 | |
|
| 78 | 84 | | view.Show(); |
| | 85 | |
|
| 78 | 86 | | bool isSmartWearable = userProfileBridge.GetOwn().avatar.wearables.Contains(pxId); |
| | 87 | |
|
| 78 | 88 | | view.SetModel(new ExperiencesConfirmationViewModel |
| | 89 | | { |
| | 90 | | Name = metadata.ExperienceName, |
| | 91 | | IconUrl = metadata.IconUrl, |
| | 92 | | Permissions = descriptionBuffer, |
| | 93 | | Description = metadata.Description, |
| | 94 | | IsSmartWearable = isSmartWearable, |
| | 95 | | }); |
| | 96 | |
|
| | 97 | | try |
| | 98 | | { |
| 222 | 99 | | InputType inputType = await userInputTask.Task.AttachExternalCancellation(cancellationToken); |
| | 100 | |
|
| | 101 | | switch (inputType) |
| | 102 | | { |
| | 103 | | case InputType.Accept: |
| 2 | 104 | | if (dontShowAnymore) |
| 1 | 105 | | confirmedExperiencesRepository.Set(experienceId, true); |
| | 106 | |
|
| 2 | 107 | | analytics.Accept(experienceId, dontShowAnymore, isSmartWearable ? "smart_wearable" : "scene" |
| | 108 | |
|
| 2 | 109 | | confirmationData.OnAcceptCallback?.Invoke(); |
| 2 | 110 | | break; |
| | 111 | | case InputType.Reject: |
| 2 | 112 | | if (dontShowAnymore) |
| 1 | 113 | | confirmedExperiencesRepository.Set(experienceId, false); |
| | 114 | |
|
| 2 | 115 | | analytics.Reject(experienceId, dontShowAnymore, isSmartWearable ? "smart_wearable" : "scene" |
| | 116 | |
|
| 2 | 117 | | confirmationData.OnRejectCallback?.Invoke(); |
| 2 | 118 | | break; |
| | 119 | | case InputType.Cancel: |
| 1 | 120 | | confirmationData.OnRejectCallback?.Invoke(); |
| | 121 | | break; |
| | 122 | | } |
| | 123 | |
|
| 5 | 124 | | view.Hide(); |
| 5 | 125 | | } |
| 122 | 126 | | catch (OperationCanceledException) { } |
| 0 | 127 | | catch (Exception e) { Debug.LogException(e); } |
| 66 | 128 | | } |
| | 129 | |
|
| 78 | 130 | | openProcessCancellationToken = openProcessCancellationToken.SafeRestart(); |
| 78 | 131 | | openProcessCancellationToken = new CancellationTokenSource(); |
| 78 | 132 | | userInputTask.TrySetCanceled(); |
| 78 | 133 | | userInputTask = new UniTaskCompletionSource<InputType>(); |
| 78 | 134 | | ShowAndWaitForUserInputThenHide(current, openProcessCancellationToken.Token).Forget(); |
| 78 | 135 | | } |
| | 136 | | } |
| | 137 | | } |