import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sport/application.dart'; import 'package:sport/bean/login.dart'; import 'package:sport/bean/sport_detail.dart'; import 'package:sport/provider/bluetooth.dart'; import 'package:sport/router/navigator_util.dart'; import 'package:sport/utils/sport_utils.dart'; class UserModel with ChangeNotifier { User _user = User(); String? _token; User get user => _user; String get token => _token ?? ""; double latitude = 0, longitude = 0; ValueNotifier durationTarget = ValueNotifier(160); ValueNotifier runUserCount = ValueNotifier(0); UserModel() { initUser(); } /// 初始化 User Future initUser() async { SharedPreferences prefs = await SharedPreferences.getInstance(); Map map = {}; prefs.getKeys().forEach((element) { map.putIfAbsent(element, () => prefs.get(element)); }); print(map); _token = prefs.getString("token"); saveUserInfo(User.fromJson(map)); if (prefs.containsKey("run_latitude")) { latitude = prefs.getDouble("run_latitude") ?? 0; longitude = prefs.getDouble("run_longitude") ?? 0; } } /// 保存用户信息到 sp saveUserInfo(User user) { _user = user; // print("User ${_user.gender}"); weight = user.weight ?? 60.0; gender = user.gender ?? 0; notifyListeners(); } saveUserInfoAvatar(String avatar) { _user.avatar = avatar; notifyListeners(); } clear() { _token = null; } logout(BuildContext context) async { clear(); SharedPreferences prefs = await SharedPreferences.getInstance(); await prefs.clear(); await GetIt.I().clearDevice(deleteStep: true); NavigatorUtil.goLogin(context); } Future sport(RecordsTodaySum? item) async { SharedPreferences prefs = await SharedPreferences.getInstance(); int type = prefs.getInt("sport_type") ?? 0; int consume = item?.consume ?? 0; if((item?.consume_jog ?? 0) > 0 && type == 0){ consume -= item?.consume_jog ?? 0; return SportUtils.calSportStr(type, SportUtils.calSportTypeData(type, consume, _user.weight ?? weight) + (item?.distance_jog ?? 0) / 1000.0); }else { return SportUtils.calSportType(type, consume, _user.weight ?? weight); } } }