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