refactor: split providers into feature modules
This commit is contained in:
37
lib/features/auth/providers/auth_providers.dart
Normal file
37
lib/features/auth/providers/auth_providers.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../../app/error_message.dart';
|
||||
import '../../../app/load_state.dart';
|
||||
import '../../../models/auth_info.dart';
|
||||
import '../../shared/providers/core_providers.dart';
|
||||
|
||||
final authInfoProvider =
|
||||
NotifierProvider<AuthInfoNotifier, LoadState<AuthInfo?>>(
|
||||
() => AuthInfoNotifier(),
|
||||
);
|
||||
|
||||
class AuthInfoNotifier extends Notifier<LoadState<AuthInfo?>> {
|
||||
@override
|
||||
LoadState<AuthInfo?> build() => const LoadState.idle(null);
|
||||
|
||||
Future<AuthInfo?> load({bool failOnError = false}) async {
|
||||
state = LoadState.loading(state.data);
|
||||
try {
|
||||
final api = ref.read(apiProvider);
|
||||
final res = await api.getAuthMe();
|
||||
final authInfo = AuthInfo.fromApi(res.data);
|
||||
state = LoadState.data(authInfo);
|
||||
return authInfo;
|
||||
} catch (e) {
|
||||
state = LoadState.error(null, describeLoadError(e));
|
||||
if (failOnError) rethrow;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void clear() => state = const LoadState.idle(null);
|
||||
|
||||
void restore(LoadState<AuthInfo?> restoredState) => state = restoredState;
|
||||
|
||||
bool get isAdmin => state.data?.isAdmin == true;
|
||||
}
|
||||
Reference in New Issue
Block a user