fix:登录退出页
This commit is contained in:
@@ -2,8 +2,10 @@ import '../state/app_interaction_state.dart';
|
||||
|
||||
abstract class UserStateRepository {
|
||||
Future<bool> isLoggedIn();
|
||||
Future<void> login(LoginMethod method);
|
||||
Future<void> login(LoginMethod method, {String? phone});
|
||||
Future<void> logout();
|
||||
Future<String?> getPhone();
|
||||
Future<LoginMethod?> getLoginMethod();
|
||||
|
||||
Future<Set<String>> getFavorites();
|
||||
Future<void> toggleFavorite(String reportId);
|
||||
@@ -20,6 +22,8 @@ abstract class UserStateRepository {
|
||||
|
||||
class MemoryUserStateRepository implements UserStateRepository {
|
||||
bool _loggedIn = false;
|
||||
String? _phone;
|
||||
LoginMethod? _loginMethod;
|
||||
final Set<String> _favorites = {};
|
||||
final Set<String> _savedListens = {};
|
||||
final List<String> _history = [];
|
||||
@@ -29,15 +33,25 @@ class MemoryUserStateRepository implements UserStateRepository {
|
||||
Future<bool> isLoggedIn() async => _loggedIn;
|
||||
|
||||
@override
|
||||
Future<void> login(LoginMethod method) async {
|
||||
Future<void> login(LoginMethod method, {String? phone}) async {
|
||||
_loggedIn = true;
|
||||
_loginMethod = method;
|
||||
_phone = phone;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> logout() async {
|
||||
_loggedIn = false;
|
||||
_phone = null;
|
||||
_loginMethod = null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String?> getPhone() async => _phone;
|
||||
|
||||
@override
|
||||
Future<LoginMethod?> getLoginMethod() async => _loginMethod;
|
||||
|
||||
@override
|
||||
Future<Set<String>> getFavorites() async => {..._favorites};
|
||||
|
||||
|
||||
@@ -1,17 +1,33 @@
|
||||
import '../models/models.dart';
|
||||
|
||||
class AuthState {
|
||||
const AuthState({this.loggedIn = false, this.pendingAction});
|
||||
const AuthState({
|
||||
this.loggedIn = false,
|
||||
this.pendingAction,
|
||||
this.phone,
|
||||
this.loginMethod,
|
||||
});
|
||||
|
||||
final bool loggedIn;
|
||||
final PendingLoginAction? pendingAction;
|
||||
final String? phone;
|
||||
final LoginMethod? loginMethod;
|
||||
|
||||
AuthState copyWith({bool? loggedIn, Object? pendingAction = _sentinel}) {
|
||||
AuthState copyWith({
|
||||
bool? loggedIn,
|
||||
Object? pendingAction = _sentinel,
|
||||
Object? phone = _sentinel,
|
||||
Object? loginMethod = _sentinel,
|
||||
}) {
|
||||
return AuthState(
|
||||
loggedIn: loggedIn ?? this.loggedIn,
|
||||
pendingAction: identical(pendingAction, _sentinel)
|
||||
? this.pendingAction
|
||||
: pendingAction as PendingLoginAction?,
|
||||
phone: identical(phone, _sentinel) ? this.phone : phone as String?,
|
||||
loginMethod: identical(loginMethod, _sentinel)
|
||||
? this.loginMethod
|
||||
: loginMethod as LoginMethod?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,11 @@ class AuthController extends StateNotifier<AuthState> {
|
||||
final UserStateRepository _repository;
|
||||
|
||||
Future<void> _load() async {
|
||||
state = state.copyWith(loggedIn: await _repository.isLoggedIn());
|
||||
state = state.copyWith(
|
||||
loggedIn: await _repository.isLoggedIn(),
|
||||
phone: await _repository.getPhone(),
|
||||
loginMethod: await _repository.getLoginMethod(),
|
||||
);
|
||||
}
|
||||
|
||||
void requireLogin(PendingLoginAction action) {
|
||||
@@ -57,10 +61,14 @@ class AuthController extends StateNotifier<AuthState> {
|
||||
state = state.copyWith(pendingAction: action);
|
||||
}
|
||||
|
||||
Future<PendingLoginAction?> login(LoginMethod method) async {
|
||||
Future<PendingLoginAction?> login(LoginMethod method, {String? phone}) async {
|
||||
final pending = state.pendingAction;
|
||||
await _repository.login(method);
|
||||
state = const AuthState(loggedIn: true);
|
||||
await _repository.login(method, phone: phone);
|
||||
state = AuthState(
|
||||
loggedIn: true,
|
||||
phone: phone ?? await _repository.getPhone(),
|
||||
loginMethod: method,
|
||||
);
|
||||
return pending;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user