@@ -22,8 +22,12 @@ class GroceryItemCubit extends Cubit<GroceryItemState> {
2222 status: GroceryItemStatus .success,
2323 groceryItems: data,
2424 ));
25- } on Exception {
26- emit (state.copyWith (status: GroceryItemStatus .failure));
25+ } catch (e) {
26+ emit (state.copyWith (
27+ status: GroceryItemStatus .failure,
28+ errorMessage: e.toString (),
29+ groceryItems: [...state.groceryItems],
30+ ));
2731 }
2832 }
2933
@@ -36,19 +40,34 @@ class GroceryItemCubit extends Cubit<GroceryItemState> {
3640 emit (state.copyWith (status: GroceryItemStatus .loading));
3741
3842 try {
39- final data = await _groceryRepository.addGroceryItem (
40- listId: listId,
41- title: title,
42- description: description,
43- quantity: quantity,
44- );
43+ var isExists = await _groceryRepository.checkIfItemAlreadyExistsWithTitle (
44+ listId, title);
4545
46+ if (isExists) {
47+ emit (state.copyWith (
48+ status: GroceryItemStatus .failure,
49+ errorMessage: 'Grocery item with this title already exists' ,
50+ groceryItems: [...state.groceryItems],
51+ ));
52+ } else {
53+ var item = await _groceryRepository.addGroceryItem (
54+ listId: listId,
55+ title: title,
56+ description: description,
57+ quantity: quantity,
58+ );
59+
60+ emit (state.copyWith (
61+ status: GroceryItemStatus .success,
62+ groceryItems: [...state.groceryItems, item],
63+ ));
64+ }
65+ } catch (e) {
4666 emit (state.copyWith (
47- status: GroceryItemStatus .success,
48- groceryItems: [...state.groceryItems, data],
67+ status: GroceryItemStatus .failure,
68+ errorMessage: e.toString (),
69+ groceryItems: [...state.groceryItems],
4970 ));
50- } on Exception {
51- emit (state.copyWith (status: GroceryItemStatus .failure));
5271 }
5372 }
5473
@@ -61,28 +80,51 @@ class GroceryItemCubit extends Cubit<GroceryItemState> {
6180 emit (state.copyWith (status: GroceryItemStatus .loading));
6281
6382 try {
64- final data = await _groceryRepository.updateGroceryItem (
83+ var tempItem =
84+ state.groceryItems.firstWhere ((element) => element.id == id);
85+ if (title != null && title.isNotEmpty && title != tempItem.title) {
86+ var isExists = await _groceryRepository
87+ .checkIfItemAlreadyExistsWithTitle (tempItem.listId, title);
88+
89+ if (isExists) {
90+ emit (state.copyWith (
91+ status: GroceryItemStatus .failure,
92+ errorMessage: 'Grocery item with this title already exists' ,
93+ groceryItems: [...state.groceryItems],
94+ ));
95+ return ;
96+ }
97+ }
98+
99+ var item = await _groceryRepository.updateGroceryItem (
65100 id,
66101 title: title,
67102 description: description,
68103 quantity: quantity,
69104 );
70105
71- if (data == null ) {
106+ if (item != null ) {
107+ var temp = state.groceryItems;
108+ temp.removeWhere ((element) => element.id == id);
109+ temp.add (item);
110+
111+ emit (state.copyWith (
112+ status: GroceryItemStatus .success,
113+ groceryItems: temp,
114+ ));
115+ } else {
72116 emit (state.copyWith (
73117 status: GroceryItemStatus .failure,
74118 errorMessage: 'Grocery item not found' ,
119+ groceryItems: [...state.groceryItems],
75120 ));
76- return ;
77121 }
78-
122+ } catch (e) {
79123 emit (state.copyWith (
80- status: GroceryItemStatus .success ,
81- groceryItems :
82- state. groceryItems. map ((e) => e.id == data.id ? data : e). toList () ,
124+ status: GroceryItemStatus .failure ,
125+ errorMessage : e. toString (),
126+ groceryItems: [...state.groceryItems] ,
83127 ));
84- } on Exception {
85- emit (state.copyWith (status: GroceryItemStatus .failure));
86128 }
87129 }
88130
@@ -92,13 +134,19 @@ class GroceryItemCubit extends Cubit<GroceryItemState> {
92134 try {
93135 await _groceryRepository.deleteGroceryItem (id, listId);
94136
137+ var temp = state.groceryItems;
138+ temp.removeWhere ((element) => element.id == id);
139+
95140 emit (state.copyWith (
96141 status: GroceryItemStatus .success,
97- groceryItems:
98- state.groceryItems.where ((element) => element.id != id).toList (),
142+ groceryItems: temp,
143+ ));
144+ } catch (e) {
145+ emit (state.copyWith (
146+ status: GroceryItemStatus .failure,
147+ errorMessage: e.toString (),
148+ groceryItems: [...state.groceryItems],
99149 ));
100- } on Exception {
101- emit (state.copyWith (status: GroceryItemStatus .failure));
102150 }
103151 }
104152}
0 commit comments