| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using DCLServices.SubscriptionsAPIService; |
| | 5 | | using SocialFeaturesAnalytics; |
| | 6 | | using System; |
| | 7 | | using System.Net.Mail; |
| | 8 | | using System.Threading; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace DCL.MyAccount |
| | 12 | | { |
| | 13 | | public class EmailNotificationsController |
| | 14 | | { |
| | 15 | | private enum ConfirmationModalStatus |
| | 16 | | { |
| | 17 | | None, |
| | 18 | | Accepted, |
| | 19 | | Rejected, |
| | 20 | | } |
| | 21 | |
|
| | 22 | | private const string CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY = "currentSubscriptionId"; |
| | 23 | |
|
| | 24 | | private readonly IEmailNotificationsComponentView view; |
| | 25 | | private readonly IUpdateEmailConfirmationHUDComponentView updateEmailConfirmationHUDComponentView; |
| | 26 | | private readonly MyAccountSectionHUDController myAccountSectionHUDController; |
| | 27 | | private readonly DataStore dataStore; |
| | 28 | | private readonly ISubscriptionsAPIService subscriptionsAPIService; |
| | 29 | | private readonly ISocialAnalytics socialAnalytics; |
| | 30 | |
|
| | 31 | | private CancellationTokenSource loadEmailCancellationToken; |
| | 32 | | private CancellationTokenSource updateEmailCancellationToken; |
| | 33 | |
|
| | 34 | | private string savedEmail; |
| | 35 | | private bool savedIsPending; |
| | 36 | | private ConfirmationModalStatus confirmationModalStatus = ConfirmationModalStatus.None; |
| | 37 | |
|
| 0 | 38 | | public EmailNotificationsController( |
| | 39 | | IEmailNotificationsComponentView view, |
| | 40 | | IUpdateEmailConfirmationHUDComponentView updateEmailConfirmationHUDComponentView, |
| | 41 | | MyAccountSectionHUDController myAccountSectionHUDController, |
| | 42 | | DataStore dataStore, |
| | 43 | | ISubscriptionsAPIService subscriptionsAPIService, |
| | 44 | | ISocialAnalytics socialAnalytics) |
| | 45 | | { |
| 0 | 46 | | this.view = view; |
| 0 | 47 | | this.updateEmailConfirmationHUDComponentView = updateEmailConfirmationHUDComponentView; |
| 0 | 48 | | this.myAccountSectionHUDController = myAccountSectionHUDController; |
| 0 | 49 | | this.dataStore = dataStore; |
| 0 | 50 | | this.subscriptionsAPIService = subscriptionsAPIService; |
| 0 | 51 | | this.socialAnalytics = socialAnalytics; |
| | 52 | |
|
| 0 | 53 | | dataStore.myAccount.isMyAccountSectionVisible.OnChange += OnMyAccountSectionOpen; |
| 0 | 54 | | dataStore.myAccount.openSection.OnChange += OnMyAccountSectionTabChanged; |
| 0 | 55 | | view.OnEmailEdited += OnEmailEdited; |
| 0 | 56 | | view.OnEmailSubmitted += OnEmailSubmitted; |
| 0 | 57 | | view.OnReSendConfirmationEmailClicked += OnReSendConfirmationEmailClicked; |
| 0 | 58 | | updateEmailConfirmationHUDComponentView.OnConfirmationModalAccepted += isAccepted => confirmationModalStatus |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public void Dispose() |
| | 62 | | { |
| 0 | 63 | | loadEmailCancellationToken.SafeCancelAndDispose(); |
| 0 | 64 | | updateEmailCancellationToken.SafeCancelAndDispose(); |
| 0 | 65 | | dataStore.myAccount.isMyAccountSectionVisible.OnChange -= OnMyAccountSectionOpen; |
| 0 | 66 | | dataStore.myAccount.openSection.OnChange -= OnMyAccountSectionTabChanged; |
| 0 | 67 | | view.OnEmailEdited -= OnEmailEdited; |
| 0 | 68 | | view.OnEmailSubmitted -= OnEmailSubmitted; |
| 0 | 69 | | view.OnReSendConfirmationEmailClicked -= OnReSendConfirmationEmailClicked; |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | private void OnMyAccountSectionOpen(bool isVisible, bool _) |
| | 73 | | { |
| 0 | 74 | | if (!isVisible || dataStore.myAccount.openSection.Get() != MyAccountSection.EmailNotifications.ToString()) |
| 0 | 75 | | return; |
| | 76 | |
|
| 0 | 77 | | RefreshForm(); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | private void OnMyAccountSectionTabChanged(string currentOpenSection, string _) |
| | 81 | | { |
| 0 | 82 | | if (currentOpenSection != MyAccountSection.EmailNotifications.ToString()) |
| 0 | 83 | | return; |
| | 84 | |
|
| 0 | 85 | | RefreshForm(); |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | private void RefreshForm() |
| | 89 | | { |
| 0 | 90 | | view.ResetForm(); |
| 0 | 91 | | loadEmailCancellationToken = loadEmailCancellationToken.SafeRestart(); |
| 0 | 92 | | LoadEmailAsync(loadEmailCancellationToken.Token).Forget(); |
| 0 | 93 | | updateEmailConfirmationHUDComponentView.HideConfirmationModal(); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | private void OnEmailEdited(string newEmail) |
| | 97 | | { |
| 0 | 98 | | view.SetEmailFormValid(IsValidEmail(newEmail)); |
| 0 | 99 | | view.SetStatusAsPending(newEmail == savedEmail && savedIsPending); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | private void OnEmailSubmitted(string newEmail) |
| | 103 | | { |
| 0 | 104 | | if (!IsValidEmail(newEmail)) |
| | 105 | | { |
| 0 | 106 | | SetSavedEmail(); |
| 0 | 107 | | return; |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | if (newEmail == savedEmail) |
| 0 | 111 | | return; |
| | 112 | |
|
| 0 | 113 | | updateEmailCancellationToken = updateEmailCancellationToken.SafeRestart(); |
| 0 | 114 | | UpdateEmailAsync(newEmail, true, updateEmailCancellationToken.Token).Forget(); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | private void SetSavedEmail() |
| | 118 | | { |
| 0 | 119 | | view.SetEmail(savedEmail); |
| 0 | 120 | | view.SetStatusAsPending(savedIsPending); |
| 0 | 121 | | view.SetEmailFormValid(true); |
| 0 | 122 | | view.SetEmailInputInteractable(true); |
| 0 | 123 | | view.SetEmailUpdateLoadingActive(false); |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | private void OnReSendConfirmationEmailClicked() |
| | 127 | | { |
| 0 | 128 | | updateEmailCancellationToken = updateEmailCancellationToken.SafeRestart(); |
| 0 | 129 | | UpdateEmailAsync(savedEmail, false, updateEmailCancellationToken.Token).Forget(); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | private static bool IsValidEmail(string email) |
| | 133 | | { |
| 0 | 134 | | if (email.Length == 0) |
| 0 | 135 | | return true; |
| | 136 | |
|
| | 137 | | try |
| | 138 | | { |
| 0 | 139 | | MailAddress mailAddress = new MailAddress(email); |
| 0 | 140 | | return mailAddress.Address == email; |
| | 141 | | } |
| 0 | 142 | | catch |
| | 143 | | { |
| 0 | 144 | | return false; |
| | 145 | | } |
| 0 | 146 | | } |
| | 147 | |
|
| | 148 | | private async UniTaskVoid LoadEmailAsync(CancellationToken cancellationToken) |
| | 149 | | { |
| 0 | 150 | | view.SetLoadingActive(true); |
| 0 | 151 | | savedEmail = string.Empty; |
| 0 | 152 | | savedIsPending = false; |
| | 153 | |
|
| 0 | 154 | | if (!string.IsNullOrEmpty(PlayerPrefsBridge.GetString(CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY))) |
| | 155 | | { |
| 0 | 156 | | string subscriptionToGet = PlayerPrefsBridge.GetString(CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY); |
| | 157 | |
|
| | 158 | | try |
| | 159 | | { |
| 0 | 160 | | var existingSubscription = await subscriptionsAPIService.GetSubscription(subscriptionToGet, cancella |
| | 161 | |
|
| 0 | 162 | | if (existingSubscription != null) |
| | 163 | | { |
| 0 | 164 | | if (existingSubscription.status == "inactive") |
| 0 | 165 | | PlayerPrefsBridge.SetString(CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY, string.Empty); |
| | 166 | | else |
| | 167 | | { |
| 0 | 168 | | savedEmail = existingSubscription.email; |
| 0 | 169 | | savedIsPending = existingSubscription.status is not "active"; |
| | 170 | | } |
| | 171 | | } |
| 0 | 172 | | } |
| 0 | 173 | | catch (Exception ex) |
| | 174 | | { |
| 0 | 175 | | if (ex.Message.Contains("404")) |
| 0 | 176 | | PlayerPrefsBridge.SetString(CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY, string.Empty); |
| | 177 | | else |
| 0 | 178 | | Debug.LogError($"An error occurred while getting the email subscription '{subscriptionToGet}': { |
| 0 | 179 | | } |
| 0 | 180 | | } |
| | 181 | |
|
| 0 | 182 | | SetSavedEmail(); |
| 0 | 183 | | view.SetLoadingActive(false); |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | private async UniTaskVoid UpdateEmailAsync(string newEmail, bool needConfirmation, CancellationToken cancellatio |
| | 187 | | { |
| 0 | 188 | | var wasSuccessfullyUpdated = false; |
| 0 | 189 | | view.SetEmailInputInteractable(false); |
| 0 | 190 | | view.SetEmailUpdateLoadingActive(true); |
| | 191 | |
|
| 0 | 192 | | if (needConfirmation && !string.IsNullOrEmpty(savedEmail)) |
| | 193 | | { |
| 0 | 194 | | confirmationModalStatus = ConfirmationModalStatus.None; |
| 0 | 195 | | updateEmailConfirmationHUDComponentView.ShowConfirmationModal( |
| | 196 | | string.IsNullOrEmpty(newEmail) ? view.deleteEmailLogo : view.updateEmailLogo, |
| | 197 | | string.IsNullOrEmpty(newEmail) ? "Are you sure you want to unsubscribe from Decentraland's newslette |
| | 198 | |
|
| 0 | 199 | | await UniTask.WaitUntil(() => confirmationModalStatus != ConfirmationModalStatus.None, cancellationToken |
| | 200 | | } |
| | 201 | |
|
| 0 | 202 | | if (confirmationModalStatus == ConfirmationModalStatus.Rejected) |
| | 203 | | { |
| 0 | 204 | | SetSavedEmail(); |
| 0 | 205 | | return; |
| | 206 | | } |
| | 207 | |
|
| 0 | 208 | | if (!string.IsNullOrEmpty(PlayerPrefsBridge.GetString(CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY))) |
| | 209 | | { |
| 0 | 210 | | string subscriptionToDelete = PlayerPrefsBridge.GetString(CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY); |
| | 211 | |
|
| | 212 | | try |
| | 213 | | { |
| 0 | 214 | | await subscriptionsAPIService.DeleteSubscription(subscriptionToDelete, cancellationToken); |
| 0 | 215 | | PlayerPrefsBridge.SetString(CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY, string.Empty); |
| 0 | 216 | | PlayerPrefsBridge.Save(); |
| 0 | 217 | | savedEmail = string.Empty; |
| 0 | 218 | | savedIsPending = false; |
| 0 | 219 | | wasSuccessfullyUpdated = true; |
| 0 | 220 | | } |
| 0 | 221 | | catch (Exception ex) |
| | 222 | | { |
| 0 | 223 | | wasSuccessfullyUpdated = false; |
| 0 | 224 | | Debug.LogError($"An error occurred while deleting the email subscription '{subscriptionToDelete}': { |
| 0 | 225 | | } |
| 0 | 226 | | } |
| | 227 | |
|
| 0 | 228 | | if (!string.IsNullOrEmpty(newEmail)) |
| | 229 | | { |
| | 230 | | try |
| | 231 | | { |
| 0 | 232 | | var newSubscription = await subscriptionsAPIService.CreateSubscription(newEmail, cancellationToken); |
| 0 | 233 | | PlayerPrefsBridge.SetString(CURRENT_SUBSCRIPTION_ID_LOCAL_STORAGE_KEY, newSubscription.id); |
| 0 | 234 | | PlayerPrefsBridge.Save(); |
| 0 | 235 | | savedEmail = newEmail; |
| 0 | 236 | | savedIsPending = newSubscription.status is not "active"; |
| 0 | 237 | | wasSuccessfullyUpdated = true; |
| 0 | 238 | | } |
| 0 | 239 | | catch (Exception ex) |
| | 240 | | { |
| 0 | 241 | | wasSuccessfullyUpdated = false; |
| 0 | 242 | | Debug.LogError($"An error occurred while creating the subscription for {newEmail}: {ex.Message}"); |
| 0 | 243 | | } |
| | 244 | | } |
| | 245 | |
|
| 0 | 246 | | view.SetEmailInputInteractable(true); |
| 0 | 247 | | view.SetStatusAsPending(savedIsPending); |
| 0 | 248 | | view.SetEmailUpdateLoadingActive(false); |
| | 249 | |
|
| 0 | 250 | | if (wasSuccessfullyUpdated) |
| | 251 | | { |
| 0 | 252 | | myAccountSectionHUDController.ShowAccountSettingsUpdatedToast(); |
| 0 | 253 | | socialAnalytics.SendProfileEdit(0, false, PlayerActionSource.EmailNotifications, ProfileField.Email); |
| | 254 | | } |
| 0 | 255 | | } |
| | 256 | | } |
| | 257 | | } |