From 7f2b06bc608a461b67b348f8558446793cf54333 Mon Sep 17 00:00:00 2001 From: Alberts Reisons Date: Wed, 29 May 2024 19:41:48 +0200 Subject: [PATCH] refactor(navigation-to-map): use LatLng instead of GeoPoint in UI --- lib/models/ui/challenge_details.dart | 6 +++--- lib/models/ui/post_details.dart | 7 ++++--- lib/models/ui/user_post_details.dart | 4 ++-- lib/viewmodels/challenge_view_model.dart | 5 +++-- .../option_selection/selected_page_view_model.dart | 3 ++- lib/viewmodels/user_posts_view_model.dart | 3 ++- lib/views/navigation/map_action.dart | 9 ++++----- .../components/info_cards/profile_info_card.dart | 4 ++-- .../components/info_cards/profile_info_pop_up.dart | 4 ++-- test/mocks/data/challenge_list.dart | 12 ++++++------ test/mocks/data/post_overview.dart | 10 +++++----- test/models/ui/challenge_details_test.dart | 8 ++++---- test/models/ui/user_post_details_test.dart | 12 ++++++------ .../posts_feed_view_model_integration_test.dart | 5 +++-- test/viewmodels/posts_feed_view_model_unit_test.dart | 9 +++++---- test/views/navigation/map_action_test.dart | 3 ++- 16 files changed, 55 insertions(+), 49 deletions(-) diff --git a/lib/models/ui/challenge_details.dart b/lib/models/ui/challenge_details.dart index 6cf76658..330c8b7c 100644 --- a/lib/models/ui/challenge_details.dart +++ b/lib/models/ui/challenge_details.dart @@ -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 @@ -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] diff --git a/lib/models/ui/post_details.dart b/lib/models/ui/post_details.dart index 02892eb6..34e7c568 100644 --- a/lib/models/ui/post_details.dart +++ b/lib/models/ui/post_details.dart @@ -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 @@ -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, @@ -102,7 +103,7 @@ class PostDetails { ) * 1000) .round(), - location: postFirestore.location.geoPoint, + location: postFirestore.location.geoPoint.toLatLng(), isChallenge: isChallenge, ); } diff --git a/lib/models/ui/user_post_details.dart b/lib/models/ui/user_post_details.dart index 0e77d3b2..190a4665 100644 --- a/lib/models/ui/user_post_details.dart +++ b/lib/models/ui/user_post_details.dart @@ -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. @@ -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, diff --git a/lib/viewmodels/challenge_view_model.dart b/lib/viewmodels/challenge_view_model.dart index 187f3132..06638315 100644 --- a/lib/viewmodels/challenge_view_model.dart +++ b/lib/viewmodels/challenge_view_model.dart @@ -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"; @@ -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(), ); } }); diff --git a/lib/viewmodels/option_selection/selected_page_view_model.dart b/lib/viewmodels/option_selection/selected_page_view_model.dart index 2093f47a..3f590212 100644 --- a/lib/viewmodels/option_selection/selected_page_view_model.dart +++ b/lib/viewmodels/option_selection/selected_page_view_model.dart @@ -16,7 +16,8 @@ class SelectedPageViewModel extends OptionsViewModel { 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); } diff --git a/lib/viewmodels/user_posts_view_model.dart b/lib/viewmodels/user_posts_view_model.dart index 7926ab6c..0e97b9a9 100644 --- a/lib/viewmodels/user_posts_view_model.dart +++ b/lib/viewmodels/user_posts_view_model.dart @@ -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"; @@ -33,7 +34,7 @@ class UserPostsViewModel extends AutoDisposeAsyncNotifier { postId: post.id, title: post.data.title, description: post.data.description, - location: post.location.geoPoint, + location: post.location.geoPoint.toLatLng(), ); return userPost; diff --git a/lib/views/navigation/map_action.dart b/lib/views/navigation/map_action.dart index 158e46da..63986a45 100644 --- a/lib/views/navigation/map_action.dart +++ b/lib/views/navigation/map_action.dart @@ -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"; @@ -17,7 +16,7 @@ class MapAction extends ConsumerWidget { final int depth; final MapSelectionOptions mapOption; - final GeoPoint initialLocation; + final LatLng initialLocation; const MapAction({ super.key, @@ -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( diff --git a/lib/views/pages/profile/components/info_cards/profile_info_card.dart b/lib/views/pages/profile/components/info_cards/profile_info_card.dart index 0367cdb5..33911d51 100644 --- a/lib/views/pages/profile/components/info_cards/profile_info_card.dart +++ b/lib/views/pages/profile/components/info_cards/profile_info_card.dart @@ -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"; @@ -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) { diff --git a/lib/views/pages/profile/components/info_cards/profile_info_pop_up.dart b/lib/views/pages/profile/components/info_cards/profile_info_pop_up.dart index 0407391e..8f12ff48 100644 --- a/lib/views/pages/profile/components/info_cards/profile_info_pop_up.dart +++ b/lib/views/pages/profile/components/info_cards/profile_info_pop_up.dart @@ -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"; @@ -19,7 +19,7 @@ class ProfileInfoPopUp extends StatelessWidget { }); final String? title; - final GeoPoint? location; + final LatLng? location; final String content; final FutureVoidCallback onDelete; diff --git a/test/mocks/data/challenge_list.dart b/test/mocks/data/challenge_list.dart index b8e9994a..b86bbe17 100644 --- a/test/mocks/data/challenge_list.dart +++ b/test/mocks/data/challenge_list.dart @@ -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 @@ -10,7 +10,7 @@ const mockChallengeList = [ distance: 700, timeLeft: 27, reward: 250, - location: userPosition0, + location: latLngLocation0, ), ChallengeDetails.solo( title: @@ -18,23 +18,23 @@ const mockChallengeList = [ 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, ), ]; diff --git a/test/mocks/data/post_overview.dart b/test/mocks/data/post_overview.dart index c91e1e06..37f7dc70 100644 --- a/test/mocks/data/post_overview.dart +++ b/test/mocks/data/post_overview.dart @@ -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 testPosts = [ PostDetails( @@ -20,7 +20,7 @@ final List testPosts = [ ownerCentauriPoints: 32, publicationDate: publicationDate1, distance: 20, - location: userPosition1, + location: latLngLocation0, ), PostDetails( postId: const PostIdFirestore(value: "post_2"), @@ -36,7 +36,7 @@ final List testPosts = [ publicationDate: publicationDate1, distance: 20, isChallenge: true, - location: userPosition1, + location: latLngLocation0, ), PostDetails( postId: const PostIdFirestore(value: "post_3"), @@ -50,7 +50,7 @@ final List testPosts = [ ownerCentauriPoints: 218281828, publicationDate: publicationDate1, distance: 20, - location: userPosition1, + location: latLngLocation0, ), ]; @@ -67,5 +67,5 @@ final timeDistancePost = PostDetails( ownerCentauriPoints: 911, publicationDate: DateTime.utc(1999), distance: 100, - location: userPosition1, + location: latLngLocation0, ); diff --git a/test/models/ui/challenge_details_test.dart b/test/models/ui/challenge_details_test.dart index ca8d4e74..487f8505 100644 --- a/test/models/ui/challenge_details_test.dart +++ b/test/models/ui/challenge_details_test.dart @@ -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", () { @@ -10,7 +10,7 @@ void main() { title: "title", distance: 50, reward: 100, - location: userPosition0, + location: latLngLocation0, ); final expectedHash = Object.hash( @@ -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); diff --git a/test/models/ui/user_post_details_test.dart b/test/models/ui/user_post_details_test.dart index a1b5318a..2fab09e1 100644 --- a/test/models/ui/user_post_details_test.dart +++ b/test/models/ui/user_post_details_test.dart @@ -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", () { @@ -11,7 +11,7 @@ void main() { postId: PostIdFirestore(value: "post_1"), title: "title", description: "description", - location: userPosition0, + location: latLngLocation0, ); final expectedHash = Object.hash( @@ -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); @@ -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); diff --git a/test/viewmodels/posts_feed_view_model_integration_test.dart b/test/viewmodels/posts_feed_view_model_integration_test.dart index 4fb231f8..ca5858e0 100644 --- a/test/viewmodels/posts_feed_view_model_integration_test.dart +++ b/test/viewmodels/posts_feed_view_model_integration_test.dart @@ -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"; @@ -126,7 +127,7 @@ void main() { .distanceBetweenInKm(geopoint: postPosition) * 1000) .round(), - location: userPosition1, + location: latLngLocation0, ), ]; @@ -227,7 +228,7 @@ void main() { .distanceBetweenInKm(geopoint: postPositions[index]) * 1000) .round(), - location: userPosition1, + location: latLngLocation0, ); return postDetails; diff --git a/test/viewmodels/posts_feed_view_model_unit_test.dart b/test/viewmodels/posts_feed_view_model_unit_test.dart index 75a96179..01e1e861 100644 --- a/test/viewmodels/posts_feed_view_model_unit_test.dart +++ b/test/viewmodels/posts_feed_view_model_unit_test.dart @@ -18,6 +18,7 @@ import "../mocks/data/firestore_challenge.dart"; import "../mocks/data/firestore_post.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_challenge_repository_service.dart"; import "../mocks/services/mock_geo_location_service.dart"; @@ -122,7 +123,7 @@ void main() { ownerCentauriPoints: owner.data.centauriPoints, publicationDate: post.data.publicationTime.toDate(), distance: 0, - location: userPosition1, + location: latLngLocation0, ), ]; @@ -182,7 +183,7 @@ void main() { ownerCentauriPoints: owner.data.centauriPoints, publicationDate: post.data.publicationTime.toDate(), distance: 0, - location: userPosition1, + location: latLngLocation0, ); return postDetails; @@ -250,7 +251,7 @@ void main() { ownerCentauriPoints: owners[index].data.centauriPoints, publicationDate: post.data.publicationTime.toDate(), distance: 0, - location: userPosition1, + location: latLngLocation0, ); return postDetails; @@ -331,7 +332,7 @@ void main() { ownerCentauriPoints: owner.data.centauriPoints, publicationDate: post.data.publicationTime.toDate(), distance: 0, - location: userPosition1, + location: latLngLocation0, ), ]; diff --git a/test/views/navigation/map_action_test.dart b/test/views/navigation/map_action_test.dart index 6c65dbdc..4ec38430 100644 --- a/test/views/navigation/map_action_test.dart +++ b/test/views/navigation/map_action_test.dart @@ -9,6 +9,7 @@ import "package:proxima/views/navigation/bottom_navigation_bar/navigation_bar_ro import "package:proxima/views/navigation/map_action.dart"; import "../../mocks/data/geopoint.dart"; +import "../../mocks/data/latlng.dart"; void main() { late SelectedPageViewModel selectedPageViewModel; @@ -25,7 +26,7 @@ void main() { home: MapAction( depth: 0, mapOption: MapSelectionOptions.myPosts, - initialLocation: userPosition0, + initialLocation: latLngLocation0, ), ), );