Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat realtime position stream #22

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions kuama_position/lib/src/blocs/position_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ class PositionBloc extends Bloc<_PositionBlocEvent, PositionBlocState> {
/// There is no need to call it if you weren't listening to the realtime position.
void unTrack() => add(const _UnTrackPositionBloc());

/// !!! WARNING !!! It is not stable. It could get breakages
late Stream<GeoPoint> onRealtimePositionChanges = _createRealtimePositionStream();

Stream<GeoPoint> _createRealtimePositionStream() {
return Stream<GeoPoint>.multi((controller) {
final subscription = stream.listen((state) {
if (state is PositionBlocLocated) controller.addSync(state.currentPosition);
});
track();
controller.onCancel = () async {
unTrack();
await subscription.cancel();
controller.closeSync();
};
});
}

Future<void> _mapEventToState(_PositionBlocEvent event, Emitter<PositionBlocState> emit) async {
return event.map<Future<void>>(updateStatus: (event) {
return _mapStatusUpdate(emit, event.hasPermissionGranted, event.isServiceEnabled);
Expand Down
32 changes: 32 additions & 0 deletions kuama_position/test/position_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,36 @@ void main() {
await bloc.close();
});
});

// TODO: Test realtimePosition method
// group('realtimePosition', () {
// test('1', () async {
// init(permission: EmissionType.alreadyHas, service: EmissionType.alreadyHas);
// bloc.emit(const PositionBlocIdle(
// lastPosition: null,
// hasPermission: true,
// isServiceEnabled: true,
// ));
//
// when(() => mockPositionService.getCurrentPosition()).thenAnswer((_) async {
// return const GeoPoint(0.0, 0.0);
// });
// when(() => mockPositionService.onPositionChanges).thenAnswer((_) async* {
// yield const GeoPoint(0.0, 0.0);
// });
//
// final sub1 = bloc.onRealtimePositionChanges.listen((event) {
// print('Listen');
// });
//
// final sub2 = bloc.onRealtimePositionChanges.listen((event) {
// print('Listen');
// });
//
// await Future.delayed(const Duration(seconds: 1), () {
// // sub1.cancel();
// sub2.cancel();
// });
// });
// });
}