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

359 lines
12 KiB
Dart
Raw Normal View History

2022-05-12 02:17:08 +05:30
2022-06-14 14:37:59 +05:30
import 'dart:convert';
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-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-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-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-05-18 19:09:26 +05:30
import 'package:rogapp/services/maxtrix_service.dart';
2022-06-14 14:37:59 +05:30
import 'package:rogapp/services/reacking_service.dart';
2022-07-09 22:51:34 +05:30
import 'package:rogapp/utils/database_helper.dart';
2022-06-04 20:16:29 +05:30
import 'dart:async';
2022-05-12 02:17:08 +05:30
2022-06-14 14:37:59 +05:30
import 'package:rogapp/widgets/bottom_sheet_widget.dart';
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-05-24 20:43:41 +05:30
List<Map<String, dynamic>> destination_index_data = <Map<String, dynamic>>[].obs;
2022-05-25 21:00:10 +05:30
2022-07-10 23:50:43 +05:30
List<Destination> currentSelectedDestinations = <Destination>[].obs;
2022-06-14 14:37:59 +05:30
bool checking_in = false;
2022-09-22 20:36:56 +05:30
var isSelected = false.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;
2022-09-22 20:36:56 +05:30
var travelMode = 0.obs;
2022-07-20 15:57:40 +05:30
2022-05-18 19:09:26 +05:30
Map<String, dynamic> matrix = {};
2022-05-12 02:17:08 +05:30
final IndexController indexController = Get.find<IndexController>();
2022-09-22 20:36:56 +05:30
void getRoutePoints() {
indexController.routePoints = [];
indexController.routePointLenght.value = 0;
DestinationService.getDestinationLine(destinations)?.then((value){
indexController.routePoints = value;
indexController.routePointLenght.value = indexController.routePoints.length;
});
}
2022-06-14 14:37:59 +05:30
2022-07-09 22:51:34 +05:30
Future<Destination?> getDEstinationForLatLong(double lat, double long)async {
2022-06-14 14:37:59 +05:30
String jjjj = '{"id":1,"type":"Feature","geometry":{"type":"MultiPoint","coordinates":[[136.731357,35.370094]]},"properties":{"location_id":915101,"location_name":"柳津","category":"買い物","zip":"〒501-6100","address":"柳津町字仙右城7696-1"}}';
for(final d in destinations){
2022-07-09 22:51:34 +05:30
if(lat == d.lat && long == d.lon){
return d;
2022-06-14 14:37:59 +05:30
}
}
}
checkForCheckin(double la, double ln){
2022-07-14 23:10:24 +05:30
2022-06-14 14:37:59 +05:30
for(final d in destinations){
2022-07-20 15:57:40 +05:30
if(!checking_in)
{
checking_in = true;
double lat = d.lat!;
double lon = d.lon!;
LatLng p = LatLng(lat, lon);
getDEstinationForLatLong(lat, lon).then((value){
var distance = Distance();
double dist = distance.as(LengthUnit.Meter, LatLng(lat, lon), LatLng(la, ln));
double rad = value!.checkin_radious ?? double.infinity;
bool auto_checkin = value.auto_checkin == 0 ? false : true;
indexController.currentDestinationFeature.add(value);
//indexController.getAction();
if(rad >= dist){
if(auto_checkin){
makeCheckin(value, true);
}
else{
2022-07-21 20:42:33 +05:30
// showModalBottomSheet(context: Get.context!, isScrollControlled: true,
// builder:((context) => BottomSheetWidget())
// ).whenComplete((){
// checking_in = false;
// });
2022-07-20 15:57:40 +05:30
}
2022-06-14 14:37:59 +05:30
}
2022-07-20 15:57:40 +05:30
// if(!checking_in){
// checking_in = true;
// if(rad >= dist){
// if(auto_checkin){
// if(indexController.currentAction.isNotEmpty){
// print(indexController.currentAction[0]);
// indexController.currentAction[0][0]["checkin"] = true;
// Map<String,dynamic> temp = Map<String,dynamic>.from(indexController.currentAction[0][0]);
// indexController.currentAction.clear();
// print("---temp---${temp}");
// indexController.currentAction.add([temp]);
// }
// indexController.makeAction(Get.context!);
// }
// else{
// showModalBottomSheet(context: Get.context!, isScrollControlled: true,
// builder:((context) => BottomSheetWidget())
// ).whenComplete((){
// checking_in = false;
// });
// }
// }
// }
});
}
2022-06-14 14:37:59 +05:30
}
}
2022-07-14 23:10:24 +05:30
void makeCheckin(Destination destination, bool action) async {
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ressssss ${action}@@@@@@@@@@@");
DatabaseHelper db = DatabaseHelper.instance;
int res = await db.updateAction(destination, action);
List<Destination> ddd = await db.getDestinationByLatLon(destination.lat!, destination.lon!);
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ddddd ${ddd[0].checkedin} @@@@@@@@@@@");
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ressssss ${res}@@@@@@@@@@@");
PopulateDestinations();
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ after populating ${res} @@@@@@@@@@@");
print("---- database update resulr ------ res : ${res}-------");
}
2022-06-14 14:37:59 +05:30
2022-05-12 02:17:08 +05:30
@override
2022-07-14 23:10:24 +05:30
void onInit() async {
2022-07-20 15:57:40 +05:30
checkPermission();
2022-05-12 02:17:08 +05:30
PopulateDestinations();
2022-07-14 23:10:24 +05:30
2022-07-23 19:28:35 +05:30
//print("------ in iniit");
2022-06-27 12:15:54 +05:30
2022-06-04 20:16:29 +05:30
2022-07-09 22:51:34 +05:30
if (defaultTargetPlatform == TargetPlatform.android) {
locationSettings = AndroidSettings(
2022-07-20 15:57:40 +05:30
accuracy: LocationAccuracy.bestForNavigation,
2022-07-16 00:08:00 +05:30
distanceFilter: 00,
2022-07-09 22:51:34 +05:30
forceLocationManager: true,
2022-07-16 00:08:00 +05:30
intervalDuration: const Duration(seconds: 1),
2022-07-09 22:51:34 +05:30
//(Optional) Set foreground notification config to keep the app alive
//when going to the background
foregroundNotificationConfig: const ForegroundNotificationConfig(
notificationText:
"Example app will continue to receive your location even when you aren't using it",
notificationTitle: "Running in Background",
enableWakeLock: true,
)
);
} else if (defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS) {
locationSettings = AppleSettings(
2022-07-20 15:57:40 +05:30
accuracy: LocationAccuracy.bestForNavigation,
2022-07-09 22:51:34 +05:30
activityType: ActivityType.fitness,
distanceFilter: 1,
pauseLocationUpdatesAutomatically: false,
// Only set to true if our app will be started up in the background.
showBackgroundLocationIndicator: true
);
} else {
locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 30,
);
}
2022-06-29 18:25:19 +05:30
2022-07-20 15:57:40 +05:30
try {
StreamSubscription<Position> positionStream = Geolocator.getPositionStream(locationSettings: locationSettings).listen(
(Position? position) {
2022-09-22 20:36:56 +05:30
if(isSelected.value){
2022-07-20 15:57:40 +05:30
double czoom = indexController.rogMapController!.zoom;
indexController.rogMapController!.move(LatLng(position!.latitude, position!.longitude), czoom);
//String user_id = indexController.currentUser[0]["user"]["id"].toString();
//TrackingService.addTrack(user_id, position!.latitude, position.longitude).then((val){
2022-07-23 19:28:35 +05:30
//print("---- postion is ${position.latitude}, ${position.longitude}");
2022-07-20 15:57:40 +05:30
gps.clear();
gps.add("-- lat : ${position.latitude}, lon : ${position.longitude} --");
checkForCheckin(position!.latitude, position.longitude);
//});
}
2022-07-23 19:28:35 +05:30
//print(position == null ? 'Unknown' : 'current position is ${position.latitude.toString()}, ${position.longitude.toString()}');
2022-07-20 15:57:40 +05:30
});
} catch (err){
locationPermission.clear();
locationPermission.add(err.toString());
}
2022-06-29 18:25:19 +05:30
2022-09-22 20:36:56 +05:30
super.onInit();
2022-06-04 20:16:29 +05:30
2022-05-12 02:17:08 +05:30
}
2022-07-14 23:10:24 +05:30
void checkPermission() async {
LocationPermission permission = await Geolocator.checkPermission();
if (permission != LocationPermission.whileInUse ||
permission != LocationPermission.always) {
2022-07-20 15:57:40 +05:30
locationPermission.clear();
locationPermission.add(permission.name);
2022-07-14 23:10:24 +05:30
permission = await Geolocator.requestPermission();
}
}
2022-07-10 23:50:43 +05:30
void deleteDestination(Destination d){
//int id = destinations[index].location_id!;
2022-07-09 22:51:34 +05:30
//print("---- index ${destinations[index].location_id!}-----");
2022-07-10 23:50:43 +05:30
for(Destination ss in currentSelectedDestinations){
if(ss.location_id == d.location_id){
currentSelectedDestinations.remove(ss);
break;
}
}
2022-07-09 22:51:34 +05:30
DatabaseHelper db = DatabaseHelper.instance;
2022-07-10 23:50:43 +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
}
2022-09-23 18:40:17 +05:30
void deleteAllDestinations(){
DatabaseHelper db = DatabaseHelper.instance;
db.deleteAllDestinations().then((value){
PopulateDestinations();
});
}
2022-07-09 22:51:34 +05:30
// ---------- database ------------------///
void addDestinations(Destination dest){
2022-07-10 23:50:43 +05:30
print('------ destination controller in add destination ${dest.name} ---- :::::');
2022-07-09 22:51:34 +05:30
DatabaseHelper db = DatabaseHelper.instance;
db.getDestinationByLatLon(dest.lat!, dest.lon!).then((value){
if(value.isNotEmpty){
db.deleteDestination(value[0].location_id!).then((value){
db.insertDestination(dest).then((value){
2022-07-10 23:50:43 +05:30
print("----- destination controller deleted and inserted destination id $value ---- :::::");
PopulateDestinations();
2022-07-09 22:51:34 +05:30
});
});
}
else {
db.insertDestination(dest).then((value){
2022-07-10 23:50:43 +05:30
print("----- destination controller added as new ${value}--- :::::");
PopulateDestinations();
2022-07-09 22:51:34 +05:30
});
}
});
2022-05-25 21:00:10 +05:30
}
2022-05-12 02:17:08 +05:30
void PopulateDestinations(){
2022-07-10 23:50:43 +05:30
print("--------- destination controller populsting destinations ----------- ::::::");
2022-07-09 22:51:34 +05:30
DatabaseHelper db = DatabaseHelper.instance;
db.getDestinations().then((value){
2022-09-22 20:36:56 +05:30
destinations.clear();
destinationCount.value = 0;
2022-07-10 23:50:43 +05:30
for(Destination d in value){
for(Destination s in currentSelectedDestinations){
if(d.location_id == s.location_id){
2022-07-14 23:10:24 +05:30
d.selected = !d.selected!;
2022-07-10 23:50:43 +05:30
}
}
destinations.add(d);
}
2022-07-09 22:51:34 +05:30
// destinationCount.value = 0;
2022-07-10 23:50:43 +05:30
print("------ destination controller destinationcount-------- ${destinationCount}-------- :::::");
2022-07-09 22:51:34 +05:30
MatrixService.getDestinations(value).then((mat){
2022-09-22 20:36:56 +05:30
print(" matrix is ------- ${mat}");
2022-07-09 22:51:34 +05:30
matrix = mat;
2022-09-22 20:36:56 +05:30
getRoutePoints();
destinationCount.value = destinations.length;
2022-07-09 22:51:34 +05:30
});
});
2022-05-12 02:17:08 +05:30
}
2022-06-04 20:16:29 +05:30
2022-05-12 02:17:08 +05:30
2022-09-23 18:40:17 +05:30
void makeOrder(Destination d, int dir){
DatabaseHelper db = DatabaseHelper.instance;
db.updateOrder(d, dir).then((value){
PopulateDestinations();
});
2022-07-09 22:51:34 +05:30
}
void makeNext(Destination pt){
for(int i=0; i<= destinations.length - 1; i++){
Destination p = destinations[i];
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-27 19:43:12 +05:30
void makePrevious(Destination pt){
2022-07-09 22:51:34 +05:30
2022-07-27 19:43:12 +05:30
for(int i=0; i<= destinations.length - 1; i++){
Destination p = destinations[i];
2022-07-09 22:51:34 +05:30
2022-07-27 19:43:12 +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-09 22:51:34 +05:30
2022-07-27 19:43:12 +05:30
}
2022-07-09 22:51:34 +05:30
2022-05-12 02:17:08 +05:30
}