HRB_HomeView.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using JCUnityLib;
  7. using DG.Tweening;
  8. using Newtonsoft.Json;
  9. using UnityEngine.SceneManagement;
  10. public class HRB_HomeView : ViewBase
  11. {
  12. HRB_Bluetooth _hRB_Bluetooth;
  13. Text _btn_ble_text;
  14. void Start()
  15. {
  16. InitButtonAB();
  17. InitColumnAB();
  18. InitGoal(3000, 300);
  19. transform.Find("Btn_Degrease").GetComponent<Button>().onClick.AddListener(() => {
  20. AudioMgr.ins.PlayBtn();
  21. HRB_ViewMgr.Instance.ShowView<HRB_DegreaseView>();
  22. });
  23. transform.Find("Btn_计划").GetComponent<Button>().onClick.AddListener(() => {
  24. AudioMgr.ins.PlayBtn();
  25. HRB_ViewMgr.Instance.ShowView<HRB_PlanView>();
  26. });
  27. transform.Find("Btn_Me").GetComponent<Button>().onClick.AddListener(() => {
  28. AudioMgr.ins.PlayBtn();
  29. HRB_ViewMgr.Instance.ShowView<HRB_MeView>();
  30. });
  31. transform.Find("Btn_BLE").GetComponent<Button>().onClick.AddListener(() => {
  32. AudioMgr.ins.PlayBtn();
  33. if (HomeView.ShowProminentBeforeConnectBLE()) return;
  34. _hRB_Bluetooth.DoConnect();
  35. });
  36. _btn_ble_text = transform.Find("Btn_BLE").GetComponentInChildren<Text>();
  37. InitMask();
  38. _hRB_Bluetooth = gameObject.AddComponent<HRB_Bluetooth>();
  39. _hRB_Bluetooth.onFindTargetDevice = () => {
  40. PopupMgr.ins.ShowTip("发现目标设备");
  41. DoTweenUtil.CallDelay(1.5f, () => {
  42. PlayerPrefs.SetInt("CanGoToApp", 1);
  43. SceneManager.LoadScene("Entry");
  44. Destroy(HRB_ViewMgr.Instance.gameObject);
  45. });
  46. };
  47. }
  48. //Mask
  49. void InitMask()
  50. {
  51. Instantiate(SceneResourceManager.Instance.GetPrefab("AuthLoginMask"));
  52. if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(true);
  53. if (CommonConfig.businessServerWsURL != null) {
  54. LoadUserInfo();
  55. } else {
  56. CoroutineStarter.Start(LoginController.Instance.LoginByToken((res) => {
  57. if (res.code == 0) {
  58. string loginToken = (string)res.data;
  59. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  60. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  61. LoadUserInfo();
  62. } else {
  63. handleAuthExpire();
  64. }
  65. }));
  66. }
  67. }
  68. void LoadUserInfo()
  69. {
  70. StartCoroutine(HRB_Controller.Instance.getUserInfo((RequestResult res) => {
  71. if (res.code == 0) {
  72. var userInfo = JsonConvert.DeserializeObject<HRB_UserInfo>(res.data.ToString());
  73. if (userInfo == null) return;
  74. HRB_UserInfo.current = userInfo;
  75. RenderTextAB();
  76. if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(false);
  77. //获取地理位置
  78. System.Action eOnAgree = () => {
  79. GPSTool.GetAddress((address) => {
  80. if (address != null) {
  81. Debug.Log("登陆时获取地理位置成功:" + string.Join(" ", address));
  82. if (HRB_UserInfo.current.country == address[0]
  83. && HRB_UserInfo.current.state == address[1]
  84. && HRB_UserInfo.current.city == address[2]
  85. ) return;
  86. HRB_UserInfo.current.country = address[0];
  87. HRB_UserInfo.current.state = address[1];
  88. HRB_UserInfo.current.city = address[2];
  89. HRB_UserInfo.current.Save(false);
  90. }
  91. });
  92. };
  93. if (!HomeView.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
  94. }
  95. }));
  96. }
  97. void handleAuthExpire()
  98. {
  99. //tip
  100. if (AuthLoginMask.ins && AuthLoginMask.ins.IsVisiable()) {
  101. AuthLoginMask.ins.SetText(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  102. } else {
  103. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
  104. }
  105. //delay back login
  106. Sequence seq = DOTween.Sequence();
  107. seq.AppendInterval(1.5f);
  108. seq.AppendCallback(() => {
  109. if (AuthLoginMask.ins) Destroy(AuthLoginMask.ins.gameObject);
  110. HRB_ViewMgr.Instance.DestroyAllViews();
  111. HRB_ViewMgr.Instance.ShowView<HRB_LoginView>();
  112. });
  113. //close userplayer
  114. PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
  115. }
  116. //===初始化(今日目标)栏目===
  117. void InitGoal(int goalVal, int consumeVal)
  118. {
  119. var progressVal = Mathf.Clamp01((float)consumeVal / goalVal);
  120. transform.Find("今日目标/TextGoal").GetComponent<Text>().text = $"今日目标 {goalVal} kcal";
  121. transform.Find("今日目标/TextConsume").GetComponent<Text>().text = $"消耗卡路里 <size=50>{consumeVal}</size> kcal";
  122. var progress = transform.Find("今日目标/Progress") as RectTransform;
  123. float progressMaxLen = progress.rect.size.x;
  124. var mask = progress.Find("Mask") as RectTransform;
  125. var head = progress.Find("Head") as RectTransform;
  126. var headPos = head.anchoredPosition;
  127. var maskWidth = headPos.x = progressMaxLen * progressVal;
  128. mask.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, maskWidth);
  129. head.anchoredPosition = headPos;
  130. }
  131. //===切换体重/腰围===
  132. List<Button> buttonAB = new List<Button>();
  133. Color btnColorAB_Light;
  134. Color btnColorAB_Dark;
  135. int currentABSwitchIndex = 0;
  136. void InitButtonAB()
  137. {
  138. buttonAB.Add(transform.Find("Btn_体重").GetComponent<Button>());
  139. buttonAB.Add(transform.Find("Btn_腰围").GetComponent<Button>());
  140. btnColorAB_Light = buttonAB[0].GetComponent<Image>().color;
  141. btnColorAB_Dark = buttonAB[1].GetComponent<Image>().color;
  142. foreach (var item in buttonAB) item.onClick.AddListener(() => OnClick_SwitchAB(item));
  143. }
  144. void OnClick_SwitchAB(Button button)
  145. {
  146. AudioMgr.ins.PlayBtn();
  147. foreach (var item in buttonAB)
  148. {
  149. bool hit = item == button;
  150. item.GetComponent<Image>().color = hit ? btnColorAB_Light : btnColorAB_Dark;
  151. if (!hit) continue;
  152. currentABSwitchIndex = Array.IndexOf(buttonAB.ToArray(), item);
  153. RenderTextAB();
  154. }
  155. }
  156. //===初始化(体重腰围栏目)===
  157. Text textAB;
  158. InputField inputFieldAB;
  159. bool inputFieldAB_Focused;
  160. void InitColumnAB()
  161. {
  162. textAB = transform.Find("体重腰围/Text").GetComponent<Text>();
  163. inputFieldAB = transform.Find("体重腰围/InputField").GetComponent<InputField>();
  164. inputFieldAB.onEndEdit.AddListener((v) => {
  165. string s = v;
  166. inputFieldAB.text = "";
  167. int value = int.Parse(s);
  168. if (value >= 0 && value <= 250)
  169. {
  170. if (currentABSwitchIndex == 0) HRB_UserInfo.current.weight = value;
  171. if (currentABSwitchIndex == 1) HRB_UserInfo.current.waistLine = value;
  172. RenderTextAB();
  173. HRB_UserInfo.current.SaveLocalData();
  174. }
  175. else PopupMgr.ins.ShowTip("输入的值不在合理范围!");
  176. });
  177. RenderTextAB();
  178. }
  179. void RenderTextAB()
  180. {
  181. if (currentABSwitchIndex == 0) textAB.text = $"体重 <size=52>{HRB_UserInfo.current.weight}</size> kg";
  182. if (currentABSwitchIndex == 1) textAB.text = $"腰围 <size=52>{HRB_UserInfo.current.waistLine}</size> cm";
  183. }
  184. void Update()
  185. {
  186. if (!inputFieldAB_Focused && inputFieldAB.isFocused)
  187. {
  188. if (currentABSwitchIndex == 0) inputFieldAB.text = HRB_UserInfo.current.weight.ToString();
  189. if (currentABSwitchIndex == 1) inputFieldAB.text = HRB_UserInfo.current.waistLine.ToString();
  190. }
  191. inputFieldAB_Focused = inputFieldAB.isFocused;
  192. textAB.gameObject.SetActive(!inputFieldAB.isFocused);
  193. UpdateBtnForConnect();
  194. }
  195. BluetoothStatusEnum bowStatus;
  196. void UpdateBtnForConnect() {
  197. if (_hRB_Bluetooth && bowStatus != _hRB_Bluetooth.status) {
  198. bowStatus = _hRB_Bluetooth.status;
  199. _btn_ble_text.text = bowStatus == BluetoothStatusEnum.Connect ? "未连接" : "连接中";
  200. _btn_ble_text.color = bowStatus == BluetoothStatusEnum.Connect ? Color.white : Color.yellow;
  201. }
  202. }
  203. }