Files
rog_app/lib/pages/index/index_controller.dart
Mohamed Nouffer 80a976baa0 update
2022-05-12 02:17:08 +05:30

302 lines
8.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:geojson/geojson.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:rogapp/routes/app_pages.dart';
import 'package:rogapp/services/action_service.dart';
import 'package:rogapp/services/auth_service.dart';
import 'package:rogapp/services/cat_service.dart';
import 'package:rogapp/services/location_service.dart';
import 'package:rogapp/services/perfecture_service.dart';
class IndexController extends GetxController {
List<GeoJsonFeatureCollection> locations = <GeoJsonFeatureCollection>[].obs;
List<GeoJsonFeature> currentFeature = <GeoJsonFeature>[].obs;
List<dynamic> perfectures = <dynamic>[].obs;
List<LatLngBounds> currentBound = <LatLngBounds>[].obs;
List<dynamic> subPerfs = <dynamic>[].obs;
List<dynamic> cats = <dynamic>[].obs;
List<String> currentCat = <String>[].obs;
List<Map<String, dynamic>> currentUser = <Map<String, dynamic>>[].obs;
List<dynamic> currentAction = <dynamic>[].obs;
var is_loading = false.obs;
MapController? mapController;
var mode = 0.obs;
String dropdownValue = "9";
String subDropdownValue = "-1";
void toggleMode(){
if(mode==0){
mode += 1;
}
else{
mode -= 1;
}
}
@override
void onInit() {
super.onInit();
if(locations.length == 0){
LocationService.loadLocations().then((value){
locations.add(value!);
//print(value);
});
}
if(perfectures.length == 0){
PerfectureService.loadPerfectures().then((value){
perfectures.add(value);
loadSubPerfFor("9");
});
}
loadCats();
}
void login(String email, String password, BuildContext context){
AuthService.login(email, password).then((value){
if(value.isNotEmpty){
currentUser.clear();
currentUser.add(value);
is_loading.value = false;
Navigator.pop(context);
if(currentFeature.isNotEmpty){
getAction();
}
Get.toNamed(AppPages.INITIAL);
}else{
is_loading.value = false;
Get.snackbar("Failed", "User login failed, please try again.");
}
});
}
void register(String email, String password, BuildContext context){
AuthService.register(email, password).then((value){
if(value.isNotEmpty){
currentUser.clear();
currentUser.add(value);
is_loading.value = false;
Navigator.pop(context);
Get.toNamed(AppPages.INITIAL);
}else{
is_loading.value = false;
Get.snackbar("Failed", "User registration failed, please try again.");
}
});
}
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"];
print("----userid----${user_id}");
if(user_id > 0){
ActionService.makeAction(user_id, location_id, wanttogo, like, checkin).then((value){
print("----action value----${value}");
});
}
}
void loadCats(){
dynamic initVal = {'category':'---'};
CatService.loadCats().then((value) {
//value!.add(initVal);
print("###########");
print(value);
cats.add(value);
});
}
void refreshLocationForCat(){
if(subDropdownValue == "-1"){
LocationService.loadLocationsFor(dropdownValue, currentCat[0]);
print("loading main------");
}
else{
LocationService.loadLocationsSubFor(subDropdownValue, currentCat[0]);
print("loading sub------");
}
}
void loadSubPerfFor(String perf){
subPerfs.clear();
dynamic initVal = {'id':'-1', 'adm2_ja':'----'};
PerfectureService.loadSubPerfectures(perf).then((value){
value!.add(initVal);
subPerfs.add(value);
subDropdownValue = getSubInitialVal();
});
}
String getSubInitialVal(){
int min = 0;
if(subPerfs.length > 0){
min = int.parse(subPerfs[0][0]['id'].toString());
for(var sub in subPerfs[0]){
int x = int.parse(sub['id'].toString()); // as int;
if(x < min){
min = x;
}
}
}
return min.toString();
}
void loadLocationforPerf(String perf, MapController mapController) async {
locations.clear();
LocationService.loadLocationsFor(perf, currentCat[0]).then((value){
locations.add(value!);
mapController.fitBounds(currentBound[0]);
});
}
void loadLocationforSubPerf(String subperf, MapController mapController) async {
locations.clear();
LocationService.loadLocationsSubFor(subperf, currentCat[0]).then((value){
locations.add(value!);
});
}
void setBound(LatLngBounds bounds){
currentBound.clear();
currentBound.add(bounds);
}
void zoomtoMainPerf(String id){
PerfectureService.getMainPerfExt(id).then((value){
LatLng lat1 = LatLng(value![1], value[0]);
LatLng lat2 = LatLng(value[3], value[2]);
LatLngBounds bound = LatLngBounds(lat1, lat2);
mapController!.fitBounds(bound);
setBound(bound);
});
}
void zoomtoSubPerf(String id){
PerfectureService.getSubExt(id).then((value){
LatLng lat1 = LatLng(value![1], value[0]);
LatLng lat2 = LatLng(value[3], value[2]);
LatLngBounds bound = LatLngBounds(lat1, lat2);
mapController!.fitBounds(bound);
setBound(bound);
});
}
void populateForPerf(String perf, MapController mapController){
loadSubPerfFor(perf);
loadLocationforPerf(perf, mapController);
zoomtoMainPerf(perf);
}
void populateForSubPerf(String subperf, MapController mapController){
subDropdownValue = subperf;
loadLocationforSubPerf(subperf, mapController);
zoomtoSubPerf(subperf);
}
GeoJsonFeature? 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){
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;
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){
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 ){
if(currentFeature.length > 0){
currentFeature.clear();
}
if(i >= locations[0].collection.length - 1 ){
currentFeature.add(locations[0].collection[0] as GeoJsonFeature);
getAction();
}
else{
currentFeature.add(locations[0].collection[i + 1] as GeoJsonFeature);
getAction();
}
}
}
}
void makePrevious(GeoJsonFeature fs){
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 ){
if(currentFeature.length > 0){
currentFeature.clear();
}
if(i == 0 ){
currentFeature.add(locations[0].collection[locations[0].collection.length -1] as GeoJsonFeature);
getAction();
}
else{
currentFeature.add(locations[0].collection[i - 1] as GeoJsonFeature);
getAction();
}
}
}
}
}