Files
rog_app/lib/pages/destination/destination_controller.dart

956 lines
31 KiB
Dart
Raw Normal View History

2022-10-12 21:46:17 +05:30
import 'dart:io';
2022-06-14 14:37:59 +05:30
2022-10-12 21:46:17 +05:30
import 'package:camera_camera/camera_camera.dart';
2022-06-04 20:16:29 +05:30
import 'package:flutter/foundation.dart';
2022-05-12 02:17:08 +05:30
import 'package:flutter/material.dart';
2022-12-27 18:52:37 +05:30
import 'package:flutter_map/flutter_map.dart';
2022-06-14 14:37:59 +05:30
import 'package:geojson/geojson.dart';
2022-06-04 20:16:29 +05:30
import 'package:geolocator/geolocator.dart';
2022-05-12 02:17:08 +05:30
import 'package:get/get.dart';
2022-11-05 22:02:21 +05:30
import 'package:intl/intl.dart';
2022-06-14 14:37:59 +05:30
import 'package:latlong2/latlong.dart';
2022-07-09 22:51:34 +05:30
import 'package:rogapp/model/destination.dart';
2022-10-30 21:43:29 +05:30
import 'package:rogapp/pages/camera/camera_page.dart';
2022-05-12 02:17:08 +05:30
import 'package:rogapp/pages/index/index_controller.dart';
import 'package:rogapp/routes/app_pages.dart';
import 'package:rogapp/services/destination_service.dart';
2022-10-19 08:53:17 +05:30
import 'package:rogapp/services/external_service.dart';
2022-12-27 18:52:37 +05:30
import 'package:rogapp/services/location_service.dart';
2022-05-18 19:09:26 +05:30
import 'package:rogapp/services/maxtrix_service.dart';
2022-12-27 18:52:37 +05:30
import 'package:rogapp/services/perfecture_service.dart';
2022-07-09 22:51:34 +05:30
import 'package:rogapp/utils/database_helper.dart';
2022-10-30 21:43:29 +05:30
import 'package:rogapp/widgets/bottom_sheet_new.dart';
2022-06-04 20:16:29 +05:30
import 'dart:async';
2022-05-12 02:17:08 +05:30
2022-11-05 22:02:21 +05:30
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
2023-09-04 22:46:53 +05:30
import 'package:shared_preferences/shared_preferences.dart';
2023-09-06 21:04:44 +05:30
import 'package:gallery_saver/gallery_saver.dart';
2022-06-14 14:37:59 +05:30
2022-05-12 02:17:08 +05:30
class DestinationController extends GetxController {
2022-06-04 20:16:29 +05:30
late LocationSettings locationSettings;
2022-05-12 02:17:08 +05:30
2022-06-29 18:25:19 +05:30
var destinationCount = 0.obs;
2022-07-09 22:51:34 +05:30
List<Destination> destinations = <Destination>[].obs;
2022-10-12 21:46:17 +05:30
double current_lat = 0.0;
double current_lon = 0.0;
2022-05-25 21:00:10 +05:30
2023-01-26 23:30:48 +05:30
bool skip_10s = false;
2023-09-04 22:46:53 +05:30
List<Destination> currentSelectedDestinations = <Destination>[].obs;
2022-07-10 23:50:43 +05:30
2022-10-30 21:43:29 +05:30
var is_in_checkin = false.obs;
2022-10-12 21:46:17 +05:30
var is_in_rog = false.obs;
2022-10-30 21:43:29 +05:30
var is_at_start = false.obs;
var is_at_goal = false.obs;
2022-11-05 22:02:21 +05:30
var is_photo_shoot = false.obs;
2023-09-04 22:46:53 +05:30
DateTime last_goal_at = DateTime.now().subtract(const Duration(days: 1));
2022-10-30 21:43:29 +05:30
//List<Rogaining> rogainings = <Rogaining>[].obs;
2022-10-12 21:46:17 +05:30
2022-06-14 14:37:59 +05:30
bool checking_in = false;
2022-10-30 21:43:29 +05:30
var is_gps_selected = true.obs;
2022-06-14 14:37:59 +05:30
BuildContext? context;
2022-07-20 15:57:40 +05:30
List<String> gps = <String>["-- stating --"].obs;
List<String> locationPermission = <String>[" -- starting -- "].obs;
2023-09-06 00:22:02 +05:30
StreamSubscription<Position>? positionStream;
Timer? _GPStimer;
2022-09-22 20:36:56 +05:30
var travelMode = 0.obs;
2022-10-30 21:43:29 +05:30
bool skip_gps = false;
2022-05-18 19:09:26 +05:30
Map<String, dynamic> matrix = {};
2022-05-12 02:17:08 +05:30
2022-10-12 21:46:17 +05:30
final photos = <File>[].obs;
2022-05-12 02:17:08 +05:30
final IndexController indexController = Get.find<IndexController>();
2022-10-12 21:46:17 +05:30
Timer? _timer;
2022-10-30 21:43:29 +05:30
int _start = 0;
2022-11-01 23:47:35 +05:30
int chekcs = 0;
2022-10-12 21:46:17 +05:30
var rogaining_counted = false.obs;
2023-09-04 22:46:53 +05:30
String getFormatedTime(DateTime datetime) {
return DateFormat('yyyy-MM-dd HH:mm:ss').format(datetime);
2022-11-14 22:22:41 +05:30
}
2023-09-04 22:46:53 +05:30
Destination festuretoDestination(GeoJsonFeature fs) {
2022-10-30 21:43:29 +05:30
GeoJsonMultiPoint mp = fs.geometry as GeoJsonMultiPoint;
2023-09-04 22:46:53 +05:30
LatLng pt = LatLng(mp.geoSerie!.geoPoints[0].latitude,
mp.geoSerie!.geoPoints[0].longitude);
//print("----- ${indexController.currentFeature[0].properties} -----");
return Destination(
name: fs.properties!["location_name"],
address: fs.properties!["address"],
phone: fs.properties!["phone"],
email: fs.properties!["email"],
webcontents: fs.properties!["webcontents"],
videos: fs.properties!["videos"],
category: fs.properties!["category"],
series: 1,
lat: pt.latitude,
lon: pt.longitude,
location_id: fs.properties!["location_id"],
list_order: 1,
photos: fs.properties!["photos"],
checkin_radious: fs.properties!["checkin_radius"],
auto_checkin: fs.properties!["auto_checkin"] == true ? 1 : 0,
cp: fs.properties!["cp"],
checkin_point: fs.properties!["checkin_point"],
buy_point: fs.properties!["buy_point"],
selected: false,
checkedin: false,
hidden_location: fs.properties!["hidden_location"] == true ? 1 : 0);
2022-10-30 21:43:29 +05:30
}
void startTimerLocation(GeoJsonFeature fs, double distance) {
print("---- in startTimer ----");
2022-11-02 22:47:49 +05:30
//skip_gps = true;
2023-08-16 14:53:32 +05:30
double checkinRadious = fs.properties!['checkin_radius'] ?? double.infinity;
2023-09-04 22:46:53 +05:30
if (checkinRadious >= distance) {
2022-11-01 23:47:35 +05:30
indexController.currentFeature.clear();
2022-10-30 21:43:29 +05:30
Destination d = festuretoDestination(fs);
2023-09-08 21:19:12 +05:30
print("----- destination lenght is ${destinations.length} -----");
// for (Destination de in destinations) {
// if (de.location_id == d.location_id) {
// d = de;
// break;
// }
// }
2022-11-01 23:47:35 +05:30
indexController.currentFeature.add(fs);
2022-10-30 21:43:29 +05:30
print("---- before calling startTimer ----");
startTimer(d, distance);
}
}
void startTimer(Destination d, double distance) async {
2023-01-26 20:09:33 +05:30
print("=== passed dest is ${d.location_id} ${d.checkedin} ====");
2022-10-30 21:43:29 +05:30
skip_gps = true;
print("---- in startTimer ----");
2023-09-08 21:19:12 +05:30
DatabaseHelper db = DatabaseHelper.instance;
List<Destination> ds = await db.getDestinationByLatLon(d.lat!, d.lon!);
2023-08-16 14:53:32 +05:30
double checkinRadious = d.checkin_radious ?? double.infinity;
bool autoCheckin = d.auto_checkin == 0 ? false : true;
2023-09-08 21:19:12 +05:30
bool locationAlreadyCheckedIn = ds.length > 0 && ds[0].checkedin == true ? true : false;
2023-08-16 14:53:32 +05:30
bool isuserLoggedIn = indexController.currentUser.isNotEmpty ? true : false;
2022-10-30 21:43:29 +05:30
//make current destination
2023-08-16 14:53:32 +05:30
print("---- checkin_radious $checkinRadious ----");
print("---- distance $distance ----");
2023-09-04 22:46:53 +05:30
if (checkinRadious >= distance) {
2022-10-30 21:43:29 +05:30
//currentSelectedDestinations.add(d);
indexController.currentDestinationFeature.clear();
indexController.currentDestinationFeature.add(d);
2023-09-04 22:46:53 +05:30
print(
"---- checked in as ${indexController.currentDestinationFeature[0].checkedin.toString()} ----");
} else {
2022-10-30 21:43:29 +05:30
skip_gps = false;
return;
}
2023-09-04 22:46:53 +05:30
if (is_photo_shoot.value == true) {
2023-01-25 10:27:49 +05:30
photos.clear();
2023-09-04 22:46:53 +05:30
showModalBottomSheet(
2023-09-06 21:36:11 +05:30
constraints: BoxConstraints.loose(Size(
Get.width,
Get.height * 0.75)),
2023-09-04 22:46:53 +05:30
context: Get.context!,
isScrollControlled: true,
builder: ((context) => CameraPage())).whenComplete(() {
skip_gps = false;
chekcs = 0;
is_in_checkin.value = false;
});
2022-11-05 22:02:21 +05:30
return;
}
2023-09-04 22:46:53 +05:30
if (ds.isEmpty) {
2022-10-30 21:43:29 +05:30
print("----- in location popup cp - ${d.cp}----");
2023-09-04 22:46:53 +05:30
if (d.cp == -1 && DateTime.now().difference(last_goal_at).inHours >= 24) {
2022-11-01 23:47:35 +05:30
chekcs = 1;
2022-10-30 21:43:29 +05:30
//start
2023-09-04 22:46:53 +05:30
print("---- in start -----");
chekcs = 1;
is_in_checkin.value = true;
is_at_start.value = true;
showModalBottomSheet(
2023-09-06 21:36:11 +05:30
constraints: BoxConstraints.loose(Size(
Get.width,
Get.height * 0.75)),
2023-09-04 22:46:53 +05:30
context: Get.context!,
isScrollControlled: true,
builder: ((context) => BottomSheetNew())).whenComplete(() {
skip_gps = false;
chekcs = 0;
is_at_start.value = false;
is_in_checkin.value = false;
});
} else if (is_in_rog.value == true && indexController.rog_mode == 1) {
2022-10-30 21:43:29 +05:30
print("----- in location popup checkin cp - ${d.cp}----");
2022-11-02 22:47:49 +05:30
chekcs = 2;
2022-11-04 14:11:18 +05:30
is_in_checkin.value = true;
2023-09-04 22:46:53 +05:30
showModalBottomSheet(
2023-09-06 21:36:11 +05:30
constraints: BoxConstraints.loose(Size(
Get.width,
Get.height * 0.75)),
2023-09-04 22:46:53 +05:30
context: Get.context!,
isScrollControlled: true,
builder: ((context) => BottomSheetNew())).whenComplete(() {
2022-10-30 21:43:29 +05:30
skip_gps = false;
2023-09-04 22:46:53 +05:30
chekcs = 0;
2022-10-30 21:43:29 +05:30
is_in_checkin.value = false;
});
}
}
2023-09-04 22:46:53 +05:30
print("---- location checkin radious ${d.checkin_radious} ----");
print("---- already checked in $locationAlreadyCheckedIn ----");
if (checkinRadious >= distance &&
locationAlreadyCheckedIn == false &&
is_in_rog.value == true) {
if (autoCheckin) {
if (!checking_in) {
print(
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ make checkin ${d.sub_loc_id}@@@@@@@@@@@");
makeCheckin(d, true, "");
if (d.cp != -1) {
rogaining_counted.value = true;
2022-10-30 21:43:29 +05:30
}
skip_gps = false;
}
2023-09-04 22:46:53 +05:30
} else {
print("--- hidden loc ${d.hidden_location} ----");
// ask for checkin
if (d.hidden_location != null &&
d.hidden_location == 0 &&
is_in_rog.value == true &&
d.cp != -1) {
chekcs = 3;
is_in_checkin.value = true;
photos.clear();
showModalBottomSheet(
2023-09-06 21:36:11 +05:30
constraints: BoxConstraints.loose(Size(
Get.width,
Get.height * 0.75)),
2023-09-04 22:46:53 +05:30
context: Get.context!,
isScrollControlled: true,
builder: ((context) => CameraPage(
destination: d,
))).whenComplete(() {
skip_gps = false;
rogaining_counted.value = true;
chekcs = 0;
is_in_checkin.value = false;
});
} else if (is_in_rog.value == true && d.cp != -1) {
chekcs = 4;
is_in_checkin.value = true;
showMaterialModalBottomSheet(
expand: true,
context: Get.context!,
backgroundColor: Colors.transparent,
builder: (context) => BottomSheetNew()).whenComplete(() {
skip_gps = false;
chekcs = 0;
is_in_checkin.value = false;
});
// showModalBottomSheet(context: Get.context!, isScrollControlled: true,
// builder:((context) => BottomSheetNew())
// ).whenComplete((){
// skip_gps = false;
// chekcs = 0;
// is_in_checkin.value = false;
// });
}
}
}
print("---- cp --- ${d.cp} -----");
print("--- at goal $is_at_goal ---");
print("--- rog counted $rogaining_counted ---");
print("--- loc already checked in $locationAlreadyCheckedIn ---");
print(
"==== date diff is ${DateTime.now().difference(last_goal_at).inHours} ====");
if (isuserLoggedIn &&
d.cp == -1 &&
locationAlreadyCheckedIn &&
skip_10s == false) {
//check for rogaining
if (is_at_goal.value == false && rogaining_counted.value) {
//goal
print("---- in goal -----");
chekcs = 5;
is_at_goal.value = true;
photos.clear();
showModalBottomSheet(
2023-09-06 21:36:11 +05:30
constraints: BoxConstraints.loose(Size(
Get.width,
Get.height * 0.75)),
2023-09-04 22:46:53 +05:30
context: Get.context!,
isScrollControlled: true,
builder: ((context) => CameraPage(
destination: d,
))).whenComplete(() {
skip_gps = false;
chekcs = 0;
is_at_goal.value = false;
});
} else if (is_in_rog.value == false &&
indexController.rog_mode == 1 &&
DateTime.now().difference(last_goal_at).inHours >= 24) {
//start
print("---- in start -----");
chekcs = 6;
is_at_start.value = true;
showModalBottomSheet(
2023-09-06 21:36:11 +05:30
constraints: BoxConstraints.loose(Size(
Get.width,
Get.height * 0.75)),
2023-09-04 22:46:53 +05:30
context: Get.context!,
isScrollControlled: true,
builder: ((context) => BottomSheetNew())).whenComplete(() {
print("----- finished start -------");
skip_gps = false;
chekcs = 0;
is_at_start.value = false;
});
}
}
print("==== _chekcs $chekcs ====");
if (chekcs == 0) {
skip_gps = false;
}
2022-10-30 21:43:29 +05:30
}
2023-01-31 10:14:23 +05:30
void resetRogaining() async {
2023-01-26 20:09:33 +05:30
print("----- resetting --------");
2022-11-08 15:29:42 +05:30
2022-10-30 21:43:29 +05:30
is_in_checkin.value = false;
is_in_rog.value = false;
is_at_start.value = false;
is_at_goal.value = false;
2023-01-26 20:09:33 +05:30
is_gps_selected.value = true;
skip_gps = false;
2022-10-30 21:43:29 +05:30
_start = 0;
2022-11-01 23:47:35 +05:30
chekcs = 0;
2022-10-30 21:43:29 +05:30
rogaining_counted.value = false;
2023-01-26 20:09:33 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2023-01-31 10:14:23 +05:30
int? _latgoal = await db.latestGoal();
2023-09-04 22:46:53 +05:30
if (_latgoal != null) {
2023-01-31 10:14:23 +05:30
last_goal_at = DateTime.fromMicrosecondsSinceEpoch(_latgoal);
2023-08-16 14:53:32 +05:30
print("===== last goal : $last_goal_at =====");
2023-01-31 10:14:23 +05:30
}
2023-09-04 22:46:53 +05:30
db.deleteAllDestinations().then((value) {
2023-01-26 20:09:33 +05:30
PopulateDestinations();
2023-09-06 00:22:02 +05:30
startGPSTimer();
//initGPS();
2023-01-26 20:09:33 +05:30
});
2023-09-04 22:46:53 +05:30
2023-01-26 20:09:33 +05:30
// currentSelectedDestinations.forEach((element) {
// deleteDestination(element);
// });
2022-10-30 21:43:29 +05:30
}
2022-10-12 21:46:17 +05:30
void openCamera(BuildContext context) {
photos.clear();
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => CameraCamera(
2023-09-04 22:46:53 +05:30
onFile: (file) {
photos.add(file);
Navigator.pop(context);
print("----image file is : $file----");
2022-10-12 21:46:17 +05:30
//setState(() {});
},
2023-09-04 22:46:53 +05:30
)));
2022-10-12 21:46:17 +05:30
}
2022-09-22 20:36:56 +05:30
void getRoutePoints() {
indexController.routePoints = [];
indexController.routePointLenght.value = 0;
2023-09-04 22:46:53 +05:30
DestinationService.getDestinationLine(destinations, matrix)?.then((value) {
2022-09-22 20:36:56 +05:30
indexController.routePoints = value;
2023-09-04 22:46:53 +05:30
indexController.routePointLenght.value =
indexController.routePoints.length;
2022-09-22 20:36:56 +05:30
});
}
2022-06-14 14:37:59 +05:30
2023-09-04 22:46:53 +05:30
Future<Destination?> getDestinationForLatLong(double lat, double long) async {
for (final d in destinations) {
if (lat == d.lat && long == d.lon) {
return d;
}
}
return null;
2022-06-14 14:37:59 +05:30
}
2023-09-04 22:46:53 +05:30
void CallforCheckin(Destination d) {
bool autoCheckin = d.auto_checkin == 0 ? false : true;
if (autoCheckin) {
if (!checking_in) {
print(
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ make checkin ${d.sub_loc_id}@@@@@@@@@@@");
makeCheckin(d, true, "");
if (d.cp != -1) {
rogaining_counted.value = true;
}
skip_gps = false;
}
} else {
print("--- hidden loc ${d.hidden_location} ----");
// ask for checkin
if (d.hidden_location != null &&
d.hidden_location == 0 &&
is_in_rog.value == true &&
d.cp != -1) {
chekcs = 3;
is_in_checkin.value = true;
photos.clear();
showModalBottomSheet(
2023-09-06 21:36:11 +05:30
constraints: BoxConstraints.loose(Size(
Get.width,
Get.height * 0.75)),
2023-09-04 22:46:53 +05:30
context: Get.context!,
isScrollControlled: true,
builder: ((context) => CameraPage(
destination: d,
))).whenComplete(() {
skip_gps = false;
rogaining_counted.value = true;
chekcs = 0;
is_in_checkin.value = false;
});
} else {
Get.snackbar("始まっていない", "ロゲイニングを始める必要があります");
}
}
}
2022-10-12 21:46:17 +05:30
2023-09-04 22:46:53 +05:30
void checkForCheckin(double la, double ln) {
2023-08-16 14:53:32 +05:30
print("--- skip_gps ---- $skip_gps----");
2022-07-20 15:57:40 +05:30
2023-09-08 21:19:12 +05:30
// for (final d in destinations) {
// print("--- check checkin for--loc_id- ${d.sub_loc_id}----");
// double lat = d.lat!;
// double lon = d.lon!;
// LatLng p = LatLng(lat, lon);
// getDestinationForLatLong(lat, lon).then((value) {
// var distance = const Distance();
// double dist =
// distance.as(LengthUnit.Meter, LatLng(lat, lon), LatLng(la, ln));
// //double checkin_radious = value!.checkin_radious ?? double.infinity;
// //bool auto_checkin = value.auto_checkin == 0 ? false : true;
// //bool location_already_checked_id = d.checkedin ?? false;
// // print("-----rogaining_counted---${rogaining_counted.value}-----");
// // print("-----is_in_rog---${is_in_rog}-----");
// // print("-----dist is ---${dist}-----");
// //print("----- ${indexController.currentUser} ----");
// if (dist <= 250 && skip_gps == false) {
// //near a destination
// print("---- time with ${d.location_id} ----");
// startTimer(d, dist);
// }
// });
// }
2022-07-20 15:57:40 +05:30
2023-09-04 22:46:53 +05:30
if (indexController.locations.isEmpty) return;
2023-03-17 11:54:12 +05:30
2022-10-30 21:43:29 +05:30
//check for location in bounds
2023-09-04 22:46:53 +05:30
for (GeoJsonFeature fs in indexController.locations[0].collection) {
GeoJsonMultiPoint mp = fs.geometry as GeoJsonMultiPoint;
LatLng pt = LatLng(mp.geoSerie!.geoPoints[0].latitude,
mp.geoSerie!.geoPoints[0].longitude);
double latFs = pt.latitude;
double lonFs = pt.longitude;
LatLng pFs = LatLng(latFs, lonFs);
var distanceFs = const Distance();
double distFs =
distanceFs.as(LengthUnit.Meter, LatLng(latFs, lonFs), LatLng(la, ln));
if (distFs <= 250 && skip_gps == false) {
//near a location
print("---- before call startTimerLocation ----");
startTimerLocation(fs, distFs);
}
2022-06-14 14:37:59 +05:30
}
}
2023-08-16 14:53:32 +05:30
void addToRogaining(double lat, double lon, int destinationId) async {
2022-10-30 21:43:29 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2023-08-16 14:53:32 +05:30
List<Destination> d = await db.getDestinationById(destinationId);
2023-09-04 22:46:53 +05:30
if (d.isEmpty) {
2022-11-01 23:47:35 +05:30
Destination df = festuretoDestination(indexController.currentFeature[0]);
print("--- made checkin ${df.location_id} ----");
2022-11-05 22:02:21 +05:30
makeCheckin(df, true, "");
2022-10-30 21:43:29 +05:30
}
2022-11-01 23:47:35 +05:30
is_in_rog.value = true;
2022-10-12 21:46:17 +05:30
}
2023-09-04 22:46:53 +05:30
void makeCheckin(
Destination destination, bool action, String imageurl) async {
print(
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ressssss ${destination.sub_loc_id}@@@@@@@@@@@");
DatabaseHelper db = DatabaseHelper.instance;
List<Destination> ddd =
await db.getDestinationByLatLon(destination.lat!, destination.lon!);
2022-10-12 21:46:17 +05:30
2023-09-04 22:46:53 +05:30
if (ddd.isEmpty) {
destination.checkedin = true;
await db.insertDestination(destination);
}
2022-11-05 22:02:21 +05:30
2023-09-06 21:04:44 +05:30
await GallerySaver.saveImage(imageurl);
2023-09-04 22:46:53 +05:30
PopulateDestinations();
2022-11-05 22:02:21 +05:30
2023-09-04 22:46:53 +05:30
/// post to NATNAT
if (indexController.currentUser.isNotEmpty) {
double cpNum = destination.cp!;
int userId = indexController.currentUser[0]["user"]["id"];
//print("--- Pressed -----");
String _team = indexController.currentUser[0]["user"]['team_name'];
//print("--- _team : ${_team}-----");
String _event_code = indexController.currentUser[0]["user"]["event_code"];
//print("--- _event_code : ${_event_code}-----");
String _token = indexController.currentUser[0]["token"];
//print("--- _token : ${_token}-----");
DateTime now = DateTime.now();
String formattedDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(now);
print("------ checkin event $_event_code ------");
ExternalService()
.makeCheckpoint(userId, _token, formattedDate, _team, cpNum.round(),
_event_code, imageurl)
.then((value) {
print("------Ext service check point $value ------");
});
}
2022-07-14 23:10:24 +05:30
}
2023-09-04 22:46:53 +05:30
void initGPS() {
2022-07-20 15:57:40 +05:30
checkPermission();
2022-05-12 02:17:08 +05:30
PopulateDestinations();
2023-09-04 22:46:53 +05:30
2022-07-23 19:28:35 +05:30
//print("------ in iniit");
2022-06-27 12:15:54 +05:30
2022-07-09 22:51:34 +05:30
if (defaultTargetPlatform == TargetPlatform.android) {
locationSettings = AndroidSettings(
2023-09-06 00:22:02 +05:30
accuracy: LocationAccuracy.high,
distanceFilter: 1,
2023-09-04 22:46:53 +05:30
forceLocationManager: true,
2023-09-06 00:22:02 +05:30
intervalDuration: const Duration(seconds: 3),
2023-09-04 22:46:53 +05:30
//(Optional) Set foreground notification config to keep the app alive
//when going to the background
foregroundNotificationConfig: const ForegroundNotificationConfig(
2022-07-09 22:51:34 +05:30
notificationText:
2023-09-04 22:46:53 +05:30
"Example app will continue to receive your location even when you aren't using it",
2022-07-09 22:51:34 +05:30
notificationTitle: "Running in Background",
enableWakeLock: true,
2023-09-04 22:46:53 +05:30
));
} else if (defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.macOS) {
2022-07-09 22:51:34 +05:30
locationSettings = AppleSettings(
2023-09-06 00:22:02 +05:30
accuracy: LocationAccuracy.high,
2023-09-04 22:46:53 +05:30
activityType: ActivityType.fitness,
distanceFilter: 0,
pauseLocationUpdatesAutomatically: false,
// Only set to true if our app will be started up in the background.
showBackgroundLocationIndicator: true);
2022-07-09 22:51:34 +05:30
} else {
2023-09-04 22:46:53 +05:30
locationSettings = const LocationSettings(
2022-07-09 22:51:34 +05:30
accuracy: LocationAccuracy.high,
2023-01-25 10:03:01 +05:30
distanceFilter: 0,
2022-07-09 22:51:34 +05:30
);
}
2022-06-29 18:25:19 +05:30
2022-07-20 15:57:40 +05:30
try {
2023-09-06 00:22:02 +05:30
positionStream = Geolocator.getPositionStream(locationSettings: locationSettings)
2023-09-04 22:46:53 +05:30
.listen((Position? position) {
2022-10-12 21:46:17 +05:30
current_lat = position != null ? position.latitude : 0;
current_lon = position != null ? position.longitude : 0;
2023-06-11 21:03:30 +05:30
indexController.current_lat = position != null ? position.latitude : 0;
current_lon = position != null ? position.longitude : 0;
2023-09-04 22:46:53 +05:30
print(
"==== gps skip is : ${skip_gps.toString()}, selected is $is_gps_selected , $current_lat");
2023-01-26 20:09:33 +05:30
2023-09-04 22:46:53 +05:30
if (is_gps_selected.value) {
2023-08-16 14:53:32 +05:30
double czoom = indexController.rogMapController.zoom;
2023-09-04 22:46:53 +05:30
indexController.rogMapController
.move(LatLng(position!.latitude, position.longitude), czoom);
2022-07-20 15:57:40 +05:30
//String user_id = indexController.currentUser[0]["user"]["id"].toString();
//TrackingService.addTrack(user_id, position!.latitude, position.longitude).then((val){
2023-09-04 22:46:53 +05:30
//print("---- postion is ${position.latitude}, ${position.longitude}");
2023-09-06 00:22:02 +05:30
//gps.clear();
//gps.add(
// "-- lat : ${position.latitude}, lon : ${position.longitude} --");
2023-09-04 22:46:53 +05:30
checkForCheckin(position.latitude, position.longitude);
print("--- call check checkin");
print("---- skip gps is ${skip_gps.toString()} ----");
2022-07-20 15:57:40 +05:30
//});
}
2023-09-04 22:46:53 +05:30
//print(position == null ? 'Unknown' : 'current position is ${position.latitude.toString()}, ${position.longitude.toString()}');
2022-07-20 15:57:40 +05:30
});
2023-09-04 22:46:53 +05:30
} catch (err) {
2022-07-20 15:57:40 +05:30
locationPermission.clear();
locationPermission.add(err.toString());
}
2022-06-29 18:25:19 +05:30
2022-11-14 22:22:41 +05:30
ever(indexController.connectionStatusName, connectionChanged);
2023-01-26 20:09:33 +05:30
}
2023-09-06 00:22:02 +05:30
void startGPSTimer() {
// If a timer is already running, we first cancel it
_GPStimer?.cancel();
// Start a new timer that fires every 3 seconds
_timer = Timer.periodic(Duration(seconds: 3), (timer) async {
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
if (is_gps_selected.value) {
double czoom = indexController.rogMapController.zoom;
indexController.rogMapController
.move(LatLng(position.latitude, position.longitude), czoom);
checkForCheckin(position.latitude, position.longitude);
print("--- call check checkin");
print("---- skip gps is ${skip_gps.toString()} ----");
}
});
}
void stopGPSTimer() {
_GPStimer?.cancel();
}
2023-01-26 20:09:33 +05:30
@override
2023-09-06 00:22:02 +05:30
void onClose() {
positionStream?.cancel();
_GPStimer?.cancel();
super.onClose();
super.onClose();
}
2022-11-14 22:22:41 +05:30
2023-09-06 00:22:02 +05:30
@override
void onInit() async {
//initGPS();
startGPSTimer();
2022-09-22 20:36:56 +05:30
super.onInit();
2023-09-04 22:46:53 +05:30
}
2022-06-04 20:16:29 +05:30
2023-09-04 22:46:53 +05:30
readUserToken() async{
final SharedPreferences prefs = await SharedPreferences.getInstance();
indexController.userToken = prefs.getString("user_token");
2022-05-12 02:17:08 +05:30
}
2022-12-27 18:52:37 +05:30
@override
2023-09-04 22:46:53 +05:30
void onReady() async {
await readUserToken();
final token = indexController.userToken;
if(token != null && token.isNotEmpty){
await indexController.loadUserDetailsForToken(token);
fixMapBound(token);
return;
}
Get.toNamed(AppPages.LOGIN)!.then((value) {
if (indexController.currentUser.isNotEmpty) {
final tk = indexController.currentUser[0]["token"];
fixMapBound(tk);
} else {
2023-07-18 22:10:30 +05:30
Get.toNamed(AppPages.TRAVEL);
2023-09-04 22:46:53 +05:30
PerfectureService.getSubExt("9").then((value) {
if (value != null) {
LatLngBounds bnds = LatLngBounds(
LatLng(value[1], value[0]), LatLng(value[3], value[2]));
indexController.mapController
.fitBounds(bnds); //.centerZoomFitBounds(bnds);
2023-07-18 22:10:30 +05:30
}
});
}
});
super.onReady();
}
2023-09-04 22:46:53 +05:30
void fixMapBound(String token) {
//String _token = indexController.currentUser[0]["token"];
indexController.switchPage(AppPages.INITIAL);
LocationService.getLocationsExt(token).then((value) {
if (value != null) {
print("--- loc ext is - $value ----");
LatLngBounds bnds = LatLngBounds(
LatLng(value[1], value[0]), LatLng(value[3], value[2]));
print("--- bnds is - $bnds ----");
indexController.mapController.fitBounds(
bnds,
);
indexController.currentBound.clear();
indexController.currentBound.add(bnds);
indexController.loadLocationsBound();
}
});
2022-12-27 18:52:37 +05:30
}
2022-11-14 22:22:41 +05:30
void connectionChanged(String val) {
2023-08-16 14:53:32 +05:30
print('----- %%%%%%%%%%%%%%%%%%%%% ----- $val');
2022-11-14 22:27:52 +05:30
Map<String, dynamic> _res = {};
2023-09-04 22:46:53 +05:30
if (val == "wifi" || val == "mobile") {
2022-11-14 22:22:41 +05:30
String _token = indexController.currentUser[0]["token"];
DatabaseHelper db = DatabaseHelper.instance;
2023-09-04 22:46:53 +05:30
db.allRogianing().then((value) {
2022-11-14 22:27:52 +05:30
value.forEach((e) async {
2023-09-04 22:46:53 +05:30
if (e.rog_action_type == 0) {
2022-11-14 22:27:52 +05:30
_res = await ExternalService().StartRogaining();
2023-09-04 22:46:53 +05:30
} else if (e.rog_action_type == 1) {
2023-08-16 14:53:32 +05:30
var datetime = DateTime.fromMicrosecondsSinceEpoch(e.checkintime!);
2023-09-04 22:46:53 +05:30
_res = await ExternalService().makeCheckpoint(
e.user_id!,
_token,
getFormatedTime(datetime),
e.team_name!,
e.cp_number!,
e.event_code!,
e.image!);
} else if (e.rog_action_type == 2) {
2023-08-16 14:53:32 +05:30
var datetime = DateTime.fromMicrosecondsSinceEpoch(e.checkintime!);
2023-09-04 22:46:53 +05:30
_res = await ExternalService().makeGoal(
e.user_id!,
_token,
e.team_name!,
e.image!,
getFormatedTime(datetime),
e.event_code!);
2022-11-14 22:22:41 +05:30
}
2022-11-14 22:27:52 +05:30
2023-09-04 22:46:53 +05:30
if (_res.isNotEmpty) {
2022-11-14 22:27:52 +05:30
db.deleteRogaining(e.id!);
}
2022-11-14 22:22:41 +05:30
});
});
}
}
2022-07-14 23:10:24 +05:30
void checkPermission() async {
2022-12-25 19:26:07 +05:30
LocationPermission permission = await Geolocator.checkPermission();
2022-07-14 23:10:24 +05:30
if (permission != LocationPermission.whileInUse ||
permission != LocationPermission.always) {
2023-09-04 22:46:53 +05:30
locationPermission.clear();
locationPermission.add(permission.name);
2022-07-14 23:10:24 +05:30
permission = await Geolocator.requestPermission();
}
}
2023-09-04 22:46:53 +05:30
Destination? destinationById(int id) {
2023-08-16 14:53:32 +05:30
Destination? d;
print("--- target des - $id ----");
2023-09-04 22:46:53 +05:30
for (Destination ss in destinations) {
2022-10-12 21:46:17 +05:30
print("--- des - ${ss.location_id} ----");
2023-09-04 22:46:53 +05:30
if (ss.location_id == id) {
2022-10-12 21:46:17 +05:30
d = ss;
break;
}
}
return d;
}
2023-09-04 22:46:53 +05:30
void deleteDestination(Destination d) {
2022-07-10 23:50:43 +05:30
//int id = destinations[index].location_id!;
2022-07-09 22:51:34 +05:30
//print("---- index ${destinations[index].location_id!}-----");
2023-09-04 22:46:53 +05:30
for (Destination ss in destinations) {
if (ss.location_id == d.location_id) {
2022-10-30 21:43:29 +05:30
destinations.remove(ss);
2022-07-10 23:50:43 +05:30
break;
}
}
2022-07-09 22:51:34 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2023-09-04 22:46:53 +05:30
db.deleteDestination(d.location_id!).then((value) {
2022-05-25 21:00:10 +05:30
PopulateDestinations();
});
2022-07-09 22:51:34 +05:30
}
2023-09-04 22:46:53 +05:30
void deleteAllDestinations() {
2022-09-23 18:40:17 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2023-09-04 22:46:53 +05:30
db.deleteAllDestinations().then((value) {
2022-09-23 18:40:17 +05:30
PopulateDestinations();
});
}
2023-09-04 22:46:53 +05:30
// ---------- database ------------------///
2022-07-09 22:51:34 +05:30
2023-09-04 22:46:53 +05:30
void addDestinations(Destination dest) {
print(
'------ destination controller in add destination ${dest.checkin_radious} ---- :::::');
2022-07-10 23:50:43 +05:30
2022-07-09 22:51:34 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2023-09-04 22:46:53 +05:30
db.getDestinationByLatLon(dest.lat!, dest.lon!).then((value) {
if (value.isNotEmpty) {
db.deleteDestination(value[0].location_id!).then((value) {
db.insertDestination(dest).then((value) {
print(
"----- destination controller deleted and inserted destination id $value ---- :::::");
2022-07-10 23:50:43 +05:30
PopulateDestinations();
2022-07-09 22:51:34 +05:30
});
});
2023-09-04 22:46:53 +05:30
} else {
db.insertDestination(dest).then((value) {
print("----- destination controller added as new $value--- :::::");
PopulateDestinations();
});
2022-07-09 22:51:34 +05:30
}
});
2022-05-25 21:00:10 +05:30
}
void toggleSelection(Destination dest) async {
DatabaseHelper db = DatabaseHelper.instance;
await db.toggleSelecttion(dest);
destinations.clear();
2023-09-04 22:46:53 +05:30
db.getDestinations().then((value) {
destinationCount.value = 0;
currentSelectedDestinations.clear();
2023-09-04 22:46:53 +05:30
for (Destination d in value) {
//print("------ destination controller populating destination-------- ${d.checkedin}-------- :::::");
//print("-----populated----- ${d.toMap()}");
2023-09-04 22:46:53 +05:30
if (d.selected!) {
currentSelectedDestinations.add(d);
}
destinations.add(d);
}
destinationCount.value = destinations.length;
});
}
2023-02-16 19:36:39 +05:30
buildShowDialog(BuildContext context) {
return showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
2023-08-16 14:53:32 +05:30
return const Center(
2023-02-16 19:36:39 +05:30
child: CircularProgressIndicator(),
);
});
}
2023-09-04 22:46:53 +05:30
void destinationMatrixFromCurrentPoint(List<Destination> points) {
2023-02-16 19:36:39 +05:30
buildShowDialog(Get.context!);
2023-09-04 22:46:53 +05:30
MatrixService.getDestinations(points).then((mat) {
2023-08-16 14:53:32 +05:30
print(" matrix is ------- $mat");
2023-02-16 19:36:39 +05:30
matrix = mat;
2023-09-04 22:46:53 +05:30
try {
2023-02-16 19:36:39 +05:30
indexController.routePoints = [];
indexController.routePointLenght.value = 0;
2023-09-04 22:46:53 +05:30
DestinationService.getDestinationLine(points, matrix)?.then((value) {
2023-02-16 19:36:39 +05:30
indexController.routePoints = value;
2023-09-04 22:46:53 +05:30
indexController.routePointLenght.value =
indexController.routePoints.length;
2023-06-11 21:03:30 +05:30
//Get.toNamed(AppPages.TRAVEL);
2023-02-16 19:36:39 +05:30
});
destinationCount.value = destinations.length;
2023-09-04 22:46:53 +05:30
} catch (_) {
2023-02-16 19:36:39 +05:30
skip_gps = false;
return;
2023-09-04 22:46:53 +05:30
} finally {
2023-02-16 19:36:39 +05:30
Get.back();
}
});
}
2023-09-04 22:46:53 +05:30
void PopulateDestinations() {
print(
"--------- destination controller populsting destinations ----------- ::::::");
2022-07-09 22:51:34 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2022-09-29 15:32:33 +05:30
destinations.clear();
2023-09-04 22:46:53 +05:30
db.getDestinations().then((value) {
2022-09-22 20:36:56 +05:30
destinationCount.value = 0;
2023-09-04 22:46:53 +05:30
for (Destination d in value) {
print(
"------ destination controller populating destination-------- ${d.checkedin}-------- :::::");
2022-11-01 23:47:35 +05:30
print("-----populated----- ${d.toMap()}");
2022-07-10 23:50:43 +05:30
destinations.add(d);
}
2023-09-04 22:46:53 +05:30
// destinationCount.value = 0;
print(
"------ destination controller destinationcount-------- $destinationCount-------- :::::");
// MatrixService.getDestinations(value).then((mat){
// print(" matrix is ------- ${mat}");
// matrix = mat;
// try{
// getRoutePoints();
// destinationCount.value = destinations.length;
// }
// catch(_){
// skip_gps = false;
// return;
// }
// });
2022-07-09 22:51:34 +05:30
});
2022-05-12 02:17:08 +05:30
}
2023-09-04 22:46:53 +05:30
void makeOrder(Destination d, int dir) {
2022-09-23 18:40:17 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2023-09-04 22:46:53 +05:30
db.updateOrder(d, dir).then((value) {
2022-09-23 18:40:17 +05:30
PopulateDestinations();
});
2022-07-09 22:51:34 +05:30
}
2023-09-04 22:46:53 +05:30
void makeNext(Destination pt) {
for (int i = 0; i <= destinations.length - 1; i++) {
2022-07-09 22:51:34 +05:30
Destination p = destinations[i];
2023-09-04 22:46:53 +05:30
if (p.lat == pt.lat && p.lon == pt.lon) {
if (indexController.currentDestinationFeature.isNotEmpty) {
indexController.currentDestinationFeature.clear();
}
if (i >= destinations.length - 1) {
indexController.currentDestinationFeature.add(destinations[0]);
//getAction();
} else {
indexController.currentDestinationFeature.add(destinations[i + 1]);
//getAction();
}
2022-07-09 22:51:34 +05:30
}
}
}
2023-09-04 22:46:53 +05:30
void makePrevious(Destination pt) {
for (int i = 0; i <= destinations.length - 1; i++) {
2022-07-27 19:43:12 +05:30
Destination p = destinations[i];
2022-07-09 22:51:34 +05:30
2023-09-04 22:46:53 +05:30
if (p.lat == pt.lat && p.lon == pt.lon) {
if (indexController.currentDestinationFeature.isNotEmpty) {
indexController.currentDestinationFeature.clear();
}
if (i <= 0) {
indexController.currentDestinationFeature
.add(destinations[destinations.length - 1]);
//getAction();
} else {
indexController.currentDestinationFeature.add(destinations[i - 1]);
//getAction();
}
2022-07-27 19:43:12 +05:30
}
}
}
2023-09-04 22:46:53 +05:30
}