< Summary

Class:DCL.MyAccount.MyProfileAdditionalInfoListComponentView
Assembly:MyAccountHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/MyAccountHUD/MyProfileAdditionalInfoListComponentView.cs
Covered lines:0
Uncovered lines:93
Coverable lines:93
Total lines:225
Line coverage:0% (0 of 93)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:12
Method coverage:0% (0 of 12)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MyProfileAdditionalInfoListComponentView()0%2100%
Awake()0%2100%
SetOptions(...)0%42600%
SetValues(...)0%6200%
ClearValues()0%6200%
ChangeCurrentOption(...)0%20400%
OnInfoRemoved(...)0%6200%
GetCurrentValue()0%42600%
UpdateAddButtonInteractibilityByDateFormat(...)0%2100%
UpdateAddButtonInteractibilityByFreeFormText(...)0%2100%
FillStrictValueOptions(...)0%6200%
SetOtherInputActive(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/MyAccountHUD/MyProfileAdditionalInfoListComponentView.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8namespace DCL.MyAccount
 9{
 10    public class MyProfileAdditionalInfoListComponentView : MonoBehaviour
 11    {
 12        private const string DEFAULT_SELECT_OPTION_VALUE = "- Select an option -";
 13        private const string OTHER_VALUE = "Other";
 14
 15        [SerializeField] private Button addButton;
 16        [SerializeField] private DropdownComponentView optionsDropdown;
 17        [SerializeField] private TMP_InputField freeFormInputField;
 18        [SerializeField] private TMP_InputField dateInputField;
 19        [SerializeField] private DropdownComponentView strictValueListDropdown;
 20        [SerializeField] private MyProfileAdditionalInfoEntryComponentView entryPrefab;
 21        [SerializeField] private RectTransform entryContainer;
 22        [SerializeField] private GameObject commonInputContainer;
 23        [SerializeField] private GameObject otherInputContainer;
 24        [SerializeField] private Button cancelOtherInputButton;
 25        [SerializeField] private TMP_InputField otherInputField;
 26
 27        public Action OnAdditionalFieldAdded;
 28        public Action OnAdditionalFieldRemoved;
 29
 030        private readonly Dictionary<string, MyProfileAdditionalInfoEntryComponentView> entries = new ();
 031        private readonly List<ToggleComponentModel> optionToggles = new ();
 032        private readonly List<ToggleComponentModel> valueToggles = new ();
 33
 34        private string currentOptionId;
 35        private AdditionalInfoOptionsModel optionsModel;
 36        private bool isOtherInputActivated;
 37
 38        private void Awake()
 39        {
 040            optionsDropdown.OnOptionSelectionChanged += (isOn, optionId, optionName) =>
 41            {
 042                if (!isOn) return;
 43
 044                ChangeCurrentOption(optionId, optionName);
 045            };
 46
 047            dateInputField.onValueChanged.AddListener(UpdateAddButtonInteractibilityByDateFormat);
 048            freeFormInputField.onValueChanged.AddListener(UpdateAddButtonInteractibilityByFreeFormText);
 049            otherInputField.onValueChanged.AddListener(UpdateAddButtonInteractibilityByFreeFormText);
 50
 051            strictValueListDropdown.OnOptionSelectionChanged += (isOn, optionId, optionName) =>
 52            {
 053                if (!isOn) return;
 54
 055                if (optionId != OTHER_VALUE)
 056                    strictValueListDropdown.SetTitle(optionName);
 57                else
 058                    SetOtherInputActive(true);
 59
 060                UpdateAddButtonInteractibilityByFreeFormText(isOtherInputActivated ? otherInputField.text : optionName);
 061            };
 62
 063            cancelOtherInputButton.onClick.AddListener(() => SetOtherInputActive(false));
 64
 065            addButton.onClick.AddListener(() =>
 66            {
 067                if (string.IsNullOrEmpty(currentOptionId)) return;
 068                optionsModel.Options[currentOptionId].OnValueSubmitted.Invoke(GetCurrentValue());
 069                OnAdditionalFieldAdded?.Invoke();
 070            });
 071        }
 72
 73        public void SetOptions(AdditionalInfoOptionsModel model)
 74        {
 075            if (optionsModel != null && optionsModel.Equals(model)) return;
 76
 077            optionsModel = model;
 78
 079            optionToggles.Clear();
 80
 081            foreach ((string optionId, AdditionalInfoOptionsModel.Option option) in model.Options)
 82            {
 083                if (!option.IsAvailable) continue;
 84
 085                optionToggles.Add(new ToggleComponentModel
 86                {
 87                    text = option.Name,
 88                    id = optionId,
 89                    isOn = false,
 90                    isTextActive = true,
 91                    changeTextColorOnSelect = true,
 92                });
 93            }
 94
 095            optionsDropdown.SetOptions(optionToggles);
 96
 097            if (optionToggles.Count > 0)
 98            {
 099                ToggleComponentModel option = optionToggles[0];
 0100                ChangeCurrentOption(option.id, option.text);
 101            }
 0102        }
 103
 104        public void SetValues(Dictionary<string, (string title, string value)> values)
 105        {
 0106            ClearValues();
 107
 0108            foreach (KeyValuePair<string, (string title, string value)> pair in values)
 109            {
 0110                MyProfileAdditionalInfoEntryComponentView entry = Instantiate(entryPrefab, entryContainer);
 0111                entry.Set(pair.Key, pair.Value.title, pair.Value.value);
 0112                entry.OnRemoved += OnInfoRemoved;
 0113                entries[pair.Key] = entry;
 114            }
 0115        }
 116
 117        private void ClearValues()
 118        {
 0119            foreach (MyProfileAdditionalInfoEntryComponentView entry in entries.Values)
 120            {
 0121                entry.OnRemoved -= OnInfoRemoved;
 0122                Destroy(entry.gameObject);
 123            }
 124
 0125            entries.Clear();
 0126        }
 127
 128        private void ChangeCurrentOption(string optionId, string optionName)
 129        {
 0130            currentOptionId = optionId;
 0131            SetOtherInputActive(false);
 132
 0133            switch (optionsModel.Options[optionId].InputType)
 134            {
 135                case AdditionalInfoOptionsModel.InputType.Date:
 0136                    dateInputField.gameObject.SetActive(true);
 0137                    freeFormInputField.gameObject.SetActive(false);
 0138                    strictValueListDropdown.gameObject.SetActive(false);
 0139                    break;
 140                case AdditionalInfoOptionsModel.InputType.FreeFormText:
 0141                    dateInputField.gameObject.SetActive(false);
 0142                    freeFormInputField.gameObject.SetActive(true);
 0143                    strictValueListDropdown.gameObject.SetActive(false);
 0144                    break;
 145                case AdditionalInfoOptionsModel.InputType.StrictValueList:
 0146                    dateInputField.gameObject.SetActive(false);
 0147                    freeFormInputField.gameObject.SetActive(false);
 0148                    strictValueListDropdown.gameObject.SetActive(true);
 0149                    FillStrictValueOptions(optionsModel.Options[optionId].Values);
 0150                    strictValueListDropdown.SetTitle(DEFAULT_SELECT_OPTION_VALUE);
 151                    break;
 152            }
 153
 0154            optionsDropdown.SetTitle(optionName);
 0155            dateInputField.text = "";
 0156            freeFormInputField.text = "";
 0157            addButton.interactable = false;
 0158        }
 159
 160        private void OnInfoRemoved(string optionId)
 161        {
 0162            optionsModel.Options[optionId].OnRemoved.Invoke();
 0163            OnAdditionalFieldRemoved?.Invoke();
 0164        }
 165
 166        private string GetCurrentValue()
 167        {
 0168            switch (optionsModel.Options[currentOptionId].InputType)
 169            {
 170                case AdditionalInfoOptionsModel.InputType.Date:
 0171                    return dateInputField.text;
 172                case AdditionalInfoOptionsModel.InputType.FreeFormText:
 0173                    return freeFormInputField.text;
 174                case AdditionalInfoOptionsModel.InputType.StrictValueList:
 0175                    return isOtherInputActivated ? otherInputField.text : strictValueListDropdown.Title;
 176            }
 177
 0178            throw new ArgumentException($"Cannot solve value, invalid input type: {optionsModel.Options[currentOptionId]
 179        }
 180
 181        private void UpdateAddButtonInteractibilityByDateFormat(string str)
 182        {
 0183            addButton.interactable = DateTime.TryParseExact(str,
 184                optionsModel.Options[currentOptionId].DateFormat,
 185                CultureInfo.InvariantCulture,
 186                DateTimeStyles.AdjustToUniversal, out DateTime _);
 0187        }
 188
 189        private void UpdateAddButtonInteractibilityByFreeFormText(string str)
 190        {
 0191            addButton.interactable = !string.IsNullOrEmpty(str);
 0192        }
 193
 194        private void FillStrictValueOptions(string[] options)
 195        {
 0196            valueToggles.Clear();
 197
 0198            foreach (string option in options)
 199            {
 0200                valueToggles.Add(new ToggleComponentModel
 201                {
 202                    text = option,
 203                    id = option,
 204                    isOn = false,
 205                    isTextActive = true,
 206                    changeTextColorOnSelect = true,
 207                });
 208            }
 209
 0210            strictValueListDropdown.SetOptions(valueToggles);
 0211        }
 212
 213        private void SetOtherInputActive(bool isActive)
 214        {
 0215            isOtherInputActivated = isActive;
 0216            commonInputContainer.SetActive(!isActive);
 0217            otherInputContainer.SetActive(isActive);
 0218            otherInputField.text = string.Empty;
 219
 0220            if (isActive) return;
 0221            strictValueListDropdown.SelectOption(null, false);
 0222            strictValueListDropdown.SetTitle(DEFAULT_SELECT_OPTION_VALUE);
 0223        }
 224    }
 225}