| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Browser; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Interface; |
| | 6 | | using DCL.Tasks; |
| | 7 | | using DCLServices.SubscriptionsAPIService; |
| | 8 | | using System; |
| | 9 | | using System.Threading; |
| | 10 | | using UnityEngine; |
| | 11 | |
|
| | 12 | | namespace SignupHUD |
| | 13 | | { |
| | 14 | | public class SignupHUDController : IHUD |
| | 15 | | { |
| | 16 | | private const string TOS_URL = "https://decentraland.org/terms/"; |
| | 17 | | private const string PRIVACY_POLICY_URL = "https://decentraland.org/privacy/"; |
| | 18 | | private const string NEW_TOS_AND_EMAIL_SUBSCRIPTION_FF = "new_terms_of_service_and_email_subscription"; |
| | 19 | | private const string CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY = "currentSubscriptionId"; |
| | 20 | |
|
| | 21 | | private readonly NewUserExperienceAnalytics newUserExperienceAnalytics; |
| | 22 | | private readonly DataStore_LoadingScreen loadingScreenDataStore; |
| | 23 | | private readonly DataStore_HUDs dataStoreHUDs; |
| | 24 | | private readonly DataStore_FeatureFlag dataStoreFeatureFlag; |
| | 25 | | private readonly DataStore_BackpackV2 dataStoreBackpack; |
| | 26 | | private readonly DataStore_Common dataStoreCommon; |
| | 27 | | private readonly IBrowserBridge browserBridge; |
| | 28 | | private readonly ISubscriptionsAPIService subscriptionsAPIService; |
| | 29 | | internal readonly ISignupHUDView view; |
| | 30 | |
|
| | 31 | | internal string name; |
| | 32 | | internal string email; |
| 31 | 33 | | private BaseVariable<bool> signupVisible => dataStoreHUDs.signupVisible; |
| 0 | 34 | | private BaseVariable<bool> backpackVisible => dataStoreHUDs.avatarEditorVisible; |
| 1 | 35 | | private bool isNewTermsOfServiceAndEmailSubscriptionEnabled => dataStoreFeatureFlag.flags.Get().IsFeatureEnabled |
| | 36 | |
|
| | 37 | | private CancellationTokenSource createSubscriptionCts; |
| | 38 | |
|
| 10 | 39 | | public SignupHUDController( |
| | 40 | | IAnalytics analytics, |
| | 41 | | ISignupHUDView view, |
| | 42 | | DataStore_LoadingScreen loadingScreenDataStore, |
| | 43 | | DataStore_HUDs dataStoreHUDs, |
| | 44 | | DataStore_FeatureFlag dataStoreFeatureFlag, |
| | 45 | | DataStore_BackpackV2 dataStoreBackpack, |
| | 46 | | DataStore_Common dataStoreCommon, |
| | 47 | | IBrowserBridge browserBridge, |
| | 48 | | ISubscriptionsAPIService subscriptionsAPIService) |
| | 49 | | { |
| 10 | 50 | | newUserExperienceAnalytics = new NewUserExperienceAnalytics(analytics); |
| 10 | 51 | | this.view = view; |
| 10 | 52 | | this.loadingScreenDataStore = loadingScreenDataStore; |
| 10 | 53 | | this.dataStoreHUDs = dataStoreHUDs; |
| 10 | 54 | | this.dataStoreFeatureFlag = dataStoreFeatureFlag; |
| 10 | 55 | | this.dataStoreBackpack = dataStoreBackpack; |
| 10 | 56 | | this.dataStoreCommon = dataStoreCommon; |
| 10 | 57 | | this.browserBridge = browserBridge; |
| 10 | 58 | | this.subscriptionsAPIService = subscriptionsAPIService; |
| 10 | 59 | | loadingScreenDataStore.decoupledLoadingHUD.visible.OnChange += OnLoadingScreenAppear; |
| 10 | 60 | | dataStoreBackpack.isWaitingToBeSavedAfterSignUp.OnChange += OnTermsOfServiceAgreedStepAfterSaveBackpack; |
| 10 | 61 | | } |
| | 62 | |
|
| | 63 | | public void Initialize() |
| | 64 | | { |
| 10 | 65 | | if (view == null) |
| 0 | 66 | | return; |
| | 67 | |
|
| 10 | 68 | | signupVisible.OnChange += OnSignupVisibleChanged; |
| | 69 | |
|
| 10 | 70 | | view.OnNameScreenNext += OnNameScreenNext; |
| 10 | 71 | | view.OnEditAvatar += OnEditAvatar; |
| 10 | 72 | | view.OnTermsOfServiceAgreed += OnTermsOfServiceAgreedStepBeforeSaveBackpack; |
| 10 | 73 | | view.OnTermsOfServiceBack += OnTermsOfServiceBack; |
| 10 | 74 | | view.OnLinkClicked += OnLinkClicked; |
| | 75 | |
|
| 10 | 76 | | CommonScriptableObjects.isLoadingHUDOpen.OnChange += OnLoadingScreenAppear; |
| 10 | 77 | | signupVisible.Set(signupVisible.Get(), true); |
| 10 | 78 | | } |
| | 79 | |
|
| | 80 | | private void OnLoadingScreenAppear(bool current, bool previous) |
| | 81 | | { |
| 0 | 82 | | if (signupVisible.Get() && current) |
| | 83 | | { |
| 0 | 84 | | signupVisible.Set(false); |
| 0 | 85 | | backpackVisible.Set(false); |
| | 86 | | } |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private void OnSignupVisibleChanged(bool current, bool previous) |
| | 90 | | { |
| 12 | 91 | | SetVisibility(current); |
| 12 | 92 | | } |
| | 93 | |
|
| | 94 | | internal void StartSignupProcess() |
| | 95 | | { |
| 3 | 96 | | name = null; |
| 3 | 97 | | email = null; |
| 3 | 98 | | view?.ShowNameScreen(); |
| 3 | 99 | | } |
| | 100 | |
|
| | 101 | | internal void OnNameScreenNext(string newName, string newEmail) |
| | 102 | | { |
| 1 | 103 | | name = newName; |
| 1 | 104 | | email = newEmail; |
| 1 | 105 | | view?.ShowTermsOfServiceScreen(); |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | internal void OnEditAvatar() |
| | 109 | | { |
| 1 | 110 | | signupVisible.Set(false); |
| 1 | 111 | | dataStoreHUDs.avatarEditorVisible.Set(true, true); |
| 1 | 112 | | } |
| | 113 | |
|
| | 114 | | private void OnTermsOfServiceAgreedStepBeforeSaveBackpack() |
| | 115 | | { |
| 1 | 116 | | WebInterface.SendPassport(name, email); |
| 1 | 117 | | dataStoreBackpack.isWaitingToBeSavedAfterSignUp.Set(true); |
| | 118 | |
|
| 1 | 119 | | newUserExperienceAnalytics?.SendTermsOfServiceAcceptedNux(name, email); |
| | 120 | |
|
| 1 | 121 | | if (!isNewTermsOfServiceAndEmailSubscriptionEnabled) |
| 1 | 122 | | return; |
| | 123 | |
|
| 0 | 124 | | createSubscriptionCts = createSubscriptionCts.SafeRestart(); |
| 0 | 125 | | CreateSubscriptionAsync(email, createSubscriptionCts.Token).Forget(); |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private void OnTermsOfServiceAgreedStepAfterSaveBackpack(bool isBackpackWaitingToBeSaved, bool _) |
| | 129 | | { |
| 1 | 130 | | if (isBackpackWaitingToBeSaved) |
| 1 | 131 | | return; |
| | 132 | |
|
| 0 | 133 | | dataStoreCommon.isSignUpFlow.Set(false); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | private async UniTaskVoid CreateSubscriptionAsync(string emailAddress, CancellationToken cancellationToken) |
| | 137 | | { |
| | 138 | | try |
| | 139 | | { |
| 0 | 140 | | var newSubscription = await subscriptionsAPIService.CreateSubscription(emailAddress, cancellationToken); |
| 0 | 141 | | PlayerPrefsBridge.SetString(CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY, newSubscription.id); |
| 0 | 142 | | PlayerPrefsBridge.Save(); |
| 0 | 143 | | } |
| 0 | 144 | | catch (Exception ex) { Debug.LogError($"An error occurred while creating the subscription for {emailAddress} |
| 0 | 145 | | } |
| | 146 | |
|
| 2 | 147 | | internal void OnTermsOfServiceBack() { StartSignupProcess(); } |
| | 148 | |
|
| | 149 | | private void OnLinkClicked(string linkId) |
| | 150 | | { |
| | 151 | | switch (linkId) |
| | 152 | | { |
| | 153 | | case "tosUrl": |
| 1 | 154 | | browserBridge.OpenUrl(TOS_URL); |
| 1 | 155 | | break; |
| | 156 | | case "privacyPolicyUrl": |
| 1 | 157 | | browserBridge.OpenUrl(PRIVACY_POLICY_URL); |
| | 158 | | break; |
| | 159 | | } |
| 1 | 160 | | } |
| | 161 | |
|
| | 162 | | public void SetVisibility(bool visible) |
| | 163 | | { |
| 12 | 164 | | view?.SetVisibility(visible); |
| 12 | 165 | | if (visible) |
| 1 | 166 | | StartSignupProcess(); |
| 12 | 167 | | } |
| | 168 | |
|
| | 169 | | public void Dispose() |
| | 170 | | { |
| 0 | 171 | | signupVisible.OnChange -= OnSignupVisibleChanged; |
| 0 | 172 | | if (view == null) |
| 0 | 173 | | return; |
| 0 | 174 | | view.OnNameScreenNext -= OnNameScreenNext; |
| 0 | 175 | | view.OnEditAvatar -= OnEditAvatar; |
| 0 | 176 | | view.OnTermsOfServiceAgreed -= OnTermsOfServiceAgreedStepBeforeSaveBackpack; |
| 0 | 177 | | view.OnTermsOfServiceBack -= OnTermsOfServiceBack; |
| 0 | 178 | | CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= OnLoadingScreenAppear; |
| 0 | 179 | | dataStoreBackpack.isWaitingToBeSavedAfterSignUp.OnChange -= OnTermsOfServiceAgreedStepAfterSaveBackpack; |
| 0 | 180 | | loadingScreenDataStore.decoupledLoadingHUD.visible.OnChange -= OnLoadingScreenAppear; |
| 0 | 181 | | createSubscriptionCts.SafeCancelAndDispose(); |
| 0 | 182 | | view.Dispose(); |
| 0 | 183 | | } |
| | 184 | | } |
| | 185 | | } |