< Summary

Class:SignupHUD.SignupHUDView
Assembly:SignupHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/SignupHUD/SignupHUDView.cs
Covered lines:59
Uncovered lines:4
Coverable lines:63
Total lines:169
Line coverage:93.6% (59 of 63)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
InitNameAndEmailScreen()0%110100%
InitTermsOfServicesScreen()0%110100%
OnFaceSnapshotReady(...)0%2100%
CreateView()0%110100%
SetVisibility(...)0%110100%
ShowNameScreen()0%110100%
ShowTermsOfServiceScreen()0%110100%
Dispose()0%220100%
UpdateNameAndEmailNextButton()0%330100%
IsValidEmail(...)0%220100%
IsValidName(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/SignupHUD/SignupHUDView.cs

#LineLine coverage
 1using System;
 2using System.Net.Mail;
 3using System.Text.RegularExpressions;
 4using DCL.Helpers;
 5using TMPro;
 6using UnityEngine;
 7using UnityEngine.UI;
 8
 9namespace SignupHUD
 10{
 11    public interface ISignupHUDView : IDisposable
 12    {
 13        delegate void NameScreenDone(string newName, string newEmail);
 14
 15        event NameScreenDone OnNameScreenNext;
 16        event Action OnEditAvatar;
 17        event Action OnTermsOfServiceAgreed;
 18        event Action OnTermsOfServiceBack;
 19
 20        void SetVisibility(bool visible);
 21        void ShowNameScreen();
 22        void ShowTermsOfServiceScreen();
 23    }
 24
 25    public class SignupHUDView : MonoBehaviour, ISignupHUDView
 26    {
 27        private const int MIN_NAME_LENGTH = 1;
 28        private const int MAX_NAME_LENGTH = 15;
 29
 30        public event ISignupHUDView.NameScreenDone OnNameScreenNext;
 31        public event Action OnEditAvatar;
 32        public event Action OnTermsOfServiceAgreed;
 33        public event Action OnTermsOfServiceBack;
 34
 35        [Header("Name and Email Screen")]
 36        [SerializeField] internal RectTransform nameAndEmailPanel;
 37
 38        [SerializeField] internal Button nameAndEmailNextButton;
 39        [SerializeField] internal TMP_InputField nameInputField;
 40        [SerializeField] internal GameObject nameInputFieldFullOrInvalid;
 41        [SerializeField] internal GameObject nameInputInvalidLabel;
 42        [SerializeField] internal TextMeshProUGUI nameCurrentCharacters;
 43        [SerializeField] internal GameObject emailInputFieldInvalid;
 44        [SerializeField] internal TMP_InputField emailInputField;
 45        [SerializeField] internal GameObject emailInputInvalidLabel;
 46        [SerializeField] internal Color colorForCharLimit;
 47
 48        [Header("Terms of Service Screen")]
 49        [SerializeField] internal RectTransform termsOfServicePanel;
 50
 51        [SerializeField] internal Button editAvatarButton;
 52        [SerializeField] internal ScrollRect termsOfServiceScrollView;
 53        [SerializeField] internal Button termsOfServiceBackButton;
 54        [SerializeField] internal Button termsOfServiceAgreeButton;
 55        [SerializeField] internal RawImage avatarPic;
 56
 57        private ILazyTextureObserver snapshotTextureObserver;
 58
 59        private void Awake()
 60        {
 961            InitNameAndEmailScreen();
 962            InitTermsOfServicesScreen();
 963        }
 64
 65        private void InitNameAndEmailScreen()
 66        {
 967            UserProfile userProfile = UserProfile.GetOwnUserProfile();
 968            snapshotTextureObserver = userProfile.snapshotObserver;
 969            snapshotTextureObserver.AddListener(OnFaceSnapshotReady);
 70
 971            nameAndEmailNextButton.interactable = false;
 972            nameCurrentCharacters.text = $"{0}/{MAX_NAME_LENGTH}";
 973            nameInputField.characterLimit = MAX_NAME_LENGTH;
 974            nameInputInvalidLabel.SetActive(false);
 975            nameInputFieldFullOrInvalid.SetActive(false);
 976            emailInputFieldInvalid.SetActive(false);
 977            emailInputInvalidLabel.SetActive(false);
 78
 979            nameInputField.onValueChanged.AddListener((text) =>
 80            {
 381                UpdateNameAndEmailNextButton();
 382                nameCurrentCharacters.text = $"{text.Length} / {MAX_NAME_LENGTH}";
 383                nameCurrentCharacters.color = text.Length < MAX_NAME_LENGTH ? Color.black : colorForCharLimit;
 384                nameInputInvalidLabel.SetActive(!IsValidName(text));
 385                nameInputFieldFullOrInvalid.SetActive(text.Length >= MAX_NAME_LENGTH || !IsValidName(text));
 386            });
 87
 988            emailInputField.onValueChanged.AddListener((text) =>
 89            {
 290                emailInputFieldInvalid.SetActive(!IsValidEmail(text));
 291                emailInputInvalidLabel.SetActive(!IsValidEmail(text));
 292                UpdateNameAndEmailNextButton();
 293            });
 94
 995            nameAndEmailNextButton.onClick.AddListener(() => OnNameScreenNext?.Invoke(nameInputField.text, emailInputFie
 996            editAvatarButton.onClick.AddListener(() => OnEditAvatar?.Invoke());
 997        }
 98
 99        private void InitTermsOfServicesScreen()
 100        {
 9101            termsOfServiceScrollView.onValueChanged.AddListener(pos =>
 102            {
 0103                if (pos.y <= 0.1f)
 0104                    termsOfServiceAgreeButton.interactable = true;
 0105            });
 106
 9107            termsOfServiceAgreeButton.interactable = false;
 9108            termsOfServiceBackButton.onClick.AddListener(() => OnTermsOfServiceBack?.Invoke());
 9109            termsOfServiceAgreeButton.onClick.AddListener(() => OnTermsOfServiceAgreed?.Invoke());
 9110        }
 111
 0112        private void OnFaceSnapshotReady(Texture2D texture) { avatarPic.texture = texture; }
 113
 114        public static SignupHUDView CreateView()
 115        {
 1116            SignupHUDView view = Instantiate(Resources.Load<GameObject>("SignupHUD")).GetComponent<SignupHUDView>();
 1117            view.gameObject.name = "_SignupHUD";
 1118            return view;
 119        }
 120
 6121        public void SetVisibility(bool visible) { gameObject.SetActive(visible); }
 122
 123        public void ShowNameScreen()
 124        {
 2125            nameAndEmailPanel.gameObject.SetActive(true);
 2126            termsOfServicePanel.gameObject.SetActive(false);
 2127        }
 128
 129        public void ShowTermsOfServiceScreen()
 130        {
 1131            nameAndEmailPanel.gameObject.SetActive(false);
 1132            termsOfServicePanel.gameObject.SetActive(true);
 1133        }
 134
 135        public void Dispose()
 136        {
 1137            snapshotTextureObserver.RemoveListener(OnFaceSnapshotReady);
 138
 1139            if (this != null)
 1140                Destroy(gameObject);
 1141        }
 142
 143        internal void UpdateNameAndEmailNextButton()
 144        {
 9145            string name = nameInputField.text;
 9146            string email = emailInputField.text;
 147
 9148            nameAndEmailNextButton.interactable = name.Length >= MIN_NAME_LENGTH && IsValidName(name) && IsValidEmail(em
 9149        }
 150
 151        private bool IsValidEmail(string email)
 152        {
 12153            if (email.Length == 0)
 4154                return true;
 155
 156            try
 157            {
 8158                MailAddress mailAddress = new MailAddress(email);
 4159                return mailAddress.Address == email;
 160            }
 4161            catch
 162            {
 4163                return false;
 164            }
 8165        }
 166
 14167        private bool IsValidName(string name) { return Regex.IsMatch(name, "^[a-zA-Z0-9]*$"); }
 168    }
 169}