improvement

main_production_asset_tracking
Sikander Saleem 1 week ago
parent d6ed4412ae
commit 2d6e17c4a0

@ -136,23 +136,20 @@ class AssetTrackingProvider extends ChangeNotifier {
}
Future getZoneFromPenguin() async {
if (zonesList.isNotEmpty) {
await Future.delayed(const Duration(milliseconds: 250));
calculateCoordinates();
return;
}
try {
final response = await ApiManager.instance.get(URLs.getPenguinZones);
if (response.statusCode >= 200 && response.statusCode < 300) {
final Map<String, dynamic> jsonData = json.decode(response.body);
List list = jsonData["data"];
zonesList.addAll(list.map((item) => ZoneModel.fromJson(item)).toList());
calculateCoordinates();
// notify();
if (zonesList.isEmpty) {
try {
final response = await ApiManager.instance.get(URLs.getPenguinZones);
if (response.statusCode >= 200 && response.statusCode < 300) {
final Map<String, dynamic> jsonData = json.decode(response.body);
List list = jsonData["data"] ?? [];
zonesList.addAll(list.map((item) => ZoneModel.fromJson(item)).toList());
}
} catch (error) {
print("getZoneFromPenguin:Error:$error");
}
} catch (error) {
print("getZoneFromPenguin:Error:$error");
}
// Building outline comes from the selected building, so always compute it.
calculateCoordinates();
}
Future getAssetsFromPenguinByFloor() async {

@ -117,6 +117,57 @@ class _MapTrackingPageState extends State<MapTrackingPage> {
});
}
/// "View" button: loads the floor map (if a floor is selected) or the building outline.
Future<void> _onViewPressed(AssetTrackingProvider provider) async {
expansibleController.collapse();
provider.coordinates = [];
provider.assetsOnTheFloorList = [];
provider.notify();
provider.getAssetsFromPenguinByFloor();
if (provider.selectedFloor != null) {
getFloorMap(provider.selectedFloor!.id!);
provider.getZoneFromPenguin();
return;
}
// Building only: remove any previously displayed floor map so the building outline is visible.
setState(() {
_geojson = null;
floorMapModel = null;
_selectedZoneProperties = null;
markers = [];
_lastFittedCoordinatesKey = null; // force re-fit even if the same building is viewed again
});
Utils.showLoading(context);
await provider.getZoneFromPenguin();
if (!mounted) return;
Utils.hideLoading(context);
// Camera fit happens in the building-outline Selector once the polygon is rendered.
}
/// Signature of the last building outline the camera was fitted to.
String? _lastFittedCoordinatesKey;
/// Zooms/pans the map so all [points] are visible. Runs after the current frame so the
/// map has its final size, and schedules a frame so the callback is guaranteed to fire.
void _fitCameraToPoints(List<LatLng> points) {
if (points.isEmpty) return;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!mounted) return;
if (points.length == 1) {
mapController.move(points.first, 18);
return;
}
mapController.fitCamera(
CameraFit.bounds(bounds: LatLngBounds.fromPoints(points), padding: const EdgeInsets.all(48), maxZoom: 20),
);
});
WidgetsBinding.instance.scheduleFrame();
}
void _onFloorMapLoaded(FloorMapModel? loadedFloorMap) {
floorMapModel = loadedFloorMap;
_selectedZoneProperties = null;
@ -409,13 +460,22 @@ class _MapTrackingPageState extends State<MapTrackingPage> {
],
if (inAppView && _geojson == null)
Selector<AssetTrackingProvider, List<Coordinates>>(
selector: (_, assetTPro) => assetTPro.coordinates,
selector: (_, assetTPro) => List<Coordinates>.of(assetTPro.coordinates),
builder: (_, coordinates, __) {
if (coordinates.isEmpty) return const SizedBox();
List<LatLng> list = [
for (var coordinate in (coordinates)) LatLng(coordinate.latitude!, coordinate.longitude!),
for (var coordinate in (coordinates))
if (coordinate.latitude != null && coordinate.longitude != null) LatLng(coordinate.latitude!, coordinate.longitude!),
];
if (list.isEmpty) return const SizedBox();
// Fit the camera to the building outline once per new outline.
final key = "${assetTrackingProvider.selectedBuilding?.id}|${list.length}|${list.first}|${list.last}";
if (key != _lastFittedCoordinatesKey) {
_lastFittedCoordinatesKey = key;
_fitCameraToPoints(list);
}
List<Polygon> zonesPolygons = [
Polygon(
label: assetTrackingProvider.selectedBuilding?.label ?? "",

@ -8,10 +8,10 @@ class Coordinates {
Coordinates({this.x, this.y, this.longitude, this.latitude, this.orderIndex});
Coordinates.fromJson(Map<String, dynamic> json) {
x = json['x'];
y = json['y'];
longitude = json['longitude'];
latitude = json['latitude'];
x = (json['x'] as num?)?.toDouble();
y = (json['y'] as num?)?.toDouble();
longitude = (json['longitude'] as num?)?.toDouble();
latitude = (json['latitude'] as num?)?.toDouble();
orderIndex = json['orderIndex'];
}

Loading…
Cancel
Save