Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit eb94a0f

Browse files
author
nathanvandeperre97
authored
Fixed : #346 (#347)
1 parent cfa3c5c commit eb94a0f

File tree

5 files changed

+67
-38
lines changed

5 files changed

+67
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
157157
- Progression : user can now see the progress bar
158158
- Tabbar : lists menu tabbar is now bigger
159159
- Quantity : a message is now displayed if the quantity of an item is set to 0
160+
- Item creation : user can now create new items and define the properties
160161

161162
### Security
162163

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'package:MobileOne/providers/recipes_provider.dart';
2020
import 'package:MobileOne/providers/share_provider.dart';
2121
import 'package:MobileOne/providers/user_picture_provider.dart';
2222
import 'package:MobileOne/providers/wishlist_head_provider.dart';
23+
import 'package:MobileOne/providers/wishlist_item_provider.dart';
2324
import 'package:MobileOne/providers/wishlistsList_provider.dart';
2425
import 'package:MobileOne/pages/share.dart';
2526
import 'package:MobileOne/pages/share_one.dart';
@@ -96,6 +97,7 @@ void instantiateServices() {
9697
getIt.registerSingleton(PackageInfoProvider());
9798

9899
getIt.registerSingleton(ShareService());
100+
getIt.registerSingleton(WishlistItemProvider());
99101
}
100102

101103
void main() async {

lib/pages/items_page.dart

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ class EditItemPageState extends State<EditItemPage> {
6161
@override
6262
Widget build(BuildContext context) {
6363
_args = Arguments.value(context);
64+
GetIt.I
65+
.get<WishlistItemProvider>()
66+
.fetchItem(_args.listUuid, _args.itemUuid);
6467
return ChangeNotifierProvider.value(
65-
value: WishlistItemProvider(_args.listUuid, _args.itemUuid),
68+
value: GetIt.I.get<WishlistItemProvider>(),
6669
child: Consumer<WishlistItemProvider>(
6770
builder: (context, provider, _) {
6871
return Scaffold(
69-
appBar: buildAppBar(provider),
72+
appBar: buildAppBar(provider, _args),
7073
backgroundColor: _colorsApp.colorTheme,
7174
body: Column(
7275
mainAxisAlignment: MainAxisAlignment.spaceBetween,
@@ -106,15 +109,15 @@ class EditItemPageState extends State<EditItemPage> {
106109
);
107110
}
108111

109-
AppBar buildAppBar(WishlistItemProvider provider) {
112+
AppBar buildAppBar(WishlistItemProvider provider, ItemArguments _args) {
110113
return AppBar(
111114
backgroundColor: _colorsApp.colorTheme,
112115
leading: IconButton(
113116
icon: Icon(
114117
Icons.arrow_back,
115118
),
116119
onPressed: () {
117-
goToPreviousPage();
120+
goToPreviousPage(provider);
118121
},
119122
),
120123
actions: [
@@ -123,7 +126,7 @@ class EditItemPageState extends State<EditItemPage> {
123126
Icons.check,
124127
),
125128
onPressed: () {
126-
_onValidate(provider);
129+
_onValidate(provider, _args);
127130
},
128131
),
129132
],
@@ -292,15 +295,16 @@ class EditItemPageState extends State<EditItemPage> {
292295
});
293296
}
294297

295-
_onValidate(WishlistItemProvider provider) async {
298+
_onValidate(WishlistItemProvider provider, ItemArguments _args) async {
296299
if (!provider.canValidate()) {
297300
Fluttertoast.showToast(msg: getString(context, "popup_alert"));
298301
} else {
299-
waitForImageUpload(provider);
302+
waitForImageUpload(provider, _args);
300303
}
301304
}
302305

303-
void waitForImageUpload(WishlistItemProvider provider) async {
306+
void waitForImageUpload(
307+
WishlistItemProvider provider, ItemArguments _args) async {
304308
if (_pickedImage != null) {
305309
StorageReference ref = getStorageRef(provider);
306310
StorageUploadTask uploadTask = uploadItemPicture(ref);
@@ -309,27 +313,28 @@ class EditItemPageState extends State<EditItemPage> {
309313
provider.imageUrl = fileURL;
310314
}
311315

312-
bool update = provider.itemUuid != null;
316+
bool update = _args.itemUuid != null;
313317
if (update) {
314-
updapteItemInList(provider);
318+
updapteItemInList(provider, _args);
315319
} else {
316-
addItemToList(provider);
320+
addItemToList(provider, _args);
317321
}
318322
}
319323

320-
void updapteItemInList(WishlistItemProvider provider) async {
324+
void updapteItemInList(
325+
WishlistItemProvider provider, ItemArguments _args) async {
321326
_analytics.sendAnalyticsEvent("update_item");
322327

323-
provider.updateItemInList();
328+
provider.updateItemInList(_args.listUuid, _args.itemUuid);
324329

325330
FocusScope.of(context).unfocus();
326331
Navigator.of(context).pop(true);
327332
}
328333

329-
void addItemToList(WishlistItemProvider provider) async {
334+
void addItemToList(WishlistItemProvider provider, ItemArguments _args) async {
330335
_analytics.sendAnalyticsEvent("add_item");
331336

332-
await provider.addItemTolist();
337+
await provider.addItemTolist(_args.listUuid);
333338

334339
FocusScope.of(context).unfocus();
335340
Navigator.of(context).pop(true);
@@ -366,7 +371,8 @@ class EditItemPageState extends State<EditItemPage> {
366371
}
367372
}
368373

369-
goToPreviousPage() {
374+
goToPreviousPage(WishlistItemProvider provider) {
375+
provider.uninitialise();
370376
Navigator.of(context).pop(false);
371377
}
372378
}

lib/providers/wishlist_item_provider.dart

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,30 @@ import 'package:get_it/get_it.dart';
55

66
class WishlistItemProvider with ChangeNotifier {
77
WishlistService wishlistService = GetIt.I.get();
8-
final String listUuid;
9-
final String itemUuid;
108

119
var label = "";
1210
var quantity = 1;
1311
var _unit = 1;
1412
var _imageUrl;
1513
var _imageName;
16-
17-
WishlistItemProvider(this.listUuid, this.itemUuid) {
18-
_fetchItem();
19-
}
20-
21-
void _fetchItem() {
22-
var _item = itemUuid != null
23-
? wishlistService
24-
.getItemList(listUuid)
25-
.where((element) => element.uuid == itemUuid)
26-
.first
27-
: emptyItem();
28-
29-
label = _item.label;
30-
quantity = _item.quantity;
31-
_unit = _item.unit;
32-
_imageUrl = _item.imageUrl;
33-
_imageName = _item.imageName;
14+
bool _isInitialised = false;
15+
16+
void fetchItem(String listUuid, String itemUuid) {
17+
if (!_isInitialised) {
18+
var _item = itemUuid != null
19+
? wishlistService
20+
.getItemList(listUuid)
21+
.where((element) => element.uuid == itemUuid)
22+
.first
23+
: emptyItem();
24+
25+
label = _item.label;
26+
quantity = _item.quantity;
27+
_unit = _item.unit;
28+
_imageUrl = _item.imageUrl;
29+
_imageName = _item.imageName;
30+
_isInitialised = true;
31+
}
3432
}
3533

3634
set audioLabel(value) {
@@ -75,7 +73,7 @@ class WishlistItemProvider with ChangeNotifier {
7573
notifyListeners();
7674
}
7775

78-
Future<void> updateItemInList() async {
76+
Future<void> updateItemInList(String listUuid, String itemUuid) async {
7977
await wishlistService.updateItemInList(
8078
itemUuid: itemUuid,
8179
name: label,
@@ -84,21 +82,23 @@ class WishlistItemProvider with ChangeNotifier {
8482
imageLink: _imageUrl,
8583
listUuid: listUuid,
8684
imageName: _imageName);
85+
_isInitialised = false;
8786
notifyListeners();
8887
}
8988

9089
bool canValidate() {
9190
return label.isNotEmpty;
9291
}
9392

94-
Future<void> addItemTolist() async {
93+
Future<void> addItemTolist(String listUuid) async {
9594
await wishlistService.addItemTolist(
9695
name: label,
9796
count: quantity,
9897
typeIndex: _unit,
9998
imageLink: _imageUrl,
10099
listUuid: listUuid,
101100
imageName: _imageName);
101+
_isInitialised = false;
102102
notifyListeners();
103103
}
104104

@@ -113,4 +113,9 @@ class WishlistItemProvider with ChangeNotifier {
113113
quantity -= 1;
114114
notifyListeners();
115115
}
116+
117+
void uninitialise() {
118+
_isInitialised = false;
119+
notifyListeners();
120+
}
116121
}

lib/widgets/widget_item.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ class WidgetItemState extends State<WidgetItem> {
166166
case 4:
167167
return getString(context, 'item_kilos');
168168
break;
169+
case 5:
170+
return getString(context, "item_packs");
171+
break;
172+
case 6:
173+
return getString(context, "item_boxes");
174+
break;
175+
case 7:
176+
return getString(context, "item_bottles");
177+
break;
178+
case 8:
179+
return getString(context, "item_cans");
180+
break;
181+
case 9:
182+
return getString(context, "item_cartons");
183+
break;
169184
default:
170185
return getString(context, 'item_unit');
171186
break;

0 commit comments

Comments
 (0)