integrated with previous

This commit is contained in:
Mohamed Nouffer
2023-06-09 15:55:59 +05:30
parent a358f65853
commit 2cd685b65e
23 changed files with 1950 additions and 397 deletions

View File

@ -0,0 +1,45 @@
import 'dart:convert' as convert;
import 'package:get/get.dart';
import 'package:rogapp/model/user.dart';
import 'package:rogapp/utils/const.dart';
import 'package:http/http.dart' as http;
class AuthController extends GetxController{
var isLoading = false.obs;
var authList = List<AuthResponse>.empty(growable: true).obs;
void signinUser(String email, String password) async {
try{
isLoading(true);
String serverUrl = ConstValues.currentServer();
String url = '$serverUrl/api/login/';
final http.Response response = await http.post(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: convert.jsonEncode(<String, String>{
'email': email,
'password': password
}),
);
if (response.statusCode == 200) {
print(response.body);
AuthResponse authResponse = AuthResponse.fromJson(convert.jsonDecode(convert.utf8.decode(response.body.codeUnits)));
print(authResponse.user.eventCode.toString());
authList.assign(authResponse);
}
else{
throw("Unable to Login, please check your creadentials");
}
}
catch(e){
Get.snackbar("Error", e.toString(), snackPosition: SnackPosition.BOTTOM);
}
finally{
isLoading(false);
}
}
}

View File

@ -5,6 +5,7 @@ import 'package:rogapp/common/ui/widgets/uis.dart';
import 'package:rogapp/routes/app_pages.dart';
import 'package:rogapp/screens/auth/common/uis/auth_text_field.dart';
import 'package:rogapp/screens/auth/common/uis/rounded_small_button.dart';
import 'package:rogapp/screens/auth/controller/auth_controller.dart';
import 'package:rogapp/theme/theme.dart';
class LoginScreen extends StatelessWidget {
@ -12,15 +13,17 @@ class LoginScreen extends StatelessWidget {
final passwordController = TextEditingController();
LoginScreen({super.key});
void login(){
}
final AuthController authController = Get.find<AuthController>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: UIs.appBar(),
body: Center(
body: Obx((){
if(authController.isLoading.value) {
return const Center(child: CircularProgressIndicator(),);
} else {
return Center(
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(8.0),
@ -39,7 +42,10 @@ class LoginScreen extends StatelessWidget {
const SizedBox(height: 40,),
Align(
alignment: Alignment.topRight,
child: RoundedSmallButton(onTap: login, label: 'Done',),
child: RoundedSmallButton(onTap: (){
authController.signinUser(emailController.text, passwordController.text);
},
label: 'Done',),
),
const SizedBox(height: 40,),
RichText(
@ -61,7 +67,9 @@ class LoginScreen extends StatelessWidget {
),
),
),
)
);
}
}),
);
}
}

View File

@ -0,0 +1,12 @@
import 'package:get/get.dart';
import 'package:rogapp/screens/home/home_controller.dart';
import 'package:rogapp/screens/home/location_controller.dart';
class HomeBinding extends Bindings{
@override
void dependencies() {
Get.put(HomeController());
Get.put(LocationController());
}
}

View File

@ -0,0 +1,124 @@
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:rogapp/model/location_response.dart';
import 'package:rogapp/screens/auth/controller/auth_controller.dart';
import 'package:rogapp/screens/home/location_controller.dart';
import 'package:rogapp/utils/const.dart';
import 'package:http/http.dart' as http;
import 'dart:convert' as convert;
import 'package:rogapp/utils/db_helper.dart';
class HomeController extends GetxController{
var isLoading = false.obs;
List<LatLngBounds> currentBound = <LatLngBounds>[].obs;
List<Feature> currentFeaturesforBound = <Feature>[].obs;
List<Feature> currentFeature = <Feature>[].obs;
MapController mapController = MapController();
late StreamSubscription<ConnectivityResult> _connectivitySubscription;
ConnectivityResult connectionStatus = ConnectivityResult.none;
var connectionStatusName = "".obs;
final Connectivity _connectivity = Connectivity();
AuthController authController = Get.find<AuthController>();
DatabaseHelper dbaseHelper = DatabaseHelper.instance;
@override
void onInit() {
super.onInit();
_connectivitySubscription = _connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
dbaseHelper.deleteAll().then((value){
fetchBoundForuser(authController.authList[0].token);
loadLocationsForBound().then((_){
fetchLocalLocationForBound(mapController.bounds!);
});
});
}
Future<void> _updateConnectionStatus(ConnectivityResult result) async {
connectionStatus = result;
connectionStatusName.value = result.name;
}
void fetchLocalLocationForBound(LatLngBounds bound) async {
final locations = await dbaseHelper.getFeaturesWithinBounds(bound);
currentFeaturesforBound.assignAll(locations);
print("loading locations ${currentFeaturesforBound.length} ------");
}
Future<void> loadLocationsForBound() async {
LatLngBounds bounds = mapController.bounds!;
String serverUrl = ConstValues.currentServer();
try{
isLoading(true);
if(bounds.southWest != null && bounds.northEast != null){
bool _rog = authController.authList[0].user.isRogaining;
String r = _rog == true ? 'True': 'False';
var grp = authController.authList[0].user.eventCode;
final url = '$serverUrl/api/inbound?rog=${r}&grp=$grp&ln1=${bounds.southWest!.longitude}&la1=${bounds.southWest!.latitude}&ln2=${bounds.northWest.longitude}&la2=${bounds.northWest.latitude}&ln3=${bounds.northEast!.longitude}&la3=${bounds.northEast!.latitude}&ln4=${bounds.southEast.longitude}&la4=${bounds.southEast.latitude}';
final response = await http.get(Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
);
if (response.statusCode == 200) {
// Parse JSON response
LocationResponse locationResponse = LocationResponse.fromMap(convert.jsonDecode(convert.utf8.decode(response.body.codeUnits)));
// For each feature in the location response
for (var feature in locationResponse.features!) {
await dbaseHelper.insertFeature(feature.toMap());
}
}
else{
throw("Unable to load locations, please try again ...");
}
}
}
catch(e, st){
Get.snackbar("Error", e.toString(), snackPosition: SnackPosition.BOTTOM);
}
finally{
isLoading(false);
}
}
void fetchBoundForuser(String usertoken) async {
try{
isLoading(true);
String serverUrl = ConstValues.currentServer();
String url = '$serverUrl/api/locsext/';
final http.Response response = await http.post(
Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization': 'Token $usertoken'
},
body: convert.jsonEncode(<String, String>{
'token': usertoken,
}),
);
if (response.statusCode == 200) {
final ext = convert.json.decode(convert.utf8.decode(response.body.codeUnits));
LatLngBounds bnds = LatLngBounds(LatLng(ext[1], ext[0]), LatLng(ext[3], ext[2]));
currentBound.assign(bnds);
mapController.fitBounds(bnds);
}
else{
throw("Unable to query current bound, please try again ...");
}
}
catch(e){
Get.snackbar("Error", e.toString(), snackPosition: SnackPosition.BOTTOM);
}
finally{
isLoading(false);
}
}
}

View File

@ -1,10 +1,183 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:rogapp/common/ui/widgets/uis.dart';
import 'package:rogapp/model/location_response.dart';
import 'package:rogapp/screens/home/home_controller.dart';
import 'package:rogapp/screens/home/location_controller.dart';
import 'package:rogapp/theme/theme.dart';
import 'package:rogapp/utils/db_helper.dart';
import 'package:rogapp/utils/text_util.dart';
import 'package:rogapp/widgets/base_layer_widget.dart';
import 'package:rogapp/widgets/bottom_sheet_new.dart';
import 'package:sqflite/sqflite.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
HomeScreen({super.key});
StreamSubscription? subscription;
final HomeController homeController = Get.find<HomeController>();
final LocationController locationController = Get.find<LocationController>();
final double zoomThreshold = 0.08;
final double boundsThreshold = 0.0005;
LatLngBounds? lastBounds;
double? lastZoom;
Widget getMarkerShape(Feature i, BuildContext context){
//print("lat is ${p.geoSerie!.geoPoints[0].latitude} and lon is ${p.geoSerie!.geoPoints[0].longitude}");
RegExp regex = RegExp(r'([.]*0)(?!.*\d)');
return Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
InkWell(
onTap: () {
homeController.currentFeature.assign(i);
if(homeController.currentFeature != null) {
showModalBottomSheet(
context: context,
isScrollControlled: true,
isDismissible: true,
builder:((context) => BottomSheetNew())
//builder:((context) => BottomSheetWidget())
).whenComplete((){
locationController.skip_gps = false;
});
}
},
child: Container(
height: 32,
width: 32,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.transparent,
border: Border.all(
color: i.properties!.buy_point! > 0 ? Colors.blue : Colors.red,
width: 3,
style: BorderStyle.solid
)
),
child: Stack(
alignment: Alignment.center,
children: [
Icon(Icons.circle,size: 6.0,),
i.properties!.cp == -1 ?
Transform.rotate(
alignment: Alignment.centerLeft,
origin: Offset.fromDirection(1, 26),
angle: 270 * pi / 180,
child: Icon(Icons.play_arrow_outlined, color: Colors.red, size: 70,)):
Container(color: Colors.transparent,),
],
)
),
),
Container(color: Colors.white, child: Text(TextUtils.getDisplayTextForFeature(i), style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color:Colors.red,))),
],
);
}
@override
Widget build(BuildContext context) {
return Placeholder();
return Scaffold(
appBar: AppBar(
actions: [
TextButton(onPressed: () async {
DatabaseHelper dbaseHelper = DatabaseHelper.instance;
final res = await dbaseHelper.getFeatureByLatLon(35.807369, 137.240684);
print(res[0].toString());
},
child: const Text("Click", style: TextStyle(color: Colors.white),))
],
),
body: Obx(() =>
FlutterMap(
mapController: homeController.mapController,
options: MapOptions(
onMapReady: (){
homeController.mapController!.mapEventStream.listen((MapEvent mapEvent) {
if (mapEvent is MapEventMoveStart) {
}
if (mapEvent is MapEventMoveEnd) {
LatLngBounds bounds = homeController.mapController!.bounds!;
homeController.currentBound.assign(bounds);
final _cz = homeController.mapController.zoom;
}
});
} ,
onPositionChanged: (position, hasGesture){
if (hasGesture) {
LatLngBounds currentBounds = position.bounds!;
if (lastBounds != null && lastZoom != null) {
// Compute difference between last bounds and current bounds
double boundsDifference = lastBounds!.north - currentBounds.north +
lastBounds!.south - currentBounds.south +
lastBounds!.east - currentBounds.east +
lastBounds!.west - currentBounds.west;
// Compute difference between last zoom and current zoom
double zoomDifference = lastZoom! - position.zoom!;
if (boundsDifference.abs() > boundsThreshold || zoomDifference.abs() > zoomThreshold) {
LatLngBounds bounds = homeController.mapController!.bounds!;
homeController.currentBound.assign(bounds);
print('----threshold reached ---');
homeController.fetchLocalLocationForBound(bounds);
//loadData(position.bounds); // Load new data when either threshold is exceeded
}
} else {
LatLngBounds bounds = homeController.mapController!.bounds!;
homeController.currentBound.assign(bounds);
homeController.fetchLocalLocationForBound(bounds);
//loadData(position.bounds); // Load new data if this is the first time
}
lastBounds = currentBounds; // Update the last bounds
lastZoom = position.zoom; // Update the last zoom
}
},
bounds: homeController.currentBound.isNotEmpty ? homeController.currentBound[0]: LatLngBounds.fromPoints([LatLng(35.03999881162295, 136.40587119778962), LatLng(36.642756778706904, 137.95226720406063)]),
zoom: 14,
maxZoom:18.4,
interactiveFlags: InteractiveFlag.pinchZoom | InteractiveFlag.drag,
),
children: [
const BaseLayer(),
CurrentLocationLayer(),
homeController.currentFeaturesforBound.isNotEmpty ?
MarkerLayer(
markers:homeController.currentFeaturesforBound.map((i) {
RegExp regex = RegExp(r'([.]*0)(?!.*\d)');
return Marker(
anchorPos: AnchorPos.exactly(Anchor(108.0, 18.0)),
height: 32.0,
width: 120.0,
point: LatLng(i.latitude!, i.longitude!),
builder: (ctx){
return getMarkerShape(i, context);
},
);
}).toList(),
)
:
Center(child: CircularProgressIndicator()),
],
)
),
);
}
}

View File

@ -0,0 +1,318 @@
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){
}
}