feat :
1 - search and filter location 2 - mapWidget
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
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';
|
||||
|
||||
class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin {
|
||||
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>();
|
||||
|
||||
final distance = Distance();
|
||||
|
||||
Rx<LatLng> currentLocation = LatLng(34.798315281272544, 48.51479142983491).obs;
|
||||
|
||||
Rx<Resource<List<PoultryLocationModel>>> allPoultryLocation =
|
||||
@@ -26,26 +24,12 @@ class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin
|
||||
RxList<Marker> markers = <Marker>[].obs;
|
||||
RxList<PoultryLocationModel> markers2 = <PoultryLocationModel>[].obs;
|
||||
|
||||
Timer? _debounceTimer;
|
||||
RxBool isLoading = false.obs;
|
||||
RxBool isSelectedDetailsLocation = false.obs;
|
||||
|
||||
RxInt filterIndex = 0.obs;
|
||||
RxInt showIndex = 0.obs;
|
||||
bool showSlideHint = true;
|
||||
RxInt currentZoom = 15.obs;
|
||||
Rx<MapController> mapController = MapController().obs;
|
||||
late final AnimatedMapController animatedMapController;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
animatedMapController = AnimatedMapController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 500),
|
||||
curve: Curves.easeInOut,
|
||||
cancelPreviousAnimations: true,
|
||||
);
|
||||
|
||||
fetchAllPoultryLocations();
|
||||
|
||||
@@ -69,82 +53,14 @@ class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin
|
||||
super.onClose();
|
||||
}
|
||||
|
||||
Future<void> determineCurrentPosition() async {
|
||||
isLoading.value = true;
|
||||
final position = await Geolocator.getCurrentPosition(
|
||||
locationSettings: AndroidSettings(accuracy: LocationAccuracy.best),
|
||||
);
|
||||
final latLng = LatLng(position.latitude, position.longitude);
|
||||
|
||||
/*currentLocation.value = latLng;
|
||||
markers.add(PoultryLocationModel(
|
||||
lat: latLng.latitude,
|
||||
long: latLng.longitude
|
||||
));*/
|
||||
animatedMapController.animateTo(
|
||||
dest: latLng,
|
||||
zoom: 18,
|
||||
curve: Curves.easeInOut,
|
||||
duration: const Duration(seconds: 1),
|
||||
);
|
||||
isLoading.value = false;
|
||||
}
|
||||
|
||||
void debouncedUpdateVisibleMarkers({required LatLng center, required double zoom}) {
|
||||
_debounceTimer?.cancel();
|
||||
_debounceTimer = Timer(const Duration(milliseconds: 300), () {
|
||||
final radius = getVisibleRadiusKm(
|
||||
zoom: zoom,
|
||||
screenWidthPx: Get.width.toDouble(),
|
||||
latitude: center.latitude,
|
||||
);
|
||||
|
||||
final filtered = filterNearbyMarkers(
|
||||
allPoultryLocation.value.data ?? [],
|
||||
center.latitude,
|
||||
center.longitude,
|
||||
radius * 1000,
|
||||
);
|
||||
final existingIds = markers2.map((e) => e.id).toSet();
|
||||
final uniqueFiltered = filtered.where((e) => !existingIds.contains(e.id)).toList();
|
||||
markers2.addAll(uniqueFiltered);
|
||||
});
|
||||
}
|
||||
|
||||
List<PoultryLocationModel> filterNearbyMarkers(
|
||||
List<PoultryLocationModel> allMarkers,
|
||||
double centerLat,
|
||||
double centerLng,
|
||||
double radiusInMeters,
|
||||
) {
|
||||
final center = LatLng(centerLat, centerLng);
|
||||
|
||||
return allMarkers.where((marker) {
|
||||
var tmp = LatLng(marker.lat ?? 0, marker.long ?? 0);
|
||||
return distance(center, tmp) <= radiusInMeters;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
Future<void> triggerSlidableAnimation() async {
|
||||
await Future.delayed(Duration(milliseconds: 200));
|
||||
//await slidController.value.openEndActionPane();
|
||||
await Future.delayed(Duration(milliseconds: 200));
|
||||
//await slidController.value.close();
|
||||
showSlideHint = false;
|
||||
}
|
||||
|
||||
Future<void> fetchAllPoultryLocations() async {
|
||||
isLoading.value = true;
|
||||
allPoultryLocation.value = Resource<List<PoultryLocationModel>>.loading();
|
||||
await safeCall(
|
||||
call: () => inspectionRepository.getNearbyLocation(
|
||||
centerLat: currentLocation.value.latitude,
|
||||
centerLng: currentLocation.value.longitude,
|
||||
radius: 15, // Radius in K meters
|
||||
),
|
||||
call: () => inspectionRepository.getNearbyLocation(),
|
||||
onSuccess: (result) {
|
||||
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',
|
||||
@@ -164,8 +80,11 @@ class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin
|
||||
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) {
|
||||
@@ -175,14 +94,4 @@ class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
double getVisibleRadiusKm({
|
||||
required double zoom,
|
||||
required double screenWidthPx,
|
||||
required double latitude,
|
||||
}) {
|
||||
double metersPerPixel = 156543.03392 * cos(latitude * pi / 180) / pow(2, zoom);
|
||||
double visibleWidthInMeters = metersPerPixel * screenWidthPx;
|
||||
return (visibleWidthInMeters / 2) / 1000; // radius in KM
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user