125 lines
4.6 KiB
Dart
125 lines
4.6 KiB
Dart
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);
|
|
}
|
|
}
|
|
|
|
}
|