Selaa lähdekoodia

我的信息页面,保存检测

lvjincheng 3 vuotta sitten
vanhempi
sitoutus
d6c9f5dfe1

+ 1 - 1
Assets/BowArrow/Resources/Prefabs/Views/MeView.prefab

@@ -4646,7 +4646,7 @@ RectTransform:
   m_AnchorMin: {x: 0, y: 0.5}
   m_AnchorMax: {x: 0, y: 0.5}
   m_AnchoredPosition: {x: 155, y: 0}
-  m_SizeDelta: {x: 300, y: 60}
+  m_SizeDelta: {x: 369.4581, y: 60}
   m_Pivot: {x: 0, y: 0.5}
 --- !u!222 &5539318500543726262
 CanvasRenderer:

+ 4 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/cn.json

@@ -5,6 +5,8 @@
     "common_reject": "拒绝",
     "common_add": "添加",
     "common_del": "删除",
+    "common_yes": "是",
+    "common_no": "否",
     "login_placeholder_user": "请输入您的账号",
     "login_placeholder_password": "请输入您的密码",
     "login_placeholder_graphicCode": "请输入图形验证码",
@@ -16,6 +18,8 @@
 
     "home_loginAuth": "正在认证登录。。。",
 
+    "me_askSave": "您修改了信息,是否保存?",
+
     "course_btn-see": "查看",
     "course_title_0": "新手指导",
     "course_title_1": "弓的介绍及护具佩戴",

+ 4 - 0
Assets/BowArrow/Scripts/Components/TextAutoLanguage2/Resources/TextAutoLanguage2/en.json

@@ -5,6 +5,8 @@
     "common_reject": "Reject",
     "common_add": "Add",
     "common_del": "Delete",
+    "common_yes": "Yes",
+    "common_no": "No",
     "login_placeholder_user": "Please Input Your Username",
     "login_placeholder_password": "Please Input Your Password",
     "login_placeholder_graphicCode": "Please Input GraphicCode",
@@ -16,6 +18,8 @@
 
     "home_loginAuth": "Authenticating Login ...",
 
+    "me_askSave": "You have modified the information, do you want to save it?",
+
     "course_btn-see": "see",
     "course_title_0": "Novice guidance",
     "course_title_1": "Introduction of bow and equipment",

+ 117 - 29
Assets/BowArrow/Scripts/View/MeView.cs

@@ -13,6 +13,8 @@ public class MeView : MonoBehaviour, MenuBackInterface
     {
         PersistenHandler.ins?.menuBackCtr.views.Add(this);
 
+        meUserInfo = new MeUserInfo(this);
+
         this.transform.Find("AvatarFrame").GetComponent<Button>().onClick.AddListener(ShowAvatarSelectView);
 
         RenderAfterSave();
@@ -24,48 +26,48 @@ public class MeView : MonoBehaviour, MenuBackInterface
     }
 
     public bool OnMenuBack() {
-        Destroy(gameObject);
-        return true;
+        if (!meUserInfo.ShowModalForSave()) {
+            ExcuteLogic_Destroy();
+            return true;
+        } else {
+            return false;
+        }
     }
 
     public void Back() {
         AudioMgr.ins.PlayBtn();
-        Destroy(this.gameObject);
+        if (!meUserInfo.ShowModalForSave()) {
+            ExcuteLogic_Destroy();
+        }
+    }
+
+    private void ExcuteLogic_Destroy() {
+        Destroy(gameObject);
     }
 
-    JCUnityLib.Throttler throttlerSave = new JCUnityLib.Throttler(2000);
     public void Save() 
     {
         AudioMgr.ins.PlayBtn();
-        string[] texts = new string[7];
-        for (int i = 0; i < inputs.transform.childCount; i++)
-        {
-            if (i == 3) {
-                Transform toggleGroup = inputs.transform.GetChild(i).Find("ToggleGroup");
-                texts[i] = toggleGroup.GetChild(0).GetComponent<Toggle>().isOn ? "1" : "2";
-                continue;
-            } 
-            InputField inputField = GetInputField(inputs.transform.GetChild(i));
-            if (i == 1 && inputField.text.Trim().Length == 0) {
+        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;
+                return true;
             }
-            texts[i] = inputField.text.Trim();
-        }
-        if (throttlerSave.CanPass() == false) {
-            PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
-            return;
-        }
-        LoginMgr.myUserInfo.nickname = texts[1];
-        LoginMgr.myUserInfo.phone = texts[2];
-        LoginMgr.myUserInfo.gender = int.Parse(texts[3]);
-        LoginMgr.myUserInfo.birthday = texts[4];
-        LoginMgr.myUserInfo.country = countryCode;
-        LoginMgr.myUserInfo.state = stateCode;
-        LoginMgr.myUserInfo.city = cityCode;
-        LoginMgr.myUserInfo.Save();
+            if (useThrottler && throttlerSave.CanPass() == false) {
+                PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
+                return true;
+            }
+            return false;
+        };
+        if (!meUserInfo.Save(interceptor)) return false;
         PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("保存成功"));
         RenderAfterSave();
+        return true;
     }
 
     InputField GetInputField(Transform transform)
@@ -201,4 +203,90 @@ public class MeView : MonoBehaviour, MenuBackInterface
             }
         }
     #endregion
+    MeUserInfo meUserInfo;
+    private class MeUserInfo {
+        private MeView m_context;
+        public MeUserInfo(MeView context) {
+            m_context = context;
+        }
+
+        #region UserInfo关联属性
+        public string nickname;
+        public int gender;
+        public string phone;
+        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.phone = texts[2];
+            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;
+            }
+            LoginMgr.myUserInfo.nickname = this.nickname;
+            LoginMgr.myUserInfo.phone = this.phone;
+            LoginMgr.myUserInfo.gender = this.gender;
+            LoginMgr.myUserInfo.birthday = this.birthday;
+            LoginMgr.myUserInfo.country = this.country;
+            LoginMgr.myUserInfo.state = this.state;
+            LoginMgr.myUserInfo.city = this.city;
+            LoginMgr.myUserInfo.Save();
+            return true;
+        }   
+        private bool IsEqualOrigin() {
+            return 
+                LoginMgr.myUserInfo.nickname == this.nickname &&
+                LoginMgr.myUserInfo.phone == this.phone &&
+                LoginMgr.myUserInfo.gender == this.gender &&
+                LoginMgr.myUserInfo.birthday == this.birthday &&
+                LoginMgr.myUserInfo.country == this.country &&
+                LoginMgr.myUserInfo.state == this.state &&
+                LoginMgr.myUserInfo.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;
+        }
+    }
+
 }

+ 12 - 2
Assets/BowArrow/Scripts/View/ModalView.cs

@@ -10,7 +10,9 @@ public class ModalView : MonoBehaviour
     public string textKey;
     public object[] textFormatArgs = {};
     public UnityAction onAgree;
+    public string onAgreeTextKey;
     public UnityAction onReject;
+    public string onRejectTextKey;
 
 
     public static ModalView Show()
@@ -29,13 +31,21 @@ public class ModalView : MonoBehaviour
         } else {
             transform.Find("Frame/Text").GetComponent<Text>().text = text;
         }
-        transform.Find("Frame/BtnReject").GetComponent<Button>().onClick.AddListener(() => {
+        Transform btnReject = transform.Find("Frame/BtnReject");
+        btnReject.GetComponent<Button>().onClick.AddListener(() => {
             onReject?.Invoke();
             Destroy(gameObject);
         });
-        transform.Find("Frame/BtnAgree").GetComponent<Button>().onClick.AddListener(() => {
+        Transform btnAgree = transform.Find("Frame/BtnAgree");
+        btnAgree.GetComponent<Button>().onClick.AddListener(() => {
             onAgree?.Invoke();
             Destroy(gameObject);
         });
+        if (!string.IsNullOrEmpty(onAgreeTextKey)) {
+            btnAgree.GetComponentInChildren<TextAutoLanguage2>().SetTextKey(onAgreeTextKey);
+        }
+        if (!string.IsNullOrEmpty(onRejectTextKey)) {
+            btnReject.GetComponentInChildren<TextAutoLanguage2>().SetTextKey(onRejectTextKey);
+        }
     }
 }