Skip to content

Commit

Permalink
feat(map): camera is now moving
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTexanCodeur committed Apr 30, 2024
1 parent f51d820 commit c82a3a2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 24 deletions.
41 changes: 41 additions & 0 deletions lib/viewmodels/map_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import "dart:async";
import "package:flutter/material.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/models/ui/map_info.dart";
import "package:proxima/viewmodels/home_view_model.dart";
import "package:proxima/views/option_widgets/map/map_selection_option.dart";

// TODO: For now, this is code with mock data.
Expand All @@ -15,6 +17,45 @@ class MapViewModel extends AsyncNotifier<MapInfo> {
);
}

//set of circles drawn on the map
final Set<Circle> _circles = {};

//getter for the circles
Set<Circle> get circles => _circles;

Future<void> redrawCircle(LatLng target) async {
_circles.clear();
_circles.add(
Circle(
circleId: const CircleId("1"),
center: target,
radius: HomeViewModel.kmPostRadius * 1000,
fillColor: Colors.black26,
strokeWidth: 0,
),
);
}

final Completer<GoogleMapController> _mapController = Completer();

void onMapCreated(GoogleMapController controller) {
if (_mapController.isCompleted) return;
_mapController.complete(controller);
}

Future<void> animateCamera(LatLng target) async {
//avoid .future getter on a completed future
final controller = await _mapController.future;
controller.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: target,
zoom: 17.5,
),
),
);
}

/// Refresh the mapInfo.
/// This will put the state of the viewmodel to loading
/// and update the state accordingly
Expand Down
29 changes: 6 additions & 23 deletions lib/views/home_content/map/post_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/models/ui/map_info.dart";
import "package:proxima/services/geolocation_service.dart";
import "package:proxima/viewmodels/home_view_model.dart";
import "package:proxima/viewmodels/map_view_model.dart";

class PostMap extends ConsumerWidget {
final MapInfo mapInfo;
Expand All @@ -18,21 +19,13 @@ class PostMap extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final currentPosition = ref.watch(liveLocationServiceProvider);

GoogleMapController? mapController;
final positionNotifier = ref.watch(mapProvider.notifier);

currentPosition.when(
data: (data) {
debugPrint("Live location: ${data!.latitude}, ${data.longitude}");
if (mapController == null) return;
mapController!.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(data.latitude, data.longitude),
zoom: 17.5,
),
),
);
positionNotifier.animateCamera(LatLng(data.latitude, data.longitude));
positionNotifier.redrawCircle(LatLng(data.latitude, data.longitude));
},
error: (error, _) {
throw Exception("Live location error: $error");
Expand All @@ -55,18 +48,8 @@ class PostMap extends ConsumerWidget {
target: mapInfo.currentLocation,
zoom: 17.5,
),
circles: {
Circle(
circleId: const CircleId("1"),
center: mapInfo.currentLocation,
radius: HomeViewModel.kmPostRadius * 1000,
fillColor: Colors.black26,
strokeWidth: 0,
),
},
onMapCreated: (GoogleMapController controller) {
mapController = controller;
},
circles: positionNotifier.circles,
onMapCreated: positionNotifier.onMapCreated,
),
);
}
Expand Down
16 changes: 15 additions & 1 deletion test/mocks/overrides/override_map_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
import "package:flutter_test/flutter_test.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/models/ui/map_info.dart";
import "package:proxima/viewmodels/map_view_model.dart";

class MockMapViewModel extends AsyncNotifier<MapInfo> implements MapViewModel {
final Future<MapInfo> Function() _build;
final Future<void> Function() _onRefresh;
final Future<void> Function(LatLng) _animateCamera;
final void Function(GoogleMapController) _onMapCreated;

MockMapViewModel({
Future<MapInfo> Function()? build,
Future<void> Function()? onRefresh,
Future<void> Function(LatLng)? animateCamera,
void Function(GoogleMapController)? onMapCreated,
}) : _build = build ??
(() async => throw Exception("Location services are disabled.")),
_onRefresh = onRefresh ?? (() async {});
_onRefresh = onRefresh ?? (() async {}),
_animateCamera = animateCamera ?? ((_) async {}),
_onMapCreated = onMapCreated ?? ((_) {});

@override
Future<MapInfo> build() => _build();

@override
Future<void> refresh() => _onRefresh();

@override
Future<void> animateCamera(LatLng target) => _animateCamera(target);

@override
void onMapCreated(GoogleMapController controller) =>
_onMapCreated(controller);
}

final mockNoGPSMapViewModelOverride = [
Expand Down

0 comments on commit c82a3a2

Please sign in to comment.