feat(routes): add route page with functioning route

route is build from previously chosen segments
This commit is contained in:
fbachus
2026-07-30 02:25:46 +02:00
parent c68b47170b
commit 1b2d0de6e4
2 changed files with 149 additions and 68 deletions
+76 -16
View File
@@ -233,12 +233,25 @@ class ChooseDeparturePage extends StatefulWidget {
class _ChooseDeparturePageState extends State<ChooseDeparturePage> { class _ChooseDeparturePageState extends State<ChooseDeparturePage> {
List<Departure> departureList = []; List<Departure> departureList = [];
Journey? singleJourney;
void loadDepartures(Station startStation) async { void loadDepartures(Station startStation) async {
departureList = await ApiHandler.departures(startStation.id); departureList = await ApiHandler.departures(startStation.id);
setState(() {}); setState(() {});
} }
void loadJourney(int journeyId) async {
singleJourney = await Journey.dbGet(journeyId);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => JourneyDetailPage(
singleJourney: singleJourney as Journey,
),
),
);
}
@override @override
initState() { initState() {
super.initState(); super.initState();
@@ -249,7 +262,9 @@ class _ChooseDeparturePageState extends State<ChooseDeparturePage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text('RoutePicker for ${widget.startStation.name}'), title: Text(
'RoutePicker for ${widget.startStation.name} // ${widget.journeyId}',
),
), ),
body: Center( body: Center(
child: Column( child: Column(
@@ -265,12 +280,11 @@ class _ChooseDeparturePageState extends State<ChooseDeparturePage> {
), ),
ElevatedButton( ElevatedButton(
onPressed: () { onPressed: () {
Navigator.push( if (widget.journeyId != null) {
context, loadJourney(widget.journeyId as int);
MaterialPageRoute(builder: (context) => ChooseStationPage()), }
);
}, },
child: const Text('Vehicle picked!'), child: const Text('Finish Route'),
), ),
], ],
), ),
@@ -355,7 +369,9 @@ class _ChooseArrivalPageState extends State<ChooseArrivalPage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text('Arrivals for ${widget.departure.vehicleName}'), title: Text(
'Arrivals for ${widget.departure.vehicleName}// ${widget.journeyId}',
),
), ),
body: Center( body: Center(
child: Column( child: Column(
@@ -370,15 +386,6 @@ class _ChooseArrivalPageState extends State<ChooseArrivalPage> {
), ),
), ),
), ),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ChooseStationPage()),
);
},
child: const Text('Vehicle picked!'),
),
], ],
), ),
), ),
@@ -443,3 +450,56 @@ class ArrivalList extends StatelessWidget {
); );
} }
} }
class JourneyDetailPage extends StatefulWidget {
final Journey singleJourney;
const JourneyDetailPage({
super.key,
required this.singleJourney,
});
@override
State<JourneyDetailPage> createState() => _JourneyDetailPageState();
}
class _JourneyDetailPageState extends State<JourneyDetailPage> {
Duration duration = Duration(minutes: 0);
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Connection Details \n$duration')),
body: Center(
child: SegmentList(journey: widget.singleJourney),
),
);
}
}
class SegmentList extends StatelessWidget {
final Journey journey;
const SegmentList({
super.key,
required this.journey,
});
@override
Widget build(BuildContext context) {
return ListView(
children: [
for (Segment seg in journey.segments)
ListTile(
title: Text(seg.start.direction),
subtitle: Text(seg.start.departureTime.toString()),
),
],
);
}
}
+72 -51
View File
@@ -224,13 +224,19 @@ class Journey {
int routeId, int routeId,
) async { ) async {
Database database = DbHelper.db; Database database = DbHelper.db;
String segment = Segment.tableName;
var tmp = await database.rawQuery(""" var tmp = await database.rawQuery("""
SELECT * FROM segments SELECT * FROM '$segment'
JOIN routesegments ON routesegments.segmentid = segments.id JOIN routesegments ON routesegments.segment = '$segment'.id
WHERE routesegments.route = '$routeId' WHERE routesegments.route = '$routeId'
ORDER BY routesegments.segmentorder ASC; ORDER BY routesegments.segmentorder ASC;
"""); """);
return tmp.map((x) => Segment.fromMap(x)).toList();
return Future.wait(
tmp.map(
(x) => Segment.dbGet(x["segment"] as int),
),
);
} }
Future<int> _dbInsert() async { Future<int> _dbInsert() async {
@@ -377,20 +383,29 @@ class Segment {
id: map["id"] as int, id: map["id"] as int,
ref: map["ref"] as String, ref: map["ref"] as String,
start: Departure.fromMap( start: Departure.fromMap(
map["start"] as Map<String, Object>, map["start"] as Map<String, Object?>,
), ),
end: Departure.fromMap( end: Departure.fromMap(
map["end"] as Map<String, Object>, map["end"] as Map<String, Object?>,
), ),
vehicle: Vehicle.fromMap(map["vehicle"] as Map<String, Object>), vehicle: Vehicle.fromMap(map["vehicle"] as Map<String, Object?>),
); );
} }
factory Segment.fromJson(Map<String, dynamic> json) { factory Segment.fromJson(Map<String, dynamic> json) {
// adding departures to db only here instead of within fromJson to only
// store used departures
// we need to store them in the db though to get an id from the db :P
Departure start = Departure.fromJson(json["Origin"]);
Departure end = Departure.fromJson(json["Destination"]);
start.dbInsert();
end.dbInsert();
Future.delayed(Duration(milliseconds: 30));
Segment tmp = Segment( Segment tmp = Segment(
ref: json["JourneyDetail"]["ref"], ref: json["JourneyDetail"]["ref"],
start: Departure.fromJson(json["Origin"]), start: start,
end: Departure.fromJson(json["Destination"]), end: end,
vehicle: Vehicle.fromJson(json["Product"]), vehicle: Vehicle.fromJson(json["Product"]),
); );
tmp.dbInsert(); tmp.dbInsert();
@@ -398,6 +413,8 @@ class Segment {
} }
static Future<Segment> fromDepartures(Departure start, Departure end) async { static Future<Segment> fromDepartures(Departure start, Departure end) async {
start.dbInsert();
end.dbInsert();
Segment tmp = Segment( Segment tmp = Segment(
ref: start.ref, ref: start.ref,
start: start, start: start,
@@ -427,9 +444,9 @@ class Segment {
CREATE TABLE IF NOT EXISTS '$tableName'( CREATE TABLE IF NOT EXISTS '$tableName'(
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
ref TEXT, ref TEXT,
startid TEXT, startid INTEGER,
endid TEXT, endid INTEGER,
vehicleid INTEGER, vehicleid TEXT,
FOREIGN KEY(startid) REFERENCES '$departure'(stationid), FOREIGN KEY(startid) REFERENCES '$departure'(stationid),
FOREIGN KEY(endid) REFERENCES '$departure'(stationid), FOREIGN KEY(endid) REFERENCES '$departure'(stationid),
FOREIGN KEY(vehicleid) REFERENCES '$vehicle'(vehicleid) FOREIGN KEY(vehicleid) REFERENCES '$vehicle'(vehicleid)
@@ -445,14 +462,15 @@ class Segment {
"""SELECT s.id, s.ref, s.startid, s.endid, s.vehicleid, """SELECT s.id, s.ref, s.startid, s.endid, s.vehicleid,
start.station as start_stationid, start.station as start_stationid,
start.stationname as start_stationname, start.arrivaltime as start_arrivaltime, start.stationname as start_stationname, start.arrivaltime as start_arrivaltime,
start.departuretime as start_departuretime, start.departuretime as start_departuretime, start.direction as direction,
end.station as end_stationid, end.stationname as end_stationname, end.station as end_stationid, end.stationname as end_stationname,
end.arrivaltime as end_arrivaltime, end.departuretime as end_departuretime, end.arrivaltime as end_arrivaltime, end.departuretime as end_departuretime,
v.name as vehiclename, v.line as vehicleline v.name as vehiclename, v.line as vehicleline
FROM $tableName s FROM $tableName s
JOIN $departure start ON s.startid = start.id JOIN $departure start ON s.startid = start.id
JOIN $departure end ON s.endid = end.id JOIN $departure end ON s.endid = end.id
JOIN $vehicle v ON s.vehicleid = v.id; JOIN $vehicle v ON s.vehicleid = v.id
where s.id = $id;
""", """,
); );
// since we are querying for the primary key, this list should never be > 1 // since we are querying for the primary key, this list should never be > 1
@@ -460,37 +478,40 @@ class Segment {
// var seg = tmp.map((x) => Segment.fromMap(x)); // var seg = tmp.map((x) => Segment.fromMap(x));
// return seg.first; // return seg.first;
var seg = tmp.first; Map<String, Object?> seg = tmp.first;
var fullMap = { Map<String, Object> startMap = {
"id": seg["id"], "id": seg["startid"] as int,
"ref": seg["ref"], "ref": seg["ref"] as String,
"start": { "direction": seg["direction"] as String,
"id": seg["startid"] as int, "vehicle": seg["vehicleid"] as String,
"ref": seg["ref"] as String, "vehiclename": seg["vehiclename"] as String,
"direction": seg["direction"] as String, "station": seg["start_stationid"] as String,
"vehicle": seg["vehicleid"] as String, "stationname": seg["start_stationname"] as String,
"vehiclename": seg["vehiclename"] as String, "arrivaltime": seg["start_arrivaltime"] as String,
"station": seg["start_stationid"] as String, "departuretime": seg["start_departuretime"] as String,
"staionname": seg["start_stationname"] as String, };
"arrivaltime": seg["start_arrivalTime"] as String, Map<String, Object> endMap = {
"departuretime": seg["start_departuretime"] as String, "id": seg["endid"] as int,
}, "ref": seg["ref"] as String,
"end": { "direction": seg["direction"] as String,
"id": seg["endid"] as int, "vehicle": seg["vehicleid"] as String,
"ref": seg["ref"] as String, "vehiclename": seg["vehiclename"] as String,
"direction": seg["direction"] as String, "station": seg["end_stationid"] as String,
"vehicle": seg["vehicleid"] as String, "stationname": seg["end_stationname"] as String,
"vehiclename": seg["vehiclename"] as String, "arrivaltime": seg["end_arrivaltime"] as String,
"station": seg["end_stationid"] as String, "departuretime": seg["end_departuretime"] as String,
"staionname": seg["end_stationname"] as String, };
"arrivaltime": seg["end_arrivalTime"] as String, Map<String, String> vehicleMap = {
"departuretime": seg["end_departuretime"] as String, "id": seg["vehicleid"] as String,
}, "name": seg["vehiclename"] as String,
"vehicle": { "line": seg["vehicleline"] as String,
"id": seg["vehicleid"] as String, };
"name": seg["vehiclename"] as String, Map<String, Object?> fullMap = {
"line": seg["vehicleline"] as String, "id": seg["id"] as int,
}, "ref": seg["ref"] as String,
"start": startMap,
"end": endMap,
"vehicle": vehicleMap,
}; };
return Segment.fromMap(fullMap); return Segment.fromMap(fullMap);
} }
@@ -819,7 +840,7 @@ class Line {
factory Line.fromJson(Map<String, dynamic> json) { factory Line.fromJson(Map<String, dynamic> json) {
Line line = Line( Line line = Line(
lineId: json["lineId"] as String, lineId: json["lineId"] ?? 'NO ID FOUND',
name: json["name"] as String, name: json["name"] as String,
mode: _vehicleTypeAsCatCode[int.parse(json["catCode"] as String)], mode: _vehicleTypeAsCatCode[int.parse(json["catCode"] as String)],
fgColor: fgColor:
@@ -908,15 +929,14 @@ class Departure {
Map<String, Object?> _toMap() { Map<String, Object?> _toMap() {
return { return {
"id": id,
"ref": ref, "ref": ref,
"direction": direction, "direction": direction,
"vehicle": vehicleId, "vehicle": vehicleId,
"vehiclename": vehicleName, "vehiclename": vehicleName,
"station": stationId, "station": stationId,
"stationname": stationName, "stationname": stationName,
"arrivaltime": arrivalTime, "arrivaltime": arrivalTime.toString(),
"departuretime": departureTime, "departuretime": departureTime.toString(),
}; };
} }
@@ -929,8 +949,8 @@ class Departure {
vehicleName: map["vehiclename"] as String, vehicleName: map["vehiclename"] as String,
stationId: map["station"] as String, stationId: map["station"] as String,
stationName: map["stationname"] as String, stationName: map["stationname"] as String,
arrivalTime: map["arrivaltime"] as DateTime, arrivalTime: DateTime.parse(map["arrivaltime"] as String),
departureTime: map["departuretime"] as DateTime, departureTime: DateTime.parse(map["departuretime"] as String),
); );
} }
@@ -1000,8 +1020,9 @@ class Departure {
String vehicle = Vehicle.tableName; String vehicle = Vehicle.tableName;
String station = Station.tableName; String station = Station.tableName;
database.execute("""CREATE TABLE IF NOT EXISTS '$tableName'( database.execute("""CREATE TABLE IF NOT EXISTS '$tableName'(
id INTEGER, id INTEGER PRIMARY KEY AUTOINCREMENT,
ref TEXT, ref TEXT,
direction TEXT,
vehicle TEXT, vehicle TEXT,
vehiclename TEXT, vehiclename TEXT,
station TEXT, station TEXT,