MeView.cs 12 KB

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