mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-19 12:44:21 +01:00
Show the live map on the run screen
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWZ9CKZsNxtD5Jr9bYD
This commit is contained in:
parent
bd12b8711d
commit
aaaecca3be
@ -4,9 +4,9 @@
|
||||
///
|
||||
/// * `RunMap.route(points: track.points)` -- a finished run. The camera
|
||||
/// fits the whole route ([fitPadding] keeps it clear of overlays).
|
||||
/// * `RunMap.live(trackListenable: notifier)` -- the live screen. Pass a
|
||||
/// `ValueListenable<RunTrack?>` (a `ValueNotifier<RunTrack?>`, or the
|
||||
/// recorder's own, anything that holds the current track); the route
|
||||
/// * `RunMap.live(pointsListenable: recorder.livePoints)` -- the live
|
||||
/// screen. Pass a `ValueListenable<List<RunPoint>>` (the recorder's
|
||||
/// accepted points, oldest first); the route
|
||||
/// redraws on every change and, in follow mode (the default), the camera
|
||||
/// keeps the last point centred. Dragging the map ends follow mode; the
|
||||
/// recentre button (icon only) turns it back on. The listenable must stay
|
||||
@ -48,11 +48,11 @@ class RunMap extends StatefulWidget {
|
||||
super.key,
|
||||
this.fitPadding = const EdgeInsets.all(40),
|
||||
this.tileProviderOpener = openOsmTileProvider,
|
||||
}) : trackListenable = null;
|
||||
}) : pointsListenable = null;
|
||||
|
||||
/// A live run: follows the last point of the track in [trackListenable].
|
||||
/// A live run: follows the last point in [pointsListenable].
|
||||
const RunMap.live({
|
||||
required ValueListenable<RunTrack?> this.trackListenable,
|
||||
required ValueListenable<List<RunPoint>> this.pointsListenable,
|
||||
super.key,
|
||||
this.tileProviderOpener = openOsmTileProvider,
|
||||
}) : points = null,
|
||||
@ -61,8 +61,8 @@ class RunMap extends StatefulWidget {
|
||||
/// The route in detail mode; null in live mode.
|
||||
final List<RunPoint>? points;
|
||||
|
||||
/// The current track in live mode; null in detail mode.
|
||||
final ValueListenable<RunTrack?>? trackListenable;
|
||||
/// The accepted points in live mode; null in detail mode.
|
||||
final ValueListenable<List<RunPoint>>? pointsListenable;
|
||||
|
||||
/// Space kept free around the fitted route (detail mode).
|
||||
final EdgeInsets fitPadding;
|
||||
@ -81,16 +81,16 @@ class _RunMapState extends State<RunMap> {
|
||||
bool _follow = true;
|
||||
bool _warned = false;
|
||||
|
||||
bool get _live => widget.trackListenable != null;
|
||||
bool get _live => widget.pointsListenable != null;
|
||||
|
||||
List<RunPoint> get _points => _live
|
||||
? widget.trackListenable!.value?.points ?? const []
|
||||
? widget.pointsListenable!.value
|
||||
: widget.points!;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
widget.trackListenable?.addListener(_followLast);
|
||||
widget.pointsListenable?.addListener(_followLast);
|
||||
unawaited(
|
||||
widget.tileProviderOpener().then((p) {
|
||||
if (mounted) setState(() => _tiles = p);
|
||||
@ -100,7 +100,7 @@ class _RunMapState extends State<RunMap> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
widget.trackListenable?.removeListener(_followLast);
|
||||
widget.pointsListenable?.removeListener(_followLast);
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
@ -173,15 +173,14 @@ class _RunMapState extends State<RunMap> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final listenable = widget.trackListenable;
|
||||
final listenable = widget.pointsListenable;
|
||||
if (listenable == null) return _map(context, widget.points!);
|
||||
return Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
ValueListenableBuilder<RunTrack?>(
|
||||
ValueListenableBuilder<List<RunPoint>>(
|
||||
valueListenable: listenable,
|
||||
builder: (context, track, _) =>
|
||||
_map(context, track?.points ?? const []),
|
||||
builder: (context, points, _) => _map(context, points),
|
||||
),
|
||||
if (!_follow)
|
||||
Positioned(
|
||||
|
||||
@ -4,20 +4,28 @@ library;
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:workout_app/models/run_track.dart';
|
||||
import 'package:workout_app/services/map_tile_cache.dart';
|
||||
import 'package:workout_app/ui/theme.dart';
|
||||
import 'package:workout_app/widgets/run_map.dart';
|
||||
|
||||
/// Placeholder for the live map: replace the body of [build] with the map
|
||||
/// widget, keeping the [ValueListenable] as its source of points.
|
||||
/// The live map on the run screen: [RunMap.live] over the recorder's points,
|
||||
/// clipped to the card shape.
|
||||
///
|
||||
/// `RunScreen` fills this space from `mapBuilder`, defaulting to this widget,
|
||||
/// so a map can also be dropped in by passing a builder with no edit here.
|
||||
/// `RunScreen` fills this space from `mapBuilder`, defaulting to this widget.
|
||||
class RunMapSlot extends StatelessWidget {
|
||||
/// Creates the slot; [points] updates with every accepted GPS fix.
|
||||
const RunMapSlot({required this.points, super.key});
|
||||
const RunMapSlot({
|
||||
required this.points,
|
||||
super.key,
|
||||
this.tileProviderOpener = openOsmTileProvider,
|
||||
});
|
||||
|
||||
/// The accepted points of the run so far, oldest first.
|
||||
final ValueListenable<List<RunPoint>> points;
|
||||
|
||||
/// Where the map's tiles come from; tests pass a fake.
|
||||
final TileProviderOpener tileProviderOpener;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
@ -27,13 +35,10 @@ class RunMapSlot extends StatelessWidget {
|
||||
color: scheme.surfaceContainerHigh,
|
||||
borderRadius: BorderRadius.circular(AppRadius.md),
|
||||
),
|
||||
child: Center(
|
||||
child: Icon(
|
||||
Icons.map_outlined,
|
||||
size: 48,
|
||||
color: scheme.onSurfaceVariant,
|
||||
semanticLabel: 'Map',
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: RunMap.live(
|
||||
pointsListenable: points,
|
||||
tileProviderOpener: tileProviderOpener,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@ -5,7 +5,6 @@ import 'package:flutter_map/flutter_map.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
import 'package:workout_app/models/run_track.dart';
|
||||
import 'package:workout_app/services/map_tile_cache.dart';
|
||||
import 'package:workout_app/widgets/run_map.dart';
|
||||
|
||||
import '_run_map_fixtures.dart';
|
||||
@ -161,17 +160,17 @@ void main() {
|
||||
testWidgets('follows the last point and draws as the track grows', (
|
||||
tester,
|
||||
) async {
|
||||
final track = ValueNotifier<RunTrack?>(null);
|
||||
final track = ValueNotifier<List<RunPoint>>(const []);
|
||||
addTearDown(track.dispose);
|
||||
await _pump(
|
||||
tester,
|
||||
RunMap.live(trackListenable: track, tileProviderOpener: fakeTiles()),
|
||||
RunMap.live(pointsListenable: track, tileProviderOpener: fakeTiles()),
|
||||
);
|
||||
expect(find.byType(PolylineLayer), findsNothing);
|
||||
expect(find.byType(MarkerLayer), findsNothing);
|
||||
|
||||
final full = buildTrack(count: 4);
|
||||
track.value = full;
|
||||
track.value = full.points;
|
||||
await tester.pump();
|
||||
expect(find.byType(PolylineLayer), findsOneWidget);
|
||||
expect(
|
||||
@ -189,11 +188,11 @@ void main() {
|
||||
tester,
|
||||
) async {
|
||||
final full = buildTrack(count: 4);
|
||||
final track = ValueNotifier<RunTrack?>(full);
|
||||
final track = ValueNotifier<List<RunPoint>>(full.points);
|
||||
addTearDown(track.dispose);
|
||||
await _pump(
|
||||
tester,
|
||||
RunMap.live(trackListenable: track, tileProviderOpener: fakeTiles()),
|
||||
RunMap.live(pointsListenable: track, tileProviderOpener: fakeTiles()),
|
||||
);
|
||||
await tester.drag(find.byType(FlutterMap), const Offset(120, 0));
|
||||
await tester.pump();
|
||||
@ -202,26 +201,26 @@ void main() {
|
||||
expect(dragged, isNot(closeTo(full.points.last.longitude, 1e-6)));
|
||||
|
||||
// New fixes no longer pull the camera while follow is off.
|
||||
track.value = buildTrack(count: 6);
|
||||
track.value = buildTrack(count: 6).points;
|
||||
await tester.pump();
|
||||
expect(_camera(tester).center.longitude, closeTo(dragged, 1e-9));
|
||||
|
||||
await tester.tap(find.byIcon(Icons.my_location));
|
||||
await tester.pump();
|
||||
expect(find.byIcon(Icons.my_location), findsNothing);
|
||||
final last = track.value!.points.last;
|
||||
final last = track.value.last;
|
||||
expect(_camera(tester).center.longitude, closeTo(last.longitude, 1e-6));
|
||||
});
|
||||
|
||||
testWidgets('stops listening when disposed', (tester) async {
|
||||
final track = ValueNotifier<RunTrack?>(buildTrack());
|
||||
final track = ValueNotifier<List<RunPoint>>(buildTrack().points);
|
||||
addTearDown(track.dispose);
|
||||
await _pump(
|
||||
tester,
|
||||
RunMap.live(trackListenable: track, tileProviderOpener: fakeTiles()),
|
||||
RunMap.live(pointsListenable: track, tileProviderOpener: fakeTiles()),
|
||||
);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
track.value = buildTrack(count: 7);
|
||||
track.value = buildTrack(count: 7).points;
|
||||
await tester.pump();
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user