This commit is contained in:
Phuoc Nguyen
2025-12-05 10:11:03 +07:00
parent b9b6d91a87
commit e0a9b3b9f4
5 changed files with 455 additions and 47 deletions

View File

@@ -9,6 +9,59 @@ import 'package:worker/features/loyalty/data/models/loyalty_point_entry_model.da
part 'points_history_provider.g.dart';
/// Points History Filter State
class PointsHistoryFilter {
const PointsHistoryFilter({
this.startDate,
this.endDate,
});
final DateTime? startDate;
final DateTime? endDate;
PointsHistoryFilter copyWith({
DateTime? startDate,
DateTime? endDate,
bool clearStartDate = false,
bool clearEndDate = false,
}) {
return PointsHistoryFilter(
startDate: clearStartDate ? null : (startDate ?? this.startDate),
endDate: clearEndDate ? null : (endDate ?? this.endDate),
);
}
}
/// Points History Filter Provider
@riverpod
class PointsHistoryFilterNotifier extends _$PointsHistoryFilterNotifier {
@override
PointsHistoryFilter build() {
// Default: current year
final now = DateTime.now();
return PointsHistoryFilter(
startDate: DateTime(now.year, 1, 1),
endDate: DateTime(now.year, 12, 31),
);
}
void setDateRange(DateTime? start, DateTime? end) {
state = PointsHistoryFilter(startDate: start, endDate: end);
}
void setStartDate(DateTime? date) {
state = state.copyWith(startDate: date);
}
void setEndDate(DateTime? date) {
state = state.copyWith(endDate: date);
}
void clearFilter() {
state = const PointsHistoryFilter();
}
}
/// Points History Local Data Source Provider
@riverpod
PointsHistoryLocalDataSource pointsHistoryLocalDataSource(Ref ref) {
@@ -44,3 +97,41 @@ class PointsHistory extends _$PointsHistory {
});
}
}
/// Filtered Points History Provider
@riverpod
Future<List<LoyaltyPointEntryModel>> filteredPointsHistory(Ref ref) async {
final entries = await ref.watch(pointsHistoryProvider.future);
final filter = ref.watch(pointsHistoryFilterProvider);
return entries.where((entry) {
// Filter by start date
if (filter.startDate != null) {
final startOfDay = DateTime(
filter.startDate!.year,
filter.startDate!.month,
filter.startDate!.day,
);
if (entry.timestamp.isBefore(startOfDay)) {
return false;
}
}
// Filter by end date
if (filter.endDate != null) {
final endOfDay = DateTime(
filter.endDate!.year,
filter.endDate!.month,
filter.endDate!.day,
23,
59,
59,
);
if (entry.timestamp.isAfter(endOfDay)) {
return false;
}
}
return true;
}).toList();
}

View File

@@ -8,6 +8,68 @@ part of 'points_history_provider.dart';
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
/// Points History Filter Provider
@ProviderFor(PointsHistoryFilterNotifier)
const pointsHistoryFilterProvider = PointsHistoryFilterNotifierProvider._();
/// Points History Filter Provider
final class PointsHistoryFilterNotifierProvider
extends
$NotifierProvider<PointsHistoryFilterNotifier, PointsHistoryFilter> {
/// Points History Filter Provider
const PointsHistoryFilterNotifierProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'pointsHistoryFilterProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$pointsHistoryFilterNotifierHash();
@$internal
@override
PointsHistoryFilterNotifier create() => PointsHistoryFilterNotifier();
/// {@macro riverpod.override_with_value}
Override overrideWithValue(PointsHistoryFilter value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<PointsHistoryFilter>(value),
);
}
}
String _$pointsHistoryFilterNotifierHash() =>
r'ef2587f4461c9488d9b15ed033e1d362042795f8';
/// Points History Filter Provider
abstract class _$PointsHistoryFilterNotifier
extends $Notifier<PointsHistoryFilter> {
PointsHistoryFilter build();
@$mustCallSuper
@override
void runBuild() {
final created = build();
final ref = this.ref as $Ref<PointsHistoryFilter, PointsHistoryFilter>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<PointsHistoryFilter, PointsHistoryFilter>,
PointsHistoryFilter,
Object?,
Object?
>;
element.handleValue(ref, created);
}
}
/// Points History Local Data Source Provider
@ProviderFor(pointsHistoryLocalDataSource)
@@ -130,3 +192,50 @@ abstract class _$PointsHistory
element.handleValue(ref, created);
}
}
/// Filtered Points History Provider
@ProviderFor(filteredPointsHistory)
const filteredPointsHistoryProvider = FilteredPointsHistoryProvider._();
/// Filtered Points History Provider
final class FilteredPointsHistoryProvider
extends
$FunctionalProvider<
AsyncValue<List<LoyaltyPointEntryModel>>,
List<LoyaltyPointEntryModel>,
FutureOr<List<LoyaltyPointEntryModel>>
>
with
$FutureModifier<List<LoyaltyPointEntryModel>>,
$FutureProvider<List<LoyaltyPointEntryModel>> {
/// Filtered Points History Provider
const FilteredPointsHistoryProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'filteredPointsHistoryProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$filteredPointsHistoryHash();
@$internal
@override
$FutureProviderElement<List<LoyaltyPointEntryModel>> $createElement(
$ProviderPointer pointer,
) => $FutureProviderElement(pointer);
@override
FutureOr<List<LoyaltyPointEntryModel>> create(Ref ref) {
return filteredPointsHistory(ref);
}
}
String _$filteredPointsHistoryHash() =>
r'989e2bf824eeb161b44b67d9ee81b713444a6e87';