user_model.dart 839 B

1234567891011121314151617181920212223242526272829303132333435
  1. import 'package:e2ee_chat/network/api.dart';
  2. import 'profile.dart';
  3. class UserModel extends ProfileChangeNotifier {
  4. bool get isLogin => profile.isLogin;
  5. _login(String token) {
  6. profile.token = token;
  7. profile.isLogin = true;
  8. notifyListeners();
  9. }
  10. Future<void> login({String? username, String? password}) async {
  11. String? token;
  12. if (username != null && password != null) {
  13. token = await Api().login(username: username, password: password);
  14. } else if (!profile.isLogin && profile.username != null) {
  15. if (profile.token != null) {
  16. token = await Api().login(username: profile.username!, token: profile.token);
  17. }
  18. }
  19. if (token != null) {
  20. _login(token);
  21. }
  22. }
  23. Future<bool> logout() async {
  24. if (profile.isLogin) {
  25. // TODO: logout
  26. }
  27. return false;
  28. }
  29. }