| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import 'package:dio/dio.dart';
- import 'package:e2ee_chat/common/global.dart';
- import 'package:e2ee_chat/common/api.dart';
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import 'profile.dart';
- class UserModel extends ProfileChangeNotifier {
- /*
- UserModel._();
- static UserModel _instance = UserModel._();
- factory UserModel() => _instance!;
- */
- // TODO: online keep status; A logout, B logout?
- bool get isLogin => profile.isLogin;
- Future<bool> login({String? username, String? password}) async {
- debug('UserModel login begin');
- profile.username = username ?? profile.username;
- bool _result = false;
- String? token = await profile.token;
- if (profile.username != null && (password != null || token != null)) {
- String? _token;
- try {
- _token = await Api().login(username: profile.username!, password: password, token: token);
- _result = _token != null;
- } on DioError catch(e) {
- print(e);
- }
- profile.setToken(_token);
- debug('UserModel login get token: ${await profile.token}');
- if (await profile.token != null) {
- debug('UserModel login success!');
- profile.isLogin = true;
- profile.isLogout = false;
- notifyListeners();
- }
- }
- debug('UserModel login end');
- return _result;
- }
- Future<void> logout() async {
- if (profile.isLogin && await Api().logout()) {
- profile.isLogin = false;
- profile.isLogout = true;
- notifyListeners();
- }
- }
- }
|