HRB_MeView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using JCUnityLib;
  6. public class HRB_MeView : JCUnityLib.ViewBase, MenuBackInterface
  7. {
  8. [SerializeField] Image avatarImage;
  9. [SerializeField] Text nameText;
  10. [SerializeField] GameObject inputs;
  11. void Start()
  12. {
  13. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  14. meUserInfo = new MeUserInfo(this);
  15. this.transform.Find("AvatarFrame").GetComponent<Button>().onClick.AddListener(ShowAvatarSelectView);
  16. RenderAfterSave();
  17. }
  18. void OnDestroy()
  19. {
  20. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  21. }
  22. public void OnClick_QuitLogin()
  23. {
  24. AudioMgr.ins.PlayBtn();
  25. PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
  26. HRB_ViewMgr.Instance.DestroyAllViews();
  27. HRB_ViewMgr.Instance.ShowView<HRB_LoginView>();
  28. }
  29. public void OnClick_QuitApp()
  30. {
  31. AudioMgr.ins.PlayBtn();
  32. Application.Quit();
  33. }
  34. public bool OnMenuBack() {
  35. if (!meUserInfo.ShowModalForSave()) {
  36. ExcuteLogic_Destroy();
  37. return true;
  38. } else {
  39. return false;
  40. }
  41. }
  42. public void Back() {
  43. AudioMgr.ins.PlayBtn();
  44. if (!meUserInfo.ShowModalForSave()) {
  45. ExcuteLogic_Destroy();
  46. }
  47. }
  48. private void ExcuteLogic_Destroy() {
  49. HRB_ViewMgr.Instance.DestroyView<HRB_MeView>();
  50. }
  51. public void Save()
  52. {
  53. AudioMgr.ins.PlayBtn();
  54. ExcuteLogic_SaveUserInfo(true);
  55. }
  56. JCUnityLib.Throttler throttlerSave = new JCUnityLib.Throttler(3000);
  57. private bool ExcuteLogic_SaveUserInfo(bool useThrottler) {
  58. System.Func<bool> interceptor = delegate() {
  59. if (string.IsNullOrEmpty(meUserInfo.nickname)) {
  60. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("昵称不能为空"));
  61. return true;
  62. }
  63. if (useThrottler && throttlerSave.CanPass() == false) {
  64. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  65. return true;
  66. }
  67. return false;
  68. };
  69. if (!meUserInfo.Save(interceptor)) return false;
  70. RenderAfterSave();
  71. return true;
  72. }
  73. InputField GetInputField(Transform transform)
  74. {
  75. return transform.Find("InputField").GetComponent<InputField>();
  76. }
  77. public void RenderAfterSave() {
  78. for (int i = 0; i < inputs.transform.childCount; i++)
  79. {
  80. if (i == 3) {
  81. inputs.transform.GetChild(i).Find("ToggleGroup")
  82. .GetChild(HRB_UserInfo.current.gender == 2 ? 1 : 0)
  83. .GetComponent<Toggle>().isOn = true;
  84. continue;
  85. }
  86. InputField inputField = GetInputField(inputs.transform.GetChild(i));
  87. if (i == 0) {
  88. inputField.text = HRB_UserInfo.current.id.ToString();
  89. }
  90. if (i == 1) {
  91. inputField.text = HRB_UserInfo.current.nickname;
  92. }
  93. else if (i == 4) {
  94. inputField.text = HRB_UserInfo.current.birthday;
  95. }
  96. else if (i == 6) {
  97. // countryCode = HRB_UserInfo.current.country;
  98. // stateCode = HRB_UserInfo.current.state;
  99. // cityCode = HRB_UserInfo.current.city;
  100. // System.Tuple<string, string, string> xxx = JC.Unity.Picker.LocationParseComponent.ins.ParseNameByCode(countryCode, stateCode, cityCode);
  101. // inputField.text = (xxx.Item1 + " " + xxx.Item2 + " " + xxx.Item3).Trim();
  102. //渲染后端返回的gps地理位置
  103. countryCode = HRB_UserInfo.current.country;
  104. stateCode = HRB_UserInfo.current.state;
  105. cityCode = HRB_UserInfo.current.city;
  106. inputField.text = (countryCode + " " + stateCode + " " + cityCode).Trim();
  107. }
  108. }
  109. nameText.text = HRB_UserInfo.current.nickname;
  110. RoleMgr.SetAvatarToImage(avatarImage, HRB_UserInfo.current.avatarID, "");
  111. // HomeView.ins.RenderNameOrGender();
  112. }
  113. #region Picker
  114. [SerializeField] GameObject datePickerPrefab;
  115. public void OpenDatePicker() {
  116. GameObject o = GameObject.Instantiate(datePickerPrefab);
  117. o.GetComponentInChildren<JC.Unity.Picker.DatePickerGroup>().onEnter += (JC.Unity.Picker.DatePickerGroup picker) => {
  118. GetInputField(inputs.transform.GetChild(4)).text = picker.GetSelectDateStr();
  119. };
  120. }
  121. [SerializeField] GameObject locationPickerPrefab;
  122. private string countryCode = "", stateCode = "", cityCode = "";
  123. public void OpenLocationPicker() {
  124. // GameObject o = GameObject.Instantiate(locationPickerPrefab);
  125. // o.GetComponentInChildren<JC.Unity.Picker.LocationPickerGroup>().onEnter += (JC.Unity.Picker.LocationInfo info) => {
  126. // countryCode = info.GetCountryRegion().Item2;
  127. // stateCode = info.GetState().Item2;
  128. // cityCode = info.GetCity().Item2;
  129. // GetInputField(inputs.transform.GetChild(6)).text =
  130. // info.GetCountryRegion().Item1 + " " +
  131. // info.GetState().Item1 + " " +
  132. // info.GetCity().Item1;
  133. // };
  134. //2022-12-6 gps获取地理位置
  135. System.Action eOnAgree = () => {
  136. GPSTool.GetAddress((address) => {
  137. if (address != null) {
  138. countryCode = address[0];
  139. stateCode = address[1];
  140. cityCode = address[2];
  141. GetInputField(inputs.transform.GetChild(6)).text =
  142. countryCode + " " +
  143. stateCode + " " +
  144. cityCode;
  145. }
  146. });
  147. };
  148. if (!HomeView.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
  149. }
  150. #endregion
  151. #region 头像选择
  152. bool avatarSelectViewInited = false;
  153. int curAvatarSelectID = 0;
  154. public void ShowAvatarSelectView() {
  155. AudioMgr.ins.PlayBtn();
  156. Transform avatarSelectView = this.transform.Find("AvatarSelectView");
  157. avatarSelectView.gameObject.SetActive(true);
  158. if (avatarSelectViewInited) {
  159. } else {
  160. avatarSelectViewInited = true;
  161. curAvatarSelectID = HRB_UserInfo.current.avatarID;
  162. avatarSelectView.Find("FrameBox/BtnClose").GetComponent<Button>().onClick.AddListener(CloseAvatarSelectView);
  163. GridLayoutGroup gridLayoutGroup = this.transform.GetComponentInChildren<GridLayoutGroup>();
  164. GameObject avatarPrefab = gridLayoutGroup.transform.Find("Avatar").gameObject;
  165. for (int id = 0; id < RoleMgr.GetAvatarListLen(); id++) {
  166. if (RoleMgr.IsRoleAvatar(id)) continue;
  167. GameObject avatar = GameObject.Instantiate(avatarPrefab, gridLayoutGroup.transform);
  168. Image avatarImage = avatar.transform.Find("Mask/Sprite").GetComponent<Image>();
  169. RoleMgr.SetAvatarToImage(avatarImage, id, "");
  170. avatar.transform.Find("Check").gameObject.SetActive(id == curAvatarSelectID);
  171. avatar.gameObject.name = id.ToString();
  172. int aid = id; //记录该值
  173. avatar.GetComponent<Button>().onClick.AddListener(() => {
  174. AudioMgr.ins.PlayBtn();
  175. curAvatarSelectID = aid;
  176. for (int i = 0; i < gridLayoutGroup.transform.childCount; i++) {
  177. Transform item = gridLayoutGroup.transform.GetChild(i);
  178. int theAvatarID = int.Parse(item.gameObject.name);
  179. item.Find("Check").gameObject.SetActive(theAvatarID == curAvatarSelectID);
  180. }
  181. });
  182. }
  183. Destroy(avatarPrefab);
  184. LayoutRebuilder.ForceRebuildLayoutImmediate(gridLayoutGroup.transform.parent.GetComponent<RectTransform>());
  185. }
  186. }
  187. public void CloseAvatarSelectView() {
  188. AudioMgr.ins.PlayBtn();
  189. this.transform.Find("AvatarSelectView").gameObject.SetActive(false);
  190. if (curAvatarSelectID != HRB_UserInfo.current.avatarID) {
  191. HRB_UserInfo.current.avatarID = curAvatarSelectID;
  192. //render
  193. RoleMgr.SetAvatarToImage(avatarImage, curAvatarSelectID, "");
  194. // HomeView.ins.RenderMyAvatarSprite();
  195. //save data
  196. HRB_UserInfo.current.Save();
  197. }
  198. }
  199. #endregion
  200. MeUserInfo meUserInfo;
  201. private class MeUserInfo {
  202. private HRB_MeView m_context;
  203. public MeUserInfo(HRB_MeView context) {
  204. m_context = context;
  205. }
  206. #region UserInfo关联属性
  207. public string nickname;
  208. public int gender;
  209. public string birthday;
  210. public string country;
  211. public string state;
  212. public string city;
  213. #endregion
  214. private void RefreshValues() {
  215. //从组件中读取值
  216. string[] texts = new string[7];
  217. for (int i = 0; i < m_context.inputs.transform.childCount; i++)
  218. {
  219. if (i == 3) {
  220. Transform toggleGroup = m_context.inputs.transform.GetChild(i).Find("ToggleGroup");
  221. texts[i] = toggleGroup.GetChild(0).GetComponent<Toggle>().isOn ? "1" : "2";
  222. continue;
  223. }
  224. InputField inputField = m_context.GetInputField(m_context.inputs.transform.GetChild(i));
  225. texts[i] = inputField.text.Trim();
  226. }
  227. //缓存读取到的值
  228. this.nickname = texts[1];
  229. this.gender = int.Parse(texts[3]);
  230. this.birthday = texts[4];
  231. this.country = m_context.countryCode;
  232. this.state = m_context.stateCode;
  233. this.city = m_context.cityCode;
  234. }
  235. public bool Save(System.Func<bool> interceptor = null) {
  236. RefreshValues();
  237. if (interceptor != null) {
  238. if (interceptor.Invoke()) return false;
  239. }
  240. HRB_UserInfo.current.nickname = this.nickname;
  241. HRB_UserInfo.current.gender = this.gender;
  242. HRB_UserInfo.current.birthday = this.birthday;
  243. HRB_UserInfo.current.country = this.country;
  244. HRB_UserInfo.current.state = this.state;
  245. HRB_UserInfo.current.city = this.city;
  246. HRB_UserInfo.current.Save();
  247. return true;
  248. }
  249. private bool IsEqualOrigin() {
  250. return
  251. HRB_UserInfo.current.nickname == this.nickname &&
  252. HRB_UserInfo.current.gender == this.gender &&
  253. HRB_UserInfo.current.birthday == this.birthday &&
  254. HRB_UserInfo.current.country == this.country &&
  255. HRB_UserInfo.current.state == this.state &&
  256. HRB_UserInfo.current.city == this.city;
  257. }
  258. ModalView modalViewForSave;
  259. public bool ShowModalForSave() {
  260. RefreshValues();
  261. if (IsEqualOrigin()) return false;
  262. if (!modalViewForSave) {
  263. modalViewForSave = ModalView.Show();
  264. modalViewForSave.textKey= "me_askSave";
  265. modalViewForSave.onAgreeTextKey = "common_yes";
  266. modalViewForSave.onRejectTextKey = "common_no";
  267. modalViewForSave.onAgree = () => {
  268. bool res = m_context.ExcuteLogic_SaveUserInfo(false);
  269. if (res) {
  270. m_context.ExcuteLogic_Destroy();
  271. }
  272. };
  273. modalViewForSave.onReject = () => m_context.ExcuteLogic_Destroy();
  274. }
  275. return true;
  276. }
  277. }
  278. }