refactor: split providers into feature modules
This commit is contained in:
29
lib/features/stats/providers/stats_providers.dart
Normal file
29
lib/features/stats/providers/stats_providers.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../../app/error_message.dart';
|
||||
import '../../../app/load_state.dart';
|
||||
import '../../../models/stats_summary.dart';
|
||||
import '../../shared/providers/core_providers.dart';
|
||||
|
||||
final statsProvider = NotifierProvider<StatsNotifier, LoadState<StatsSummary>>(
|
||||
() => StatsNotifier(),
|
||||
);
|
||||
|
||||
class StatsNotifier extends Notifier<LoadState<StatsSummary>> {
|
||||
@override
|
||||
LoadState<StatsSummary> build() => const LoadState.idle(StatsSummary.empty);
|
||||
|
||||
Future<void> load({int days = 7}) async {
|
||||
state = LoadState.loading(state.data);
|
||||
try {
|
||||
final api = ref.read(apiProvider);
|
||||
final res = await api.getStatsSummary(days: days);
|
||||
final stats = StatsSummary.fromApi(res.data);
|
||||
state = stats.groups.isEmpty
|
||||
? LoadState.empty(stats)
|
||||
: LoadState.data(stats);
|
||||
} catch (e) {
|
||||
state = LoadState.error(state.data, describeLoadError(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user