HRB_MeView.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 == 2) {
  94. inputField.text = HRB_UserInfo.current.phone;
  95. }
  96. else if (i == 4) {
  97. inputField.text = HRB_UserInfo.current.birthday;
  98. }
  99. else if (i == 6) {
  100. // countryCode = HRB_UserInfo.current.country;
  101. // stateCode = HRB_UserInfo.current.state;
  102. // cityCode = HRB_UserInfo.current.city;
  103. // System.Tuple<string, string, string> xxx = JC.Unity.Picker.LocationParseComponent.ins.ParseNameByCode(countryCode, stateCode, cityCode);
  104. // inputField.text = (xxx.Item1 + " " + xxx.Item2 + " " + xxx.Item3).Trim();
  105. //渲染后端返回的gps地理位置
  106. countryCode = HRB_UserInfo.current.country;
  107. stateCode = HRB_UserInfo.current.state;
  108. cityCode = HRB_UserInfo.current.city;
  109. inputField.text = (countryCode + " " + stateCode + " " + cityCode).Trim();
  110. }
  111. }
  112. nameText.text = HRB_UserInfo.current.nickname;
  113. avatarImage.sprite = RoleMgr.GetAvatar(HRB_UserInfo.current.avatarID);
  114. // HomeView.ins.RenderNameOrGender();
  115. }
  116. #region Picker
  117. [SerializeField] GameObject datePickerPrefab;
  118. public void OpenDatePicker() {
  119. GameObject o = GameObject.Instantiate(datePickerPrefab);
  120. o.GetComponentInChildren<JC.Unity.Picker.DatePickerGroup>().onEnter += (JC.Unity.Picker.DatePickerGroup picker) => {
  121. GetInputField(inputs.transform.GetChild(4)).text = picker.GetSelectDateStr();
  122. };
  123. }
  124. [SerializeField] GameObject locationPickerPrefab;
  125. private string countryCode = "", stateCode = "", cityCode = "";
  126. public void OpenLocationPicker() {
  127. // GameObject o = GameObject.Instantiate(locationPickerPrefab);
  128. // o.GetComponentInChildren<JC.Unity.Picker.LocationPickerGroup>().onEnter += (JC.Unity.Picker.LocationInfo info) => {
  129. // countryCode = info.GetCountryRegion().Item2;
  130. // stateCode = info.GetState().Item2;
  131. // cityCode = info.GetCity().Item2;
  132. // GetInputField(inputs.transform.GetChild(6)).text =
  133. // info.GetCountryRegion().Item1 + " " +
  134. // info.GetState().Item1 + " " +
  135. // info.GetCity().Item1;
  136. // };
  137. //2022-12-6 gps获取地理位置
  138. System.Action eOnAgree = () => {
  139. GPSTool.GetAddress((address) => {
  140. if (address != null) {
  141. countryCode = address[0];
  142. stateCode = address[1];
  143. cityCode = address[2];
  144. GetInputField(inputs.transform.GetChild(6)).text =
  145. countryCode + " " +
  146. stateCode + " " +
  147. cityCode;
  148. }
  149. });
  150. };
  151. if (!HomeView.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
  152. }
  153. #endregion
  154. #region 头像选择
  155. bool avatarSelectViewInited = false;
  156. int curAvatarSelectID = 0;
  157. public void ShowAvatarSelectView() {
  158. AudioMgr.ins.PlayBtn();
  159. Transform avatarSelectView = this.transform.Find("AvatarSelectView");
  160. avatarSelectView.gameObject.SetActive(true);
  161. if (avatarSelectViewInited) {
  162. } else {
  163. avatarSelectViewInited = true;
  164. curAvatarSelectID = HRB_UserInfo.current.avatarID;
  165. avatarSelectView.Find("FrameBox/BtnClose").GetComponent<Button>().onClick.AddListener(CloseAvatarSelectView);
  166. GridLayoutGroup gridLayoutGroup = this.transform.GetComponentInChildren<GridLayoutGroup>();
  167. GameObject avatarPrefab = gridLayoutGroup.transform.Find("Avatar").gameObject;
  168. for (int id = 0; id < RoleMgr.GetAvatarListLen(); id++) {
  169. if (RoleMgr.IsRoleAvatar(id)) continue;
  170. GameObject avatar = GameObject.Instantiate(avatarPrefab, gridLayoutGroup.transform);
  171. avatar.transform.Find("Mask/Sprite").GetComponent<Image>().sprite = RoleMgr.GetAvatar(id);
  172. avatar.transform.Find("Check").gameObject.SetActive(id == curAvatarSelectID);
  173. avatar.gameObject.name = id.ToString();
  174. int aid = id; //记录该值
  175. avatar.GetComponent<Button>().onClick.AddListener(() => {
  176. AudioMgr.ins.PlayBtn();
  177. curAvatarSelectID = aid;
  178. for (int i = 0; i < gridLayoutGroup.transform.childCount; i++) {
  179. Transform item = gridLayoutGroup.transform.GetChild(i);
  180. int theAvatarID = int.Parse(item.gameObject.name);
  181. item.Find("Check").gameObject.SetActive(theAvatarID == curAvatarSelectID);
  182. }
  183. });
  184. }
  185. Destroy(avatarPrefab);
  186. LayoutRebuilder.ForceRebuildLayoutImmediate(gridLayoutGroup.transform.parent.GetComponent<RectTransform>());
  187. }
  188. }
  189. public void CloseAvatarSelectView() {
  190. AudioMgr.ins.PlayBtn();
  191. this.transform.Find("AvatarSelectView").gameObject.SetActive(false);
  192. if (curAvatarSelectID != HRB_UserInfo.current.avatarID) {
  193. HRB_UserInfo.current.avatarID = curAvatarSelectID;
  194. //render
  195. avatarImage.sprite = RoleMgr.GetAvatar(curAvatarSelectID);
  196. // HomeView.ins.RenderMyAvatarSprite();
  197. //save data
  198. HRB_UserInfo.current.Save();
  199. }
  200. }
  201. #endregion
  202. MeUserInfo meUserInfo;
  203. private class MeUserInfo {
  204. private HRB_MeView m_context;
  205. public MeUserInfo(HRB_MeView context) {
  206. m_context = context;
  207. }
  208. #region UserInfo关联属性
  209. public string nickname;
  210. public int gender;
  211. public string phone;
  212. public string birthday;
  213. public string country;
  214. public string state;
  215. public string city;
  216. #endregion
  217. private void RefreshValues() {
  218. //从组件中读取值
  219. string[] texts = new string[7];
  220. for (int i = 0; i < m_context.inputs.transform.childCount; i++)
  221. {
  222. if (i == 3) {
  223. Transform toggleGroup = m_context.inputs.transform.GetChild(i).Find("ToggleGroup");
  224. texts[i] = toggleGroup.GetChild(0).GetComponent<Toggle>().isOn ? "1" : "2";
  225. continue;
  226. }
  227. InputField inputField = m_context.GetInputField(m_context.inputs.transform.GetChild(i));
  228. texts[i] = inputField.text.Trim();
  229. }
  230. //缓存读取到的值
  231. this.nickname = texts[1];
  232. this.phone = texts[2];
  233. this.gender = int.Parse(texts[3]);
  234. this.birthday = texts[4];
  235. this.country = m_context.countryCode;
  236. this.state = m_context.stateCode;
  237. this.city = m_context.cityCode;
  238. }
  239. public bool Save(System.Func<bool> interceptor = null) {
  240. RefreshValues();
  241. if (interceptor != null) {
  242. if (interceptor.Invoke()) return false;
  243. }
  244. HRB_UserInfo.current.nickname = this.nickname;
  245. HRB_UserInfo.current.phone = this.phone;
  246. HRB_UserInfo.current.gender = this.gender;
  247. HRB_UserInfo.current.birthday = this.birthday;
  248. HRB_UserInfo.current.country = this.country;
  249. HRB_UserInfo.current.state = this.state;
  250. HRB_UserInfo.current.city = this.city;
  251. HRB_UserInfo.current.Save();
  252. return true;
  253. }
  254. private bool IsEqualOrigin() {
  255. return
  256. HRB_UserInfo.current.nickname == this.nickname &&
  257. HRB_UserInfo.current.phone == this.phone &&
  258. HRB_UserInfo.current.gender == this.gender &&
  259. HRB_UserInfo.current.birthday == this.birthday &&
  260. HRB_UserInfo.current.country == this.country &&
  261. HRB_UserInfo.current.state == this.state &&
  262. HRB_UserInfo.current.city == this.city;
  263. }
  264. ModalView modalViewForSave;
  265. public bool ShowModalForSave() {
  266. RefreshValues();
  267. if (IsEqualOrigin()) return false;
  268. if (!modalViewForSave) {
  269. modalViewForSave = ModalView.Show();
  270. modalViewForSave.textKey= "me_askSave";
  271. modalViewForSave.onAgreeTextKey = "common_yes";
  272. modalViewForSave.onRejectTextKey = "common_no";
  273. modalViewForSave.onAgree = () => {
  274. bool res = m_context.ExcuteLogic_SaveUserInfo(false);
  275. if (res) {
  276. m_context.ExcuteLogic_Destroy();
  277. }
  278. };
  279. modalViewForSave.onReject = () => m_context.ExcuteLogic_Destroy();
  280. }
  281. return true;
  282. }
  283. }
  284. }