Skip to content

Commit

Permalink
Let user record route locations live
Browse files Browse the repository at this point in the history
  • Loading branch information
greghart committed Jan 15, 2025
1 parent 210fdb0 commit 8cdf123
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 5 deletions.
2 changes: 1 addition & 1 deletion clutter/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ subprojects {

tasks.register("clean", Delete) {
delete rootProject.buildDir
}
}
2 changes: 1 addition & 1 deletion clutter/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "8.1.0" apply false
id "com.android.application" version "8.2.1" apply false
id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

Expand Down
45 changes: 45 additions & 0 deletions clutter/lib/explorer/map/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class RouteMap extends StatelessWidget {
),
],
),
PendingRouteMarker(id: route.id),
BoulderMapDirect(boulder: model.boulder!),
AnimateTo(
mapController: MapController.of(context),
Expand All @@ -43,3 +44,47 @@ class RouteMap extends StatelessWidget {
);
}
}

class PendingRouteMarker extends StatefulWidget {
const PendingRouteMarker({super.key, required this.id});

final int id;

@override
State<PendingRouteMarker> createState() => _PendingRouteMarkerState();
}

class _PendingRouteMarkerState extends State<PendingRouteMarker> {
late Future<models.LatLng?> _latLng;

@override
void initState() {
super.initState();
_latLng = Provider.of<ExplorerLocationModel>(context, listen: false)
.getPendingRouteLocation(widget.id);
}

@override
Widget build(BuildContext context) {
return FutureBuilder<models.LatLng?>(
future: _latLng,
builder: (context, snapshot) {
if (snapshot.hasData) {
return MarkerLayer(
markers: [
Marker(
point: snapshot.data!,
child: const Icon(
Icons.location_pin,
size: 20,
color: Colors.pink,
),
),
],
);
}
return const SizedBox.shrink();
},
);
}
}
2 changes: 1 addition & 1 deletion clutter/lib/explorer/map/sun.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SunLayer extends StatelessWidget {
}

final sunPosition = suncalc.getPosition(
time: DateTime.now().add(const Duration(hours: -9)),
time: DateTime.now(),
lat: coordinate.latitude,
lng: coordinate.longitude,
);
Expand Down
21 changes: 21 additions & 0 deletions clutter/lib/explorer/model.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math' as math;

import 'package:flutter/material.dart';
import 'package:flutter_map_location_marker/flutter_map_location_marker.dart';
import 'package:latlong2/latlong.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../entities/index.dart' as models;

enum EntityType { crag, area, boulder, route }
Expand Down Expand Up @@ -188,6 +191,24 @@ class ExplorerLocationModel extends ChangeNotifier {
_setHeading(0, notify: false);
}

final SharedPreferencesAsync routeLocations = SharedPreferencesAsync();
void recordRouteLocation(models.Route r) async {
print("recordRouteLocation: ${r.id} -> ${currentPosition.latLng}");
await routeLocations.setString(
'route_location_${r.id}',
jsonEncode(currentPosition.latLng.toJson()),
);
notifyListeners();
print("Done!");
}

Future<LatLng?> getPendingRouteLocation(int id) async {
final str = await routeLocations.getString('route_location_$id');
print("getPendingRouteLocation: $id -> $str");
if (str == null) return null;
return LatLng.fromJson(jsonDecode(str));
}

void setPosition(LocationMarkerPosition? position, {notify = true}) {
if (position == null) return;

Expand Down
9 changes: 9 additions & 0 deletions clutter/lib/explorer/overlay/route.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../entities/index.dart' as models;
import '../model.dart';
import 'difficulty_span.dart';
import 'layout.dart';

Expand Down Expand Up @@ -36,6 +38,13 @@ class RouteOverlay extends StatelessWidget {
const DiagramsLayout(
chart: null,
),
FilledButton(
onPressed: () {
Provider.of<ExplorerLocationModel>(context, listen: false)
.recordRouteLocation(route);
},
child: const Text('Record route location here'),
)
],
),
);
Expand Down
1 change: 1 addition & 0 deletions clutter/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ void main() async {

// Load the user's preferred theme while the splash screen is displayed.
// This prevents a sudden theme change when the app is first displayed.
WidgetsFlutterBinding.ensureInitialized();
await settingsController.loadSettings();

// Run the app and pass in the SettingsController. The app listens to the
Expand Down
4 changes: 2 additions & 2 deletions clutter/lib/settings/settings_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class SettingsController with ChangeNotifier {

// Make ThemeMode a private variable so it is not updated directly without
// also persisting the changes with the SettingsService.
late ThemeMode _themeMode;
late ThemeMode _themeMode = ThemeMode.dark;

// Allow Widgets to read the user's preferred ThemeMode.
ThemeMode get themeMode => _themeMode;

late bool _cozyCompass;
late bool _cozyCompass = false;

// Whether to use the small cozy compass in breadcrumbs
bool get cozyCompass => _cozyCompass;
Expand Down

0 comments on commit 8cdf123

Please sign in to comment.