2021-08-23 07:59:30 +03:00
|
|
|
// To parse this JSON data, do
|
|
|
|
//
|
|
|
|
// final trainData = trainDataFromJson(jsonString);
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
import 'dart:convert';
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
TrainData trainDataFromJson(String str) => TrainData.fromJson(json.decode(str));
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
String trainDataToJson(TrainData data) => json.encode(data.toJson());
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
/// Results of scrapping InfoFer website for train info
|
|
|
|
class TrainData {
|
|
|
|
TrainData({
|
|
|
|
required this.date,
|
|
|
|
required this.number,
|
|
|
|
required this.operator,
|
|
|
|
required this.rank,
|
|
|
|
required this.route,
|
|
|
|
required this.stations,
|
|
|
|
this.status,
|
|
|
|
});
|
|
|
|
|
|
|
|
final String date;
|
|
|
|
final String number;
|
|
|
|
final String operator;
|
|
|
|
final String rank;
|
|
|
|
final Route route;
|
|
|
|
final List<Station> stations;
|
|
|
|
final TrainDataStatus? status;
|
|
|
|
|
|
|
|
factory TrainData.fromJson(Map<String, dynamic> json) => TrainData(
|
|
|
|
date: json["date"],
|
|
|
|
number: json["number"],
|
|
|
|
operator: json["operator"],
|
|
|
|
rank: json["rank"],
|
|
|
|
route: Route.fromJson(json["route"]),
|
|
|
|
stations: List<Station>.from(
|
|
|
|
json["stations"].map((x) => Station.fromJson(x))),
|
|
|
|
status: json["status"] == null
|
|
|
|
? null
|
|
|
|
: TrainDataStatus.fromJson(json["status"]),
|
2019-09-15 02:33:07 +03:00
|
|
|
);
|
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
"date": date,
|
|
|
|
"number": number,
|
|
|
|
"operator": operator,
|
|
|
|
"rank": rank,
|
|
|
|
"route": route.toJson(),
|
|
|
|
"stations": List<dynamic>.from(stations.map((x) => x.toJson())),
|
|
|
|
"status": status?.toJson(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Route of the train
|
|
|
|
class Route {
|
|
|
|
Route({
|
|
|
|
required this.from,
|
|
|
|
required this.to,
|
|
|
|
});
|
|
|
|
|
|
|
|
final String from;
|
|
|
|
final String to;
|
|
|
|
|
|
|
|
factory Route.fromJson(Map<String, dynamic> json) => Route(
|
|
|
|
from: json["from"],
|
|
|
|
to: json["to"],
|
2019-07-23 16:56:51 +03:00
|
|
|
);
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
"from": from,
|
|
|
|
"to": to,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class Station {
|
|
|
|
Station({
|
|
|
|
this.arrival,
|
|
|
|
this.departure,
|
|
|
|
required this.km,
|
|
|
|
required this.name,
|
|
|
|
this.platform,
|
|
|
|
this.stoppingTime,
|
|
|
|
});
|
|
|
|
|
|
|
|
final StationArrDepTime? arrival;
|
|
|
|
final StationArrDepTime? departure;
|
|
|
|
final int km;
|
|
|
|
final String name;
|
|
|
|
final String? platform;
|
|
|
|
final int? stoppingTime;
|
|
|
|
|
|
|
|
factory Station.fromJson(Map<String, dynamic> json) => Station(
|
|
|
|
arrival: json["arrival"] == null
|
|
|
|
? null
|
|
|
|
: StationArrDepTime.fromJson(json["arrival"]),
|
|
|
|
departure: json["departure"] == null
|
|
|
|
? null
|
|
|
|
: StationArrDepTime.fromJson(json["departure"]),
|
|
|
|
km: json["km"],
|
|
|
|
name: json["name"],
|
|
|
|
platform: json["platform"] == null ? null : json["platform"],
|
|
|
|
stoppingTime:
|
|
|
|
json["stoppingTime"] == null ? null : json["stoppingTime"],
|
|
|
|
);
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
"arrival": arrival?.toJson(),
|
|
|
|
"departure": departure?.toJson(),
|
|
|
|
"km": km,
|
|
|
|
"name": name,
|
|
|
|
"platform": platform == null ? null : platform,
|
|
|
|
"stoppingTime": stoppingTime == null ? null : stoppingTime,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
class StationArrDepTime {
|
|
|
|
StationArrDepTime({
|
|
|
|
required this.scheduleTime,
|
|
|
|
this.status,
|
|
|
|
});
|
|
|
|
|
|
|
|
final String scheduleTime;
|
|
|
|
final StationArrDepTimeStatus? status;
|
|
|
|
|
|
|
|
factory StationArrDepTime.fromJson(Map<String, dynamic> json) =>
|
|
|
|
StationArrDepTime(
|
|
|
|
scheduleTime: json["scheduleTime"],
|
|
|
|
status: json["status"] == null ? null : StationArrDepTimeStatus.fromJson(json["status"]),
|
|
|
|
);
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
"scheduleTime": scheduleTime,
|
|
|
|
"status": status?.toJson(),
|
|
|
|
};
|
|
|
|
}
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
class StationArrDepTimeStatus {
|
|
|
|
StationArrDepTimeStatus({
|
|
|
|
required this.delay,
|
|
|
|
required this.real,
|
|
|
|
});
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
final int delay;
|
|
|
|
final bool real;
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
factory StationArrDepTimeStatus.fromJson(Map<String, dynamic> json) =>
|
|
|
|
StationArrDepTimeStatus(
|
|
|
|
delay: json["delay"],
|
|
|
|
real: json["real"],
|
|
|
|
);
|
2019-07-23 16:56:51 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
"delay": delay,
|
|
|
|
"real": real,
|
|
|
|
};
|
2019-07-23 16:56:51 +03:00
|
|
|
}
|
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
class TrainDataStatus {
|
|
|
|
TrainDataStatus({
|
|
|
|
required this.delay,
|
|
|
|
required this.state,
|
|
|
|
required this.station,
|
|
|
|
});
|
2019-07-23 16:56:51 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
final int delay;
|
|
|
|
final State state;
|
|
|
|
final String station;
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
factory TrainDataStatus.fromJson(Map<String, dynamic> json) =>
|
|
|
|
TrainDataStatus(
|
|
|
|
delay: json["delay"],
|
|
|
|
state: stateValues.map[json["state"]]!,
|
|
|
|
station: json["station"],
|
|
|
|
);
|
2019-09-15 02:33:07 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
Map<String, dynamic> toJson() => {
|
|
|
|
"delay": delay,
|
|
|
|
"state": stateValues.reverse[state],
|
|
|
|
"station": station,
|
|
|
|
};
|
2019-09-15 02:33:07 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() {
|
2021-08-23 07:59:30 +03:00
|
|
|
String result = '';
|
|
|
|
if (delay == 0) {
|
|
|
|
result += 'La timp';
|
2019-09-15 02:33:07 +03:00
|
|
|
}
|
2021-08-23 07:59:30 +03:00
|
|
|
else {
|
|
|
|
result += '${delay.abs()} min';
|
2019-09-15 02:33:07 +03:00
|
|
|
}
|
2021-08-23 07:59:30 +03:00
|
|
|
result += ' la ';
|
|
|
|
switch (state) {
|
|
|
|
case State.PASSING:
|
|
|
|
result += 'trecerea fără oprire prin';
|
|
|
|
break;
|
|
|
|
case State.ARRIVAL:
|
|
|
|
result += 'sosirea în';
|
|
|
|
break;
|
|
|
|
case State.DEPARTURE:
|
|
|
|
result += 'plecarea din';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result += station;
|
2019-07-23 16:56:51 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
enum State { PASSING, ARRIVAL, DEPARTURE }
|
2019-07-23 16:56:51 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
final stateValues = EnumValues({
|
|
|
|
"arrival": State.ARRIVAL,
|
|
|
|
"departure": State.DEPARTURE,
|
|
|
|
"passing": State.PASSING
|
|
|
|
});
|
2019-07-23 16:56:51 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
class EnumValues<T> {
|
|
|
|
Map<String, T> map;
|
|
|
|
Map<T, String>? reverseMap;
|
2019-07-23 16:56:51 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
EnumValues(this.map);
|
2019-07-23 16:56:51 +03:00
|
|
|
|
2021-08-23 07:59:30 +03:00
|
|
|
Map<T, String> get reverse {
|
|
|
|
if (reverseMap == null) {
|
|
|
|
reverseMap = map.map((k, v) => new MapEntry(v, k));
|
|
|
|
}
|
|
|
|
return reverseMap!;
|
2019-07-23 16:56:51 +03:00
|
|
|
}
|
2019-09-15 02:33:07 +03:00
|
|
|
}
|