From c34a2f516fcb779860b97466179fb3d74cac4e0c Mon Sep 17 00:00:00 2001
From: Myzel394 <50424412+Myzel394@users.noreply.github.com>
Date: Sat, 9 Sep 2023 12:22:20 +0200
Subject: [PATCH] feat: Improve distance bento element

---
 lib/l10n/app_en.arb                           |  9 +++
 .../ViewDetails.dart                          | 56 ++++++++++---------
 lib/services/location_point_service.dart      |  3 +
 3 files changed, 41 insertions(+), 27 deletions(-)

diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index c1d703b7..1e9d191a 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -767,6 +767,15 @@
   "locations_values_lastLocation_description": "Last location update",
   "locations_values_distance_description": "Distance",
   "locations_values_distance_permissionRequired": "Grant permission",
+  "locations_values_distance_nearby": "<10 m",
+  "locations_values_distance_m": "{distance} m",
+  "@locations_values_distance_m": {
+    "placeholders": {
+      "distance": {
+        "type": "String"
+      }
+    }
+  },
   "locations_values_distance_km": "{distance, select, 0 {<1 km} 1 {one km} other {{distance} km}}",
   "@locations_values_distance_km": {
     "placeholders": {
diff --git a/lib/screens/locations_overview_screen_widgets/ViewDetails.dart b/lib/screens/locations_overview_screen_widgets/ViewDetails.dart
index 9d0bfb36..7b04e79c 100644
--- a/lib/screens/locations_overview_screen_widgets/ViewDetails.dart
+++ b/lib/screens/locations_overview_screen_widgets/ViewDetails.dart
@@ -15,6 +15,8 @@ import 'package:latlong2/latlong.dart';
 import 'package:locus/utils/location/index.dart';
 import 'package:locus/utils/permissions/has-granted.dart';
 import 'package:locus/utils/permissions/request.dart';
+import 'package:locus/widgets/OpenInMaps.dart';
+import 'package:map_launcher/map_launcher.dart';
 import '../../constants/spacing.dart';
 import '../../services/location_point_service.dart';
 import '../../utils/icon.dart';
@@ -116,9 +118,6 @@ class _ViewDetailsState extends State<ViewDetails> {
             ),
             DistanceBentoElement(
               lastLocation: lastLocation,
-              onTap: () {
-                widget.onGoToPosition(lastLocation);
-              },
             ),
             BentoGridElement(
               title: lastLocation.altitude == null
@@ -184,10 +183,8 @@ class _ViewDetailsState extends State<ViewDetails> {
 
 class DistanceBentoElement extends StatefulWidget {
   final LocationPointService lastLocation;
-  final VoidCallback onTap;
 
   const DistanceBentoElement({
-    required this.onTap,
     required this.lastLocation,
     super.key,
   });
@@ -238,19 +235,15 @@ class _DistanceBentoElementState extends State<DistanceBentoElement>
     final l10n = AppLocalizations.of(context);
 
     return BentoGridElement(
-      onTap: hasGrantedPermission == false
-          ? () async {
-              final hasGranted = await requestBasicLocationPermission();
-
-              if (hasGranted) {
-                fetchCurrentPosition();
-
-                setState(() {
-                  hasGrantedPermission = true;
-                });
-              }
-            }
-          : widget.onTap,
+      onTap: () {
+        showPlatformModalSheet(
+          context: context,
+          material: MaterialModalSheetData(),
+          builder: (context) => OpenInMaps(
+            destination: widget.lastLocation.asCoords(),
+          ),
+        );
+      },
       title: (() {
         if (!hasGrantedPermission) {
           return l10n.locations_values_distance_permissionRequired;
@@ -260,16 +253,25 @@ class _DistanceBentoElementState extends State<DistanceBentoElement>
           return l10n.loading;
         }
 
+        final distanceInMeters = Geolocator.distanceBetween(
+          currentPosition!.latitude,
+          currentPosition!.longitude,
+          widget.lastLocation.latitude,
+          widget.lastLocation.longitude,
+        );
+
+        if (distanceInMeters < 10) {
+          return l10n.locations_values_distance_nearby;
+        }
+
+        if (distanceInMeters < 1000) {
+          return l10n.locations_values_distance_m(
+            distanceInMeters.toStringAsFixed(0).toString(),
+          );
+        }
+
         return l10n.locations_values_distance_km(
-          (Geolocator.distanceBetween(
-                    currentPosition!.latitude,
-                    currentPosition!.longitude,
-                    widget.lastLocation.latitude,
-                    widget.lastLocation.longitude,
-                  ) /
-                  1000)
-              .floor()
-              .toString(),
+          (distanceInMeters / 1000).toStringAsFixed(0),
         );
       })(),
       type: hasGrantedPermission && currentPosition != null
diff --git a/lib/services/location_point_service.dart b/lib/services/location_point_service.dart
index 517a4fd1..cfd75e87 100644
--- a/lib/services/location_point_service.dart
+++ b/lib/services/location_point_service.dart
@@ -7,6 +7,7 @@ import 'package:cryptography/cryptography.dart';
 import 'package:flutter/services.dart';
 import 'package:geolocator/geolocator.dart';
 import 'package:locus/utils/cryptography/decrypt.dart';
+import 'package:map_launcher/map_launcher.dart';
 import 'package:uuid/uuid.dart';
 
 const uuid = Uuid();
@@ -204,6 +205,8 @@ class LocationPointService {
 
   LatLng asLatLng() => LatLng(latitude, longitude);
 
+  Coords asCoords() => Coords(latitude, longitude);
+
   LocationPointService copyWith({
     final double? latitude,
     final double? longitude,