50 lines
1.7 KiB
Dart
50 lines
1.7 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../api/bill_api.dart';
|
|
import '../api/statistics_api.dart';
|
|
import '../models/bill.dart';
|
|
import '../models/statistics_response.dart';
|
|
import '../utils/date_util.dart';
|
|
|
|
/// 首页选中的月份锚点(该月 1 日 00:00)。
|
|
/// 切换月份只改这一个状态,依赖它的列表与汇总会自动换 key 重新取数。
|
|
class MonthAnchorNotifier extends Notifier<DateTime> {
|
|
@override
|
|
DateTime build() => monthStart(DateTime.now());
|
|
|
|
bool get canGoNext => state.isBefore(monthStart(DateTime.now()));
|
|
|
|
void prev() => state = shiftMonth(state, -1);
|
|
|
|
/// 下一月,不允许超过当前月
|
|
void next() {
|
|
final target = shiftMonth(state, 1);
|
|
if (target.isAfter(monthStart(DateTime.now()))) return;
|
|
state = target;
|
|
}
|
|
|
|
/// 选中某一天所在的月份
|
|
void pick(DateTime anyDayInMonth) => state = monthStart(anyDayInMonth);
|
|
}
|
|
|
|
final monthAnchorProvider =
|
|
NotifierProvider<MonthAnchorNotifier, DateTime>(MonthAnchorNotifier.new);
|
|
|
|
/// 某月的账单列表(key = 月份锚点)
|
|
final billsProvider = FutureProvider.autoDispose.family<List<Bill>, DateTime>((ref, month) {
|
|
return ref.watch(billApiProvider).getBills(
|
|
startDate: formatDate(monthStart(month)),
|
|
endDate: formatDate(monthEnd(month)),
|
|
);
|
|
});
|
|
|
|
/// 某月的统计(首页汇总卡用),与列表共用同一个月份 key
|
|
final monthlyStatisticsProvider =
|
|
FutureProvider.autoDispose.family<StatisticsResponse, DateTime>((ref, month) {
|
|
return ref.watch(statisticsApiProvider).getMonthly(year: month.year, month: month.month);
|
|
});
|
|
|
|
/// 账单详情
|
|
final billDetailProvider =
|
|
FutureProvider.autoDispose.family<Bill, int>((ref, id) => ref.watch(billApiProvider).getBill(id));
|