refactoring

This commit is contained in:
Mohamed Nouffer
2022-07-30 20:42:44 +05:30
parent a4adf24e99
commit 244b7eb9ac
14 changed files with 605 additions and 481 deletions

View File

@ -1,14 +1,11 @@
import 'dart:convert';
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/destination.dart';
import 'package:rogapp/model/location.dart';
import 'package:rogapp/pages/index/index_controller.dart';
import 'package:rogapp/routes/app_pages.dart';
import 'package:rogapp/services/destination_service.dart';
@ -24,10 +21,10 @@ class DestinationController extends GetxController {
late LocationSettings locationSettings;
var destinationCount = 0.obs;
List<Destination> destinations = <Destination>[].obs;
List<Location> destinations = <Location>[].obs;
List<Map<String, dynamic>> destination_index_data = <Map<String, dynamic>>[].obs;
List<Destination> currentSelectedDestinations = <Destination>[].obs;
List<Location> currentSelectedDestinations = <Location>[].obs;
bool checking_in = false;
List<bool> isSelected = [true].obs;
@ -42,23 +39,12 @@ class DestinationController extends GetxController {
final IndexController indexController = Get.find<IndexController>();
Future<Destination?> getDEstinationForLatLong(double lat, double long)async {
Future<Location?> 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){
// var geom = d["location"]["geometry"];
// var props = d["location"]["properties"];
// print("--props- ${d["location"]["geometry"]["coordinates"][0][1]}");
// List<dynamic> geom_multi = [geom];
// Map<String, dynamic> final_geom = {"features":[{"id":d["id"],"type":"Feature", "geometry": geom, "properties": props}]};
// //print("----- geom : ${final_geom}");
// String js = json.encode(final_geom);
// //print("---features-- ${js}-----");
// GeoJsonFeatureCollection features = await featuresFromGeoJson(js);
// GeoJsonMultiPoint p = features.collection[0].geometry as GeoJsonMultiPoint;
if(lat == d.lat && long == d.lon){
if(lat == d.latitude && long == d.longitude){
return d;
}
}
@ -71,56 +57,22 @@ class DestinationController extends GetxController {
if(!checking_in)
{
checking_in = true;
double lat = d.lat!;
double lon = d.lon!;
LatLng p = LatLng(lat, lon);
double lat = d.latitude!;
double lon = d.longitude!;
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;
int rad = value!.checkin_radius! ?? 1000000;
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{
// 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;
// });
// }
// }
// }
});
}
@ -128,12 +80,12 @@ class DestinationController extends GetxController {
}
}
void makeCheckin(Destination destination, bool action) async {
void makeCheckin(Location 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!);
List<Location> ddd = await db.getDestinationByLatLon(destination.lat!, destination.lon!);
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ddddd ${ddd[0].checkedin} @@@@@@@@@@@");
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ressssss ${res}@@@@@@@@@@@");

View File

@ -6,7 +6,6 @@ import 'package:flutter_map/plugin_api.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:flutter_map_marker_popup/flutter_map_marker_popup.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:geojson/geojson.dart';
import 'package:geolocator/geolocator.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';

View File

@ -4,10 +4,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:geojson/geojson.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:rogapp/model/destination.dart';
import 'package:rogapp/model/location.dart';
import 'package:rogapp/pages/destination/destination_binding.dart';
import 'package:rogapp/pages/destination/destination_controller.dart';
import 'package:rogapp/pages/destination/destination_page.dart';
@ -21,9 +21,9 @@ import 'package:rogapp/services/perfecture_service.dart';
import 'package:rogapp/utils/database_helper.dart';
class IndexController extends GetxController {
List<GeoJsonFeatureCollection> locations = <GeoJsonFeatureCollection>[].obs;
List<GeoJsonFeature> currentFeature = <GeoJsonFeature>[].obs;
List<Destination> currentDestinationFeature = <Destination>[].obs;
List<Location> locations = <Location>[].obs;
List<Location> currentFeature = <Location>[].obs;
List<Location> currentDestinationFeature = <Location>[].obs;
List<dynamic> perfectures = <dynamic>[].obs;
List<LatLngBounds> currentBound = <LatLngBounds>[].obs;
List<dynamic> subPerfs = <dynamic>[].obs;
@ -62,6 +62,10 @@ class IndexController extends GetxController {
String areaDropdownValue = "-1";
String cateogory = "-all-";
LocationService locationService = Get.find<LocationService>();
late Worker _ever;
void toggleMode(){
@ -154,10 +158,10 @@ LatLngBounds boundsFromLatLngList(List<LatLng> list) {
List<LatLng> getLocationsList(){
List<LatLng> locs = [];
for(int i=0; i<= locations[0].collection.length - 1; i++){
GeoJsonMultiPoint p = locations[0].collection[i].geometry as GeoJsonMultiPoint;
for(int i=0; i<= locations.length - 1; i++){
Location p = locations[i];
LatLng latLng = LatLng(p.geoSerie!.geoPoints[0].latitude, p.geoSerie!.geoPoints[0].longitude);
LatLng latLng = LatLng(p.latitude!, p.longitude!);
locs.add(latLng);
}
return locs;
@ -172,9 +176,6 @@ void login(String email, String password, BuildContext context){
is_loading.value = false;
Navigator.pop(context);
loadUserDetails();
if(currentFeature.isNotEmpty){
getAction();
}
if(rog_mode.value == 1){
switchPage(AppPages.TRAVEL);
}
@ -222,18 +223,6 @@ void login(String email, String password, BuildContext context){
});
}
void makeAction(BuildContext context){
int user_id = currentUser[0]["user"]["id"] as int;
int location_id = currentFeature[0].properties!["location_id"] as int;
bool wanttogo = currentAction[0][0]["wanttogo"];
bool like = currentAction[0][0]["like"];
bool checkin = currentAction[0][0]["checkin"];
if(user_id > 0){
ActionService.makeAction(user_id, location_id, wanttogo, like, checkin).then((value){
});
}
}
void loadCatsv2(){
@ -272,22 +261,6 @@ void login(String email, String password, BuildContext context){
void refreshLocationForCat(){
loadLocationsBound();
// if(subDropdownValue == "-1"){
// LocationService.loadLocationsFor(dropdownValue, currentCat[0]).then((value){
// locations.clear();
// locations.add(value!);
// is_loading.value = false;
// });
// print("loading main------");
// }
// else{
// LocationService.loadLocationsSubFor(subDropdownValue, currentCat[0]).then((value){
// locations.clear();
// locations.add(value!);
// is_loading.value = false;
// });
// print("loading sub------");
// }
}
void loadAreaFor(String perf){
@ -349,11 +322,6 @@ void login(String email, String password, BuildContext context){
void loadLocationforPerf(String perf, MapController mapController) async {
String cat = currentCat.isNotEmpty == true ? currentCat[0] : "";
print(currentCat);
// LocationService.loadLocationsFor(perf, cat).then((value){
// locations.clear();
// locations.add(value!);
// mapController.fitBounds(currentBound[0]);
// });
locations.clear();
mapController.fitBounds(currentBound[0]);
}
@ -363,18 +331,18 @@ void login(String email, String password, BuildContext context){
if(currentCat[0] == "-all-"){
cat = "";
}
LocationService.loadLocationsSubFor(subperf, cat).then((value){
locationService.loadLocationsSubFor(subperf, cat).then((value){
locations.clear();
locations.add(value!);
locations.addAll(value);
});
}
void loadCustomLocation(String customarea) async {
String cat = currentCat.isNotEmpty == true ? currentCat[0] : "";
print("----- ${customarea}");
LocationService.loadCustomLocations(customarea, cat).then((value){
locationService.loadCustomLocations(customarea, cat).then((value){
locations.clear();
locations.add(value!);
locations.addAll(value);
List<LatLng> locs = getLocationsList();
LatLngBounds bounds = boundsFromLatLngList(locs);
mapController!.fitBounds(bounds);
@ -396,13 +364,13 @@ void login(String email, String password, BuildContext context){
currentBound.add(bounds);
//print(currentCat);
if(bounds.southEast != null && bounds.southWest != null && bounds.northEast != null && bounds.southEast != null ){
LocationService.loadLocationsBound(bounds.southWest!.latitude, bounds.southWest!.longitude, bounds.northWest.latitude, bounds.northWest.longitude, bounds.northEast!.latitude, bounds.northEast!.longitude, bounds.southEast.latitude, bounds.southEast.longitude, cat).then((value){
locationService.loadLocationsBound(bounds.southWest!.latitude, bounds.southWest!.longitude, bounds.northWest.latitude, bounds.northWest.longitude, bounds.northEast!.latitude, bounds.northEast!.longitude, bounds.southEast.latitude, bounds.southEast.longitude, cat).then((value){
//print("---value length ------ ${value!.collection.length}");
if(value == null){
return;
}
locations.clear();
if(value != null && value.collection.isEmpty){
if(value != null && value.isEmpty){
if(showPopup == false) {
return;
}
@ -418,9 +386,9 @@ void login(String email, String password, BuildContext context){
showPopup = false;
//Get.showSnackbar(GetSnackBar(message: "Too many points, please zoom in",));
}
if(value != null && value.collection.isNotEmpty){
if(value != null && value.isNotEmpty){
//print("---- added---");
locations.add(value);
locations.addAll(value);
loadCatsv2();
}
});
@ -482,104 +450,65 @@ void login(String email, String password, BuildContext context){
}
GeoJsonFeature? getFeatureForLatLong(double lat, double long){
Location? getFeatureForLatLong(double lat, double long){
if(locations.length > 0){
for(GeoJsonFeature i in locations[0].collection){
GeoJsonMultiPoint p = i.geometry as GeoJsonMultiPoint;
if(p.geoSerie!.geoPoints[0].latitude == lat && p.geoSerie!.geoPoints[0].longitude == long){
for(Location i in locations){
if(i.latitude == lat && i.longitude == long){
return i;
}
}
}
}
void getAction(){
//print(currentUser[0]["user"]["id"]);
//print(currentFeature[0].properties!["location_id"]);
if(currentUser.length == 0){
return;
}
int user_id = currentUser[0]["user"]["id"] as int;
print("---- loc id ${currentFeature[0].properties}");
int location_id = currentFeature[0].properties!["location_id"] as int;
ActionService.userAction(user_id, location_id).then((value){
print("------${value}");
if(value != null && value.length > 0){
currentAction.clear();
currentAction.add(value);
print("------${currentAction[0]}");
}else{
List<dynamic> initval = [{"user": user_id, "location": location_id, "wanttogo": false, "like": false, "checkin": false}];
currentAction.clear();
currentAction.add(initval);
}
});
}
void makeNext(GeoJsonFeature fs){
void makeNext(Location fs){
if(rog_mode == 1){
DestinationController destinationController = Get.find<DestinationController>();
print("---- destination index--- ${destinationController.destination_index_data} --------");
}
else {
GeoJsonFeature<GeoJsonMultiPoint> pt = fs as GeoJsonFeature<GeoJsonMultiPoint>;
for(int i=0; i<= locations[0].collection.length - 1; i++){
GeoJsonMultiPoint p = locations[0].collection[i].geometry as GeoJsonMultiPoint;
if(p.geoSerie!.geoPoints[0].latitude == pt.geometry!.geoSerie!.geoPoints[0].latitude && p.geoSerie!.geoPoints[0].longitude == pt.geometry!.geoSerie!.geoPoints[0].longitude ){
for(int i=0; i<= locations.length - 1; i++){
Location p = locations[i];
if(p.latitude == fs.latitude && p.longitude == fs.longitude ){
if(currentFeature.length > 0){
currentFeature.clear();
}
if(i >= locations[0].collection.length - 1 ){
currentFeature.add(locations[0].collection[0] as GeoJsonFeature);
getAction();
if(i >= locations.length - 1 ){
currentFeature.add(locations[0]);
}
else{
currentFeature.add(locations[0].collection[i + 1] as GeoJsonFeature);
getAction();
currentFeature.add(locations[i + 1]);
}
}
}
}
}
void makePrevious(GeoJsonFeature fs){
void makePrevious(Location fs){
if(rog_mode == 1){
DestinationController destinationController = Get.find<DestinationController>();
print("---- destination index--- ${destinationController.destination_index_data} --------");
}
else {
GeoJsonFeature<GeoJsonMultiPoint> pt = fs as GeoJsonFeature<GeoJsonMultiPoint>;
for(int i=0; i<= locations.length - 1; i++){
Location p = locations[i];
for(int i=0; i<= locations[0].collection.length - 1; i++){
GeoJsonMultiPoint p = locations[0].collection[i].geometry as GeoJsonMultiPoint;
if(p.geoSerie!.geoPoints[0].latitude == pt.geometry!.geoSerie!.geoPoints[0].latitude && p.geoSerie!.geoPoints[0].longitude == pt.geometry!.geoSerie!.geoPoints[0].longitude ){
if(p.latitude == fs.longitude && p.longitude == fs.longitude ){
if(currentFeature.length > 0){
currentFeature.clear();
}
if(i == 0 ){
currentFeature.add(locations[0].collection[locations[0].collection.length -1] as GeoJsonFeature);
getAction();
currentFeature.add(locations[locations.length -1]);
}
else{
currentFeature.add(locations[0].collection[i - 1] as GeoJsonFeature);
getAction();
currentFeature.add(locations[i - 1]);
}
}
}
}
}
}

View File

@ -1,7 +1,6 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:geojson/geojson.dart';
import 'package:get/get.dart';
import 'package:rogapp/pages/index/index_controller.dart';
import 'package:rogapp/pages/search/search_controller.dart';