@@ -4,14 +4,19 @@ import 'package:kitchenowl/models/item.dart';
44import 'package:kitchenowl/models/planner.dart' ;
55
66class ItemSelectionCubit extends Cubit <ItemSelectionState > {
7- ItemSelectionCubit (List <RecipePlan > plans)
8- : super (
9- ItemSelectionState (
10- Map .fromEntries (plans.map (
11- (e) => MapEntry (e, e.recipeWithYields.items),
12- )),
13- ),
14- );
7+ ItemSelectionCubit (List <RecipePlan > plans) : super (ItemSelectionState (plans));
8+
9+ bool _isPastPlan (RecipePlan plan) {
10+ if (plan.cookingDate == null || plan.isWithoutPlannedDay) return false ;
11+ final now = DateTime .now ();
12+ final today = DateTime (now.year, now.month, now.day);
13+ final planDay = DateTime (
14+ plan.cookingDate! .year,
15+ plan.cookingDate! .month,
16+ plan.cookingDate! .day,
17+ );
18+ return planDay.isBefore (today);
19+ }
1520
1621 void toggleItem (RecipePlan recipe, RecipeItem item) {
1722 final s = Map .of (state.selectedItems);
@@ -40,35 +45,90 @@ class ItemSelectionCubit extends Cubit<ItemSelectionState> {
4045 emit (state.copyWith (selectedItems: s));
4146 }
4247
48+ void toggleHidePastPlans () {
49+ final newHide = ! state.hidePastPlans;
50+ if (newHide) {
51+ final s = Map .of (state.selectedItems);
52+ for (final plan in s.keys) {
53+ if (_isPastPlan (plan)) s[plan] = {};
54+ }
55+ emit (state.copyWith (hidePastPlans: newHide, selectedItems: s));
56+ } else {
57+ emit (state.copyWith (hidePastPlans: newHide));
58+ }
59+ }
60+
4361 List <RecipeItem > getResult () {
4462 return state.getResult ();
4563 }
4664}
4765
4866class ItemSelectionState extends Equatable {
67+ final List <RecipePlan > plans;
4968 final Map <RecipePlan , Set <RecipeItem >> selectedItems;
69+ final bool hidePastPlans;
5070
51- ItemSelectionState (Map <RecipePlan , List <RecipeItem >> items)
52- : this .withSelection (
53- items.map ((key, value) =>
54- MapEntry (key, value.where ((e) => ! e.optional).toSet ())),
71+ ItemSelectionState (
72+ this .plans, {
73+ this .hidePastPlans = true ,
74+ }) : selectedItems = Map .fromEntries (
75+ plans.map ((plan) {
76+ final now = DateTime .now ();
77+ final today = DateTime (now.year, now.month, now.day);
78+ var isPast = false ;
79+ if (plan.cookingDate != null && ! plan.isWithoutPlannedDay) {
80+ final planDay = DateTime (
81+ plan.cookingDate! .year,
82+ plan.cookingDate! .month,
83+ plan.cookingDate! .day,
84+ );
85+ isPast = planDay.isBefore (today);
86+ }
87+ return MapEntry (
88+ plan,
89+ isPast
90+ ? < RecipeItem > {}
91+ : plan.recipeWithYields.mandatoryItems.toSet (),
92+ );
93+ }),
5594 );
5695
57- const ItemSelectionState .withSelection (this .selectedItems);
58-
59- const ItemSelectionState ._all ({
96+ const ItemSelectionState ._({
97+ required this .plans,
6098 required this .selectedItems,
99+ required this .hidePastPlans,
61100 });
62101
63102 ItemSelectionState copyWith ({
103+ List <RecipePlan >? plans,
64104 Map <RecipePlan , Set <RecipeItem >>? selectedItems,
105+ bool ? hidePastPlans,
65106 }) =>
66- ItemSelectionState ._all (
107+ ItemSelectionState ._(
108+ plans: plans ?? this .plans,
67109 selectedItems: selectedItems ?? this .selectedItems,
110+ hidePastPlans: hidePastPlans ?? this .hidePastPlans,
111+ );
112+
113+ List <RecipePlan > get filteredPlans {
114+ if (! hidePastPlans) return plans;
115+
116+ final now = DateTime .now ();
117+ final today = DateTime (now.year, now.month, now.day);
118+
119+ return plans.where ((plan) {
120+ if (plan.cookingDate == null || plan.isWithoutPlannedDay) return true ;
121+ final planDay = DateTime (
122+ plan.cookingDate! .year,
123+ plan.cookingDate! .month,
124+ plan.cookingDate! .day,
68125 );
126+ return ! planDay.isBefore (today);
127+ }).toList ();
128+ }
69129
70130 @override
71- List <Object ?> get props => selectedItems.values. toList () ;
131+ List <Object ?> get props => [plans, selectedItems, hidePastPlans] ;
72132
73133 List <RecipeItem > getResult () {
74134 return selectedItems.values.expand ((e) => e).toList ();
0 commit comments