| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using JCUnityLib;
- public class HRB_MeView : JCUnityLib.ViewBase, MenuBackInterface
- {
- [SerializeField] Image avatarImage;
- [SerializeField] Text nameText;
- [SerializeField] GameObject inputs;
- void Start()
- {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- meUserInfo = new MeUserInfo(this);
- this.transform.Find("AvatarFrame").GetComponent<Button>().onClick.AddListener(ShowAvatarSelectView);
- RenderAfterSave();
- }
- void OnDestroy()
- {
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- }
- public void OnClick_QuitLogin()
- {
- AudioMgr.ins.PlayBtn();
- PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
- HRB_ViewMgr.Instance.DestroyAllViews();
- HRB_ViewMgr.Instance.ShowView<HRB_LoginView>();
- }
- public void OnClick_QuitApp()
- {
- AudioMgr.ins.PlayBtn();
- Application.Quit();
- }
- public bool OnMenuBack() {
- if (!meUserInfo.ShowModalForSave()) {
- ExcuteLogic_Destroy();
- return true;
- } else {
- return false;
- }
- }
- public void Back() {
- AudioMgr.ins.PlayBtn();
- if (!meUserInfo.ShowModalForSave()) {
- ExcuteLogic_Destroy();
- }
- }
- private void ExcuteLogic_Destroy() {
- HRB_ViewMgr.Instance.DestroyView<HRB_MeView>();
- }
- public void Save()
- {
- AudioMgr.ins.PlayBtn();
- ExcuteLogic_SaveUserInfo(true);
- }
- JCUnityLib.Throttler throttlerSave = new JCUnityLib.Throttler(3000);
- private bool ExcuteLogic_SaveUserInfo(bool useThrottler) {
- System.Func<bool> interceptor = delegate() {
- if (string.IsNullOrEmpty(meUserInfo.nickname)) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("昵称不能为空"));
- return true;
- }
- if (useThrottler && throttlerSave.CanPass() == false) {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
- return true;
- }
- return false;
- };
- if (!meUserInfo.Save(interceptor)) return false;
- RenderAfterSave();
- return true;
- }
- InputField GetInputField(Transform transform)
- {
- return transform.Find("InputField").GetComponent<InputField>();
- }
- public void RenderAfterSave() {
- for (int i = 0; i < inputs.transform.childCount; i++)
- {
- if (i == 3) {
- inputs.transform.GetChild(i).Find("ToggleGroup")
- .GetChild(HRB_UserInfo.current.gender == 2 ? 1 : 0)
- .GetComponent<Toggle>().isOn = true;
- continue;
- }
- InputField inputField = GetInputField(inputs.transform.GetChild(i));
- if (i == 0) {
- inputField.text = HRB_UserInfo.current.id.ToString();
- }
- if (i == 1) {
- inputField.text = HRB_UserInfo.current.nickname;
- }
- else if (i == 4) {
- inputField.text = HRB_UserInfo.current.birthday;
- }
- else if (i == 6) {
- // countryCode = HRB_UserInfo.current.country;
- // stateCode = HRB_UserInfo.current.state;
- // cityCode = HRB_UserInfo.current.city;
- // System.Tuple<string, string, string> xxx = JC.Unity.Picker.LocationParseComponent.ins.ParseNameByCode(countryCode, stateCode, cityCode);
- // inputField.text = (xxx.Item1 + " " + xxx.Item2 + " " + xxx.Item3).Trim();
- //渲染后端返回的gps地理位置
- countryCode = HRB_UserInfo.current.country;
- stateCode = HRB_UserInfo.current.state;
- cityCode = HRB_UserInfo.current.city;
- inputField.text = (countryCode + " " + stateCode + " " + cityCode).Trim();
- }
- }
- nameText.text = HRB_UserInfo.current.nickname;
- RoleMgr.SetAvatarToImage(avatarImage, HRB_UserInfo.current.avatarID, "");
- // HomeView.ins.RenderNameOrGender();
- }
- #region Picker
- [SerializeField] GameObject datePickerPrefab;
- public void OpenDatePicker() {
- GameObject o = GameObject.Instantiate(datePickerPrefab);
- o.GetComponentInChildren<JC.Unity.Picker.DatePickerGroup>().onEnter += (JC.Unity.Picker.DatePickerGroup picker) => {
- GetInputField(inputs.transform.GetChild(4)).text = picker.GetSelectDateStr();
- };
- }
- [SerializeField] GameObject locationPickerPrefab;
- private string countryCode = "", stateCode = "", cityCode = "";
- public void OpenLocationPicker() {
- // GameObject o = GameObject.Instantiate(locationPickerPrefab);
- // o.GetComponentInChildren<JC.Unity.Picker.LocationPickerGroup>().onEnter += (JC.Unity.Picker.LocationInfo info) => {
- // countryCode = info.GetCountryRegion().Item2;
- // stateCode = info.GetState().Item2;
- // cityCode = info.GetCity().Item2;
- // GetInputField(inputs.transform.GetChild(6)).text =
- // info.GetCountryRegion().Item1 + " " +
- // info.GetState().Item1 + " " +
- // info.GetCity().Item1;
- // };
- //2022-12-6 gps获取地理位置
- System.Action eOnAgree = () => {
- GPSTool.GetAddress((address) => {
- if (address != null) {
- countryCode = address[0];
- stateCode = address[1];
- cityCode = address[2];
- GetInputField(inputs.transform.GetChild(6)).text =
- countryCode + " " +
- stateCode + " " +
- cityCode;
- }
- });
- };
- if (!HomeView.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
- }
- #endregion
- #region 头像选择
- bool avatarSelectViewInited = false;
- int curAvatarSelectID = 0;
- public void ShowAvatarSelectView() {
- AudioMgr.ins.PlayBtn();
- Transform avatarSelectView = this.transform.Find("AvatarSelectView");
- avatarSelectView.gameObject.SetActive(true);
- if (avatarSelectViewInited) {
- } else {
- avatarSelectViewInited = true;
- curAvatarSelectID = HRB_UserInfo.current.avatarID;
- avatarSelectView.Find("FrameBox/BtnClose").GetComponent<Button>().onClick.AddListener(CloseAvatarSelectView);
- GridLayoutGroup gridLayoutGroup = this.transform.GetComponentInChildren<GridLayoutGroup>();
- GameObject avatarPrefab = gridLayoutGroup.transform.Find("Avatar").gameObject;
- for (int id = 0; id < RoleMgr.GetAvatarListLen(); id++) {
- if (RoleMgr.IsRoleAvatar(id)) continue;
- GameObject avatar = GameObject.Instantiate(avatarPrefab, gridLayoutGroup.transform);
- Image avatarImage = avatar.transform.Find("Mask/Sprite").GetComponent<Image>();
- RoleMgr.SetAvatarToImage(avatarImage, id, "");
- avatar.transform.Find("Check").gameObject.SetActive(id == curAvatarSelectID);
- avatar.gameObject.name = id.ToString();
- int aid = id; //记录该值
- avatar.GetComponent<Button>().onClick.AddListener(() => {
- AudioMgr.ins.PlayBtn();
- curAvatarSelectID = aid;
- for (int i = 0; i < gridLayoutGroup.transform.childCount; i++) {
- Transform item = gridLayoutGroup.transform.GetChild(i);
- int theAvatarID = int.Parse(item.gameObject.name);
- item.Find("Check").gameObject.SetActive(theAvatarID == curAvatarSelectID);
- }
- });
- }
- Destroy(avatarPrefab);
- LayoutRebuilder.ForceRebuildLayoutImmediate(gridLayoutGroup.transform.parent.GetComponent<RectTransform>());
- }
- }
- public void CloseAvatarSelectView() {
- AudioMgr.ins.PlayBtn();
- this.transform.Find("AvatarSelectView").gameObject.SetActive(false);
- if (curAvatarSelectID != HRB_UserInfo.current.avatarID) {
- HRB_UserInfo.current.avatarID = curAvatarSelectID;
- //render
- RoleMgr.SetAvatarToImage(avatarImage, curAvatarSelectID, "");
- // HomeView.ins.RenderMyAvatarSprite();
- //save data
- HRB_UserInfo.current.Save();
- }
- }
- #endregion
- MeUserInfo meUserInfo;
- private class MeUserInfo {
- private HRB_MeView m_context;
- public MeUserInfo(HRB_MeView context) {
- m_context = context;
- }
- #region UserInfo关联属性
- public string nickname;
- public int gender;
- public string birthday;
- public string country;
- public string state;
- public string city;
- #endregion
- private void RefreshValues() {
- //从组件中读取值
- string[] texts = new string[7];
- for (int i = 0; i < m_context.inputs.transform.childCount; i++)
- {
- if (i == 3) {
- Transform toggleGroup = m_context.inputs.transform.GetChild(i).Find("ToggleGroup");
- texts[i] = toggleGroup.GetChild(0).GetComponent<Toggle>().isOn ? "1" : "2";
- continue;
- }
- InputField inputField = m_context.GetInputField(m_context.inputs.transform.GetChild(i));
- texts[i] = inputField.text.Trim();
- }
- //缓存读取到的值
- this.nickname = texts[1];
- this.gender = int.Parse(texts[3]);
- this.birthday = texts[4];
- this.country = m_context.countryCode;
- this.state = m_context.stateCode;
- this.city = m_context.cityCode;
- }
- public bool Save(System.Func<bool> interceptor = null) {
- RefreshValues();
- if (interceptor != null) {
- if (interceptor.Invoke()) return false;
- }
- HRB_UserInfo.current.nickname = this.nickname;
- HRB_UserInfo.current.gender = this.gender;
- HRB_UserInfo.current.birthday = this.birthday;
- HRB_UserInfo.current.country = this.country;
- HRB_UserInfo.current.state = this.state;
- HRB_UserInfo.current.city = this.city;
- HRB_UserInfo.current.Save();
- return true;
- }
- private bool IsEqualOrigin() {
- return
- HRB_UserInfo.current.nickname == this.nickname &&
- HRB_UserInfo.current.gender == this.gender &&
- HRB_UserInfo.current.birthday == this.birthday &&
- HRB_UserInfo.current.country == this.country &&
- HRB_UserInfo.current.state == this.state &&
- HRB_UserInfo.current.city == this.city;
- }
- ModalView modalViewForSave;
- public bool ShowModalForSave() {
- RefreshValues();
- if (IsEqualOrigin()) return false;
- if (!modalViewForSave) {
- modalViewForSave = ModalView.Show();
- modalViewForSave.textKey= "me_askSave";
- modalViewForSave.onAgreeTextKey = "common_yes";
- modalViewForSave.onRejectTextKey = "common_no";
- modalViewForSave.onAgree = () => {
- bool res = m_context.ExcuteLogic_SaveUserInfo(false);
- if (res) {
- m_context.ExcuteLogic_Destroy();
- }
- };
- modalViewForSave.onReject = () => m_context.ExcuteLogic_Destroy();
- }
- return true;
- }
- }
- }
|