Skip to content

Commit

Permalink
feat: Improve distance bento element
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Sep 9, 2023
1 parent 5fea95d commit c34a2f5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
9 changes: 9 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
56 changes: 29 additions & 27 deletions lib/screens/locations_overview_screen_widgets/ViewDetails.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -116,9 +118,6 @@ class _ViewDetailsState extends State<ViewDetails> {
),
DistanceBentoElement(
lastLocation: lastLocation,
onTap: () {
widget.onGoToPosition(lastLocation);
},
),
BentoGridElement(
title: lastLocation.altitude == null
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/services/location_point_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -204,6 +205,8 @@ class LocationPointService {

LatLng asLatLng() => LatLng(latitude, longitude);

Coords asCoords() => Coords(latitude, longitude);

LocationPointService copyWith({
final double? latitude,
final double? longitude,
Expand Down

0 comments on commit c34a2f5

Please sign in to comment.