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

@ -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]);
}
}
}
}
}
}