Files
rasadyar_application/packages/inspection/lib/presentation/pages/inspection_map/logic.dart

90 lines
3.2 KiB
Dart

import 'dart:async';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_inspection/data/model/response/poultry_location/poultry_location_model.dart';
import 'package:rasadyar_inspection/data/repositories/inspection/inspection_repository_imp.dart';
import 'package:rasadyar_inspection/injection/inspection_di.dart';
import 'package:rasadyar_inspection/presentation/widget/base_page/logic.dart' hide BaseLogic;
import 'widget/map/logic.dart';
class InspectionMapLogic extends GetxController {
final BaseLogic baseLogic = Get.find<BaseLogic>();
final MapLogic mapLogic = Get.find<MapLogic>();
InspectionRepositoryImp inspectionRepository = diInspection.get<InspectionRepositoryImp>();
Rx<LatLng> currentLocation = LatLng(34.798315281272544, 48.51479142983491).obs;
Rx<Resource<List<PoultryLocationModel>>> allPoultryLocation =
Resource<List<PoultryLocationModel>>.initial().obs;
Rx<Resource<List<PoultryLocationModel>>> searchedPoultryLocation =
Resource<List<PoultryLocationModel>>.initial().obs;
RxList<Marker> markers = <Marker>[].obs;
RxList<PoultryLocationModel> markers2 = <PoultryLocationModel>[].obs;
RxInt filterIndex = 0.obs;
RxInt showIndex = 0.obs;
@override
void onInit() {
super.onInit();
fetchAllPoultryLocations();
debounce(baseLogic.searchValue, (callback) {
if (callback != null &&
callback.trim().isNotEmpty &&
searchedPoultryLocation.value.status != ResourceStatus.loading) {
searchPoultryLocations();
}
}, time: Duration(seconds: 2));
}
Future<void> fetchAllPoultryLocations() async {
allPoultryLocation.value = Resource<List<PoultryLocationModel>>.loading();
await safeCall(
call: () => inspectionRepository.getNearbyLocation(),
onSuccess: (result) async{
if (result != null) {
allPoultryLocation.value = Resource<List<PoultryLocationModel>>.success(result);
mapLogic.allLocations.value = Resource<List<PoultryLocationModel>>.success(result);
} else {
allPoultryLocation.value = Resource<List<PoultryLocationModel>>.error(
'No locations found',
);
}
},
onError: (error, stackTrace) {
allPoultryLocation.value = Resource<List<PoultryLocationModel>>.error(error.toString());
},
);
}
Future<void> searchPoultryLocations() async {
searchedPoultryLocation.value = Resource<List<PoultryLocationModel>>.loading();
await safeCall(
call: () => inspectionRepository.getNearbyLocation(value: baseLogic.searchValue.value),
onSuccess: (result) {
if (result != null || result!.isNotEmpty) {
searchedPoultryLocation.value = Resource<List<PoultryLocationModel>>.success(result);
mapLogic.hasFilterOrSearch.value = true;
mapLogic.filteredLocations.value = Resource<List<PoultryLocationModel>>.success(result);
} else {
searchedPoultryLocation.value = Resource<List<PoultryLocationModel>>.empty();
mapLogic.filteredLocations.value = Resource<List<PoultryLocationModel>>.empty();
}
},
onError: (error, stackTrace) {
searchedPoultryLocation.value = Resource<List<PoultryLocationModel>>.error(
error.toString(),
);
},
);
}
}