Skip to content

Commit

Permalink
stop ride after receiving stopride
Browse files Browse the repository at this point in the history
  • Loading branch information
kruegercharles committed Dec 19, 2023
1 parent 8a4289a commit 2f1e8a1
Showing 1 changed file with 48 additions and 5 deletions.
53 changes: 48 additions & 5 deletions lib/ride/views/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter/services.dart';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:priobike/common/layout/buttons.dart';
import 'package:priobike/common/layout/ci.dart';
import 'package:priobike/common/layout/dialog.dart';
import 'package:priobike/common/lock.dart';
import 'package:priobike/common/mapbox_attribution.dart';
import 'package:priobike/main.dart';
Expand Down Expand Up @@ -57,15 +58,25 @@ class RideViewState extends State<RideView> {
/// A bool indicating whether the camera should follow the user location.
bool cameraFollowsUserLocation = true;

/// The finish ride button.
late FinishRideButton finishRideButton;

/// The associated simulator service, which is injected by the provider.
late Simulator simulator;

/// Called when a listener callback of a ChangeNotifier is fired.
void update() => setState(() {});
void update() => setState(() {
if (settings.enableSimulatorMode && simulator.receivedStopRide) stopRide(context);
});

@override
void initState() {
super.initState();

settings = getIt<Settings>();
settings.addListener(update);
simulator = getIt<Simulator>();
simulator.addListener(update);

SchedulerBinding.instance.addPostFrameCallback(
(_) async {
Expand Down Expand Up @@ -93,7 +104,6 @@ class RideViewState extends State<RideView> {
await ride.startNavigation(sgStatus.onNewPredictionStatusDuringRide);
await ride.selectRoute(routing.selectedRoute!);

settings = getIt<Settings>();
if (settings.enableSimulatorMode) sendTrafficlightsToSimulator();

// Connect the datastream mqtt client, if the user enabled real-time data.
Expand Down Expand Up @@ -154,10 +164,9 @@ class RideViewState extends State<RideView> {

/// Send the selected route to the simulator at the beginning of the ride.
Future<void> sendTrafficlightsToSimulator() async {
if (getIt<Settings>().enableSimulatorMode == false) return;
if (settings.enableSimulatorMode == false) return;
if (routing.selectedRoute == null) return;

final simulator = getIt<Simulator>();
for (final sg in routing.selectedRoute!.signalGroups) {
// format {"type":"TrafficLight", "deviceID":"123", "tlID":"456", "longitude":"10.12345", "latitude":"50.12345"}
final tlID = sg.id;
Expand All @@ -177,6 +186,37 @@ class RideViewState extends State<RideView> {
}
}

Future<void> stopRide(context) async {
if (settings.enableSimulatorMode == false) return;
await showGeneralDialog(
context: context,
barrierDismissible: true,
barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
barrierColor: Colors.black.withOpacity(0.4),
pageBuilder: (BuildContext dialogContext, Animation<double> animation, Animation<double> secondaryAnimation) {
return DialogLayout(
title: 'Fahrt beendet',
text: "Der Simulator hat die Fahrt beendet.",
iconColor: CI.radkulturYellow,
actions: [
BigButton(
iconColor: Colors.black,
textColor: Colors.black,
icon: Icons.home_rounded,
fillColor: CI.radkulturRed,
label: "Okay",
onPressed: () async {
Navigator.of(context).pop();
},
boxConstraints: BoxConstraints(minWidth: MediaQuery.of(context).size.width),
),
],
);
},
);
await finishRideButton.onTap(context);
}

/// Called when the user moves the map.
Future<void> onMapMoved() async {
if (cameraFollowsUserLocation) {
Expand All @@ -189,6 +229,7 @@ class RideViewState extends State<RideView> {
@override
void dispose() {
settings.removeListener(update);
simulator.removeListener(update);
super.dispose();
}

Expand Down Expand Up @@ -224,6 +265,8 @@ class RideViewState extends State<RideView> {
positionSpeedometerRight = 6.0;
}

finishRideButton = FinishRideButton();

return PopScope(
onPopInvoked: (type) async => false,
child: Scaffold(
Expand Down Expand Up @@ -266,7 +309,7 @@ class RideViewState extends State<RideView> {
child: RideSpeedometerView(puckHeight: heightToPuckBoundingBox),
),
const DatastreamView(),
const FinishRideButton(),
finishRideButton,
if (!cameraFollowsUserLocation)
SafeArea(
bottom: true,
Expand Down

0 comments on commit 2f1e8a1

Please sign in to comment.