471 lines
15 KiB
Dart
471 lines
15 KiB
Dart
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:camera_camera/camera_camera.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:geojson/geojson.dart';
|
|
import 'package:geolocator/geolocator.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:latlong2/latlong.dart';
|
|
import 'package:rogapp/model/Rogaining.dart';
|
|
import 'package:rogapp/model/destination.dart';
|
|
import 'package:rogapp/pages/index/index_controller.dart';
|
|
import 'package:rogapp/routes/app_pages.dart';
|
|
import 'package:rogapp/services/action_service.dart';
|
|
import 'package:rogapp/services/destination_service.dart';
|
|
import 'package:rogapp/services/maxtrix_service.dart';
|
|
import 'package:rogapp/services/reacking_service.dart';
|
|
import 'package:rogapp/utils/database_helper.dart';
|
|
import 'dart:async';
|
|
|
|
import 'package:rogapp/widgets/bottom_sheet_widget.dart';
|
|
import 'package:sqflite/sqlite_api.dart';
|
|
|
|
class DestinationController extends GetxController {
|
|
|
|
late LocationSettings locationSettings;
|
|
|
|
var destinationCount = 0.obs;
|
|
List<Destination> destinations = <Destination>[].obs;
|
|
List<Map<String, dynamic>> destination_index_data = <Map<String, dynamic>>[].obs;
|
|
double current_lat = 0.0;
|
|
double current_lon = 0.0;
|
|
|
|
List<Destination> currentSelectedDestinations = <Destination>[].obs;
|
|
|
|
var is_in_rog = false.obs;
|
|
List<Rogaining> rogainings = <Rogaining>[].obs;
|
|
|
|
bool checking_in = false;
|
|
var isSelected = false.obs;
|
|
BuildContext? context;
|
|
|
|
List<String> gps = <String>["-- stating --"].obs;
|
|
List<String> locationPermission = <String>[" -- starting -- "].obs;
|
|
|
|
var travelMode = 0.obs;
|
|
|
|
|
|
Map<String, dynamic> matrix = {};
|
|
|
|
final photos = <File>[].obs;
|
|
|
|
final IndexController indexController = Get.find<IndexController>();
|
|
|
|
Timer? _timer;
|
|
int _start = 1;
|
|
var rogaining_counted = false.obs;
|
|
|
|
void startTimer(Destination d, double distance) {
|
|
const oneSec = const Duration(seconds: 1);
|
|
_timer = Timer.periodic(
|
|
oneSec,
|
|
(Timer timer) {
|
|
if (_start == 0) {
|
|
if(is_in_rog.value == false){
|
|
is_in_rog.value = true;
|
|
addToRogaining(current_lat, current_lon, d.location_id!);
|
|
Get.snackbar("Rogaining Started ", "Rogaining session started");
|
|
}
|
|
else{
|
|
//finish rogiain
|
|
//is_in_rog.value = true;
|
|
if (rogaining_counted.value && distance <=250){
|
|
Get.toNamed(AppPages.CAMERA_PAGE);
|
|
}
|
|
}
|
|
timer.cancel();
|
|
} else {
|
|
_start--;
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
void openCamera(BuildContext context) {
|
|
photos.clear();
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (_) => CameraCamera(
|
|
onFile: (file) {
|
|
photos.add(file);
|
|
Navigator.pop(context);
|
|
print("----image file is : ${file}----");
|
|
//setState(() {});
|
|
},
|
|
)));
|
|
}
|
|
|
|
|
|
void getRoutePoints() {
|
|
indexController.routePoints = [];
|
|
indexController.routePointLenght.value = 0;
|
|
DestinationService.getDestinationLine(destinations, matrix)?.then((value){
|
|
indexController.routePoints = value;
|
|
indexController.routePointLenght.value = indexController.routePoints.length;
|
|
});
|
|
}
|
|
|
|
Future<Destination?> getDEstinationForLatLong(double lat, double long)async {
|
|
|
|
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){
|
|
if(lat == d.lat && long == d.lon){
|
|
return d;
|
|
}
|
|
}
|
|
}
|
|
|
|
// void addToRogaining(Rogaining rog){
|
|
// DatabaseHelper db = DatabaseHelper.instance;
|
|
// db.insertRogaining(rog);
|
|
// }
|
|
|
|
checkForCheckin(double la, double ln){
|
|
|
|
for(final d in destinations){
|
|
|
|
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;
|
|
|
|
if(d.cp != -1 && is_in_rog == true){
|
|
rogaining_counted.value == true;
|
|
}
|
|
|
|
if(d.cp == -1 && dist <= 250 && rogaining_counted.value == false){
|
|
startTimer(d, dist);
|
|
}
|
|
|
|
indexController.currentDestinationFeature.add(value);
|
|
//indexController.getAction();
|
|
|
|
|
|
if(rad >= dist){
|
|
if(auto_checkin){
|
|
makeCheckin(value, true);
|
|
}
|
|
else{
|
|
// showModalBottomSheet(context: Get.context!, isScrollControlled: true,
|
|
// builder:((context) => BottomSheetWidget())
|
|
// ).whenComplete((){
|
|
// checking_in = false;
|
|
// });
|
|
}
|
|
}
|
|
|
|
|
|
// 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;
|
|
// });
|
|
// }
|
|
// }
|
|
// }
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
void addToRogaining(double lat, double lon, int destination_id){
|
|
Rogaining rog = Rogaining(
|
|
rog_id: null,
|
|
course_id: 1,
|
|
user_id: indexController.currentUser[0]['user']['id'],
|
|
location_id: destination_id,
|
|
lat: lat,
|
|
lon:lon,
|
|
time_stamp: DateTime.now().toUtc().microsecondsSinceEpoch
|
|
);
|
|
DatabaseHelper db = DatabaseHelper.instance;
|
|
db.insertRogaining(rog).then((value){
|
|
print("----- inserted value ${value} ---------");
|
|
});
|
|
}
|
|
|
|
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}-------");
|
|
|
|
/// post to NATNAT
|
|
if(indexController.currentUser.length > 0){
|
|
String cp_num = destination.cp.toString();
|
|
String team_name = indexController.currentUser[0]['user']['group'];
|
|
ActionService.postCheckin(cp_num, team_name).then((value){
|
|
print('------- NATNAT RES ${value} ------------');
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
@override
|
|
void onInit() async {
|
|
checkPermission();
|
|
PopulateDestinations();
|
|
|
|
//print("------ in iniit");
|
|
|
|
|
|
|
|
if (defaultTargetPlatform == TargetPlatform.android) {
|
|
locationSettings = AndroidSettings(
|
|
accuracy: LocationAccuracy.bestForNavigation,
|
|
distanceFilter: 00,
|
|
forceLocationManager: true,
|
|
intervalDuration: const Duration(seconds: 1),
|
|
//(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(
|
|
accuracy: LocationAccuracy.bestForNavigation,
|
|
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,
|
|
);
|
|
}
|
|
|
|
try {
|
|
|
|
StreamSubscription<Position> positionStream = Geolocator.getPositionStream(locationSettings: locationSettings).listen(
|
|
|
|
(Position? position) {
|
|
current_lat = position != null ? position.latitude : 0;
|
|
current_lon = position != null ? position.longitude : 0;
|
|
|
|
if(isSelected.value){
|
|
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){
|
|
//print("---- postion is ${position.latitude}, ${position.longitude}");
|
|
gps.clear();
|
|
gps.add("-- lat : ${position.latitude}, lon : ${position.longitude} --");
|
|
checkForCheckin(position!.latitude, position.longitude);
|
|
//});
|
|
|
|
}
|
|
//print(position == null ? 'Unknown' : 'current position is ${position.latitude.toString()}, ${position.longitude.toString()}');
|
|
});
|
|
} catch (err){
|
|
locationPermission.clear();
|
|
locationPermission.add(err.toString());
|
|
}
|
|
|
|
super.onInit();
|
|
|
|
}
|
|
|
|
void checkPermission() async {
|
|
LocationPermission permission = await Geolocator.checkPermission();
|
|
if (permission != LocationPermission.whileInUse ||
|
|
permission != LocationPermission.always) {
|
|
locationPermission.clear();
|
|
locationPermission.add(permission.name);
|
|
permission = await Geolocator.requestPermission();
|
|
}
|
|
}
|
|
|
|
Destination? destinationById(int id){
|
|
Destination? d = null;
|
|
print("--- target des - ${id} ----");
|
|
for(Destination ss in destinations){
|
|
print("--- des - ${ss.location_id} ----");
|
|
if(ss.location_id == id){
|
|
d = ss;
|
|
break;
|
|
}
|
|
}
|
|
return d;
|
|
}
|
|
|
|
void deleteDestination(Destination d){
|
|
//int id = destinations[index].location_id!;
|
|
//print("---- index ${destinations[index].location_id!}-----");
|
|
for(Destination ss in currentSelectedDestinations){
|
|
if(ss.location_id == d.location_id){
|
|
currentSelectedDestinations.remove(ss);
|
|
break;
|
|
}
|
|
}
|
|
DatabaseHelper db = DatabaseHelper.instance;
|
|
db.deleteDestination(d.location_id!).then((value){
|
|
PopulateDestinations();
|
|
});
|
|
}
|
|
|
|
void deleteAllDestinations(){
|
|
DatabaseHelper db = DatabaseHelper.instance;
|
|
db.deleteAllDestinations().then((value){
|
|
PopulateDestinations();
|
|
});
|
|
}
|
|
|
|
// ---------- database ------------------///
|
|
|
|
void addDestinations(Destination dest){
|
|
|
|
print('------ destination controller in add destination ${dest.name} ---- :::::');
|
|
|
|
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){
|
|
print("----- destination controller deleted and inserted destination id $value ---- :::::");
|
|
PopulateDestinations();
|
|
});
|
|
});
|
|
}
|
|
else {
|
|
db.insertDestination(dest).then((value){
|
|
print("----- destination controller added as new ${value}--- :::::");
|
|
PopulateDestinations();
|
|
});
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
void PopulateDestinations(){
|
|
print("--------- destination controller populsting destinations ----------- ::::::");
|
|
|
|
DatabaseHelper db = DatabaseHelper.instance;
|
|
destinations.clear();
|
|
db.getDestinations().then((value){
|
|
destinationCount.value = 0;
|
|
for(Destination d in value){
|
|
for(Destination s in currentSelectedDestinations){
|
|
if(d.location_id == s.location_id){
|
|
d.selected = !d.selected!;
|
|
}
|
|
}
|
|
|
|
destinations.add(d);
|
|
}
|
|
// destinationCount.value = 0;
|
|
print("------ destination controller destinationcount-------- ${destinationCount}-------- :::::");
|
|
|
|
|
|
MatrixService.getDestinations(value).then((mat){
|
|
print(" matrix is ------- ${mat}");
|
|
matrix = mat;
|
|
|
|
getRoutePoints();
|
|
destinationCount.value = destinations.length;
|
|
|
|
});
|
|
});
|
|
|
|
}
|
|
|
|
|
|
void makeOrder(Destination d, int dir){
|
|
DatabaseHelper db = DatabaseHelper.instance;
|
|
db.updateOrder(d, dir).then((value){
|
|
PopulateDestinations();
|
|
});
|
|
}
|
|
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
void makePrevious(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 <= 0){
|
|
indexController.currentDestinationFeature.add(destinations[destinations.length -1]);
|
|
//getAction();
|
|
}
|
|
else{
|
|
indexController.currentDestinationFeature.add(destinations[i - 1]);
|
|
//getAction();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
} |