import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sport/bean/login.dart';

class UserModel with ChangeNotifier {
  User _user = User();
  String _token;

  User get user => _user;
  String get token => _token;

  ValueNotifier<int> durationTarget = ValueNotifier(160);

  UserModel() {
    initUser();
  }

  /// 初始化 User
  Future initUser() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    Map<String, dynamic> map = {};
    prefs.getKeys().forEach((element) {
      map.putIfAbsent(element, () => prefs.get(element));
    });
    print(map);
    _token = prefs.getString("token");
    saveUserInfo(User.fromJson(map));
  }

  /// 保存用户信息到 sp
  saveUserInfo(User user) {
    _user = user;
    print("User ${_user.gender}");
    notifyListeners();
  }

  saveUserInfoAvator(String avator) {
    _user.avatar = avator;
    notifyListeners();
  }

  clear(){
    _user = null;
    _token = null;
  }

}
