TaptapAntiAddictionTimeSelectorController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using System;
  2. using LC.Newtonsoft.Json;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TapSDK.UI;
  6. using TapSDK.UI.AillieoTech;
  7. using TapTap.AntiAddiction;
  8. using TapTap.AntiAddiction.Model;
  9. using TapTap.AntiAddiction.Internal;
  10. /// <summary>
  11. /// 年/月/日 选项中的每个 item
  12. /// </summary>
  13. namespace TapTap.AntiAddiction.Internal {
  14. public class DateTimeItem : MonoBehaviour
  15. {
  16. public TaptapAntiAddictionTimeSelectorController context;
  17. public int type;
  18. public int index;
  19. public Button clicker;
  20. public Image backgroundImage;
  21. public Text text;
  22. public void Init()
  23. {
  24. clicker = transform.GetComponent<Button>();
  25. backgroundImage = gameObject.GetComponent<Image>();
  26. text = transform.Find("Label").GetComponent<Text>();
  27. clicker.onClick.AddListener(() => context.OnDateTimeItemClicked(this, index, type));
  28. }
  29. public void Recover()
  30. {
  31. backgroundImage.color = Color.white;
  32. text.fontStyle = FontStyle.Normal;
  33. var v = 102 / 255.0f;
  34. text.color = new Color(v, v, v);
  35. }
  36. public void Highlight()
  37. {
  38. var v = 250 / 255.0f;
  39. backgroundImage.color = new Color(v, v, v);
  40. v = 34 / 255.0f;
  41. text.fontStyle = FontStyle.Bold;
  42. text.color = new Color(v, v, v);
  43. }
  44. }
  45. public class TaptapAntiAddictionTimeSelectorController : BasePanelController
  46. {
  47. // 默认时间
  48. private int _defaultYear = 2000;
  49. private const int DefaultMonth = 6;
  50. private const int DefaultDay = 15;
  51. // 至今时间,最低年份
  52. private const int MinYear = 2022;
  53. public Button closeButton;
  54. public Button confirmButton;
  55. public Button backgroundButton;
  56. public Toggle yearToggle;
  57. public Toggle monthToggle;
  58. public Toggle dayToggle;
  59. public ScrollViewEx yearScrollView;
  60. public ScrollViewEx monthScrollView;
  61. public ScrollViewEx dayScrollView;
  62. public Text yearHolder;
  63. public Text monthHolder;
  64. public Text dayHolder;
  65. public Text yearText;
  66. public Text monthText;
  67. public Text dayText;
  68. private GameObject _activeDropList;
  69. public Text titleText;
  70. public Text descriptionText;
  71. public Text buttonText;
  72. public DateTimeItem selectedYear;
  73. public DateTimeItem selectedMonth;
  74. public DateTimeItem selectedDay;
  75. internal Action<VerificationResult> OnVerified;
  76. internal Action OnException;
  77. internal Action OnClosed;
  78. private int Year { get; set; }
  79. private int Month { get; set; }
  80. private int Day { get; set; }
  81. private int _minYear;
  82. private bool _isSending;
  83. private bool IsSending
  84. {
  85. get => _isSending;
  86. set
  87. {
  88. if (value != _isSending)
  89. {
  90. _isSending = value;
  91. if (_isSending)
  92. UIManager.Instance.OpenLoading();
  93. else
  94. UIManager.Instance.CloseLoading();
  95. }
  96. }
  97. }
  98. /// <summary>
  99. /// bind ugui components for every panel
  100. /// </summary>
  101. protected override void BindComponents()
  102. {
  103. closeButton = gameObject.transform.Find("Root/CloseButton").GetComponent<Button>();
  104. confirmButton = gameObject.transform.Find("Root/ConfirmButton").GetComponent<Button>();
  105. backgroundButton = gameObject.transform.Find("Root").GetComponent<Button>();
  106. titleText = gameObject.transform.Find("Root/Title").GetComponent<Text>();
  107. descriptionText = gameObject.transform.Find("Root/Description").GetComponent<Text>();
  108. buttonText = gameObject.transform.Find("Root/ConfirmButton/Text").GetComponent<Text>();
  109. yearToggle = gameObject.transform.Find("Root/YearToggle").GetComponent<Toggle>();
  110. monthToggle = gameObject.transform.Find("Root/MonthToggle").GetComponent<Toggle>();
  111. dayToggle = gameObject.transform.Find("Root/DayToggle").GetComponent<Toggle>();
  112. yearScrollView = gameObject.transform.Find("Root/YearScrollView").GetComponent<ScrollViewEx>();
  113. monthScrollView = gameObject.transform.Find("Root/MonthScrollView").GetComponent<ScrollViewEx>();
  114. dayScrollView = gameObject.transform.Find("Root/DayScrollView").GetComponent<ScrollViewEx>();
  115. yearHolder = yearToggle.transform.Find("HolderLabel").GetComponent<Text>();
  116. monthHolder = monthToggle.transform.Find("HolderLabel").GetComponent<Text>();
  117. dayHolder = dayToggle.transform.Find("HolderLabel").GetComponent<Text>();
  118. yearText = yearToggle.transform.Find("Label").GetComponent<Text>();
  119. monthText = monthToggle.transform.Find("Label").GetComponent<Text>();
  120. dayText = dayToggle.transform.Find("Label").GetComponent<Text>();
  121. yearText.gameObject.SetActive(false);
  122. monthText.gameObject.SetActive(false);
  123. dayText.gameObject.SetActive(false);
  124. yearScrollView.gameObject.SetActive(false);
  125. monthScrollView.gameObject.SetActive(false);
  126. dayScrollView.gameObject.SetActive(false);
  127. yearHolder.gameObject.SetActive(true);
  128. monthHolder.gameObject.SetActive(true);
  129. dayHolder.gameObject.SetActive(true);
  130. }
  131. protected override void OnLoadSuccess()
  132. {
  133. base.OnLoadSuccess();
  134. closeButton.onClick.AddListener(OnCloseButtonClicked);
  135. confirmButton.onClick.AddListener(OnConfirmButtonClicked);
  136. backgroundButton.onClick.AddListener(OnClickBackgroundButton);
  137. yearToggle.isOn = false;
  138. monthToggle.isOn = false;
  139. dayToggle.isOn = false;
  140. //日期选择器默认选中时间:2000 年 6 月 15 日 日期可选范围:前 100 年 - 至今(非未来日期)
  141. var nowYear = DateTime.Now.Year;
  142. nowYear = Math.Max(nowYear, MinYear);
  143. _defaultYear = nowYear - 22;
  144. _minYear = nowYear - 100;
  145. int yearCount = nowYear - _minYear + 1;
  146. yearScrollView.SetItemCountFunc(() => yearCount);
  147. yearScrollView.SetUpdateFunc(UpdateYearInfo);
  148. yearScrollView.UpdateData(false);
  149. monthScrollView.SetItemCountFunc(() => 12);
  150. monthScrollView.SetUpdateFunc(UpdateMonthInfo);
  151. monthScrollView.UpdateData(false);
  152. dayScrollView.SetUpdateFunc(UpdateDayInfo);
  153. yearToggle.onValueChanged.AddListener((b) => OnTimeToggleChange(b, 0));
  154. monthToggle.onValueChanged.AddListener((b) => OnTimeToggleChange(b, 1));
  155. dayToggle.onValueChanged.AddListener((b) => OnTimeToggleChange(b, 2));
  156. var config = Config.Current?.UIConfig?.InputRealNameInfoVietnam;
  157. if (config != null)
  158. {
  159. titleText.text = config.title;
  160. descriptionText.text = config.description;
  161. buttonText.text = config.button;
  162. }
  163. yearHolder.text = _defaultYear.ToString();
  164. monthHolder.text = DefaultMonth.ToString("D2");
  165. dayHolder.text = DefaultDay.ToString("D2");
  166. Year = _defaultYear;
  167. Month = DefaultMonth;
  168. Day = DefaultDay;
  169. OnChangeYearOrMonth(_defaultYear, DefaultMonth);
  170. IsSending = false;
  171. }
  172. private DateTimeItem CreateDateItem(RectTransform item)
  173. {
  174. item.gameObject.SetActive(true);
  175. DateTimeItem dateItem = item.GetComponent<DateTimeItem>();
  176. if (dateItem == null)
  177. {
  178. dateItem = item.gameObject.AddComponent<DateTimeItem>();
  179. }
  180. dateItem.Init();
  181. return dateItem;
  182. }
  183. private void UpdateTimeInfo(int index, RectTransform item, int type, string info)
  184. {
  185. DateTimeItem dateItem = CreateDateItem(item);
  186. dateItem.context = this;
  187. dateItem.type = type;
  188. dateItem.index = index;
  189. var text = dateItem.text;
  190. text.gameObject.SetActive(true);
  191. text.text = info;
  192. int intV = int.Parse(info);
  193. int comV = 0;
  194. if (type == 0)
  195. {
  196. comV = Year;
  197. }
  198. else if (type == 1)
  199. {
  200. comV = Month;
  201. }
  202. else if (type == 2)
  203. {
  204. comV = Day;
  205. }
  206. if (comV == intV)
  207. {
  208. dateItem.Highlight();
  209. }
  210. else
  211. {
  212. dateItem.Recover();
  213. }
  214. }
  215. private void UpdateYearInfo(int index, RectTransform item)
  216. {
  217. UpdateTimeInfo(index, item, 0, $"{(_minYear + index).ToString()}");
  218. }
  219. private void UpdateMonthInfo(int index, RectTransform item)
  220. {
  221. UpdateTimeInfo(index, item, 1, $"{(index + 1).ToString("D2")}");
  222. }
  223. private void UpdateDayInfo(int index, RectTransform item)
  224. {
  225. UpdateTimeInfo(index, item, 2, $"{(index + 1).ToString("D2")}");
  226. }
  227. private void OnClickBackgroundButton()
  228. {
  229. OnTimeToggleChange(false, 0);
  230. OnTimeToggleChange(false, 1);
  231. OnTimeToggleChange(false, 2);
  232. yearToggle.isOn = false;
  233. monthToggle.isOn = false;
  234. dayToggle.isOn = false;
  235. }
  236. public void OnDateTimeItemClicked(DateTimeItem item, int index, int type)
  237. {
  238. if (type == 0)
  239. {
  240. UpdateCurYear(_minYear + index);
  241. yearHolder.gameObject.SetActive(false);
  242. if (selectedYear != null)
  243. selectedYear.Recover();
  244. item.Highlight();
  245. selectedYear = item;
  246. }
  247. else if (type == 1)
  248. {
  249. UpdateCurMonth(1 + index);
  250. monthHolder.gameObject.SetActive(false);
  251. if (selectedMonth != null)
  252. selectedMonth.Recover();
  253. item.Highlight();
  254. selectedMonth = item;
  255. }
  256. else if (type == 2)
  257. {
  258. UpdateCurDay(1 + index);
  259. dayHolder.gameObject.SetActive(false);
  260. if (selectedDay != null)
  261. selectedDay.Recover();
  262. item.Highlight();
  263. selectedDay = item;
  264. }
  265. }
  266. private void UpdateCurYear(int newYear)
  267. {
  268. Year = newYear;
  269. var timeText = $"{Year.ToString()}";
  270. yearText.text = timeText;
  271. yearText.gameObject.SetActive(true);
  272. yearText.text = timeText;
  273. yearToggle.isOn = false;
  274. OnChangeYearOrMonth(Year, Month);
  275. }
  276. private void UpdateCurMonth(int newMonth)
  277. {
  278. Month = newMonth;
  279. var timeText = $"{Month.ToString("D2")}";
  280. monthText.text = timeText;
  281. monthText.gameObject.SetActive(true);
  282. monthText.text = timeText;
  283. monthToggle.isOn = false;
  284. OnChangeYearOrMonth(Year, Month);
  285. }
  286. private void UpdateCurDay(int newDay)
  287. {
  288. Day = newDay;
  289. var timeText = $"{Day.ToString("D2")}";
  290. dayText.text = timeText;
  291. dayText.gameObject.SetActive(true);
  292. dayText.text = timeText;
  293. dayToggle.isOn = false;
  294. }
  295. private void OnChangeYearOrMonth(int year, int month)
  296. {
  297. int newDayCount = Tool.GetMonthDayCount(year, month);
  298. dayScrollView.SetItemCountFunc(() => newDayCount);
  299. dayScrollView.UpdateData();
  300. if (Day > newDayCount)
  301. {
  302. UpdateCurDay(newDayCount);
  303. }
  304. }
  305. /// <summary>
  306. /// 切换具体时间(年/月/日)详情是否显示
  307. /// </summary>
  308. /// <param name="toggle">是否显示</param>
  309. /// <param name="type">0-year;1-month;2-day</param>
  310. private void OnTimeToggleChange(bool toggle, int type)
  311. {
  312. if (_activeDropList != null)
  313. _activeDropList.SetActive(false);
  314. if (toggle)
  315. {
  316. ScrollViewEx target = null;
  317. int index = 0;
  318. switch (type)
  319. {
  320. case 0:
  321. if (_activeDropList != null)
  322. {
  323. monthToggle.isOn = false;
  324. dayToggle.isOn = false;
  325. }
  326. target = yearScrollView;
  327. index = Mathf.Max(0, Year - _minYear - 1);
  328. break;
  329. case 1:
  330. if (_activeDropList != null)
  331. {
  332. yearToggle.isOn = false;
  333. dayToggle.isOn = false;
  334. }
  335. target = monthScrollView;
  336. index = Mathf.Max(0, Month - 2);
  337. break;
  338. case 2:
  339. if (_activeDropList != null)
  340. {
  341. yearToggle.isOn = false;
  342. monthToggle.isOn = false;
  343. }
  344. target = dayScrollView;
  345. index = Mathf.Max(0, Day - 2);
  346. break;
  347. }
  348. _activeDropList = target.gameObject;
  349. _activeDropList.SetActive(true);
  350. target.ScrollTo(index);
  351. }
  352. else
  353. {
  354. _activeDropList = null;
  355. }
  356. }
  357. private bool Validate(out DateTime time)
  358. {
  359. try
  360. {
  361. time = new DateTime(Year, Month, Day);
  362. return true;
  363. }
  364. catch (Exception)
  365. {
  366. time = default;
  367. return false;
  368. }
  369. }
  370. private async void OnConfirmButtonClicked()
  371. {
  372. if (IsSending) return;
  373. if (Validate(out DateTime dateTime))
  374. {
  375. Debug.LogFormat("Time: {0}", dateTime.ToString("yyyy/MM/dd"));
  376. var dateJson = new { birthDate = dateTime.ToString("yyyy-MM-dd") };
  377. string json = JsonConvert.SerializeObject(dateJson, Formatting.Indented);
  378. try
  379. {
  380. IsSending = true;
  381. var verificationResult = await Verification.VerifyKycAsync(TapTapAntiAddictionManager.UserId, json);
  382. IsSending = false;
  383. Close();
  384. OnVerified?.Invoke(verificationResult);
  385. }
  386. catch
  387. {
  388. IsSending = false;
  389. OnException?.Invoke();
  390. }
  391. }
  392. else
  393. {
  394. await UIManager.Instance.OpenToastAsync(Config.Current.UIConfig.InputRealNameInfoVietnam.invalidateMessage);
  395. }
  396. }
  397. private void OnCloseButtonClicked()
  398. {
  399. Close();
  400. OnClosed?.Invoke();
  401. }
  402. }
  403. }