Skip to content

Commit

Permalink
refactor(navigation-to-map): use LatLng instead of GeoPoint in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Aderfish committed May 29, 2024
1 parent fcce0c8 commit 7f2b06b
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 49 deletions.
6 changes: 3 additions & 3 deletions lib/models/ui/challenge_details.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "package:cloud_firestore/cloud_firestore.dart";
import "package:flutter/foundation.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";

/// This class was hard to design. We considered multiple possible implementations,
/// and decided to use this one. If we add a parameter requiring to double the number of
/// constructors once more, it should be changed. Here are all the possibilities:
/// 1) Use one constructor per possibility, storing unused parameter as null internally (it is
/// the possibility we chose here).
/// 2) Use a single constructor, asking the developer instanciating it to set the correct
/// 2) Use a single constructor, asking the developer instantiating it to set the correct
/// parameters to null (a null distance to finish the challenge, for instance).
/// 3) Use a single constructor, but with additional boolean parameters to specify if the
/// class is a group challenge or if it is finished. This requires asserts to check that
Expand All @@ -19,7 +19,7 @@ class ChallengeDetails {
final int? distance;
final int? timeLeft;
final int reward;
final GeoPoint location;
final LatLng location;

/// Creates a [ChallengeDetails] with the given parameters. The [title] is the
/// post's title, the [distance] is the distance to the challenge in meters, the [timeLeft]
Expand Down
7 changes: 4 additions & 3 deletions lib/models/ui/post_details.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import "package:cloud_firestore/cloud_firestore.dart";
import "package:flutter/foundation.dart";
import "package:geoflutterfire_plus/geoflutterfire_plus.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";
import "package:proxima/models/database/post/post_firestore.dart";
import "package:proxima/models/database/post/post_id_firestore.dart";
import "package:proxima/models/database/user/user_firestore.dart";
import "package:proxima/models/database/user/user_id_firestore.dart";
import "package:proxima/utils/extensions/geopoint_extensions.dart";

@immutable

Expand All @@ -22,7 +23,7 @@ class PostDetails {
final DateTime publicationDate;
final int distance; // in meters
final bool isChallenge;
final GeoPoint location;
final LatLng location;

const PostDetails({
required this.postId,
Expand Down Expand Up @@ -102,7 +103,7 @@ class PostDetails {
) *
1000)
.round(),
location: postFirestore.location.geoPoint,
location: postFirestore.location.geoPoint.toLatLng(),
isChallenge: isChallenge,
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/models/ui/user_post_details.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "package:cloud_firestore/cloud_firestore.dart";
import "package:flutter/foundation.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";
import "package:proxima/models/database/post/post_id_firestore.dart";

/// A post that belongs to the current logged in user.
Expand All @@ -9,7 +9,7 @@ class UserPostDetails {
final PostIdFirestore postId;
final String title;
final String description;
final GeoPoint location;
final LatLng location;

const UserPostDetails({
required this.postId,
Expand Down
5 changes: 3 additions & 2 deletions lib/viewmodels/challenge_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "package:proxima/models/ui/challenge_details.dart";
import "package:proxima/services/database/challenge_repository_service.dart";
import "package:proxima/services/database/post_repository_service.dart";
import "package:proxima/services/sensors/geolocation_service.dart";
import "package:proxima/utils/extensions/geopoint_extensions.dart";
import "package:proxima/viewmodels/dynamic_user_avatar_view_model.dart";
import "package:proxima/viewmodels/login_view_model.dart";
import "package:proxima/viewmodels/map/map_pin_view_model.dart";
Expand Down Expand Up @@ -49,14 +50,14 @@ class ChallengeViewModel
distance: distanceM,
timeLeft: timeLeft.inHours,
reward: ChallengeRepositoryService.soloChallengeReward,
location: post.location.geoPoint,
location: post.location.geoPoint.toLatLng(),
);
} else {
return ChallengeDetails.soloFinished(
title: post.data.title,
timeLeft: timeLeft.inHours,
reward: ChallengeRepositoryService.soloChallengeReward,
location: post.location.geoPoint,
location: post.location.geoPoint.toLatLng(),
);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class SelectedPageViewModel extends OptionsViewModel<SelectedPageDetails> {
void setOption(SelectedPageDetails option) {
if (option.route.routeDestination != null) {
throw Exception(
"This page should be pushed and not set as the selected page, push it directly from your context instead.");
"This page should be pushed and not set as the selected page, push it directly from your context instead.",
);
}
super.setOption(option);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/viewmodels/user_posts_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/models/database/post/post_id_firestore.dart";
import "package:proxima/models/ui/user_post_details.dart";
import "package:proxima/services/database/post_repository_service.dart";
import "package:proxima/utils/extensions/geopoint_extensions.dart";
import "package:proxima/viewmodels/login_view_model.dart";
import "package:proxima/viewmodels/map/map_pin_view_model.dart";
import "package:proxima/viewmodels/posts_feed_view_model.dart";
Expand Down Expand Up @@ -33,7 +34,7 @@ class UserPostsViewModel extends AutoDisposeAsyncNotifier<UserPostsState> {
postId: post.id,
title: post.data.title,
description: post.data.description,
location: post.location.geoPoint,
location: post.location.geoPoint.toLatLng(),
);

return userPost;
Expand Down
9 changes: 4 additions & 5 deletions lib/views/navigation/map_action.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import "package:cloud_firestore/cloud_firestore.dart";
import "package:flutter/material.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:proxima/utils/extensions/geopoint_extensions.dart";
import "package:proxima/viewmodels/option_selection/map_selection_options_view_model.dart";
import "package:proxima/viewmodels/option_selection/selected_page_view_model.dart";
import "package:proxima/views/components/options/map/map_selection_options.dart";
Expand All @@ -17,7 +16,7 @@ class MapAction extends ConsumerWidget {
final int depth;

final MapSelectionOptions mapOption;
final GeoPoint initialLocation;
final LatLng initialLocation;

const MapAction({
super.key,
Expand Down Expand Up @@ -50,14 +49,14 @@ class MapAction extends ConsumerWidget {
BuildContext context,
MapSelectionOptions mapOption,
int depth,
GeoPoint initialLocation,
LatLng initialLocation,
) async {
for (var i = 0; i < depth; i++) {
Navigator.pop(context);
}
ref.watch(selectedPageViewModelProvider.notifier).selectPage(
NavigationBarRoutes.map,
initialLocation.toLatLng(),
initialLocation,
);

ref.watch(mapSelectionOptionsViewModelProvider.notifier).setOption(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "package:cloud_firestore/cloud_firestore.dart";
import "package:flutter/material.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";
import "package:proxima/views/components/async/loading_icon_button.dart";
import "package:proxima/views/helpers/types/future_void_callback.dart";
import "package:proxima/views/pages/profile/components/info_cards/profile_info_pop_up.dart";
Expand All @@ -20,7 +20,7 @@ class ProfileInfoCard extends StatelessWidget {
final String content;
final FutureVoidCallback onDelete;
final String? title;
final GeoPoint? location;
final LatLng? location;

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "package:cloud_firestore/cloud_firestore.dart";
import "package:flutter/material.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";
import "package:proxima/views/components/async/loading_icon_button.dart";
import "package:proxima/views/components/content/info_pop_up.dart";
import "package:proxima/views/components/options/map/map_selection_options.dart";
Expand All @@ -19,7 +19,7 @@ class ProfileInfoPopUp extends StatelessWidget {
});

final String? title;
final GeoPoint? location;
final LatLng? location;
final String content;
final FutureVoidCallback onDelete;

Expand Down
12 changes: 6 additions & 6 deletions test/mocks/data/challenge_list.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "package:proxima/models/ui/challenge_details.dart";

import "geopoint.dart";
import "latlng.dart";

// All values are purposely different to test the UI more easily

Expand All @@ -10,31 +10,31 @@ const mockChallengeList = [
distance: 700,
timeLeft: 27,
reward: 250,
location: userPosition0,
location: latLngLocation0,
),
ChallengeDetails.solo(
title:
"John fell here lmaoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo",
distance: 3200,
timeLeft: 28,
reward: 400,
location: userPosition0,
location: latLngLocation0,
),
ChallengeDetails.group(
title: "I'm moving out",
distance: 4000,
reward: 350,
location: userPosition0,
location: latLngLocation0,
),
ChallengeDetails.soloFinished(
title: "What a view!",
timeLeft: 29,
reward: 200,
location: userPosition0,
location: latLngLocation0,
),
ChallengeDetails.groupFinished(
title: "I saw a bird here once",
reward: 50,
location: userPosition0,
location: latLngLocation0,
),
];
10 changes: 5 additions & 5 deletions test/mocks/data/post_overview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import "package:proxima/models/database/user/user_id_firestore.dart";
import "package:proxima/models/ui/post_details.dart";

import "datetime.dart";
import "geopoint.dart";
import "latlng.dart";

final List<PostDetails> testPosts = [
PostDetails(
Expand All @@ -20,7 +20,7 @@ final List<PostDetails> testPosts = [
ownerCentauriPoints: 32,
publicationDate: publicationDate1,
distance: 20,
location: userPosition1,
location: latLngLocation0,
),
PostDetails(
postId: const PostIdFirestore(value: "post_2"),
Expand All @@ -36,7 +36,7 @@ final List<PostDetails> testPosts = [
publicationDate: publicationDate1,
distance: 20,
isChallenge: true,
location: userPosition1,
location: latLngLocation0,
),
PostDetails(
postId: const PostIdFirestore(value: "post_3"),
Expand All @@ -50,7 +50,7 @@ final List<PostDetails> testPosts = [
ownerCentauriPoints: 218281828,
publicationDate: publicationDate1,
distance: 20,
location: userPosition1,
location: latLngLocation0,
),
];

Expand All @@ -67,5 +67,5 @@ final timeDistancePost = PostDetails(
ownerCentauriPoints: 911,
publicationDate: DateTime.utc(1999),
distance: 100,
location: userPosition1,
location: latLngLocation0,
);
8 changes: 4 additions & 4 deletions test/models/ui/challenge_details_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "package:flutter_test/flutter_test.dart";
import "package:proxima/models/ui/challenge_details.dart";

import "../../mocks/data/geopoint.dart";
import "../../mocks/data/latlng.dart";

void main() {
group("Challenge card data testing", () {
Expand All @@ -10,7 +10,7 @@ void main() {
title: "title",
distance: 50,
reward: 100,
location: userPosition0,
location: latLngLocation0,
);

final expectedHash = Object.hash(
Expand All @@ -31,13 +31,13 @@ void main() {
title: "title",
distance: 50,
reward: 100,
location: userPosition0,
location: latLngLocation0,
);
const challengeCardDataCopy = ChallengeDetails.group(
title: "title",
distance: 50,
reward: 100,
location: userPosition0,
location: latLngLocation0,
);

expect(challengeCardData, challengeCardDataCopy);
Expand Down
12 changes: 6 additions & 6 deletions test/models/ui/user_post_details_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "package:flutter_test/flutter_test.dart";
import "package:proxima/models/database/post/post_id_firestore.dart";
import "package:proxima/models/ui/user_post_details.dart";

import "../../mocks/data/geopoint.dart";
import "../../mocks/data/latlng.dart";

void main() {
group("User Post model testing", () {
Expand All @@ -11,7 +11,7 @@ void main() {
postId: PostIdFirestore(value: "post_1"),
title: "title",
description: "description",
location: userPosition0,
location: latLngLocation0,
);

final expectedHash = Object.hash(
Expand All @@ -31,14 +31,14 @@ void main() {
postId: PostIdFirestore(value: "post_1"),
title: "title",
description: "description",
location: userPosition0,
location: latLngLocation0,
);

const userPostCopy = UserPostDetails(
postId: PostIdFirestore(value: "post_1"),
title: "title",
description: "description",
location: userPosition0,
location: latLngLocation0,
);

expect(userPost, userPostCopy);
Expand All @@ -49,14 +49,14 @@ void main() {
postId: PostIdFirestore(value: "post_1"),
title: "title",
description: "description 1",
location: userPosition0,
location: latLngLocation0,
);

const userPost2 = UserPostDetails(
postId: PostIdFirestore(value: "post_1"),
title: "title",
description: "description 2",
location: userPosition0,
location: latLngLocation0,
);

expect(userPost1 != userPost2, true);
Expand Down
5 changes: 3 additions & 2 deletions test/viewmodels/posts_feed_view_model_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import "package:test/test.dart";

import "../mocks/data/firestore_user.dart";
import "../mocks/data/geopoint.dart";
import "../mocks/data/latlng.dart";
import "../mocks/data/post_data.dart";
import "../mocks/services/mock_geo_location_service.dart";

Expand Down Expand Up @@ -126,7 +127,7 @@ void main() {
.distanceBetweenInKm(geopoint: postPosition) *
1000)
.round(),
location: userPosition1,
location: latLngLocation0,
),
];

Expand Down Expand Up @@ -227,7 +228,7 @@ void main() {
.distanceBetweenInKm(geopoint: postPositions[index]) *
1000)
.round(),
location: userPosition1,
location: latLngLocation0,
);

return postDetails;
Expand Down
Loading

0 comments on commit 7f2b06b

Please sign in to comment.