| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Globalization; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace 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 | |
|
| 0 | 30 | | private readonly Dictionary<string, MyProfileAdditionalInfoEntryComponentView> entries = new (); |
| 0 | 31 | | private readonly List<ToggleComponentModel> optionToggles = new (); |
| 0 | 32 | | 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 | | { |
| 0 | 40 | | optionsDropdown.OnOptionSelectionChanged += (isOn, optionId, optionName) => |
| | 41 | | { |
| 0 | 42 | | if (!isOn) return; |
| | 43 | |
|
| 0 | 44 | | ChangeCurrentOption(optionId, optionName); |
| 0 | 45 | | }; |
| | 46 | |
|
| 0 | 47 | | dateInputField.onValueChanged.AddListener(UpdateAddButtonInteractibilityByDateFormat); |
| 0 | 48 | | freeFormInputField.onValueChanged.AddListener(UpdateAddButtonInteractibilityByFreeFormText); |
| 0 | 49 | | otherInputField.onValueChanged.AddListener(UpdateAddButtonInteractibilityByFreeFormText); |
| | 50 | |
|
| 0 | 51 | | strictValueListDropdown.OnOptionSelectionChanged += (isOn, optionId, optionName) => |
| | 52 | | { |
| 0 | 53 | | if (!isOn) return; |
| | 54 | |
|
| 0 | 55 | | if (optionId != OTHER_VALUE) |
| 0 | 56 | | strictValueListDropdown.SetTitle(optionName); |
| | 57 | | else |
| 0 | 58 | | SetOtherInputActive(true); |
| | 59 | |
|
| 0 | 60 | | UpdateAddButtonInteractibilityByFreeFormText(isOtherInputActivated ? otherInputField.text : optionName); |
| 0 | 61 | | }; |
| | 62 | |
|
| 0 | 63 | | cancelOtherInputButton.onClick.AddListener(() => SetOtherInputActive(false)); |
| | 64 | |
|
| 0 | 65 | | addButton.onClick.AddListener(() => |
| | 66 | | { |
| 0 | 67 | | if (string.IsNullOrEmpty(currentOptionId)) return; |
| 0 | 68 | | optionsModel.Options[currentOptionId].OnValueSubmitted.Invoke(GetCurrentValue()); |
| 0 | 69 | | OnAdditionalFieldAdded?.Invoke(); |
| 0 | 70 | | }); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public void SetOptions(AdditionalInfoOptionsModel model) |
| | 74 | | { |
| 0 | 75 | | if (optionsModel != null && optionsModel.Equals(model)) return; |
| | 76 | |
|
| 0 | 77 | | optionsModel = model; |
| | 78 | |
|
| 0 | 79 | | optionToggles.Clear(); |
| | 80 | |
|
| 0 | 81 | | foreach ((string optionId, AdditionalInfoOptionsModel.Option option) in model.Options) |
| | 82 | | { |
| 0 | 83 | | if (!option.IsAvailable) continue; |
| | 84 | |
|
| 0 | 85 | | optionToggles.Add(new ToggleComponentModel |
| | 86 | | { |
| | 87 | | text = option.Name, |
| | 88 | | id = optionId, |
| | 89 | | isOn = false, |
| | 90 | | isTextActive = true, |
| | 91 | | changeTextColorOnSelect = true, |
| | 92 | | }); |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | optionsDropdown.SetOptions(optionToggles); |
| | 96 | |
|
| 0 | 97 | | if (optionToggles.Count > 0) |
| | 98 | | { |
| 0 | 99 | | ToggleComponentModel option = optionToggles[0]; |
| 0 | 100 | | ChangeCurrentOption(option.id, option.text); |
| | 101 | | } |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public void SetValues(Dictionary<string, (string title, string value)> values) |
| | 105 | | { |
| 0 | 106 | | ClearValues(); |
| | 107 | |
|
| 0 | 108 | | foreach (KeyValuePair<string, (string title, string value)> pair in values) |
| | 109 | | { |
| 0 | 110 | | MyProfileAdditionalInfoEntryComponentView entry = Instantiate(entryPrefab, entryContainer); |
| 0 | 111 | | entry.Set(pair.Key, pair.Value.title, pair.Value.value); |
| 0 | 112 | | entry.OnRemoved += OnInfoRemoved; |
| 0 | 113 | | entries[pair.Key] = entry; |
| | 114 | | } |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | private void ClearValues() |
| | 118 | | { |
| 0 | 119 | | foreach (MyProfileAdditionalInfoEntryComponentView entry in entries.Values) |
| | 120 | | { |
| 0 | 121 | | entry.OnRemoved -= OnInfoRemoved; |
| 0 | 122 | | Destroy(entry.gameObject); |
| | 123 | | } |
| | 124 | |
|
| 0 | 125 | | entries.Clear(); |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private void ChangeCurrentOption(string optionId, string optionName) |
| | 129 | | { |
| 0 | 130 | | currentOptionId = optionId; |
| 0 | 131 | | SetOtherInputActive(false); |
| | 132 | |
|
| 0 | 133 | | switch (optionsModel.Options[optionId].InputType) |
| | 134 | | { |
| | 135 | | case AdditionalInfoOptionsModel.InputType.Date: |
| 0 | 136 | | dateInputField.gameObject.SetActive(true); |
| 0 | 137 | | freeFormInputField.gameObject.SetActive(false); |
| 0 | 138 | | strictValueListDropdown.gameObject.SetActive(false); |
| 0 | 139 | | break; |
| | 140 | | case AdditionalInfoOptionsModel.InputType.FreeFormText: |
| 0 | 141 | | dateInputField.gameObject.SetActive(false); |
| 0 | 142 | | freeFormInputField.gameObject.SetActive(true); |
| 0 | 143 | | strictValueListDropdown.gameObject.SetActive(false); |
| 0 | 144 | | break; |
| | 145 | | case AdditionalInfoOptionsModel.InputType.StrictValueList: |
| 0 | 146 | | dateInputField.gameObject.SetActive(false); |
| 0 | 147 | | freeFormInputField.gameObject.SetActive(false); |
| 0 | 148 | | strictValueListDropdown.gameObject.SetActive(true); |
| 0 | 149 | | FillStrictValueOptions(optionsModel.Options[optionId].Values); |
| 0 | 150 | | strictValueListDropdown.SetTitle(DEFAULT_SELECT_OPTION_VALUE); |
| | 151 | | break; |
| | 152 | | } |
| | 153 | |
|
| 0 | 154 | | optionsDropdown.SetTitle(optionName); |
| 0 | 155 | | dateInputField.text = ""; |
| 0 | 156 | | freeFormInputField.text = ""; |
| 0 | 157 | | addButton.interactable = false; |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | private void OnInfoRemoved(string optionId) |
| | 161 | | { |
| 0 | 162 | | optionsModel.Options[optionId].OnRemoved.Invoke(); |
| 0 | 163 | | OnAdditionalFieldRemoved?.Invoke(); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | private string GetCurrentValue() |
| | 167 | | { |
| 0 | 168 | | switch (optionsModel.Options[currentOptionId].InputType) |
| | 169 | | { |
| | 170 | | case AdditionalInfoOptionsModel.InputType.Date: |
| 0 | 171 | | return dateInputField.text; |
| | 172 | | case AdditionalInfoOptionsModel.InputType.FreeFormText: |
| 0 | 173 | | return freeFormInputField.text; |
| | 174 | | case AdditionalInfoOptionsModel.InputType.StrictValueList: |
| 0 | 175 | | return isOtherInputActivated ? otherInputField.text : strictValueListDropdown.Title; |
| | 176 | | } |
| | 177 | |
|
| 0 | 178 | | throw new ArgumentException($"Cannot solve value, invalid input type: {optionsModel.Options[currentOptionId] |
| | 179 | | } |
| | 180 | |
|
| | 181 | | private void UpdateAddButtonInteractibilityByDateFormat(string str) |
| | 182 | | { |
| 0 | 183 | | addButton.interactable = DateTime.TryParseExact(str, |
| | 184 | | optionsModel.Options[currentOptionId].DateFormat, |
| | 185 | | CultureInfo.InvariantCulture, |
| | 186 | | DateTimeStyles.AdjustToUniversal, out DateTime _); |
| 0 | 187 | | } |
| | 188 | |
|
| | 189 | | private void UpdateAddButtonInteractibilityByFreeFormText(string str) |
| | 190 | | { |
| 0 | 191 | | addButton.interactable = !string.IsNullOrEmpty(str); |
| 0 | 192 | | } |
| | 193 | |
|
| | 194 | | private void FillStrictValueOptions(string[] options) |
| | 195 | | { |
| 0 | 196 | | valueToggles.Clear(); |
| | 197 | |
|
| 0 | 198 | | foreach (string option in options) |
| | 199 | | { |
| 0 | 200 | | valueToggles.Add(new ToggleComponentModel |
| | 201 | | { |
| | 202 | | text = option, |
| | 203 | | id = option, |
| | 204 | | isOn = false, |
| | 205 | | isTextActive = true, |
| | 206 | | changeTextColorOnSelect = true, |
| | 207 | | }); |
| | 208 | | } |
| | 209 | |
|
| 0 | 210 | | strictValueListDropdown.SetOptions(valueToggles); |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | private void SetOtherInputActive(bool isActive) |
| | 214 | | { |
| 0 | 215 | | isOtherInputActivated = isActive; |
| 0 | 216 | | commonInputContainer.SetActive(!isActive); |
| 0 | 217 | | otherInputContainer.SetActive(isActive); |
| 0 | 218 | | otherInputField.text = string.Empty; |
| | 219 | |
|
| 0 | 220 | | if (isActive) return; |
| 0 | 221 | | strictValueListDropdown.SelectOption(null, false); |
| 0 | 222 | | strictValueListDropdown.SetTitle(DEFAULT_SELECT_OPTION_VALUE); |
| 0 | 223 | | } |
| | 224 | | } |
| | 225 | | } |