Files
rog_app/lib/pages/index/index_controller.dart

436 lines
13 KiB
Dart
Raw Normal View History

2022-05-18 19:09:26 +05:30
import 'dart:async';
2022-11-14 22:22:41 +05:30
import 'package:connectivity_plus/connectivity_plus.dart';
2022-05-12 02:17:08 +05:30
import 'package:flutter/material.dart';
2022-11-14 22:22:41 +05:30
import 'package:flutter/services.dart';
2022-04-17 11:45:21 +05:30
import 'package:flutter_map/flutter_map.dart';
2022-05-24 20:43:41 +05:30
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
2023-12-06 11:16:17 +05:30
import 'package:geojson_vi/geojson_vi.dart';
2022-04-17 11:45:21 +05:30
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
2022-07-09 22:51:34 +05:30
import 'package:rogapp/model/destination.dart';
import 'package:rogapp/pages/destination/destination_controller.dart';
2022-05-12 02:17:08 +05:30
import 'package:rogapp/routes/app_pages.dart';
import 'package:rogapp/services/auth_service.dart';
2022-04-17 11:45:21 +05:30
import 'package:rogapp/services/location_service.dart';
2022-07-09 22:51:34 +05:30
import 'package:rogapp/utils/database_helper.dart';
2023-09-04 22:46:53 +05:30
import 'package:shared_preferences/shared_preferences.dart';
2022-04-17 11:45:21 +05:30
class IndexController extends GetxController {
2023-12-06 11:16:17 +05:30
List<GeoJSONFeatureCollection> locations = <GeoJSONFeatureCollection>[].obs;
List<GeoJSONFeature> currentFeature = <GeoJSONFeature>[].obs;
2022-07-09 22:51:34 +05:30
List<Destination> currentDestinationFeature = <Destination>[].obs;
2022-04-17 11:45:21 +05:30
List<dynamic> perfectures = <dynamic>[].obs;
List<LatLngBounds> currentBound = <LatLngBounds>[].obs;
List<dynamic> subPerfs = <dynamic>[].obs;
2022-06-06 21:15:58 +05:30
List<dynamic> areas = <dynamic>[].obs;
2022-06-14 14:37:59 +05:30
List<dynamic> customAreas = <dynamic>[].obs;
2022-05-12 02:17:08 +05:30
List<dynamic> cats = <dynamic>[].obs;
List<String> currentCat = <String>[].obs;
List<Map<String, dynamic>> currentUser = <Map<String, dynamic>>[].obs;
List<dynamic> currentAction = <dynamic>[].obs;
2022-05-24 20:43:41 +05:30
List<PointLatLng> routePoints = <PointLatLng>[].obs;
2022-09-22 20:36:56 +05:30
var routePointLenght = 0.obs;
2022-05-12 02:17:08 +05:30
2023-10-06 16:25:21 +05:30
double currentLat = 0.0, currentLon = 0.0;
2023-06-11 21:03:30 +05:30
2023-10-06 16:25:21 +05:30
var isLoading = false.obs;
2022-04-17 11:45:21 +05:30
2023-10-06 16:25:21 +05:30
var isRogMapcontrollerLoaded = false.obs;
2022-07-09 22:51:34 +05:30
2023-10-06 16:25:21 +05:30
var isCustomAreaSelected = false.obs;
2022-07-28 19:01:45 +05:30
2023-03-17 11:54:12 +05:30
MapController mapController = MapController();
MapController rogMapController = MapController();
2022-04-17 11:45:21 +05:30
2023-09-04 22:46:53 +05:30
String? userToken;
2023-10-06 16:25:21 +05:30
// mode = 0 is map mode, mode = 1 list mode
2022-04-17 11:45:21 +05:30
var mode = 0.obs;
2022-07-14 23:10:24 +05:30
// master mode, rog or selection
2023-10-06 16:25:21 +05:30
var rogMode = 1.obs;
2022-06-14 14:37:59 +05:30
2023-10-06 16:25:21 +05:30
var desinationMode = 1.obs;
2022-05-24 20:43:41 +05:30
2022-07-10 23:50:43 +05:30
bool showPopup = true;
2023-10-06 16:25:21 +05:30
String dropdownValue = "9";
2022-04-17 11:45:21 +05:30
String subDropdownValue = "-1";
2022-06-06 21:15:58 +05:30
String areaDropdownValue = "-1";
2022-07-23 19:28:35 +05:30
String cateogory = "-all-";
2022-04-17 11:45:21 +05:30
2022-11-14 22:22:41 +05:30
ConnectivityResult connectionStatus = ConnectivityResult.none;
var connectionStatusName = "".obs;
final Connectivity _connectivity = Connectivity();
late StreamSubscription<ConnectivityResult> _connectivitySubscription;
2023-10-06 16:25:21 +05:30
void toggleMode() {
if (mode.value == 0) {
2022-04-17 11:45:21 +05:30
mode += 1;
2023-10-06 16:25:21 +05:30
} else {
2022-04-17 11:45:21 +05:30
mode -= 1;
}
}
2023-10-06 16:25:21 +05:30
void toggleDestinationMode() {
if (desinationMode.value == 0) {
desinationMode.value += 1;
} else {
desinationMode.value -= 1;
2022-05-24 20:43:41 +05:30
}
}
2023-10-06 16:25:21 +05:30
void switchPage(String page) {
////print("######## ${currentUser[0]["user"]["id"]}");
2022-06-14 14:37:59 +05:30
switch (page) {
2023-10-08 19:13:14 +05:30
case AppPages.INDEX:
2023-10-06 16:25:21 +05:30
{
rogMode.value = 0;
//print("-- rog mode is ctrl is ${rog_mode.value}");
Get.toNamed(page);
}
2022-06-14 14:37:59 +05:30
break;
2023-10-06 16:25:21 +05:30
case AppPages.TRAVEL:
{
rogMode.value = 1;
//Get.back();
//Get.off(DestnationPage(), binding: DestinationBinding());
}
break;
case AppPages.LOGIN:
{
rogMode.value = 2;
Get.toNamed(page);
}
break;
default:
{
2023-11-18 21:02:00 +05:30
rogMode.value = 1;
2023-10-08 19:13:14 +05:30
Get.toNamed(AppPages.INDEX);
2023-10-06 16:25:21 +05:30
}
2022-06-14 14:37:59 +05:30
}
}
2022-04-17 11:45:21 +05:30
@override
void onInit() {
2023-10-06 16:25:21 +05:30
_connectivitySubscription =
_connectivity.onConnectivityChanged.listen(_updateConnectionStatus);
2022-06-14 14:37:59 +05:30
super.onInit();
2023-10-06 16:25:21 +05:30
}
2022-06-14 14:37:59 +05:30
2023-10-06 16:25:21 +05:30
@override
void onClose() {
2022-11-14 22:22:41 +05:30
_connectivitySubscription.cancel();
super.onClose();
2023-10-06 16:25:21 +05:30
}
2022-11-14 22:22:41 +05:30
2023-10-06 16:25:21 +05:30
Future<void> _updateConnectionStatus(ConnectivityResult result) async {
connectionStatus = result;
connectionStatusName.value = result.name;
}
2022-11-14 22:22:41 +05:30
2023-10-06 16:25:21 +05:30
Future<void> initConnectivity() async {
2022-11-14 22:22:41 +05:30
late ConnectivityResult result;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
result = await _connectivity.checkConnectivity();
2023-10-06 16:25:21 +05:30
} on PlatformException catch (_) {
//print('Couldn\'t check connectivity status --- $e');
2022-11-14 22:22:41 +05:30
return;
}
return _updateConnectionStatus(result);
}
2022-06-14 14:37:59 +05:30
2023-10-06 16:25:21 +05:30
LatLngBounds boundsFromLatLngList(List<LatLng> list) {
double? x0, x1, y0, y1;
for (LatLng latLng in list) {
if (x0 == null || x1 == null || y0 == null || y1 == null) {
x0 = x1 = latLng.latitude;
y0 = y1 = latLng.longitude;
} else {
if (latLng.latitude > x1) x1 = latLng.latitude;
if (latLng.latitude < x0) x0 = latLng.latitude;
if (latLng.longitude > y1) y1 = latLng.longitude;
if (latLng.longitude < y0) y0 = latLng.longitude;
}
2022-06-14 14:37:59 +05:30
}
2022-05-12 02:17:08 +05:30
2023-10-06 16:25:21 +05:30
return LatLngBounds(LatLng(x1!, y1!), LatLng(x0!, y0!));
}
2022-06-14 14:37:59 +05:30
2023-09-04 22:46:53 +05:30
void login(String email, String password, BuildContext context) {
AuthService.login(email, password).then((value) {
2024-04-05 12:32:58 +09:00
print("------- logged in user details ######## $value ###### --------");
2023-09-04 22:46:53 +05:30
if (value.isNotEmpty) {
Navigator.pop(context);
2024-04-05 12:32:58 +09:00
print("--------- user details login ----- $value");
2023-09-04 22:46:53 +05:30
changeUser(value);
} else {
2023-10-06 16:25:21 +05:30
isLoading.value = false;
2022-06-29 18:25:19 +05:30
Get.snackbar(
2024-04-03 21:39:12 +09:00
"ログイン失敗",
"ログインIDかパスワードを確認して下さい。",
2023-09-04 22:46:53 +05:30
icon: const Icon(Icons.error, size: 40.0, color: Colors.blue),
snackPosition: SnackPosition.TOP,
2024-04-03 21:39:12 +09:00
duration: const Duration(seconds: 3),
2023-09-04 22:46:53 +05:30
backgroundColor: Colors.yellow,
//icon:Image(image:AssetImage("assets/images/dora.png"))
);
}
});
}
2022-05-12 02:17:08 +05:30
2023-10-06 16:25:21 +05:30
void changePassword(
String oldpassword, String newpassword, BuildContext context) {
String token = currentUser[0]['token'];
////print("------- change password ######## ${currentUser[0]['token']} ###### --------");
AuthService.changePassword(oldpassword, newpassword, token).then((value) {
////print("------- change password ######## $value ###### --------");
if (value.isNotEmpty) {
isLoading.value = false;
2022-10-12 21:46:17 +05:30
Navigator.pop(context);
2023-10-06 16:25:21 +05:30
if (rogMode.value == 1) {
2022-10-12 21:46:17 +05:30
switchPage(AppPages.TRAVEL);
2023-10-06 16:25:21 +05:30
} else {
2023-10-08 19:13:14 +05:30
switchPage(AppPages.INDEX);
2022-10-12 21:46:17 +05:30
}
2023-10-06 16:25:21 +05:30
} else {
2022-10-12 21:46:17 +05:30
Get.snackbar(
2023-10-06 16:25:21 +05:30
'failed'.tr,
'password_change_failed_please_try_again'.tr,
icon: const Icon(Icons.error, size: 40.0, color: Colors.blue),
snackPosition: SnackPosition.TOP,
duration: const Duration(milliseconds: 800),
backgroundColor: Colors.yellow,
//icon:Image(image:AssetImage("assets/images/dora.png"))
);
}
});
isLoading.value = false;
}
void logout() async {
2022-09-22 20:36:56 +05:30
locations.clear();
DatabaseHelper db = DatabaseHelper.instance;
2023-10-06 16:25:21 +05:30
db.deleteAllDestinations().then((value) {
DestinationController destinationController =
Get.find<DestinationController>();
destinationController.populateDestinations();
2022-09-22 20:36:56 +05:30
});
currentUser.clear();
2022-10-12 21:46:17 +05:30
cats.clear();
2023-10-06 16:25:21 +05:30
}
2022-09-22 20:36:56 +05:30
2023-10-06 16:25:21 +05:30
void register(String email, String password, BuildContext context) {
AuthService.register(email, password).then((value) {
if (value.isNotEmpty) {
2022-05-12 02:17:08 +05:30
currentUser.clear();
currentUser.add(value);
2023-10-06 16:25:21 +05:30
isLoading.value = false;
2022-05-12 02:17:08 +05:30
Navigator.pop(context);
2023-10-08 19:13:14 +05:30
Get.toNamed(AppPages.INDEX);
2023-10-06 16:25:21 +05:30
} else {
isLoading.value = false;
Get.snackbar(
'failed'.tr,
'user_registration_failed_please_try_again'.tr,
icon: const Icon(Icons.error, size: 40.0, color: Colors.blue),
snackPosition: SnackPosition.TOP,
duration: const Duration(milliseconds: 800),
backgroundColor: Colors.yellow,
//icon:Image(image:AssetImage("assets/images/dora.png"))
);
2022-10-12 21:46:17 +05:30
}
2022-06-14 14:37:59 +05:30
});
}
2023-09-04 22:46:53 +05:30
void saveToDevice(String val) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString("user_token", val);
2024-04-05 12:32:58 +09:00
print("saveToDevice: ${val}");
2023-09-04 22:46:53 +05:30
}
void changeUser(Map<String, dynamic> value, {bool replace = true}) {
2024-04-05 12:32:58 +09:00
print("---- change user to $value -----");
2023-09-04 22:46:53 +05:30
currentUser.clear();
currentUser.add(value);
2023-10-06 16:25:21 +05:30
if (replace) {
2023-09-04 22:46:53 +05:30
saveToDevice(currentUser[0]["token"]);
}
2023-10-06 16:25:21 +05:30
isLoading.value = false;
2023-09-04 22:46:53 +05:30
loadLocationsBound();
if (currentUser.isNotEmpty) {
2023-10-06 16:25:21 +05:30
rogMode.value = 0;
2023-09-04 22:46:53 +05:30
} else {
2023-10-06 16:25:21 +05:30
rogMode.value = 1;
2023-09-04 22:46:53 +05:30
}
2024-04-05 12:32:58 +09:00
print('--- c rog mode --- ${rogMode.value}');
2023-10-08 19:13:14 +05:30
Get.toNamed(AppPages.INDEX);
2023-09-04 22:46:53 +05:30
}
loadUserDetailsForToken(String token) async {
2023-10-06 16:25:21 +05:30
AuthService.userForToken(token).then((value) {
2024-04-05 12:32:58 +09:00
print("----token val-- $value ------");
2023-10-06 16:25:21 +05:30
if (value![0]["user"].isEmpty) {
Get.toNamed(AppPages.LOGIN);
return;
2022-04-17 11:45:21 +05:30
}
2023-10-06 16:25:21 +05:30
changeUser(value[0], replace: false);
2022-06-06 21:15:58 +05:30
});
}
2024-04-03 21:39:12 +09:00
/*
2023-10-06 16:25:21 +05:30
void loadLocationsBound() {
if (isCustomAreaSelected.value == true) {
2022-07-28 19:01:45 +05:30
return;
}
2022-09-01 19:14:18 +05:30
locations.clear();
2022-05-18 19:09:26 +05:30
String cat = currentCat.isNotEmpty ? currentCat[0] : "";
2023-10-06 16:25:21 +05:30
if (currentCat.isNotEmpty && currentCat[0] == "-all-") {
2022-09-01 19:14:18 +05:30
cat = "";
}
2023-08-16 14:53:32 +05:30
LatLngBounds bounds = mapController.bounds!;
2022-06-29 18:25:19 +05:30
currentBound.clear();
currentBound.add(bounds);
2023-10-06 16:25:21 +05:30
////print(currentCat);
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) {
2022-05-18 19:09:26 +05:30
return;
}
2023-12-06 11:16:17 +05:30
if (value.features.isEmpty) {
2023-10-06 16:25:21 +05:30
if (showPopup == false) {
2022-07-10 23:50:43 +05:30
return;
}
2022-06-27 12:15:54 +05:30
Get.snackbar(
2023-10-06 16:25:21 +05:30
"Too many Points",
"please zoom in",
icon: const Icon(Icons.assistant_photo_outlined,
size: 40.0, color: Colors.blue),
snackPosition: SnackPosition.TOP,
2024-04-03 21:39:12 +09:00
duration: const Duration(seconds: 2),
2023-10-06 16:25:21 +05:30
backgroundColor: Colors.yellow,
//icon:Image(image:AssetImage("assets/images/dora.png"))
);
2022-07-10 23:50:43 +05:30
showPopup = false;
2022-06-27 12:15:54 +05:30
//Get.showSnackbar(GetSnackBar(message: "Too many points, please zoom in",));
2022-05-18 19:09:26 +05:30
}
2023-12-06 11:16:17 +05:30
if (value.features.isNotEmpty) {
2023-10-06 16:25:21 +05:30
////print("---- added---");
locations.add(value);
}
});
2022-05-18 19:09:26 +05:30
}
2024-04-03 21:39:12 +09:00
*/
// 2024-04-03 Akira .. Update the code . See ticket 2800.
//
void loadLocationsBound() {
if (isCustomAreaSelected.value == true) {
return;
}
locations.clear();
String cat = currentCat.isNotEmpty ? currentCat[0] : "";
if (currentCat.isNotEmpty && currentCat[0] == "-all-") {
cat = "";
}
LatLngBounds bounds = mapController.bounds!;
currentBound.clear();
currentBound.add(bounds);
isLoading.value = true; // ローディング状態をtrueに設定
2024-04-05 12:32:58 +09:00
Future.delayed(const Duration(seconds: 1), () async {
//print("bounds --- (${bounds.southWest.latitude},${bounds.southWest.longitude}),(${bounds.northWest.latitude},${bounds.northWest.longitude}),(${bounds.northEast.latitude},${bounds.northEast.longitude}),(${bounds.southEast.latitude},${bounds.southEast.longitude})");
var value = await 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
);
if ( value == null ) {
// エラーハンドリング: APIからのレスポンスがnullの場合
/*
Get.snackbar(
"Error",
"Failed to load locations. Please try again. === 1",
icon: const Icon(Icons.error, size: 40.0, color: Colors.red),
snackPosition: SnackPosition.TOP,
duration: const Duration(seconds: 3),
backgroundColor: Colors.yellow,
);
*/
// APIからのレスポンスがnullの場合
DestinationController destinationController = Get.find<DestinationController>(); // 追加
final tk = currentUser[0]["token"]; // 追加
if (tk != null) { // 追加
destinationController.fixMapBound(tk); // 追加
} // 追加
// return;
2024-04-03 21:39:12 +09:00
}
isLoading.value = false; // ローディング状態をfalseに設定
2024-04-05 12:32:58 +09:00
if (value!=null && value.features.isEmpty) {
if (showPopup == false) {
return;
}
Get.snackbar(
"Too many Points",
"please zoom in",
icon: const Icon(Icons.assistant_photo_outlined, size: 40.0, color: Colors.blue),
snackPosition: SnackPosition.TOP,
duration: const Duration(seconds: 3),
backgroundColor: Colors.yellow,
);
showPopup = false;
}
if (value!=null && value.features.isNotEmpty) {
locations.add(value);
}
2024-04-03 21:39:12 +09:00
});
}
2022-05-18 19:09:26 +05:30
2023-10-06 16:25:21 +05:30
void setBound(LatLngBounds bounds) {
2022-04-17 11:45:21 +05:30
currentBound.clear();
currentBound.add(bounds);
}
2023-12-06 11:16:17 +05:30
GeoJSONFeature? getFeatureForLatLong(double lat, double long) {
2023-10-06 16:25:21 +05:30
if (locations.isNotEmpty) {
2023-12-06 11:16:17 +05:30
GeoJSONFeature? foundFeature;
locations[0].features.forEach((i) {
GeoJSONMultiPoint p = i!.geometry as GeoJSONMultiPoint;
if (p.coordinates[0][1] == lat && p.coordinates[0][0] == long) {
foundFeature = i;
2022-07-09 22:51:34 +05:30
}
2023-12-06 11:16:17 +05:30
});
return foundFeature;
2022-04-17 11:45:21 +05:30
}
2023-10-06 16:25:21 +05:30
return null;
2022-04-17 11:45:21 +05:30
}
2023-10-06 16:25:21 +05:30
}