Skip to content

Commit

Permalink
Refactored RecordingMap into own file
Browse files Browse the repository at this point in the history
  • Loading branch information
TKFRvisionOfficial committed Jan 24, 2023
1 parent aaaad2b commit c7a66ea
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 197 deletions.
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'package:stasi/db/database_bloc.dart';
import 'package:stasi/pages/running_recording.dart';
import 'package:stasi/notifiers/running_recording.dart';
import 'package:stasi/util/theme.dart';
import 'package:stasi/pages/recording_manager.dart';
import 'package:stasi/pages/vehicle_selection.dart';
Expand Down
File renamed without changes.
198 changes: 5 additions & 193 deletions lib/pages/recording_editor.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';
import 'package:stasi/db/database_bloc.dart';
import 'package:url_launcher/url_launcher.dart';

import '../model/recording.dart';
import 'package:stasi/db/database_bloc.dart';
import 'package:stasi/widgets/recording_map.dart';
import 'package:stasi/model/recording.dart';


class RecordingEditor extends StatefulWidget {
Expand All @@ -19,7 +18,7 @@ class RecordingEditor extends StatefulWidget {
final Recording recording;

@override
State<StatefulWidget> createState() => _RecordingEditorState();
State<RecordingEditor> createState() => _RecordingEditorState();
}

class _RecordingEditorState extends State<RecordingEditor> {
Expand Down Expand Up @@ -126,7 +125,7 @@ class _RecordingEditorControlState extends State<_RecordingEditorControl> {
return Column(
children: [
Expanded(
child: _RecordingEditorMap(
child: RecordingMap(
pointList: [
for (final time in widget.points.keys)
if (_startTime.compareTo(time) <= 0 && _endTime.compareTo(time) >= 0)
Expand Down Expand Up @@ -177,170 +176,6 @@ class _RecordingEditorControlState extends State<_RecordingEditorControl> {
}
}

class _RecordingEditorMap extends StatefulWidget {
const _RecordingEditorMap({
Key? key,
required this.pointList,
}) : super(key: key);

final List<LatLng> pointList;

@override
_RecordingEditorMapState createState() => _RecordingEditorMapState();
}

class _RecordingEditorMapState extends State<_RecordingEditorMap> {
late MapController _mapController;
bool _shouldApplyBounds = true;

@override
void initState() {
super.initState();
_mapController = MapController();
}

@override
void didUpdateWidget(_RecordingEditorMap oldWidget) {
super.didUpdateWidget(oldWidget);

// yeah this won't get triggered when the orientation changes.
// too bad...
if (_shouldApplyBounds && oldWidget.pointList != widget.pointList) {
_updateBounds();
}
}

void _updateBounds() {
_mapController.fitBounds(
_getBoundsFromPoints(widget.pointList),
options: const FitBoundsOptions(padding: EdgeInsets.all(80.0)),
);
}

@override
Widget build(BuildContext context) {
return Stack(
children: [
FlutterMap(
mapController: _mapController,
options: MapOptions(
interactiveFlags: InteractiveFlag.all & ~InteractiveFlag.rotate,
bounds: _getBoundsFromPoints(widget.pointList),
boundsOptions: const FitBoundsOptions(padding: EdgeInsets.all(80.0)),
),
nonRotatedChildren: const [_MapAttribution()],
children: [
TileLayer(
urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
subdomains: const ["a", "b", "c"],
userAgentPackageName: "solutions.tlm.stasi",
tileBuilder: darkModeTileBuilder,
backgroundColor: Colors.black54,
),
PolylineLayer(
polylines: [Polyline(
points: widget.pointList,
strokeWidth: 10,
)],
),
],
),
Positioned(
top: 3,
right: 3,
child: ToggleButtons(
isSelected: [_shouldApplyBounds, false],
color: Colors.white,
onPressed: (index) {
switch (index) {
case 0: // lock
setState(() {
_shouldApplyBounds = !_shouldApplyBounds;
});

if (_shouldApplyBounds) _updateBounds();
break;

case 1: // center
_updateBounds();
break;
}
},
children: const [
Icon(Icons.lock),
Icon(Icons.center_focus_strong),
],
),
),
],
);
}

@override
void dispose() {
_mapController.dispose();
super.dispose();
}
}


class _MapAttribution extends StatelessWidget {
const _MapAttribution({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) => AttributionWidget(
attributionBuilder: (context) => ColoredBox(
color: Colors.black.withOpacity(0.5),
child: GestureDetector(
onTap: () async {
/*
* This is bad practice.
* We will have to change this with the next stable release of flutter
* to "if(context.mounted)".
* https://github.com/flutter/flutter/issues/110694
*/
final scaffoldMessenger = ScaffoldMessenger.of(context);

const osmLink = "https://www.openstreetmap.org/copyright";

final osmUri = Uri.parse(osmLink);
if (await canLaunchUrl(osmUri)) {
await launchUrl(osmUri, mode: LaunchMode.externalApplication);
return;
}
await Clipboard.setData(const ClipboardData(text: osmLink));

scaffoldMessenger.showSnackBar(
const SnackBar(content: Text(
"Couldn't open browser! Copied link to clipboard.",
)),
);
},
child: Padding(
padding: const EdgeInsets.all(3.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: const [
Text("flutter_map | © "),
MouseRegion(
cursor: SystemMouseCursors.click,
child: Text(
"OpenStreetMap",
style: TextStyle(
color: Color(0xFF0000EE),
decoration: TextDecoration.underline,
),
),
),
Text(" contributors"),
],
),
),
),
),
);
}


class _RecordingEditorSlider extends StatelessWidget {
const _RecordingEditorSlider({
Expand Down Expand Up @@ -481,26 +316,3 @@ class _RecordingEditorButtonsState extends State<_RecordingEditorButtons> {
super.dispose();
}
}

LatLngBounds _getBoundsFromPoints(List<LatLng> points) {
final bounds = LatLngBounds.fromPoints(points);

/*
When all the points are the same the southWest and northEast
become the same too. This leads to an error in flutter map.
(Unsupported operation: Infinity or NaN toInt)
This normally only happens during debugging.
*/

if (bounds.southWest!.latitude == bounds.northEast!.latitude) {
bounds.southWest!.latitude += 0.0005;
bounds.northEast!.latitude -= 0.0005;
}

if (bounds.southWest!.longitude == bounds.northEast!.longitude) {
bounds.southWest!.longitude -= 0.0005;
bounds.northEast!.longitude += 0.0005;
}

return bounds;
}
2 changes: 1 addition & 1 deletion lib/pages/recording_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'package:http/http.dart' as http;

import 'package:stasi/db/database_bloc.dart';
import 'package:stasi/routes/recording_editor_route.dart';
import 'package:stasi/pages/running_recording.dart';
import 'package:stasi/notifiers/running_recording.dart';
import 'package:stasi/util/theme.dart';
import 'package:stasi/util/api_client.dart';
import 'package:stasi/model/recording.dart';
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/vehicle_selection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:background_location/background_location.dart';
import 'package:provider/provider.dart';
import 'package:stasi/pages/running_recording.dart';

import '../db/database_bloc.dart';
import 'package:stasi/notifiers/running_recording.dart';
import 'package:stasi/db/database_bloc.dart';


class VehicleSelection extends StatefulWidget {
Expand Down
Loading

0 comments on commit c7a66ea

Please sign in to comment.