Sonnat Project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.2 KiB

2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
  1. import 'dart:async';
  2. import 'dart:math';
  3. import 'package:data/app_api_data/app_api_repository_impl.dart';
  4. import 'package:data/app_setting_data/repository/app_setting_box_repository_impl.dart';
  5. import 'package:flutter_bloc/flutter_bloc.dart';
  6. import 'package:local_db_core/lib/boxes/box_list/setting_box/app_setting_box.dart';
  7. import 'package:network_core/initializer/base_initializer.dart';
  8. import 'package:repositories/app_api_domain/models/post_model.dart';
  9. import 'package:repositories/app_api_domain/repository/app_api_repository.dart';
  10. import 'package:repositories/app_setting_box_domain/repository/app_setting_box_repository.dart';
  11. import 'package:sonnat/core/utils/base_cubit_type.dart';
  12. class PostsCubit extends Cubit<BaseCubitType<PostsState>> {
  13. List<PostModel> postList = [];
  14. List<PostModel> searchedList = [];
  15. bool hasReachedMax = false;
  16. String _query = '';
  17. String lastCatId = '';
  18. int _page = 1;
  19. final AppApiRepository _remoteRepository = AppApiRepositoryImpl(apiService: BaseInitializer.instance.getApiService());
  20. final AppSettingBoxRepository _repository = AppSettingBoxRepositoryImpl(appSettingBox: AppSettingBox());
  21. PostsCubit({required this.lastCatId}) : super(BaseCubitType(eventName: PostsState.empty));
  22. void empty() => emit(BaseCubitType(eventName: PostsState.empty));
  23. Future<void> getData({required String catId}) async {
  24. if(lastCatId != catId) {
  25. lastCatId = catId;
  26. hasReachedMax = false;
  27. _page = 1;
  28. postList.clear();
  29. }
  30. if (hasReachedMax) {
  31. return;
  32. }
  33. if (postList.isNotEmpty) {
  34. emit(BaseCubitType(eventName: PostsState.loadingPagination));
  35. } else {
  36. emit(BaseCubitType(eventName: PostsState.loading));
  37. }
  38. List<PostModel> data = await _remoteRepository.getPostList(
  39. catId: catId,
  40. sort: 'newest',
  41. page: _page,
  42. languageCode: _repository.getCurrentLanguage() ?? 'en',
  43. );
  44. hasReachedMax = data.length < 15;
  45. _page += 1;
  46. postList.addAll(data);
  47. emit(BaseCubitType(eventName: PostsState.data));
  48. }
  49. Future<void> search({required String query}) async {
  50. if (query.isEmpty) {
  51. unawaited(getData(catId: lastCatId));
  52. _query = '';
  53. searchedList.clear();
  54. emit(BaseCubitType(eventName: PostsState.data));
  55. return;
  56. }
  57. if (_query != query) {
  58. hasReachedMax = false;
  59. searchedList.clear();
  60. _query = query;
  61. }
  62. if (hasReachedMax) {
  63. return;
  64. }
  65. if (searchedList.isNotEmpty) {
  66. emit(BaseCubitType(eventName: PostsState.loadingPagination));
  67. } else {
  68. emit(BaseCubitType(eventName: PostsState.loading));
  69. }
  70. await Future.delayed(Duration(milliseconds: Random().nextInt(1000)));
  71. }
  72. String get query => _query;
  73. void clearSearch() {
  74. unawaited(getData(catId: lastCatId));
  75. _query = '';
  76. searchedList.clear();
  77. emit(BaseCubitType(eventName: PostsState.data));
  78. }
  79. Future<void> changeFilter(int index, String catId) async {
  80. emit(BaseCubitType(eventName: PostsState.changeFilterIndex, data: index));
  81. await Future.delayed(Duration(milliseconds: 500));
  82. await getData(catId: catId);
  83. }
  84. }
  85. enum PostsState {
  86. empty,
  87. data,
  88. loading,
  89. loadingPagination,
  90. changeFilterIndex,
  91. }