Files
rog_app/lib/screens/home/location_controller.dart
2023-06-09 15:55:59 +05:30

318 lines
11 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:modal_bottom_sheet/modal_bottom_sheet.dart';
import 'package:rogapp/model/location_response.dart';
import 'package:rogapp/pages/camera/camera_page.dart';
import 'package:rogapp/screens/auth/controller/auth_controller.dart';
import 'package:rogapp/screens/home/home_controller.dart';
import 'package:rogapp/utils/checkin_db_helper.dart';
import 'package:rogapp/widgets/bottom_sheet_new.dart';
class LocationController extends GetxController{
HomeController homeController = Get.find<HomeController>();
AuthController authController = Get.find<AuthController>();
CheckinDBHelper checkinDBHelper = CheckinDBHelper.instance;
late LocationSettings locationSettings;
List<String> locationPermission = <String>[" -- starting -- "].obs;
double current_lat = 0.0;
double current_lon = 0.0;
var is_gps_selected = true.obs;
var is_checkingIn = false.obs;
var moveMapwithGPS = false.obs;
bool skip_gps = false;
var is_photo_shoot = false.obs;
final photos = <File>[].obs;
Timer? _timer;
int _start = 0;
int chekcs = 0;
var rogaining_counted = false.obs;
var is_in_checkin = false.obs;
var is_in_rog = false.obs;
var is_at_start = false.obs;
var is_at_goal = false.obs;
DateTime last_goal_at = DateTime.now().subtract(Duration(days:1));
bool skip_10s = false;
bool checking_in = false;
BuildContext? context;
@override
void onInit() {
initGPS();
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();
}
}
void initGPS(){
checkPermission();
//print("------ in iniit");
if (defaultTargetPlatform == TargetPlatform.android) {
locationSettings = AndroidSettings(
accuracy: LocationAccuracy.bestForNavigation,
distanceFilter: 0,
forceLocationManager: true,
intervalDuration: const Duration(seconds: 1),
foregroundNotificationConfig: const ForegroundNotificationConfig(
notificationText:
"Thi 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: 0,
pauseLocationUpdatesAutomatically: false,
// Only set to true if our app will be started up in the background.
showBackgroundLocationIndicator: true
);
} else {
locationSettings = const LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 0,
);
}
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;
print('current GPS point is - $current_lat, $current_lon');
if(is_gps_selected.value){
double czoom = homeController.mapController.zoom;
if(moveMapwithGPS.value){
homeController.mapController.move(LatLng(position!.latitude, position!.longitude), czoom);
}
//gps.clear();
//gps.add("-- lat : ${position.latitude}, lon : ${position.longitude} --");
if(is_checkingIn.value ==false || skip_gps == false){
print("--- calling checkFoCheckin ---");
checkFoCheckin();
}
//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());
}
//ever(indexController.connectionStatusName, connectionChanged);
}
void checkFoCheckin(){
for(final f in homeController.currentFeaturesforBound){
if(skip_gps) return;
var distance = Distance();
var dist = distance.as(LengthUnit.Meter, LatLng(f.latitude!, f.longitude!), LatLng(current_lat, current_lon));
if(dist <= 250 && is_checkingIn.value == false){
startTimer(f, dist);
}
}
}
void startTimer(Feature d, double distance) async {
skip_gps = true;
print("---- in startTimer ----");
double checkin_radious = d.properties!.checkin_radius ?? double.infinity;
bool auto_checkin = d.properties!.auto_checkin == 0 ? false : true;
bool location_already_checked_in = await checkinDBHelper.exists(d.location_id!);
bool isUser_logged_in = authController.authList.length > 0 ? true : false;
//make current destination
print("---- locationid : ${d.location_id} checkin radius ---- ${checkin_radious} --- distance : ${distance} ----");
print("---- distance ${distance} ----");
if(checkin_radious >= distance){
//currentSelectedDestinations.add(d);
homeController.currentFeature.assign(d);
}
else {
skip_gps = false;
return;
}
if(is_photo_shoot.value == true){
photos.clear();
showModalBottomSheet(context: Get.context!, isScrollControlled: true,
builder:((context) => CameraPage())
).whenComplete((){
skip_gps = false;
chekcs = 0;
is_in_checkin.value = false;
});
return;
}
List<Feature> ds = homeController.currentFeature;
if(ds.isNotEmpty){
if(d.properties!.cp == -1 && DateTime.now().difference(last_goal_at).inHours >= 24){
chekcs = 1;
//start
print("---- in start -----");
chekcs = 1;
is_in_checkin.value = true;
is_at_start.value = true;
showModalBottomSheet(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)
{
print("----- in location popup checkin cp - ${d.properties!.cp}----");
chekcs = 2;
is_in_checkin.value = true;
showModalBottomSheet(context: Get.context!, isScrollControlled: true,
builder:((context) => BottomSheetNew())
).whenComplete((){
skip_gps = false;
chekcs =0;
is_in_checkin.value = false;
});
}
}
print("---- location checkin radious ${d.properties!.checkin_radius} ----");
print("---- already checked in ${location_already_checked_in} ----");
if(checkin_radious >= distance && location_already_checked_in == false){
if(auto_checkin){
if(!checking_in){
makeCheckin(d, true,"");
if(d.properties!.cp != -1){
rogaining_counted.value =true;
}
skip_gps = false;
}
}
else{
print("--- hidden loc ${d.properties!.hidden_location} ----");
// ask for checkin
if(d.properties!.hidden_location != null && d.properties!.hidden_location == 0 && d.properties!.cp != -1){
chekcs = 3;
is_in_checkin.value = true;
photos.clear();
showModalBottomSheet(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.properties!.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;
// });
}
}
}
if(isUser_logged_in && d.properties!.cp == -1 && location_already_checked_in && 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(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 && DateTime.now().difference(last_goal_at).inHours >= 24){
//start
print("---- in start -----");
chekcs = 6;
is_at_start.value = true;
showModalBottomSheet(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;
}
}
void makeCheckin(Feature destination, bool action, String imageurl) async {
}
void addToRogaining(double lat, double lon, int destination_id) async {
}
void destinationMatrixFromCurrentPoint(List<Feature> ls){
}
}