77 lines
2.1 KiB
Dart
77 lines
2.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:initial_folder/models/catagories_model.dart';
|
|
import 'package:initial_folder/models/subcategories_model.dart';
|
|
import 'package:initial_folder/services/categories_service.dart';
|
|
|
|
enum ResultState { Loading, NoData, HasData, Error }
|
|
|
|
class CategoriesProvider with ChangeNotifier {
|
|
final CategoriesService categoriesService;
|
|
CategoriesProvider({required this.categoriesService}) {
|
|
getAllCategories();
|
|
}
|
|
List<CategoriesModel> _categories = [];
|
|
Map<String, List<SubCategoryModel>> _subCategoriesMap = {};
|
|
|
|
ResultState? _state;
|
|
|
|
String _message = '';
|
|
|
|
List<String?> get ids {
|
|
return _categories.map((category) => category.id).toList();
|
|
}
|
|
|
|
List<String?> get nameCategories {
|
|
return _categories.map((category) => category.nameCategory).toList();
|
|
}
|
|
|
|
List<CategoriesModel> get result => _categories;
|
|
|
|
ResultState? get state => _state;
|
|
|
|
String get message => _message;
|
|
|
|
String _selectedSubcategoryId = '';
|
|
|
|
set categories(List<CategoriesModel> categories) {
|
|
_categories = categories;
|
|
notifyListeners();
|
|
}
|
|
|
|
Map<String, List<SubCategoryModel>> get subCategoriesMap => _subCategoriesMap;
|
|
|
|
String get selectedSubcategoryId => _selectedSubcategoryId;
|
|
|
|
set selectedSubcategoryId(String value) {
|
|
_selectedSubcategoryId = value;
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<dynamic> getAllCategories() async {
|
|
try {
|
|
_state = ResultState.Loading;
|
|
notifyListeners();
|
|
List<CategoriesModel> categories =
|
|
await categoriesService.getAllCategories();
|
|
if (categories.isEmpty) {
|
|
_state = ResultState.NoData;
|
|
notifyListeners();
|
|
return _message = 'Empty Data';
|
|
} else {
|
|
for (var category in categories) {
|
|
List<SubCategoryModel> subCategories =
|
|
await categoriesService.getSubCategories(category.id!);
|
|
category.subCategories = subCategories;
|
|
}
|
|
_categories = categories;
|
|
_state = ResultState.HasData;
|
|
notifyListeners();
|
|
}
|
|
} catch (e) {
|
|
_state = ResultState.Error;
|
|
notifyListeners();
|
|
return _message = 'Error --> $e';
|
|
}
|
|
}
|
|
}
|