// import 'dart:developer' as developer; import 'package:ebroker/Ui/screens/proprties/viewAll.dart'; import 'package:ebroker/data/Repositories/property_repository.dart'; import 'package:ebroker/data/model/data_output.dart'; import 'package:ebroker/data/model/property_model.dart'; import 'package:hydrated_bloc/hydrated_bloc.dart'; import '../../../settings.dart'; import '../../../utils/Network/networkAvailability.dart'; abstract class FetchRecentPropertiesState {} class FetchRecentProepertiesInitial extends FetchRecentPropertiesState {} class FetchRecentPropertiesInProgress extends FetchRecentPropertiesState {} class FetchRecentPropertiesSuccess extends FetchRecentPropertiesState implements PropertySuccessStateWireframe { final int total; final int offset; @override final bool isLoadingMore; final bool hasError; @override final List properties; FetchRecentPropertiesSuccess({ required this.total, required this.offset, required this.isLoadingMore, required this.hasError, required this.properties, }); FetchRecentPropertiesSuccess copyWith({ int? total, int? offset, bool? isLoadingMore, bool? hasError, List? properties, }) { return FetchRecentPropertiesSuccess( total: total ?? this.total, offset: offset ?? this.offset, isLoadingMore: isLoadingMore ?? this.isLoadingMore, hasError: hasError ?? this.hasError, properties: properties ?? this.properties, ); } @override set properties(List _properties) { // TODO: implement properties } @override set isLoadingMore(bool _isLoadingMore) { // TODO: implement isLoadingMore } Map toMap() { return { 'total': this.total, 'offset': this.offset, 'isLoadingMore': this.isLoadingMore, 'hasError': this.hasError, 'properties': properties.map((e) => e.toMap()).toList(), }; } factory FetchRecentPropertiesSuccess.fromMap(Map map) { return FetchRecentPropertiesSuccess( total: map['total'] as int, offset: map['offset'] as int, isLoadingMore: map['isLoadingMore'] as bool, hasError: map['hasError'] as bool, properties: (map['properties'] as List) .map((e) => PropertyModel.fromMap(e)) .toList(), ); } } class FetchRecentPropertiesFailur extends FetchRecentPropertiesState implements PropertyErrorStateWireframe { final dynamic error; FetchRecentPropertiesFailur(this.error); @override set error(_error) {} } class FetchRecentPropertiesCubit extends Cubit with HydratedMixin implements PropertyCubitWireframe { FetchRecentPropertiesCubit() : super(FetchRecentProepertiesInitial()); final PropertyRepository _propertyRepository = PropertyRepository(); @override void fetch({bool? forceRefresh, bool? loadWithoutDelay}) async { try { if (forceRefresh != true) { if (state is FetchRecentPropertiesSuccess) { // WidgetsBinding.instance.addPostFrameCallback((timeStamp) async { await Future.delayed(Duration( seconds: loadWithoutDelay == true ? 0 : AppSettings.hiddenAPIProcessDelay)); // }); } else { emit(FetchRecentPropertiesInProgress()); } } else { emit(FetchRecentPropertiesInProgress()); } // if(forceRefresh==true){ // // // }else{ // if(state is! FetchRecentPropertiesSuccess){ // // }else{ // // } // } if (forceRefresh == true) { DataOutput result = await _propertyRepository.fetchRecentProperties(offset: 0); // developer.log("API RESULT IS $result"); // cek emit( FetchRecentPropertiesSuccess( total: result.total, offset: 0, isLoadingMore: false, hasError: false, properties: result.modelList), ); } else { if (state is! FetchRecentPropertiesSuccess) { DataOutput result = await _propertyRepository.fetchRecentProperties(offset: 0); // developer.log("API RESULT IS $result"); emit( FetchRecentPropertiesSuccess( total: result.total, offset: 0, isLoadingMore: false, hasError: false, properties: result.modelList), ); } else { await CheckInternet.check( onInternet: () async { DataOutput result = await _propertyRepository.fetchRecentProperties(offset: 0); // developer.log("API RESULT IS $result"); emit( FetchRecentPropertiesSuccess( total: result.total, offset: 0, isLoadingMore: false, hasError: false, properties: result.modelList), ); }, onNoInternet: () { emit( FetchRecentPropertiesSuccess( total: (state as FetchRecentPropertiesSuccess).total, offset: (state as FetchRecentPropertiesSuccess).offset, isLoadingMore: (state as FetchRecentPropertiesSuccess).isLoadingMore, hasError: (state as FetchRecentPropertiesSuccess).hasError, properties: (state as FetchRecentPropertiesSuccess).properties), ); }, ); } } } catch (e) { emit(FetchRecentPropertiesFailur(e.toString())); } } @override void fetchMore() async { if (state is FetchRecentPropertiesSuccess) { FetchRecentPropertiesSuccess mystate = (state as FetchRecentPropertiesSuccess); if (mystate.isLoadingMore) { return; } emit((state as FetchRecentPropertiesSuccess) .copyWith(isLoadingMore: true)); DataOutput result = await _propertyRepository.fetchRecentProperties( offset: (state as FetchRecentPropertiesSuccess).properties.length, ); // developer.log("API RESULT IS $result"); FetchRecentPropertiesSuccess propertymodelState = (state as FetchRecentPropertiesSuccess); propertymodelState.properties.addAll(result.modelList); emit(FetchRecentPropertiesSuccess( isLoadingMore: false, hasError: false, properties: propertymodelState.properties, offset: (state as FetchRecentPropertiesSuccess).properties.length, total: result.total)); } } @override bool hasMoreData() { if (state is FetchRecentPropertiesSuccess) { return (state as FetchRecentPropertiesSuccess).properties.length < (state as FetchRecentPropertiesSuccess).total; } return false; } @override FetchRecentPropertiesState? fromJson(Map json) { try { if (json['cubit_state'] == "FetchRecentPropertiesSuccess") { FetchRecentPropertiesSuccess fetchRecentPropertiesSuccess = FetchRecentPropertiesSuccess.fromMap(json); return fetchRecentPropertiesSuccess; } } catch (e) {} return null; } @override Map? toJson(FetchRecentPropertiesState state) { try { if (state is FetchRecentPropertiesSuccess) { Map mapped = state.toMap(); mapped['cubit_state'] = "FetchRecentPropertiesSuccess"; return mapped; } } catch (e) {} return null; } }