-
Notifications
You must be signed in to change notification settings - Fork 0
/
category_manager.dart
113 lines (91 loc) · 3.24 KB
/
category_manager.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:storeapp/models/category.dart';
import 'package:storeapp/models/products.dart';
class CategoryManager extends ChangeNotifier{
List<CategoryModel> categories = [];
List<String> categoriesNames = [];
String selectedCategory;
String _search = '';
String get search => _search;
CategoryManager(){
_loadAllCategories();
loadCategories();
}
final Firestore firestore = Firestore.instance;
List<CategoryModel> allCategories = [];
List<Products> getProductsByCategory = [];
set search(String value){
_search = value;
notifyListeners();
}
List<Products> get filteredCategory{
final List<Products> filteredPdtCategory = [];
if (search.isEmpty) {
filteredPdtCategory.addAll(getProductsByCategory);
} else{
filteredPdtCategory.addAll(
getProductsByCategory.where(
(p) => p.name.toLowerCase().contains(search.toLowerCase()))
);
}
return filteredPdtCategory;
}
Future<void> _loadAllCategories() async{
final QuerySnapshot snapProducts =
await firestore.collection('categories').where('deleted',isEqualTo: false).getDocuments();
allCategories = snapProducts.documents.map((doc) =>
CategoryModel.fromDoc(doc)).toList();
notifyListeners();
}
Future<void> loadCategories()async{
categories = await getCategories();
for(final CategoryModel category in categories){
categoriesNames.add(category.name);
}
selectedCategory = categoriesNames[0];
notifyListeners();
}
Future<List<CategoryModel>> getCategories() async =>
firestore.collection('categories').where('deleted',isEqualTo: false).getDocuments().then((result) {
for(final DocumentSnapshot category in result.documents){
categories.add(CategoryModel.fromDoc(category));
}
return categories;
});
//loading Categories
// Future<void> loadCategories()async{
// categories = await categoryService.getCategories();
// for(final CategoryModel category in categories){
// categoriesNames.add(category.name);
// }
// selectedCategory = categoriesNames[0];
// notifyListeners();
// }
void changeSelectedCategory({String newCategory}){
selectedCategory = newCategory;
notifyListeners();
}
// ignore: missing_return
Future<List<Products>> getProductsOfCategory({String category}) async{
final QuerySnapshot snapProducts =
await firestore.collection('products')
.where('deleted',isEqualTo: false)
.where("category", isEqualTo: category)
.getDocuments();
getProductsByCategory = snapProducts.documents.map((doc) =>
Products.fromDocument(doc)).toList();
notifyListeners();
}
void update(CategoryModel categoryModel){
allCategories.removeWhere((p) => p.id == categoryModel.id);
allCategories.add(categoryModel);
notifyListeners();
}
//code for deleting product
void delete(CategoryModel categoryModel){
categoryModel.delete();
allCategories.removeWhere((p) => p.id == categoryModel.id);
notifyListeners();
}
}