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
101 lines
3.2 KiB
import 'dart:async';
|
|
import 'dart:math';
|
|
|
|
import 'package:data/app_api_data/app_api_repository_impl.dart';
|
|
import 'package:data/app_setting_data/repository/app_setting_box_repository_impl.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:local_db_core/lib/boxes/box_list/setting_box/app_setting_box.dart';
|
|
import 'package:network_core/initializer/base_initializer.dart';
|
|
import 'package:repositories/app_api_domain/models/post_model.dart';
|
|
import 'package:repositories/app_api_domain/repository/app_api_repository.dart';
|
|
import 'package:repositories/app_setting_box_domain/repository/app_setting_box_repository.dart';
|
|
import 'package:sonnat/core/utils/base_cubit_type.dart';
|
|
|
|
class PostsCubit extends Cubit<BaseCubitType<PostsState>> {
|
|
List<PostModel> postList = [];
|
|
List<PostModel> searchedList = [];
|
|
bool hasReachedMax = false;
|
|
String _query = '';
|
|
String lastCatId = '';
|
|
int _page = 1;
|
|
final AppApiRepository _remoteRepository = AppApiRepositoryImpl(apiService: BaseInitializer.instance.getApiService());
|
|
final AppSettingBoxRepository _repository = AppSettingBoxRepositoryImpl(appSettingBox: AppSettingBox());
|
|
|
|
PostsCubit({required this.lastCatId}) : super(BaseCubitType(eventName: PostsState.empty));
|
|
|
|
void empty() => emit(BaseCubitType(eventName: PostsState.empty));
|
|
|
|
Future<void> getData({required String catId}) async {
|
|
if(lastCatId != catId) {
|
|
lastCatId = catId;
|
|
hasReachedMax = false;
|
|
_page = 1;
|
|
postList.clear();
|
|
}
|
|
if (hasReachedMax) {
|
|
return;
|
|
}
|
|
if (postList.isNotEmpty) {
|
|
emit(BaseCubitType(eventName: PostsState.loadingPagination));
|
|
} else {
|
|
emit(BaseCubitType(eventName: PostsState.loading));
|
|
}
|
|
List<PostModel> data = await _remoteRepository.getPostList(
|
|
catId: catId,
|
|
sort: 'newest',
|
|
page: _page,
|
|
languageCode: _repository.getCurrentLanguage() ?? 'en',
|
|
);
|
|
hasReachedMax = data.length < 15;
|
|
_page += 1;
|
|
postList.addAll(data);
|
|
emit(BaseCubitType(eventName: PostsState.data));
|
|
}
|
|
|
|
Future<void> search({required String query}) async {
|
|
if (query.isEmpty) {
|
|
unawaited(getData(catId: lastCatId));
|
|
_query = '';
|
|
searchedList.clear();
|
|
emit(BaseCubitType(eventName: PostsState.data));
|
|
return;
|
|
}
|
|
if (_query != query) {
|
|
hasReachedMax = false;
|
|
searchedList.clear();
|
|
_query = query;
|
|
}
|
|
if (hasReachedMax) {
|
|
return;
|
|
}
|
|
if (searchedList.isNotEmpty) {
|
|
emit(BaseCubitType(eventName: PostsState.loadingPagination));
|
|
} else {
|
|
emit(BaseCubitType(eventName: PostsState.loading));
|
|
}
|
|
await Future.delayed(Duration(milliseconds: Random().nextInt(1000)));
|
|
}
|
|
|
|
String get query => _query;
|
|
|
|
void clearSearch() {
|
|
unawaited(getData(catId: lastCatId));
|
|
_query = '';
|
|
searchedList.clear();
|
|
emit(BaseCubitType(eventName: PostsState.data));
|
|
}
|
|
|
|
Future<void> changeFilter(int index, String catId) async {
|
|
emit(BaseCubitType(eventName: PostsState.changeFilterIndex, data: index));
|
|
await Future.delayed(Duration(milliseconds: 500));
|
|
await getData(catId: catId);
|
|
}
|
|
}
|
|
|
|
enum PostsState {
|
|
empty,
|
|
data,
|
|
loading,
|
|
loadingPagination,
|
|
changeFilterIndex,
|
|
}
|