diff --git a/lib/main.dart b/lib/main.dart index 652136c..399ddc5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -233,12 +233,25 @@ class ChooseDeparturePage extends StatefulWidget { class _ChooseDeparturePageState extends State { List departureList = []; + Journey? singleJourney; void loadDepartures(Station startStation) async { departureList = await ApiHandler.departures(startStation.id); setState(() {}); } + void loadJourney(int journeyId) async { + singleJourney = await Journey.dbGet(journeyId); + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => JourneyDetailPage( + singleJourney: singleJourney as Journey, + ), + ), + ); + } + @override initState() { super.initState(); @@ -249,7 +262,9 @@ class _ChooseDeparturePageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text('RoutePicker for ${widget.startStation.name}'), + title: Text( + 'RoutePicker for ${widget.startStation.name} // ${widget.journeyId}', + ), ), body: Center( child: Column( @@ -265,12 +280,11 @@ class _ChooseDeparturePageState extends State { ), ElevatedButton( onPressed: () { - Navigator.push( - context, - MaterialPageRoute(builder: (context) => ChooseStationPage()), - ); + if (widget.journeyId != null) { + loadJourney(widget.journeyId as int); + } }, - child: const Text('Vehicle picked!'), + child: const Text('Finish Route'), ), ], ), @@ -355,7 +369,9 @@ class _ChooseArrivalPageState extends State { Widget build(BuildContext context) { return Scaffold( appBar: AppBar( - title: Text('Arrivals for ${widget.departure.vehicleName}'), + title: Text( + 'Arrivals for ${widget.departure.vehicleName}// ${widget.journeyId}', + ), ), body: Center( child: Column( @@ -370,15 +386,6 @@ class _ChooseArrivalPageState extends State { ), ), ), - 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 createState() => _JourneyDetailPageState(); +} + +class _JourneyDetailPageState extends State { + 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()), + ), + ], + ); + } +} \ No newline at end of file diff --git a/lib/transport_helper.dart b/lib/transport_helper.dart index f4355a7..3967d87 100644 --- a/lib/transport_helper.dart +++ b/lib/transport_helper.dart @@ -224,13 +224,19 @@ class Journey { int routeId, ) async { Database database = DbHelper.db; + String segment = Segment.tableName; var tmp = await database.rawQuery(""" - SELECT * FROM segments - JOIN routesegments ON routesegments.segmentid = segments.id + SELECT * FROM '$segment' + JOIN routesegments ON routesegments.segment = '$segment'.id WHERE routesegments.route = '$routeId' 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 _dbInsert() async { @@ -377,20 +383,29 @@ class Segment { id: map["id"] as int, ref: map["ref"] as String, start: Departure.fromMap( - map["start"] as Map, + map["start"] as Map, ), end: Departure.fromMap( - map["end"] as Map, + map["end"] as Map, ), - vehicle: Vehicle.fromMap(map["vehicle"] as Map), + vehicle: Vehicle.fromMap(map["vehicle"] as Map), ); } factory Segment.fromJson(Map 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( ref: json["JourneyDetail"]["ref"], - start: Departure.fromJson(json["Origin"]), - end: Departure.fromJson(json["Destination"]), + start: start, + end: end, vehicle: Vehicle.fromJson(json["Product"]), ); tmp.dbInsert(); @@ -398,6 +413,8 @@ class Segment { } static Future fromDepartures(Departure start, Departure end) async { + start.dbInsert(); + end.dbInsert(); Segment tmp = Segment( ref: start.ref, start: start, @@ -427,9 +444,9 @@ class Segment { CREATE TABLE IF NOT EXISTS '$tableName'( id INTEGER PRIMARY KEY AUTOINCREMENT, ref TEXT, - startid TEXT, - endid TEXT, - vehicleid INTEGER, + startid INTEGER, + endid INTEGER, + vehicleid TEXT, FOREIGN KEY(startid) REFERENCES '$departure'(stationid), FOREIGN KEY(endid) REFERENCES '$departure'(stationid), FOREIGN KEY(vehicleid) REFERENCES '$vehicle'(vehicleid) @@ -445,14 +462,15 @@ class Segment { """SELECT s.id, s.ref, s.startid, s.endid, s.vehicleid, start.station as start_stationid, 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.arrivaltime as end_arrivaltime, end.departuretime as end_departuretime, v.name as vehiclename, v.line as vehicleline FROM $tableName s JOIN $departure start ON s.startid = start.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 @@ -460,37 +478,40 @@ class Segment { // var seg = tmp.map((x) => Segment.fromMap(x)); // return seg.first; - var seg = tmp.first; - var fullMap = { - "id": seg["id"], - "ref": seg["ref"], - "start": { - "id": seg["startid"] as int, - "ref": seg["ref"] as String, - "direction": seg["direction"] as String, - "vehicle": seg["vehicleid"] as String, - "vehiclename": seg["vehiclename"] as String, - "station": seg["start_stationid"] as String, - "staionname": seg["start_stationname"] as String, - "arrivaltime": seg["start_arrivalTime"] as String, - "departuretime": seg["start_departuretime"] as String, - }, - "end": { - "id": seg["endid"] as int, - "ref": seg["ref"] as String, - "direction": seg["direction"] as String, - "vehicle": seg["vehicleid"] as String, - "vehiclename": seg["vehiclename"] as String, - "station": seg["end_stationid"] as String, - "staionname": seg["end_stationname"] as String, - "arrivaltime": seg["end_arrivalTime"] as String, - "departuretime": seg["end_departuretime"] as String, - }, - "vehicle": { - "id": seg["vehicleid"] as String, - "name": seg["vehiclename"] as String, - "line": seg["vehicleline"] as String, - }, + Map seg = tmp.first; + Map startMap = { + "id": seg["startid"] as int, + "ref": seg["ref"] as String, + "direction": seg["direction"] as String, + "vehicle": seg["vehicleid"] as String, + "vehiclename": seg["vehiclename"] as String, + "station": seg["start_stationid"] as String, + "stationname": seg["start_stationname"] as String, + "arrivaltime": seg["start_arrivaltime"] as String, + "departuretime": seg["start_departuretime"] as String, + }; + Map endMap = { + "id": seg["endid"] as int, + "ref": seg["ref"] as String, + "direction": seg["direction"] as String, + "vehicle": seg["vehicleid"] as String, + "vehiclename": seg["vehiclename"] as String, + "station": seg["end_stationid"] as String, + "stationname": seg["end_stationname"] as String, + "arrivaltime": seg["end_arrivaltime"] as String, + "departuretime": seg["end_departuretime"] as String, + }; + Map vehicleMap = { + "id": seg["vehicleid"] as String, + "name": seg["vehiclename"] as String, + "line": seg["vehicleline"] as String, + }; + Map fullMap = { + "id": seg["id"] as int, + "ref": seg["ref"] as String, + "start": startMap, + "end": endMap, + "vehicle": vehicleMap, }; return Segment.fromMap(fullMap); } @@ -819,7 +840,7 @@ class Line { factory Line.fromJson(Map json) { Line line = Line( - lineId: json["lineId"] as String, + lineId: json["lineId"] ?? 'NO ID FOUND', name: json["name"] as String, mode: _vehicleTypeAsCatCode[int.parse(json["catCode"] as String)], fgColor: @@ -908,15 +929,14 @@ class Departure { Map _toMap() { return { - "id": id, "ref": ref, "direction": direction, "vehicle": vehicleId, "vehiclename": vehicleName, "station": stationId, "stationname": stationName, - "arrivaltime": arrivalTime, - "departuretime": departureTime, + "arrivaltime": arrivalTime.toString(), + "departuretime": departureTime.toString(), }; } @@ -929,8 +949,8 @@ class Departure { vehicleName: map["vehiclename"] as String, stationId: map["station"] as String, stationName: map["stationname"] as String, - arrivalTime: map["arrivaltime"] as DateTime, - departureTime: map["departuretime"] as DateTime, + arrivalTime: DateTime.parse(map["arrivaltime"] as String), + departureTime: DateTime.parse(map["departuretime"] as String), ); } @@ -1000,8 +1020,9 @@ class Departure { String vehicle = Vehicle.tableName; String station = Station.tableName; database.execute("""CREATE TABLE IF NOT EXISTS '$tableName'( - id INTEGER, + id INTEGER PRIMARY KEY AUTOINCREMENT, ref TEXT, + direction TEXT, vehicle TEXT, vehiclename TEXT, station TEXT, @@ -1128,4 +1149,4 @@ enum VehicleType { subway, regionalTrain, PLACEHOLDER, -} \ No newline at end of file +}