diff --git a/lib/pages/analytics/services/device_location/reverse_geocode_device_location_service_decorator.dart b/lib/pages/analytics/services/device_location/reverse_geocode_device_location_service_decorator.dart new file mode 100644 index 00000000..a3ac1e55 --- /dev/null +++ b/lib/pages/analytics/services/device_location/reverse_geocode_device_location_service_decorator.dart @@ -0,0 +1,40 @@ +import 'package:geocoding/geocoding.dart'; +import 'package:syncrow_web/pages/analytics/models/device_location_info.dart'; +import 'package:syncrow_web/pages/analytics/params/get_device_location_data_param.dart'; +import 'package:syncrow_web/pages/analytics/services/device_location/device_location_service.dart'; + +class ReverseGeocodeDeviceLocationServiceDecorator implements DeviceLocationService { + const ReverseGeocodeDeviceLocationServiceDecorator(this._decoratee); + + final DeviceLocationService _decoratee; + + @override + Future get(GetDeviceLocationDataParam param) async { + try { + final deviceLocationInfo = await _decoratee.get(param); + + final placemarks = await placemarkFromCoordinates( + param.latitude, + param.longitude, + ); + + if (placemarks.isNotEmpty) { + final place = placemarks.first; + + final city = place.locality; + final country = place.country; + final address = place.street; + + return deviceLocationInfo.copyWith( + city: city, + country: country, + address: address, + ); + } + + return deviceLocationInfo; + } catch (e) { + throw Exception('Failed to reverse load device location info'); + } + } +}