| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using JCUnityLib;
- using DG.Tweening;
- using Newtonsoft.Json;
- using UnityEngine.SceneManagement;
- public class HRB_HomeView : ViewBase
- {
- HRB_Bluetooth _hRB_Bluetooth;
- Text _btn_ble_text;
- void Start()
- {
- InitButtonAB();
- InitColumnAB();
- InitGoal(3000, 300);
- transform.Find("Btn_Degrease").GetComponent<Button>().onClick.AddListener(() => {
- AudioMgr.ins.PlayBtn();
- HRB_ViewMgr.Instance.ShowView<HRB_DegreaseView>();
- });
- transform.Find("Btn_计划").GetComponent<Button>().onClick.AddListener(() => {
- AudioMgr.ins.PlayBtn();
- HRB_ViewMgr.Instance.ShowView<HRB_PlanView>();
- });
- transform.Find("Btn_Me").GetComponent<Button>().onClick.AddListener(() => {
- AudioMgr.ins.PlayBtn();
- HRB_ViewMgr.Instance.ShowView<HRB_MeView>();
- });
- transform.Find("Btn_BLE").GetComponent<Button>().onClick.AddListener(() => {
- AudioMgr.ins.PlayBtn();
- if (HomeView.ShowProminentBeforeConnectBLE()) return;
- _hRB_Bluetooth.DoConnect();
- });
- _btn_ble_text = transform.Find("Btn_BLE").GetComponentInChildren<Text>();
- InitMask();
- _hRB_Bluetooth = gameObject.AddComponent<HRB_Bluetooth>();
- _hRB_Bluetooth.onFindTargetDevice = () => {
- PopupMgr.ins.ShowTip("发现目标设备");
- DoTweenUtil.CallDelay(1.5f, () => {
- PlayerPrefs.SetInt("CanGoToApp", 1);
- SceneManager.LoadScene("Entry");
- Destroy(HRB_ViewMgr.Instance.gameObject);
- });
- };
- }
- //Mask
- void InitMask()
- {
- Instantiate(SceneResourceManager.Instance.GetPrefab("AuthLoginMask"));
- if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(true);
- if (CommonConfig.businessServerWsURL != null) {
- LoadUserInfo();
- } else {
- CoroutineStarter.Start(LoginController.Instance.LoginByToken((res) => {
- if (res.code == 0) {
- string loginToken = (string)res.data;
- CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
- PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
- LoadUserInfo();
- } else {
- handleAuthExpire();
- }
- }));
- }
- }
- void LoadUserInfo()
- {
- StartCoroutine(HRB_Controller.Instance.getUserInfo((RequestResult res) => {
- if (res.code == 0) {
- var userInfo = JsonConvert.DeserializeObject<HRB_UserInfo>(res.data.ToString());
- if (userInfo == null) return;
- HRB_UserInfo.current = userInfo;
- RenderTextAB();
- if (AuthLoginMask.ins) AuthLoginMask.ins.SetVisiable(false);
- //获取地理位置
- System.Action eOnAgree = () => {
- GPSTool.GetAddress((address) => {
- if (address != null) {
- Debug.Log("登陆时获取地理位置成功:" + string.Join(" ", address));
- if (HRB_UserInfo.current.country == address[0]
- && HRB_UserInfo.current.state == address[1]
- && HRB_UserInfo.current.city == address[2]
- ) return;
- HRB_UserInfo.current.country = address[0];
- HRB_UserInfo.current.state = address[1];
- HRB_UserInfo.current.city = address[2];
- HRB_UserInfo.current.Save(false);
- }
- });
- };
- if (!HomeView.ShowProminentBeforeConnectBLE(eOnAgree)) eOnAgree.Invoke();
- }
- }));
- }
- void handleAuthExpire()
- {
- //tip
- if (AuthLoginMask.ins && AuthLoginMask.ins.IsVisiable()) {
- AuthLoginMask.ins.SetText(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
- } else {
- PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("登录认证过期"));
- }
- //delay back login
- Sequence seq = DOTween.Sequence();
- seq.AppendInterval(1.5f);
- seq.AppendCallback(() => {
- if (AuthLoginMask.ins) Destroy(AuthLoginMask.ins.gameObject);
- HRB_ViewMgr.Instance.DestroyAllViews();
- HRB_ViewMgr.Instance.ShowView<HRB_LoginView>();
- });
- //close userplayer
- PlayerPrefs.DeleteKey(LoginMgr.LoginTokenKey);
- }
- //===初始化(今日目标)栏目===
- void InitGoal(int goalVal, int consumeVal)
- {
- var progressVal = Mathf.Clamp01((float)consumeVal / goalVal);
- transform.Find("今日目标/TextGoal").GetComponent<Text>().text = $"今日目标 {goalVal} kcal";
- transform.Find("今日目标/TextConsume").GetComponent<Text>().text = $"消耗卡路里 <size=50>{consumeVal}</size> kcal";
- var progress = transform.Find("今日目标/Progress") as RectTransform;
- float progressMaxLen = progress.rect.size.x;
- var mask = progress.Find("Mask") as RectTransform;
- var head = progress.Find("Head") as RectTransform;
- var headPos = head.anchoredPosition;
- var maskWidth = headPos.x = progressMaxLen * progressVal;
- mask.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, maskWidth);
- head.anchoredPosition = headPos;
- }
- //===切换体重/腰围===
- List<Button> buttonAB = new List<Button>();
- Color btnColorAB_Light;
- Color btnColorAB_Dark;
- int currentABSwitchIndex = 0;
- void InitButtonAB()
- {
- buttonAB.Add(transform.Find("Btn_体重").GetComponent<Button>());
- buttonAB.Add(transform.Find("Btn_腰围").GetComponent<Button>());
- btnColorAB_Light = buttonAB[0].GetComponent<Image>().color;
- btnColorAB_Dark = buttonAB[1].GetComponent<Image>().color;
- foreach (var item in buttonAB) item.onClick.AddListener(() => OnClick_SwitchAB(item));
- }
- void OnClick_SwitchAB(Button button)
- {
- AudioMgr.ins.PlayBtn();
- foreach (var item in buttonAB)
- {
- bool hit = item == button;
- item.GetComponent<Image>().color = hit ? btnColorAB_Light : btnColorAB_Dark;
- if (!hit) continue;
- currentABSwitchIndex = Array.IndexOf(buttonAB.ToArray(), item);
- RenderTextAB();
- }
- }
- //===初始化(体重腰围栏目)===
- Text textAB;
- InputField inputFieldAB;
- bool inputFieldAB_Focused;
- void InitColumnAB()
- {
- textAB = transform.Find("体重腰围/Text").GetComponent<Text>();
- inputFieldAB = transform.Find("体重腰围/InputField").GetComponent<InputField>();
- inputFieldAB.onEndEdit.AddListener((v) => {
- string s = v;
- inputFieldAB.text = "";
- int value = int.Parse(s);
- if (value >= 0 && value <= 250)
- {
- if (currentABSwitchIndex == 0) HRB_UserInfo.current.weight = value;
- if (currentABSwitchIndex == 1) HRB_UserInfo.current.waistLine = value;
- RenderTextAB();
- HRB_UserInfo.current.SaveLocalData();
- }
- else PopupMgr.ins.ShowTip("输入的值不在合理范围!");
- });
- RenderTextAB();
- }
- void RenderTextAB()
- {
- if (currentABSwitchIndex == 0) textAB.text = $"体重 <size=52>{HRB_UserInfo.current.weight}</size> kg";
- if (currentABSwitchIndex == 1) textAB.text = $"腰围 <size=52>{HRB_UserInfo.current.waistLine}</size> cm";
- }
- void Update()
- {
- if (!inputFieldAB_Focused && inputFieldAB.isFocused)
- {
- if (currentABSwitchIndex == 0) inputFieldAB.text = HRB_UserInfo.current.weight.ToString();
- if (currentABSwitchIndex == 1) inputFieldAB.text = HRB_UserInfo.current.waistLine.ToString();
- }
- inputFieldAB_Focused = inputFieldAB.isFocused;
- textAB.gameObject.SetActive(!inputFieldAB.isFocused);
- UpdateBtnForConnect();
- }
- BluetoothStatusEnum bowStatus;
- void UpdateBtnForConnect() {
- if (_hRB_Bluetooth && bowStatus != _hRB_Bluetooth.status) {
- bowStatus = _hRB_Bluetooth.status;
- _btn_ble_text.text = bowStatus == BluetoothStatusEnum.Connect ? "未连接" : "连接中";
- _btn_ble_text.color = bowStatus == BluetoothStatusEnum.Connect ? Color.white : Color.yellow;
- }
- }
- }
|