Skip to content

Commit

Permalink
Merge pull request #5 from natsuk4ze/Add-CompassXException
Browse files Browse the repository at this point in the history
Add CompassXException
  • Loading branch information
natsuk4ze authored Mar 27, 2024
2 parents d6770f5 + e979b93 commit eece9c3
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,17 @@ Request permission to get true heading in Android. Not required on iOS.
$ flutter pub add permission_handler
```
Specify the permissions one or both of the following in *AndroidManifest.xml*.
It can be copied from [exmaple](https://github.com/natsuk4ze/compassx/blob/main/example/android/app/src/main/AndroidManifest.xml).
It can be copied from [example](https://github.com/natsuk4ze/compassx/blob/main/example/android/app/src/main/AndroidManifest.xml).
```xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
```
- `ACCESS_COARSE_LOCATION`: Used when normal accuracy is required.
- `ACCESS_FINE_LOCATION`: Used when the highest quality accuracy is required.

Add code to request premissions.
Add code to request permissions.
```dart
if (!Platform.isAndroid) return;
await Permission.location.request();
if (Platform.isAndroid) await Permission.location.request();
```

## Precautions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public void onListen(Object arguments, EventChannel.EventSink events) {
} else if (rotationVectorSensor != null) {
sensorManager.registerListener(sensorEventListener, rotationVectorSensor,
SensorManager.SENSOR_DELAY_GAME);
} else {
events.error("SENSOR_NOT_FOUND", "No compass sensor found.", null);
events.endOfStream();
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/src/compassx.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ final class CompassX {
const CompassX._();

/// [CompassXEvent] stream for using the compass sensor.
///
/// Throws [CompassXException] for older or excessively cheap Android devices
/// that do not have a compass sensor.
static Stream<CompassXEvent> get events => CompassXPlatform.events;
}
51 changes: 51 additions & 0 deletions lib/src/compassx_exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

@immutable
final class CompassXException implements Exception {
const CompassXException._({
required this.type,
required this.platformException,
required this.stackTrace,
});

final CompassXExceptionType type;
final PlatformException platformException;
final StackTrace stackTrace;

factory CompassXException.fromPlatformException({
required PlatformException platformException,
required StackTrace stackTrace,
}) =>
CompassXException._(
type: CompassXExceptionType.values.firstWhere(
(type) => type.code == platformException.code,
orElse: () => CompassXExceptionType.unexpected,
),
platformException: platformException,
stackTrace: stackTrace,
);

@override
String toString() => "[CompassXException/${type.code}]: ${type.message}";
}

enum CompassXExceptionType {
/// This is used for older or excessively cheap Android devices
/// that do not have a compass sensor.
sensorNotFound,

/// This is used when an unexpected error occurs.
/// See [PlatformException] for more information.
unexpected;

String get code => switch (this) {
sensorNotFound => 'SENSOR_NOT_FOUND',
unexpected => 'UNEXPECTED',
};

String get message => switch (this) {
sensorNotFound => 'Compass sensor not found.',
unexpected => 'An unexpected error has occurred.',
};
}
10 changes: 9 additions & 1 deletion lib/src/compassx_platform.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:compassx/src/compassx_event.dart';
import 'package:compassx/src/compassx_exception.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

Expand All @@ -14,5 +15,12 @@ final class CompassXPlatform {
static final _stream = _channel.receiveBroadcastStream();

/// CompassXEvent stream for [CompassX].
static Stream<CompassXEvent> get events => _stream.map(CompassXEvent.fromMap);
static Stream<CompassXEvent> get events =>
_stream.map(CompassXEvent.fromMap).handleError(
(e, st) => throw CompassXException.fromPlatformException(
platformException: e,
stackTrace: st,
),
test: (e) => e is PlatformException,
);
}

0 comments on commit eece9c3

Please sign in to comment.