diff --git a/geolocator_platform_interface/CHANGELOG.md b/geolocator_platform_interface/CHANGELOG.md index a7d475370..67474e0cc 100644 --- a/geolocator_platform_interface/CHANGELOG.md +++ b/geolocator_platform_interface/CHANGELOG.md @@ -1,5 +1,6 @@ -## NEXT +## 4.2.7 +- Adds `updatePositionStream` API. - Updated `flutter_lints` to version `^5.0.0`. ## 4.2.6 diff --git a/geolocator_platform_interface/lib/src/errors/errors.dart b/geolocator_platform_interface/lib/src/errors/errors.dart index 3bbfcfe57..c04688289 100644 --- a/geolocator_platform_interface/lib/src/errors/errors.dart +++ b/geolocator_platform_interface/lib/src/errors/errors.dart @@ -2,6 +2,7 @@ export 'activity_missing_exception.dart'; export 'already_subscribed_exception.dart'; export 'invalid_permission_exception.dart'; export 'location_service_disabled_exception.dart'; +export 'not_subscribed_exception.dart'; export 'permission_definitions_not_found_exception.dart'; export 'permission_denied_exception.dart'; export 'permission_request_in_progress_exception.dart'; diff --git a/geolocator_platform_interface/lib/src/errors/not_subscribed_exception.dart b/geolocator_platform_interface/lib/src/errors/not_subscribed_exception.dart new file mode 100644 index 000000000..5c5936731 --- /dev/null +++ b/geolocator_platform_interface/lib/src/errors/not_subscribed_exception.dart @@ -0,0 +1,12 @@ +/// An exception thrown when trying to update location stream settings without +/// having an active subscription to the location stream. +class NotSubscribedException implements Exception { + /// Constructs the [NotSubscribedException] + const NotSubscribedException(); + + @override + String toString() => + 'The App is not listening to a stream of position updates and thus it is' + 'not possible to update location stream settings. Call ' + 'getPositionStream() first.'; +} diff --git a/geolocator_platform_interface/lib/src/geolocator_platform_interface.dart b/geolocator_platform_interface/lib/src/geolocator_platform_interface.dart index e16e8ff08..8c557de56 100644 --- a/geolocator_platform_interface/lib/src/geolocator_platform_interface.dart +++ b/geolocator_platform_interface/lib/src/geolocator_platform_interface.dart @@ -172,6 +172,17 @@ abstract class GeolocatorPlatform extends PlatformInterface { throw UnimplementedError('getPositionStream() has not been implemented.'); } + /// Updates the parameters of the active position stream. + /// + /// Throws a [NotSubscribedException] when there is no active position stream + /// to update. + Future updatePositionStream({ + required LocationSettings locationSettings, + }) { + throw UnimplementedError( + 'updatePositionStream() has not been implemented.'); + } + /// Asks the user for Temporary Precise location access (iOS 14 or above). /// /// Returns [LocationAccuracyStatus.precise] if the user already gave diff --git a/geolocator_platform_interface/lib/src/implementations/method_channel_geolocator.dart b/geolocator_platform_interface/lib/src/implementations/method_channel_geolocator.dart index fefc185e8..346f8b28b 100644 --- a/geolocator_platform_interface/lib/src/implementations/method_channel_geolocator.dart +++ b/geolocator_platform_interface/lib/src/implementations/method_channel_geolocator.dart @@ -6,8 +6,8 @@ import '../enums/enums.dart'; import '../errors/errors.dart'; import '../extensions/extensions.dart'; import '../geolocator_platform_interface.dart'; -import '../models/position.dart'; import '../models/location_settings.dart'; +import '../models/position.dart'; /// An implementation of [GeolocatorPlatform] that uses method channels. class MethodChannelGeolocator extends GeolocatorPlatform { @@ -191,6 +191,22 @@ class MethodChannelGeolocator extends GeolocatorPlatform { return _positionStream!; } + @override + Future updatePositionStream( + {required LocationSettings locationSettings}) async { + if (_positionStream == null) { + throw const NotSubscribedException(); + } + try { + await _methodChannel.invokeMethod( + 'updatePositionStream', locationSettings.toJson()); + } on PlatformException catch (e) { + final error = _handlePlatformException(e); + + throw error; + } + } + Stream _wrapStream(Stream incoming) { return incoming.asBroadcastStream(onCancel: (subscription) { subscription.cancel(); @@ -234,6 +250,8 @@ class MethodChannelGeolocator extends GeolocatorPlatform { return const LocationServiceDisabledException(); case 'LOCATION_SUBSCRIPTION_ACTIVE': return const AlreadySubscribedException(); + case 'LOCATION_SUBSCRIPTION_INACTIVE': + return const NotSubscribedException(); case 'PERMISSION_DEFINITIONS_NOT_FOUND': return PermissionDefinitionsNotFoundException(exception.message); case 'PERMISSION_DENIED': diff --git a/geolocator_platform_interface/pubspec.yaml b/geolocator_platform_interface/pubspec.yaml index 152201786..f14d78df6 100644 --- a/geolocator_platform_interface/pubspec.yaml +++ b/geolocator_platform_interface/pubspec.yaml @@ -3,23 +3,22 @@ description: A common platform interface for the geolocator plugin. repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_platform_interface # NOTE: We strongly prefer non-breaking changes, even at the expense of a # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes -version: 4.2.6 - +version: 4.2.7 dependencies: flutter: sdk: flutter - - plugin_platform_interface: ^2.1.6 + + plugin_platform_interface: ^2.1.8 vector_math: ^2.1.4 meta: ^1.9.1 dev_dependencies: - async: ^2.11.0 + async: ^2.12.0 flutter_test: sdk: flutter flutter_lints: ^5.0.0 - mockito: ^5.4.2 + mockito: ^5.6.1 environment: sdk: ^3.5.0 diff --git a/geolocator_platform_interface/test/geolocator_platform_interface_test.dart b/geolocator_platform_interface/test/geolocator_platform_interface_test.dart index 587a87631..8fab16091 100644 --- a/geolocator_platform_interface/test/geolocator_platform_interface_test.dart +++ b/geolocator_platform_interface/test/geolocator_platform_interface_test.dart @@ -165,6 +165,21 @@ void main() { ); }); + test( + // ignore: lines_longer_than_80_chars + 'Default implementation of updatePositionStream should throw unimplemented error', + () { + // Arrange + final geolocatorPlatform = ExtendsGeolocatorPlatform(); + + // Act & Assert + expect( + () => geolocatorPlatform.updatePositionStream( + locationSettings: LocationSettings()), + throwsUnimplementedError, + ); + }); + test( // ignore: lines_longer_than_80_chars 'Default implementation of openAppSettings should throw unimplemented error',