From 0508c50b97be5527399bf357c8ef12fce1be1f2e Mon Sep 17 00:00:00 2001 From: Mohamed Nouffer Date: Sun, 8 Oct 2023 19:13:14 +0530 Subject: [PATCH] optimized and removed travel mode --- lib/main.dart | 4 - .../destination/destination_controller.dart | 46 +- lib/pages/destination/destination_page.dart | 488 +++++++++--------- lib/pages/index/index_controller.dart | 53 +- lib/routes/app_pages.dart | 2 +- 5 files changed, 270 insertions(+), 323 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 50786d1..e5fe222 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -85,10 +85,6 @@ class _MyAppState extends State with WidgetsBindingObserver { case AppLifecycleState.resumed: //print("RESUMED"); restoreGame(); - if (destinationController.positionStream == null) { - //destinationController.startGPSTimer(); - //destinationController.startGame(); - } break; case AppLifecycleState.inactive: //print("INACTIVE"); diff --git a/lib/pages/destination/destination_controller.dart b/lib/pages/destination/destination_controller.dart index 6654250..67ac878 100644 --- a/lib/pages/destination/destination_controller.dart +++ b/lib/pages/destination/destination_controller.dart @@ -1,7 +1,5 @@ import 'dart:io'; - import 'package:camera_camera/camera_camera.dart'; -import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_map/flutter_map.dart'; import 'package:geojson/geojson.dart'; @@ -30,6 +28,8 @@ import 'package:gallery_saver/gallery_saver.dart'; class DestinationController extends GetxController { late LocationSettings locationSettings; + Timer? _GPStimer; + var destinationCount = 0.obs; List destinations = [].obs; double currentLat = 0.0; @@ -54,10 +54,6 @@ class DestinationController extends GetxController { List gps = ["-- stating --"].obs; List locationPermission = [" -- starting -- "].obs; - StreamSubscription? positionStream; - - Timer? gpsTimer; - var travelMode = 0.obs; bool skipGps = false; @@ -68,7 +64,6 @@ class DestinationController extends GetxController { final IndexController indexController = Get.find(); - Timer? _timer; int _start = 0; int chekcs = 0; var rogainingCounted = false.obs; @@ -110,7 +105,7 @@ class DestinationController extends GetxController { } Future startTimerLocation(GeoJsonFeature fs, double distance) async { - // print("---- in startTimer ----"); + //print("---- in startTimer ----"); // print("---- is in rog is $is_in_rog ----"); double checkinRadious = fs.properties!['checkin_radius'] ?? double.infinity; if (checkinRadious >= distance) { @@ -125,7 +120,7 @@ class DestinationController extends GetxController { } Future startTimer(Destination d, double distance) async { - //print("=== passed dest is ${d.location_id} ${d.checkedin} ===="); + print("=== passed dest is ${d.location_id} ${d.checkedin} ===="); skipGps = true; //print("---- in startTimer ----"); @@ -388,21 +383,12 @@ class DestinationController extends GetxController { lastGoalAt = DateTime.fromMicrosecondsSinceEpoch(latgoal); //print("===== last goal : $last_goal_at ====="); } - - //deleteDBDestinations(); - - // currentSelectedDestinations.forEach((element) { - // deleteDestination(element); - // }); } void deleteAllDestinations() { DatabaseHelper db = DatabaseHelper.instance; db.deleteAllDestinations().then((value) { populateDestinations(); - //startGPSTimer(); - //startGame(); - //initGPS(); }); } @@ -510,13 +496,18 @@ class DestinationController extends GetxController { } Future checkForCheckin() async { - //print("--- Start of checkForCheckin function ---"); + print("--- Start of checkForCheckin function ---"); try { //print("--- 000 ---- $skip_gps----"); await Future.delayed(const Duration(milliseconds: 2500)); - final la = currentLat; - final ln = currentLon; + Position position = await Geolocator.getCurrentPosition( + desiredAccuracy: LocationAccuracy.bestForNavigation); + currentLat = position.latitude; + currentLon = position.longitude; + final la = position.latitude; + final ln = position.longitude; + for (GeoJsonFeature fs in indexController.locations[0].collection) { GeoJsonMultiPoint mp = fs.geometry as GeoJsonMultiPoint; LatLng pt = LatLng(mp.geoSerie!.geoPoints[0].latitude, @@ -525,10 +516,16 @@ class DestinationController extends GetxController { double latFs = pt.latitude; double lonFs = pt.longitude; var distanceFs = const Distance(); + //print("--- points : ${pt.latitude}, ${pt.longitude} ----"); + //print("--- points : ${pt.latitude}, ${pt.longitude} ----"); double distFs = distanceFs.as( LengthUnit.Meter, LatLng(latFs, lonFs), LatLng(la, ln)); Destination des = festuretoDestination(fs); + print( + "--- position is ---- ${position.longitude}, --- ${position.longitude}----"); + + print("--- distFs ---- $distFs, --- ${des.checkin_radious}----"); if (distFs <= des.checkin_radious! && skipGps == false) { //print("--- 789 ---- $skip_gps----"); //near a location @@ -642,21 +639,18 @@ class DestinationController extends GetxController { } Future startGame() async { + //print("------ starting game ------"); await checkForCheckin(); } @override void onClose() { - positionStream?.cancel(); - gpsTimer?.cancel(); super.onClose(); super.onClose(); } @override void onInit() async { - //initGPS(); - //startGPSTimer(); startGame(); super.onInit(); } @@ -697,7 +691,7 @@ class DestinationController extends GetxController { void fixMapBound(String token) { //String _token = indexController.currentUser[0]["token"]; - indexController.switchPage(AppPages.INITIAL); + indexController.switchPage(AppPages.INDEX); LocationService.getLocationsExt(token).then((value) { if (value != null) { //print("--- loc ext is - $value ----"); diff --git a/lib/pages/destination/destination_page.dart b/lib/pages/destination/destination_page.dart index fb9e796..7b7928f 100644 --- a/lib/pages/destination/destination_page.dart +++ b/lib/pages/destination/destination_page.dart @@ -1,252 +1,252 @@ -import 'package:flutter/material.dart'; -import 'package:geolocator/geolocator.dart'; -import 'package:get/get.dart'; -import 'package:latlong2/latlong.dart'; -import 'package:rogapp/pages/destination/destination_controller.dart'; -import 'package:rogapp/pages/destination_map/destination_map_page.dart'; -import 'package:rogapp/pages/drawer/drawer_page.dart'; -import 'package:rogapp/pages/index/index_controller.dart'; -import 'package:rogapp/routes/app_pages.dart'; -import 'package:rogapp/widgets/destination_widget.dart'; +// import 'package:flutter/material.dart'; +// import 'package:geolocator/geolocator.dart'; +// import 'package:get/get.dart'; +// import 'package:latlong2/latlong.dart'; +// import 'package:rogapp/pages/destination/destination_controller.dart'; +// import 'package:rogapp/pages/destination_map/destination_map_page.dart'; +// import 'package:rogapp/pages/drawer/drawer_page.dart'; +// import 'package:rogapp/pages/index/index_controller.dart'; +// import 'package:rogapp/routes/app_pages.dart'; +// import 'package:rogapp/widgets/destination_widget.dart'; -class XDestnationPage extends StatelessWidget { - XDestnationPage({Key? key}) : super(key: key); +// class XDestnationPage extends StatelessWidget { +// XDestnationPage({Key? key}) : super(key: key); - final DestinationController destinationController = - Get.find(); +// final DestinationController destinationController = +// Get.find(); - final IndexController indexController = Get.find(); +// final IndexController indexController = Get.find(); - final List _items = List.generate(50, (int index) => index); +// final List _items = List.generate(50, (int index) => index); - Future showCurrentPosition() async { - LocationPermission permission = await Geolocator.checkPermission(); - if (permission != LocationPermission.whileInUse || - permission != LocationPermission.always) { - permission = await Geolocator.requestPermission(); - } - Position position = await Geolocator.getCurrentPosition( - desiredAccuracy: LocationAccuracy.high, - forceAndroidLocationManager: true); - indexController.rogMapController - .move(LatLng(position.latitude, position.longitude), 14); - } +// Future showCurrentPosition() async { +// LocationPermission permission = await Geolocator.checkPermission(); +// if (permission != LocationPermission.whileInUse || +// permission != LocationPermission.always) { +// permission = await Geolocator.requestPermission(); +// } +// Position position = await Geolocator.getCurrentPosition( +// desiredAccuracy: LocationAccuracy.high, +// forceAndroidLocationManager: true); +// indexController.rogMapController +// .move(LatLng(position.latitude, position.longitude), 14); +// } - Image getImage(int index) { - if (destinationController.destinations[index].photos == null || - destinationController.destinations[index].photos == "") { - return const Image(image: AssetImage('assets/images/empty_image.png')); - } else { - return Image( - image: - NetworkImage(destinationController.destinations[index].photos!)); - } - } +// Image getImage(int index) { +// if (destinationController.destinations[index].photos == null || +// destinationController.destinations[index].photos == "") { +// return const Image(image: AssetImage('assets/images/empty_image.png')); +// } else { +// return Image( +// image: +// NetworkImage(destinationController.destinations[index].photos!)); +// } +// } - Widget getRoutingImage(int route) { - switch (route) { - case 0: - return const Image( - image: AssetImage('assets/images/p4_9_man.png'), - width: 35.0, - ); - case 1: - return const Image( - image: AssetImage('assets/images/p4_8_car.png'), - width: 35.0, - ); - case 2: - return const Image( - image: AssetImage('assets/images/p4_10_train.png'), - width: 35.0, - ); - default: - return const Image( - image: AssetImage('assets/images/p4_9_man.png'), - width: 35.0, - ); - } - } +// Widget getRoutingImage(int route) { +// switch (route) { +// case 0: +// return const Image( +// image: AssetImage('assets/images/p4_9_man.png'), +// width: 35.0, +// ); +// case 1: +// return const Image( +// image: AssetImage('assets/images/p4_8_car.png'), +// width: 35.0, +// ); +// case 2: +// return const Image( +// image: AssetImage('assets/images/p4_10_train.png'), +// width: 35.0, +// ); +// default: +// return const Image( +// image: AssetImage('assets/images/p4_9_man.png'), +// width: 35.0, +// ); +// } +// } - @override - Widget build(BuildContext context) { - return WillPopScope( - onWillPop: () async { - indexController.switchPage(AppPages.INITIAL); - return false; - }, - child: Scaffold( - drawer: DrawerPage(), - bottomNavigationBar: BottomAppBar( - child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - Padding( - padding: const EdgeInsets.only(left: 13.0), - child: InkWell( - child: Obx((() => getRoutingImage( - destinationController.travelMode.value))), - onTap: () { - Get.bottomSheet( - Obx( - () => ListView( - children: [ - Padding( - padding: const EdgeInsets.only( - top: 30.0, bottom: 30), - child: Center( - child: Text( - "select_travel_mode".tr, - style: const TextStyle( - fontSize: 22.0, - color: Colors.red, - fontWeight: FontWeight.bold), - ), - ), - ), - ListTile( - selected: - destinationController.travelMode.value == - 0 - ? true - : false, - selectedTileColor: Colors.amber.shade200, - leading: const Image( - image: AssetImage( - 'assets/images/p4_9_man.png'), - ), - title: Text("walking".tr), - onTap: () { - destinationController.travelMode.value = 0; - destinationController - .populateDestinations(); - Get.back(); - }, - ), - ListTile( - selected: - destinationController.travelMode.value == - 1 - ? true - : false, - selectedTileColor: Colors.amber.shade200, - leading: const Image( - image: AssetImage( - 'assets/images/p4_8_car.png'), - ), - title: Text("driving".tr), - onTap: () { - destinationController.travelMode.value = 1; - destinationController - .populateDestinations(); - Get.back(); - }, - ), - // ListTile( - // selected: destinationController.travelMode == 2 ? true : false, - // selectedTileColor: Colors.amber.shade200, - // leading: Image(image: AssetImage('assets/images/p4_10_train.png'),), - // title: Text("transit".tr), - // onTap:(){ - // destinationController.travelMode.value = 2; - // destinationController.PopulateDestinations(); - // Get.back(); - // }, - // ), - ], - ), - ), - isScrollControlled: false, - backgroundColor: Colors.white, - ); - //destinationController.PopulateDestinations(); - }), - ), - IconButton( - icon: const Icon( - Icons.travel_explore, - size: 35, - ), - onPressed: () { - indexController.switchPage(AppPages.INITIAL); - }), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: () { - //print("######"); - indexController.toggleDestinationMode(); - }, - tooltip: 'Increment', - elevation: 4.0, - child: Obx(() => indexController.desinationMode.value == 1 - ? const Image(image: AssetImage('assets/images/list2.png')) - : const Image(image: AssetImage('assets/images/map.png'))), - ), - floatingActionButtonLocation: - FloatingActionButtonLocation.centerDocked, - appBar: AppBar( - automaticallyImplyLeading: true, - title: Text("app_title".tr), - actions: [ - InkWell( - onTap: () { - Get.toNamed(AppPages.CAMERA_PAGE); - }, - child: destinationController.isInRog.value == true - ? Image.asset( - "assets/images/basic-walking.gif", - height: 10.0, - ) - : destinationController.isAtGoal.value == true - ? IconButton( - onPressed: () { - Get.toNamed(AppPages.CAMERA_PAGE); - }, - icon: const Icon(Icons.assistant_photo), - ) - : IconButton( - onPressed: () { - Get.toNamed(AppPages.CAMERA_PAGE); - }, - icon: const Icon(Icons.accessibility), - ), - ), - // Obx(() => - // Text(indexController.connectionStatusName.value) - // ), - Obx( - () => ToggleButtons( - disabledColor: Colors.grey.shade200, - selectedColor: Colors.red, - onPressed: (int index) { - destinationController.isGpsSelected.value = - !destinationController.isGpsSelected.value; - if (destinationController.isGpsSelected.value) { - destinationController.chekcs = 0; - destinationController.skipGps = false; - //destinationController.resetRogaining(); - } - }, - isSelected: [destinationController.isGpsSelected.value], - children: const [ - Icon( - Icons.explore, - size: 35.0, - ) - ], - ), - ), - // IconButton(onPressed: (){ - // showCurrentPosition(); - // }, - // icon: Icon(Icons.location_on_outlined)) - ], - ), - body: Obx(() => indexController.desinationMode.value == 0 - ? DestinationWidget() - : DestinationMapPage())), - ); - } -} +// @override +// Widget build(BuildContext context) { +// return WillPopScope( +// onWillPop: () async { +// indexController.switchPage(AppPages.INITIAL); +// return false; +// }, +// child: Scaffold( +// drawer: DrawerPage(), +// bottomNavigationBar: BottomAppBar( +// child: Row( +// mainAxisAlignment: MainAxisAlignment.spaceBetween, +// children: [ +// Padding( +// padding: const EdgeInsets.only(left: 13.0), +// child: InkWell( +// child: Obx((() => getRoutingImage( +// destinationController.travelMode.value))), +// onTap: () { +// Get.bottomSheet( +// Obx( +// () => ListView( +// children: [ +// Padding( +// padding: const EdgeInsets.only( +// top: 30.0, bottom: 30), +// child: Center( +// child: Text( +// "select_travel_mode".tr, +// style: const TextStyle( +// fontSize: 22.0, +// color: Colors.red, +// fontWeight: FontWeight.bold), +// ), +// ), +// ), +// ListTile( +// selected: +// destinationController.travelMode.value == +// 0 +// ? true +// : false, +// selectedTileColor: Colors.amber.shade200, +// leading: const Image( +// image: AssetImage( +// 'assets/images/p4_9_man.png'), +// ), +// title: Text("walking".tr), +// onTap: () { +// destinationController.travelMode.value = 0; +// destinationController +// .populateDestinations(); +// Get.back(); +// }, +// ), +// ListTile( +// selected: +// destinationController.travelMode.value == +// 1 +// ? true +// : false, +// selectedTileColor: Colors.amber.shade200, +// leading: const Image( +// image: AssetImage( +// 'assets/images/p4_8_car.png'), +// ), +// title: Text("driving".tr), +// onTap: () { +// destinationController.travelMode.value = 1; +// destinationController +// .populateDestinations(); +// Get.back(); +// }, +// ), +// // ListTile( +// // selected: destinationController.travelMode == 2 ? true : false, +// // selectedTileColor: Colors.amber.shade200, +// // leading: Image(image: AssetImage('assets/images/p4_10_train.png'),), +// // title: Text("transit".tr), +// // onTap:(){ +// // destinationController.travelMode.value = 2; +// // destinationController.PopulateDestinations(); +// // Get.back(); +// // }, +// // ), +// ], +// ), +// ), +// isScrollControlled: false, +// backgroundColor: Colors.white, +// ); +// //destinationController.PopulateDestinations(); +// }), +// ), +// IconButton( +// icon: const Icon( +// Icons.travel_explore, +// size: 35, +// ), +// onPressed: () { +// indexController.switchPage(AppPages.INITIAL); +// }), +// ], +// ), +// ), +// floatingActionButton: FloatingActionButton( +// onPressed: () { +// //print("######"); +// indexController.toggleDestinationMode(); +// }, +// tooltip: 'Increment', +// elevation: 4.0, +// child: Obx(() => indexController.desinationMode.value == 1 +// ? const Image(image: AssetImage('assets/images/list2.png')) +// : const Image(image: AssetImage('assets/images/map.png'))), +// ), +// floatingActionButtonLocation: +// FloatingActionButtonLocation.centerDocked, +// appBar: AppBar( +// automaticallyImplyLeading: true, +// title: Text("app_title".tr), +// actions: [ +// InkWell( +// onTap: () { +// Get.toNamed(AppPages.CAMERA_PAGE); +// }, +// child: destinationController.isInRog.value == true +// ? Image.asset( +// "assets/images/basic-walking.gif", +// height: 10.0, +// ) +// : destinationController.isAtGoal.value == true +// ? IconButton( +// onPressed: () { +// Get.toNamed(AppPages.CAMERA_PAGE); +// }, +// icon: const Icon(Icons.assistant_photo), +// ) +// : IconButton( +// onPressed: () { +// Get.toNamed(AppPages.CAMERA_PAGE); +// }, +// icon: const Icon(Icons.accessibility), +// ), +// ), +// // Obx(() => +// // Text(indexController.connectionStatusName.value) +// // ), +// Obx( +// () => ToggleButtons( +// disabledColor: Colors.grey.shade200, +// selectedColor: Colors.red, +// onPressed: (int index) { +// destinationController.isGpsSelected.value = +// !destinationController.isGpsSelected.value; +// if (destinationController.isGpsSelected.value) { +// destinationController.chekcs = 0; +// destinationController.skipGps = false; +// //destinationController.resetRogaining(); +// } +// }, +// isSelected: [destinationController.isGpsSelected.value], +// children: const [ +// Icon( +// Icons.explore, +// size: 35.0, +// ) +// ], +// ), +// ), +// // IconButton(onPressed: (){ +// // showCurrentPosition(); +// // }, +// // icon: Icon(Icons.location_on_outlined)) +// ], +// ), +// body: Obx(() => indexController.desinationMode.value == 0 +// ? DestinationWidget() +// : DestinationMapPage())), +// ); +// } +// } diff --git a/lib/pages/index/index_controller.dart b/lib/pages/index/index_controller.dart index f3162e0..e357393 100644 --- a/lib/pages/index/index_controller.dart +++ b/lib/pages/index/index_controller.dart @@ -10,15 +10,10 @@ import 'package:geojson/geojson.dart'; import 'package:get/get.dart'; import 'package:latlong2/latlong.dart'; import 'package:rogapp/model/destination.dart'; -import 'package:rogapp/pages/destination/destination_binding.dart'; import 'package:rogapp/pages/destination/destination_controller.dart'; -import 'package:rogapp/pages/destination/destination_page.dart'; import 'package:rogapp/routes/app_pages.dart'; -import 'package:rogapp/services/action_service.dart'; import 'package:rogapp/services/auth_service.dart'; -import 'package:rogapp/services/cat_service.dart'; import 'package:rogapp/services/location_service.dart'; -import 'package:rogapp/services/perfecture_service.dart'; import 'package:rogapp/utils/database_helper.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -68,8 +63,6 @@ class IndexController extends GetxController { String areaDropdownValue = "-1"; String cateogory = "-all-"; - late Worker _ever; - ConnectivityResult connectionStatus = ConnectivityResult.none; var connectionStatusName = "".obs; final Connectivity _connectivity = Connectivity(); @@ -94,7 +87,7 @@ class IndexController extends GetxController { void switchPage(String page) { ////print("######## ${currentUser[0]["user"]["id"]}"); switch (page) { - case AppPages.INITIAL: + case AppPages.INDEX: { rogMode.value = 0; //print("-- rog mode is ctrl is ${rog_mode.value}"); @@ -117,24 +110,13 @@ class IndexController extends GetxController { default: { rogMode.value = 0; - Get.toNamed(AppPages.INITIAL); + Get.toNamed(AppPages.INDEX); } } } @override void onInit() { - //_ever = ever(rogMode, (_) => print("$_ has been changed (ever)")); - - // if (perfectures.isEmpty) { - // PerfectureService.loadPerfectures().then((value) { - // perfectures.add(value); - // loadAreaFor("9"); - - // //loadSubPerfFor("9"); - // }); - // } - _connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus); super.onInit(); @@ -181,19 +163,6 @@ class IndexController extends GetxController { return LatLngBounds(LatLng(x1!, y1!), LatLng(x0!, y0!)); } - List getLocationsList() { - List locs = []; - for (int i = 0; i <= locations[0].collection.length - 1; i++) { - GeoJsonMultiPoint p = - locations[0].collection[i].geometry as GeoJsonMultiPoint; - - LatLng latLng = LatLng(p.geoSerie!.geoPoints[0].latitude, - p.geoSerie!.geoPoints[0].longitude); - locs.add(latLng); - } - return locs; - } - void login(String email, String password, BuildContext context) { AuthService.login(email, password).then((value) { //print("------- logged in user details ######## $value ###### --------"); @@ -228,7 +197,7 @@ class IndexController extends GetxController { if (rogMode.value == 1) { switchPage(AppPages.TRAVEL); } else { - switchPage(AppPages.INITIAL); + switchPage(AppPages.INDEX); } } else { Get.snackbar( @@ -264,7 +233,7 @@ class IndexController extends GetxController { currentUser.add(value); isLoading.value = false; Navigator.pop(context); - Get.toNamed(AppPages.INITIAL); + Get.toNamed(AppPages.INDEX); } else { isLoading.value = false; Get.snackbar( @@ -299,7 +268,7 @@ class IndexController extends GetxController { } else { rogMode.value = 1; } - Get.toNamed(AppPages.INITIAL); + Get.toNamed(AppPages.INDEX); } loadUserDetailsForToken(String token) async { @@ -370,18 +339,6 @@ class IndexController extends GetxController { currentBound.add(bounds); } - void zoomtoSubPerf(String id) { - ////print("zooooom"); - - PerfectureService.getSubExt(id).then((value) { - LatLng lat1 = LatLng(value![1], value[0]); - LatLng lat2 = LatLng(value[3], value[2]); - LatLngBounds bound = LatLngBounds(lat1, lat2); - mapController.fitBounds(bound); - setBound(bound); - }); - } - GeoJsonFeature? getFeatureForLatLong(double lat, double long) { if (locations.isNotEmpty) { for (GeoJsonFeature i in locations[0].collection) { diff --git a/lib/routes/app_pages.dart b/lib/routes/app_pages.dart index 790e11b..a8222d1 100644 --- a/lib/routes/app_pages.dart +++ b/lib/routes/app_pages.dart @@ -26,7 +26,7 @@ part 'app_routes.dart'; class AppPages { // ignore: constant_identifier_names - static const INITIAL = Routes.INDEX; + static const INDEX = Routes.INDEX; // ignore: constant_identifier_names static const SPA = Routes.SPA; static const LANDING = Routes.LANDING;