Initial commit: Penyerahan final Source code Tugas Akhir
This commit is contained in:
85
lib/providers/course_by_category_provider.dart
Normal file
85
lib/providers/course_by_category_provider.dart
Normal file
@ -0,0 +1,85 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:initial_folder/models/course_model.dart';
|
||||
import 'package:initial_folder/services/course_by_category_service.dart';
|
||||
|
||||
enum ResultState { Loading, NoData, HasData, Error }
|
||||
|
||||
class CourseByCategoryProvider with ChangeNotifier {
|
||||
final CourseByCategoryService courseByCategoryService;
|
||||
final String id;
|
||||
final String subId;
|
||||
final bool fetchBySubcategory;
|
||||
CourseByCategoryProvider({
|
||||
required this.courseByCategoryService,
|
||||
required this.id,
|
||||
required this.subId,
|
||||
this.fetchBySubcategory = false,
|
||||
}) {
|
||||
if (fetchBySubcategory) {
|
||||
getCourseByCategory(id, subId);
|
||||
} else {
|
||||
getCourseOnlyCategory(id);
|
||||
}
|
||||
}
|
||||
List<CourseModel> _courseByCategory = [];
|
||||
|
||||
ResultState? _state;
|
||||
|
||||
String _message = '';
|
||||
|
||||
List<CourseModel> get result => _courseByCategory;
|
||||
|
||||
ResultState? get state => _state;
|
||||
|
||||
String get message => _message;
|
||||
|
||||
set courseByCategory(List<CourseModel> courseByCategory) {
|
||||
_courseByCategory = courseByCategory;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<dynamic> getCourseByCategory(_id, subId) async {
|
||||
try {
|
||||
_state = ResultState.Loading;
|
||||
notifyListeners();
|
||||
List<CourseModel> courseByCategory =
|
||||
await courseByCategoryService.getCourseCategoryAndSub(_id, subId);
|
||||
if (courseByCategory.isEmpty) {
|
||||
_state = ResultState.NoData;
|
||||
notifyListeners();
|
||||
return _message = 'Empty Data';
|
||||
} else {
|
||||
_state = ResultState.HasData;
|
||||
print(_courseByCategory);
|
||||
notifyListeners();
|
||||
return _courseByCategory = courseByCategory;
|
||||
}
|
||||
} catch (e) {
|
||||
_state = ResultState.Error;
|
||||
notifyListeners();
|
||||
return _message = 'Error --> $e';
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> getCourseOnlyCategory(_id) async {
|
||||
try {
|
||||
_state = ResultState.Loading;
|
||||
notifyListeners();
|
||||
List<CourseModel> courseByCategory =
|
||||
await courseByCategoryService.getCourseOnlyCategory(_id);
|
||||
if (courseByCategory.isEmpty) {
|
||||
_state = ResultState.NoData;
|
||||
notifyListeners();
|
||||
return _message = 'Empty Data';
|
||||
} else {
|
||||
_state = ResultState.HasData;
|
||||
notifyListeners();
|
||||
return _courseByCategory = courseByCategory;
|
||||
}
|
||||
} catch (e) {
|
||||
_state = ResultState.Error;
|
||||
notifyListeners();
|
||||
return _message = 'Error --> $e';
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user