change app name, add route calculator
This commit is contained in:
286
lib/components/calculator/calculator.dart
Normal file
286
lib/components/calculator/calculator.dart
Normal file
@@ -0,0 +1,286 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:mileograph_flutter/objects/objects.dart';
|
||||
import 'package:mileograph_flutter/services/apiService.dart';
|
||||
import 'package:mileograph_flutter/services/dataService.dart';
|
||||
import './routeSummaryWidget.dart';
|
||||
|
||||
class RouteCalculatorLoader extends StatelessWidget {
|
||||
const RouteCalculatorLoader({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final data = context.read<DataService>();
|
||||
|
||||
return FutureBuilder<List<Station>>(
|
||||
future: data.fetchStations(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
return Center(child: Text('Error: ${snapshot.error}'));
|
||||
}
|
||||
|
||||
final stations = snapshot.data!;
|
||||
return RouteCalculator(allStations: stations);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class StationAutocomplete extends StatefulWidget {
|
||||
const StationAutocomplete({
|
||||
super.key,
|
||||
required this.allStations,
|
||||
this.initialValue,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
final List<Station> allStations;
|
||||
final String? initialValue;
|
||||
final ValueChanged<String> onChanged;
|
||||
|
||||
@override
|
||||
State<StationAutocomplete> createState() => _StationAutocompleteState();
|
||||
}
|
||||
|
||||
class _StationAutocompleteState extends State<StationAutocomplete> {
|
||||
late final TextEditingController _controller;
|
||||
|
||||
// Simulated list of over 10,000 stations
|
||||
final List<String> stations = List.generate(10000, (i) => 'Station $i');
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_controller = TextEditingController(text: widget.initialValue ?? '');
|
||||
_controller.addListener(() {
|
||||
widget.onChanged(_controller.text);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Autocomplete<String>(
|
||||
optionsBuilder: (TextEditingValue textEditingValue) {
|
||||
if (textEditingValue.text.isEmpty) {
|
||||
return const Iterable<String>.empty();
|
||||
}
|
||||
final query = textEditingValue.text.toLowerCase();
|
||||
final matches = widget.allStations
|
||||
.map((s) => s.name)
|
||||
.where((name) => name.toLowerCase().contains(query))
|
||||
.toList();
|
||||
|
||||
matches.sort((a, b) => a.length.compareTo(b.length));
|
||||
|
||||
return matches.take(10);
|
||||
},
|
||||
onSelected: (String selection) {
|
||||
_controller.text = selection;
|
||||
widget.onChanged(selection);
|
||||
},
|
||||
fieldViewBuilder:
|
||||
(context, textEditingController, focusNode, onFieldSubmitted) {
|
||||
textEditingController.value = _controller.value;
|
||||
return TextField(
|
||||
controller: textEditingController,
|
||||
focusNode: focusNode,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Select station',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class RouteCalculator extends StatefulWidget {
|
||||
final List<Station> allStations;
|
||||
|
||||
const RouteCalculator({super.key, required this.allStations});
|
||||
|
||||
@override
|
||||
State<RouteCalculator> createState() => _RouteCalculatorState();
|
||||
}
|
||||
|
||||
class _RouteCalculatorState extends State<RouteCalculator> {
|
||||
List<String> stations = [''];
|
||||
|
||||
RouteResult? _routeResult;
|
||||
RouteResult? get result => _routeResult;
|
||||
String? _errorMessage;
|
||||
|
||||
bool _showDetails = false;
|
||||
|
||||
Future<void> _calculateRoute(List<String> stations) async {
|
||||
setState(() {
|
||||
_errorMessage = null;
|
||||
_routeResult = null;
|
||||
});
|
||||
final api = context.read<ApiService>(); // context is valid here
|
||||
final res = await api.post('/route/distance2', {
|
||||
'route': stations.where((s) => s.trim().isNotEmpty).toList(),
|
||||
});
|
||||
|
||||
if (res['error'] == false) {
|
||||
setState(() {
|
||||
_routeResult = RouteResult.fromJson(res);
|
||||
});
|
||||
} else {
|
||||
setState(() {
|
||||
_errorMessage =
|
||||
RouteError.fromJson(res["error_obj"][0]).msg ??
|
||||
'Unknown error occurred';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _addStation() {
|
||||
setState(() {
|
||||
stations.add('');
|
||||
});
|
||||
}
|
||||
|
||||
void _removeStation(int index) {
|
||||
setState(() {
|
||||
stations.removeAt(index);
|
||||
});
|
||||
}
|
||||
|
||||
void _updateStation(int index, String value) {
|
||||
setState(() {
|
||||
stations[index] = value;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (_showDetails && _routeResult != null) {
|
||||
return RouteDetailsView(
|
||||
route: _routeResult!.calculatedRoute,
|
||||
costs: _routeResult!.costs,
|
||||
onBack: () => setState(() => _showDetails = false),
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ReorderableListView(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
onReorder: (oldIndex, newIndex) {
|
||||
if (newIndex > oldIndex) newIndex -= 1;
|
||||
setState(() {
|
||||
final moved = stations.removeAt(oldIndex);
|
||||
stations.insert(newIndex, moved);
|
||||
});
|
||||
},
|
||||
children: List.generate(stations.length, (index) {
|
||||
return Container(
|
||||
key: ValueKey('$index-${stations[index]}'),
|
||||
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
ReorderableDragStartListener(
|
||||
index: index,
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.only(top: 16),
|
||||
child: Icon(Icons.drag_indicator),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Expanded(
|
||||
child: Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: StationAutocomplete(
|
||||
allStations: widget.allStations,
|
||||
initialValue: stations[index],
|
||||
onChanged: (val) =>
|
||||
_updateStation(index, val),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.close),
|
||||
onPressed: () => _removeStation(index),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
),
|
||||
if (_errorMessage != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
_errorMessage!,
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||||
),
|
||||
)
|
||||
else if (_routeResult != null)
|
||||
RouteSummaryWidget(
|
||||
distance: _routeResult!.distance,
|
||||
onDetailsPressed: () => setState(() => _showDetails = true),
|
||||
)
|
||||
else
|
||||
SizedBox.shrink(),
|
||||
const SizedBox(height: 10),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ElevatedButton.icon(
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('Add Station'),
|
||||
onPressed: _addStation,
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
ElevatedButton.icon(
|
||||
icon: const Icon(Icons.route),
|
||||
label: const Text('Calculate Route'),
|
||||
onPressed: () async {
|
||||
await _calculateRoute(stations);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget debugPanel(List<String> stations) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
||||
child: Wrap(
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
spacing: 8,
|
||||
children: [
|
||||
const Text(
|
||||
'Current Order:',
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
...stations.map((s) => Chip(label: Text(s.isEmpty ? '<empty>' : s))),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
73
lib/components/calculator/routeSummaryWidget.dart
Normal file
73
lib/components/calculator/routeSummaryWidget.dart
Normal file
@@ -0,0 +1,73 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class RouteSummaryWidget extends StatelessWidget {
|
||||
final double distance;
|
||||
final VoidCallback onDetailsPressed;
|
||||
|
||||
const RouteSummaryWidget({
|
||||
super.key,
|
||||
required this.distance,
|
||||
required this.onDetailsPressed,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
"Total Distance: ${distance.toStringAsFixed(2)} mi",
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: onDetailsPressed,
|
||||
child: const Text("View Details"),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class RouteDetailsView extends StatelessWidget {
|
||||
final List<String> route;
|
||||
final List<double> costs;
|
||||
final VoidCallback onBack;
|
||||
|
||||
const RouteDetailsView({
|
||||
super.key,
|
||||
required this.route,
|
||||
required this.costs,
|
||||
required this.onBack,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: TextButton.icon(
|
||||
onPressed: onBack,
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
label: const Text('Back'),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: route.length,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Text(route[index]),
|
||||
trailing: Text("${costs[index].toStringAsFixed(2)} mi"),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -21,52 +21,53 @@ class TopTractionPanel extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
Column(
|
||||
children: List.generate(data.homepageStats?.topLocos.length ?? 0, (
|
||||
index,
|
||||
) {
|
||||
final loco = data.homepageStats!.topLocos[index];
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
child: Container(
|
||||
margin: EdgeInsets.symmetric(horizontal: 0, vertical: 8),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
children: List.generate(
|
||||
data.homepageStats?.topLocos.length ?? 0,
|
||||
(index) {
|
||||
final loco = data.homepageStats!.topLocos[index];
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
child: Container(
|
||||
margin: EdgeInsets.symmetric(horizontal: 0, vertical: 8),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: '${index + 1}. ',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(
|
||||
text: '${index + 1}. ',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
TextSpan(
|
||||
text:
|
||||
'${loco.locoClass} ${loco.locoNumber}',
|
||||
),
|
||||
],
|
||||
TextSpan(
|
||||
text:
|
||||
'${loco.locoClass} ${loco.number}',
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
'${loco.locoName}',
|
||||
style: TextStyle(fontStyle: FontStyle.italic),
|
||||
),
|
||||
],
|
||||
),
|
||||
Text('${loco.locoMileage?.toStringAsFixed(1)} mi'),
|
||||
],
|
||||
Text(
|
||||
'${loco.name}',
|
||||
style: TextStyle(fontStyle: FontStyle.italic),
|
||||
),
|
||||
],
|
||||
),
|
||||
Text('${loco.mileage?.toStringAsFixed(1)} mi'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -19,12 +19,22 @@ class LoginScreen extends StatelessWidget {
|
||||
Text.rich(
|
||||
TextSpan(
|
||||
children: [
|
||||
TextSpan(text: "Mile"),
|
||||
TextSpan(
|
||||
text: "Mile",
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).textTheme.bodyLarge?.color,
|
||||
),
|
||||
),
|
||||
TextSpan(
|
||||
text: "O",
|
||||
style: TextStyle(color: Colors.red),
|
||||
),
|
||||
TextSpan(text: "graph"),
|
||||
TextSpan(
|
||||
text: "graph",
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).textTheme.bodyLarge?.color,
|
||||
),
|
||||
),
|
||||
],
|
||||
style: TextStyle(
|
||||
decoration: TextDecoration.none,
|
||||
|
||||
10
lib/components/pages/calculator.dart
Normal file
10
lib/components/pages/calculator.dart
Normal file
@@ -0,0 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mileograph_flutter/components/calculator/calculator.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:mileograph_flutter/services/dataService.dart';
|
||||
|
||||
class CalculatorPage extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return RouteCalculatorLoader();
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ class TractionPage extends StatelessWidget {
|
||||
margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(16),
|
||||
child: Text('${loco.locoClass} ${loco.locoNumber}'),
|
||||
child: Text('${loco.locoClass} ${loco.number}'),
|
||||
),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:dynamic_color/dynamic_color.dart';
|
||||
import 'package:mileograph_flutter/components/pages/calculator.dart';
|
||||
import 'package:mileograph_flutter/components/pages/traction.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
@@ -124,7 +125,7 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
int pageIndex = 0;
|
||||
final List<Widget> contentPages = [
|
||||
Dashboard(),
|
||||
Center(child: Text("Calculator Page")),
|
||||
CalculatorPage(),
|
||||
LegsPage(),
|
||||
TractionPage(),
|
||||
Center(child: Text("Trips Page")),
|
||||
|
||||
@@ -81,37 +81,71 @@ class YearlyMileage {
|
||||
);
|
||||
}
|
||||
|
||||
class LocoSummary {
|
||||
final String locoType, locoClass, locoNumber, locoName, locoOperator;
|
||||
final String? locoNotes, locoEvn;
|
||||
final int locoId;
|
||||
final int? locoJourneys;
|
||||
final double? locoMileage;
|
||||
class Loco {
|
||||
final int id;
|
||||
final String type, number, locoClass;
|
||||
final String? name, operator, notes, evn;
|
||||
|
||||
LocoSummary({
|
||||
required this.locoType,
|
||||
Loco({
|
||||
required this.id,
|
||||
required this.type,
|
||||
required this.number,
|
||||
required this.name,
|
||||
required this.locoClass,
|
||||
required this.locoNumber,
|
||||
required this.locoName,
|
||||
required this.locoOperator,
|
||||
this.locoNotes,
|
||||
this.locoEvn,
|
||||
required this.locoId,
|
||||
this.locoMileage,
|
||||
this.locoJourneys,
|
||||
required this.operator,
|
||||
this.notes,
|
||||
this.evn,
|
||||
});
|
||||
|
||||
factory LocoSummary.fromJson(Map<String, dynamic> json) => LocoSummary(
|
||||
locoType: json['loco_type'],
|
||||
factory Loco.fromJson(Map<String, dynamic> json) => Loco(
|
||||
id: json['loco_id'],
|
||||
type: json['loco_type'],
|
||||
number: json['loco_number'],
|
||||
name: json['loco_name'] ?? "",
|
||||
locoClass: json['loco_class'],
|
||||
operator: json['loco_operator'],
|
||||
notes: json['loco_notes'],
|
||||
evn: json['loco_evn'],
|
||||
);
|
||||
}
|
||||
|
||||
class LocoSummary extends Loco {
|
||||
final double? mileage;
|
||||
final int? journeys;
|
||||
|
||||
LocoSummary({
|
||||
required int locoId,
|
||||
required String locoType,
|
||||
required String locoNumber,
|
||||
required String locoName,
|
||||
required String locoClass,
|
||||
required String locoOperator,
|
||||
String? locoNotes,
|
||||
String? locoEvn,
|
||||
this.mileage,
|
||||
this.journeys,
|
||||
}) : super(
|
||||
id: locoId,
|
||||
type: locoType,
|
||||
number: locoNumber,
|
||||
name: locoName,
|
||||
locoClass: locoClass,
|
||||
operator: locoOperator,
|
||||
notes: locoNotes,
|
||||
evn: locoEvn,
|
||||
);
|
||||
|
||||
factory LocoSummary.fromJson(Map<String, dynamic> json) => LocoSummary(
|
||||
locoId: json['loco_id'],
|
||||
locoType: json['loco_type'],
|
||||
locoNumber: json['loco_number'],
|
||||
locoName: json['loco_name'] ?? "",
|
||||
locoClass: json['loco_class'],
|
||||
locoOperator: json['loco_operator'],
|
||||
locoNotes: json['loco_notes'],
|
||||
locoEvn: json['loco_evn'],
|
||||
locoId: json['loco_id'],
|
||||
locoMileage: (json['loco_mileage'] as num).toDouble(),
|
||||
locoJourneys: json['loco_journeys'],
|
||||
mileage: (json['loco_mileage'] as num?)?.toDouble(),
|
||||
journeys: json['loco_journeys'],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -153,34 +187,6 @@ class TripSummary {
|
||||
);
|
||||
}
|
||||
|
||||
class Loco {
|
||||
final int id;
|
||||
final String type, number, name, locoClass, operator;
|
||||
final String? notes, evn;
|
||||
|
||||
Loco({
|
||||
required this.id,
|
||||
required this.type,
|
||||
required this.number,
|
||||
required this.name,
|
||||
required this.locoClass,
|
||||
required this.operator,
|
||||
this.notes,
|
||||
this.evn,
|
||||
});
|
||||
|
||||
factory Loco.fromJson(Map<String, dynamic> json) => Loco(
|
||||
id: json['loco_id'],
|
||||
type: json['loco_type'],
|
||||
number: json['loco_number'],
|
||||
name: json['loco_name'] ?? "",
|
||||
locoClass: json['loco_class'],
|
||||
operator: json['loco_operator'],
|
||||
notes: json['loco_notes'],
|
||||
evn: json['loco_evn'],
|
||||
);
|
||||
}
|
||||
|
||||
class Leg {
|
||||
final int id, tripId, timezone, driving;
|
||||
final String start, end, route, network, notes, headcode, user;
|
||||
@@ -224,3 +230,58 @@ class Leg {
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
class RouteError {
|
||||
final String error;
|
||||
final String msg;
|
||||
|
||||
RouteError({required this.error, required this.msg});
|
||||
|
||||
factory RouteError.fromJson(Map<String, dynamic> json) {
|
||||
return RouteError(error: json["error"], msg: json["msg"]);
|
||||
}
|
||||
}
|
||||
|
||||
class RouteResult {
|
||||
final List<String> inputRoute;
|
||||
final List<String> calculatedRoute;
|
||||
final List<double> costs;
|
||||
final double distance;
|
||||
|
||||
RouteResult({
|
||||
required this.inputRoute,
|
||||
required this.calculatedRoute,
|
||||
required this.costs,
|
||||
required this.distance,
|
||||
});
|
||||
|
||||
factory RouteResult.fromJson(Map<String, dynamic> json) {
|
||||
return RouteResult(
|
||||
inputRoute: List<String>.from(json['input_route']),
|
||||
calculatedRoute: List<String>.from(json['calculated_route']),
|
||||
costs: (json['costs'] as List).map((e) => (e as num).toDouble()).toList(),
|
||||
distance: (json['distance'] as num).toDouble(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Station {
|
||||
final int id;
|
||||
final String name;
|
||||
final String network;
|
||||
final String country;
|
||||
|
||||
Station({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.network,
|
||||
required this.country,
|
||||
});
|
||||
|
||||
factory Station.fromJson(Map<String, dynamic> json) => Station(
|
||||
id: json['id'],
|
||||
name: json['name'],
|
||||
network: json['network'],
|
||||
country: json['country'],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,15 +7,22 @@ class DataService extends ChangeNotifier {
|
||||
|
||||
DataService({required this.api});
|
||||
|
||||
// Homepage Data
|
||||
HomepageStats? _homepageStats;
|
||||
HomepageStats? get homepageStats => _homepageStats;
|
||||
|
||||
// Legs Data
|
||||
List<Leg> _legs = [];
|
||||
List<Leg> get legs => _legs;
|
||||
|
||||
// Traction Data
|
||||
List<LocoSummary> _traction = [];
|
||||
List<LocoSummary> get traction => _traction;
|
||||
|
||||
// Station Data
|
||||
List<Station>? _cachedStations;
|
||||
DateTime? _stationsFetchedAt;
|
||||
|
||||
bool _isHomepageLoading = false;
|
||||
bool get isHomepageLoading => _isHomepageLoading;
|
||||
|
||||
@@ -84,4 +91,23 @@ class DataService extends ChangeNotifier {
|
||||
.mileage ??
|
||||
0;
|
||||
}
|
||||
|
||||
Future<List<Station>> fetchStations() async {
|
||||
final now = DateTime.now();
|
||||
|
||||
// If cache exists and is less than 30 minutes old, return it
|
||||
if (_cachedStations != null &&
|
||||
_stationsFetchedAt != null &&
|
||||
now.difference(_stationsFetchedAt!) < Duration(minutes: 30)) {
|
||||
return _cachedStations!;
|
||||
}
|
||||
|
||||
final response = await api.get('/location');
|
||||
final parsed = (response as List).map((e) => Station.fromJson(e)).toList();
|
||||
|
||||
_cachedStations = parsed;
|
||||
_stationsFetchedAt = now;
|
||||
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user