From 2f329669e92f51fdaf79517db8e10c57da782ecb Mon Sep 17 00:00:00 2001 From: Mohamed Nouffer Date: Tue, 5 Mar 2024 19:32:42 +0530 Subject: [PATCH] fixed position error and added goad condition button --- .../destination/destination_controller.dart | 85 +++++++++++-------- lib/utils/database_gps.dart | 34 +++++--- lib/widgets/bottom_sheet_new.dart | 2 +- lib/widgets/map_widget.dart | 2 +- 4 files changed, 71 insertions(+), 52 deletions(-) diff --git a/lib/pages/destination/destination_controller.dart b/lib/pages/destination/destination_controller.dart index 2cda607..4303eaf 100644 --- a/lib/pages/destination/destination_controller.dart +++ b/lib/pages/destination/destination_controller.dart @@ -562,19 +562,24 @@ class DestinationController extends GetxController { } Future addGPStoDB(double la, double ln, {isCheckin = 0}) async { - GpsDatabaseHelper db = GpsDatabaseHelper.instance; - final team_name = indexController.currentUser[0]["user"]['team_name']; - final event_code = indexController.currentUser[0]["user"]["event_code"]; - GpsData gps_data = GpsData( - id: 0, - team_name: team_name, - event_code: event_code, - lat: la, - lon: ln, - is_checkin: isCheckin, - created_at: DateTime.now().millisecondsSinceEpoch); - var res = await db.insertGps(gps_data); - //print("==gps res == ${res}"); + //print("in addGPStoDB ${indexController.currentUser}"); + try { + GpsDatabaseHelper db = GpsDatabaseHelper.instance; + final team_name = indexController.currentUser[0]["user"]['team_name']; + final event_code = indexController.currentUser[0]["user"]["event_code"]; + GpsData gps_data = GpsData( + id: 0, + team_name: team_name, + event_code: event_code, + lat: la, + lon: ln, + is_checkin: isCheckin, + created_at: DateTime.now().millisecondsSinceEpoch); + var res = await db.insertGps(gps_data); + } catch (err) { + print("errr ready gps ${err}"); + return; + } } Future checkForCheckin() async { @@ -762,33 +767,38 @@ class DestinationController extends GetxController { } void handleLocationUpdate(Position? position) async { - if (position != null) { - if (distanceToStart() >= 1000) { - ready_for_goal = true; - } + try { + if (position != null) { + if (distanceToStart() >= 1000) { + ready_for_goal = true; + } - var distance = const Distance(); - double distanceToDest = distance.as( - LengthUnit.Meter, - LatLng(position.latitude, position.longitude), - LatLng(currentLat, currentLon)); - Duration difference = - lastGPSCollectedTime.difference(DateTime.now()).abs(); - if (difference.inSeconds >= 10 || distanceToDest >= 10) { - // print( - // "^^^^^^^^ GPS data collected ${DateFormat('kk:mm:ss \n EEE d MMM').format(DateTime.now())}, ^^^ ${position.latitude}, ${position.longitude}"); + var distance = const Distance(); + double distanceToDest = distance.as( + LengthUnit.Meter, + LatLng(position.latitude, position.longitude), + LatLng(currentLat, currentLon)); - LogManager().addLog( - "GPS : $currentLat, $currentLon - ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}:${DateTime.now().microsecond}"); + Duration difference = + lastGPSCollectedTime.difference(DateTime.now()).abs(); + if (difference.inSeconds >= 10 || distanceToDest >= 10) { + // print( + // "^^^^^^^^ GPS data collected ${DateFormat('kk:mm:ss \n EEE d MMM').format(DateTime.now())}, ^^^ ${position.latitude}, ${position.longitude}"); - if (isInRog.value) { - await addGPStoDB(position.latitude, position.longitude); - lastGPSCollectedTime = DateTime.now(); + LogManager().addLog( + "GPS : $currentLat, $currentLon - ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}:${DateTime.now().microsecond}"); + if (isInRog.value) { + await addGPStoDB(position.latitude, position.longitude); + lastGPSCollectedTime = DateTime.now(); + } } } - - currentLat = position.latitude; - currentLon = position.longitude; + } finally { + if (position != null && + (position.latitude != 0 || position.longitude != 0)) { + currentLat = position.latitude; + currentLon = position.longitude; + } } } @@ -908,7 +918,10 @@ class DestinationController extends GetxController { } void centerMapToCurrentLocation() { - indexController.mapController.move(LatLng(currentLat, currentLon), 17.0); + print("center is ${currentLat}, ${currentLon}"); + if (currentLat != 0 || currentLon != 0) { + indexController.mapController.move(LatLng(currentLat, currentLon), 17.0); + } } void connectionChanged(String val) { diff --git a/lib/utils/database_gps.dart b/lib/utils/database_gps.dart index a60daa6..857c21d 100644 --- a/lib/utils/database_gps.dart +++ b/lib/utils/database_gps.dart @@ -44,20 +44,26 @@ class GpsDatabaseHelper { } Future insertGps(GpsData gps) async { - Database db = await instance.database; - int? nextOrder = - Sqflite.firstIntValue(await db.rawQuery('SELECT MAX(id) FROM gps')); - nextOrder = nextOrder ?? 0; - nextOrder = nextOrder + 1; - gps.id = nextOrder; - //print("---- insering ${gps.toMap()}"); - int res = await db.insert( - 'gps', - gps.toMap(), - conflictAlgorithm: ConflictAlgorithm.replace, - ); - //print("------ database helper insert $res-----------::::::::"); - return res; + try { + print("---- try insering ${gps.toMap()}"); + Database db = await instance.database; + int? nextOrder = + Sqflite.firstIntValue(await db.rawQuery('SELECT MAX(id) FROM gps')); + nextOrder = nextOrder ?? 0; + nextOrder = nextOrder + 1; + gps.id = nextOrder; + print("---- insering ${gps.toMap()}"); + int res = await db.insert( + 'gps', + gps.toMap(), + conflictAlgorithm: ConflictAlgorithm.replace, + ); + print("------ database helper insert $res-----------::::::::"); + return res; + } catch (err) { + print("------ error $err-----------::::::::"); + return -1; + } } Future> getGPSData(String team_name, String event_code) async { diff --git a/lib/widgets/bottom_sheet_new.dart b/lib/widgets/bottom_sheet_new.dart index c4bba62..d59fd08 100644 --- a/lib/widgets/bottom_sheet_new.dart +++ b/lib/widgets/bottom_sheet_new.dart @@ -129,7 +129,7 @@ class BottomSheetNew extends GetView { style: ElevatedButton.styleFrom(backgroundColor: Colors.red), onPressed: destinationController.rogainingCounted.value == true && destinationController.distanceToStart() <= 500 && - //destination.cp == -1 && + destination.cp == -1 && DestinationController.ready_for_goal == true ? () async { destinationController.isAtGoal.value = true; diff --git a/lib/widgets/map_widget.dart b/lib/widgets/map_widget.dart index ad909f1..b38d283 100644 --- a/lib/widgets/map_widget.dart +++ b/lib/widgets/map_widget.dart @@ -176,7 +176,7 @@ class _MapWidgetState extends State { } void _centerMapOnUser() { - print("showBottomSheet ${destinationController.shouldShowBottomSheet}"); + //print("showBottomSheet ${destinationController.shouldShowBottomSheet}"); if (destinationController.shouldShowBottomSheet) { destinationController.centerMapToCurrentLocation(); }