import 'dart:convert'; import 'dart:io'; import 'dart:isolate'; import 'dart:ui'; import 'package:diplomaticquarterapp/config/shared_pref_kay.dart'; import 'package:diplomaticquarterapp/core/model/geofencing/responses/GeoZonesResponseModel.dart'; import 'package:diplomaticquarterapp/uitl/LocalNotification.dart'; import 'package:diplomaticquarterapp/uitl/app_shared_preferences.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:geofencing/geofencing.dart'; class HMG_Geofence { static var _isolatePortName = "hmg_geofencing_send_port"; List _zones; List registeredGeofences = []; final AndroidGeofencingSettings androidSettings = AndroidGeofencingSettings(initialTrigger: [GeofenceEvent.enter, GeofenceEvent.exit, GeofenceEvent.dwell], loiteringDelay: 1000 * 60); final BuildContext context; final List triggers = List(); ReceivePort port = ReceivePort(); HMG_Geofence(this.context) { triggers.add(GeofenceEvent.enter); triggers.add(GeofenceEvent.exit); // triggers.add(GeofenceEvent.dwell); } void initiate(List zones) async { _zones = zones; if (kDebugMode) { // debug check addTestingGeofences(); } _saveZones(); await GeofencingManager.initialize(); await Future.delayed(Duration(seconds: 2)); _registerIsolatePort(); _registerGeofences().then((value) { debugPrint(value.toString()); }); } void _saveZones() { var list = List(); _zones.forEach((element) { list.add(element.toJson()); }); var jsonString = jsonEncode(list); AppSharedPreferences pref = AppSharedPreferences(); pref.setString(HMG_GEOFENCES, jsonString); } Future> _registerGeofences() async { registeredGeofences = await GeofencingManager.getRegisteredGeofenceIds(); var maxLimit = Platform.isIOS ? 20 : 100; if (registeredGeofences.length < maxLimit) { var notRegistered = _zones.where((element) => !(registeredGeofences.contains(element.geofenceId()))).toList(); for (int i = 0; i < notRegistered.length; i++) { var zone = notRegistered.elementAt(i); var lat = double.tryParse(zone.latitude); var lon = double.tryParse(zone.longitude); var rad = double.tryParse(zone.radius.toString()); if (lat != null || lon != null || rad != null) { await GeofencingManager.registerGeofence(GeofenceRegion(zone.geofenceId(), lat, lon, rad, triggers), transitionTrigger); registeredGeofences.add(zone.geofenceId()); if (registeredGeofences.length >= maxLimit) { break; } await Future.delayed(Duration(milliseconds: 100)); debugPrint("Geofence: ${zone.description} registered"); } else { debugPrint("Geofence: ${zone.description} registered"); } } } return registeredGeofences; } void addTestingGeofences() { _zones.add(GeoZonesResponseModel.get("24.777577,46.652675", 150, "msH")); _zones.add(GeoZonesResponseModel.get("24.691136,46.650116", 150, "zkH")); _zones.add(GeoZonesResponseModel.get("24.7087913,46.6656461", 150, "csO")); } static void transitionTrigger(List id, Location location, GeofenceEvent event) async { id.forEach((element) { final SendPort send = IsolateNameServer.lookupPortByName(_isolatePortName); send?.send(event.toString()); }); } void _registerIsolatePort() { IsolateNameServer.registerPortWithName(port.sendPort, _isolatePortName); AppSharedPreferences pref = AppSharedPreferences(); var jsonString = pref.getString(HMG_GEOFENCES); List jsonList = json.decode(jsonString); jsonList.forEach((element) { }) port.listen((dynamic data) { LocalNotification.showNow( title: "HMG-Geofence", subtitle: element, onClickNotification: (payload) { debugPrint(payload); }, payload: "Zohaib Kambrani"); }); } }