30 lines
950 B
Dart
30 lines
950 B
Dart
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));
|
|
}
|
|
}
|
|
}
|