list traction and add leaderboard panel

This commit is contained in:
2025-07-25 17:36:26 +01:00
parent 985eddb35a
commit 3bef606d41
7 changed files with 191 additions and 66 deletions

View File

@@ -0,0 +1,70 @@
import 'package:flutter/material.dart';
import 'package:mileograph_flutter/services/dataService.dart';
import 'package:provider/provider.dart';
class LeaderboardPanel extends StatelessWidget {
Widget build(BuildContext context) {
final data = context.watch<DataService>();
return Padding(
padding: const EdgeInsets.all(10.0),
child: Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Leaderboard",
style: TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
decoration: TextDecoration.underline,
),
),
Column(
children: List.generate(
data.homepageStats?.leaderboard.length ?? 0,
(index) {
final leaderboardEntry =
data.homepageStats!.leaderboard[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: [
Text.rich(
TextSpan(
children: [
TextSpan(
text: '${index + 1}. ',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
TextSpan(
text: '${leaderboardEntry.userFullName}',
),
],
),
),
Text(
'${leaderboardEntry.mileage.toStringAsFixed(1)} mi',
),
],
),
),
),
);
},
),
),
],
),
),
);
}
}

View File

@@ -8,52 +8,69 @@ class TopTractionPanel extends StatelessWidget {
final data = context.watch<DataService>(); final data = context.watch<DataService>();
return Padding( return Padding(
padding: const EdgeInsets.all(10.0), padding: const EdgeInsets.all(10.0),
child: Card( child: Column( child: Card(
mainAxisAlignment: MainAxisAlignment.center, child: Column(
children: [ mainAxisAlignment: MainAxisAlignment.center,
Text("Top Traction", style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold, decoration: TextDecoration.underline)), children: [
Column( Text(
children: List.generate( "Top Traction",
data.homepageStats?.topLocos.length ?? 0, style: TextStyle(
(index) { fontSize: 24,
final loco = data.homepageStats!.topLocos[index]; fontWeight: FontWeight.bold,
return Container( decoration: TextDecoration.underline,
width: double.infinity, ),
child: Card( ),
margin: EdgeInsets.symmetric(horizontal: 0, vertical: 8), Column(
child: Padding( children: List.generate(data.homepageStats?.topLocos.length ?? 0, (
padding: EdgeInsets.all(16), index,
) {
child: Row( final loco = data.homepageStats!.topLocos[index];
mainAxisAlignment: MainAxisAlignment.spaceBetween, return Container(
children: [ width: double.infinity,
Column( child: Container(
crossAxisAlignment: CrossAxisAlignment.start, margin: EdgeInsets.symmetric(horizontal: 0, vertical: 8),
children: [ child: Padding(
Text.rich( padding: EdgeInsets.all(8),
TextSpan(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
TextSpan(text: '${index + 1}. ', style: TextStyle(fontWeight: FontWeight.bold)), Column(
TextSpan(text: '${loco.locoClass} ${loco.locoNumber}'), crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text.rich(
TextSpan(
children: [
TextSpan(
text: '${index + 1}. ',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
TextSpan(
text:
'${loco.locoClass} ${loco.locoNumber}',
),
],
),
),
Text(
'${loco.locoName}',
style: TextStyle(fontStyle: FontStyle.italic),
),
],
),
Text('${loco.locoMileage?.toStringAsFixed(1)} mi'),
], ],
), ),
), ),
Text('${loco.locoName}', style: TextStyle(fontStyle: FontStyle.italic)), ),
);
], }),
),
Text('${loco.locoMileage?.toStringAsFixed(1)} mi'),
],
), ),
), ],
), ),
); ),
},
),
)
],
)),
); );
} }
} }

View File

@@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mileograph_flutter/components/dashboard/leaderboardPanel.dart';
import 'package:mileograph_flutter/services/authservice.dart'; import 'package:mileograph_flutter/services/authservice.dart';
import 'package:mileograph_flutter/services/dataService.dart'; import 'package:mileograph_flutter/services/dataService.dart';
import 'package:mileograph_flutter/components/dashboard/topTractionPanel.dart'; import 'package:mileograph_flutter/components/dashboard/topTractionPanel.dart';
@@ -84,7 +85,7 @@ class DashboardHeader extends StatelessWidget {
Expanded( Expanded(
child: ListView( child: ListView(
scrollDirection: Axis.vertical, scrollDirection: Axis.vertical,
children: [TopTractionPanel()], children: [TopTractionPanel(), LeaderboardPanel()],
), ),
), ),
], ],

View File

@@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
import 'package:mileograph_flutter/objects/objects.dart';
import 'package:provider/provider.dart';
import 'package:mileograph_flutter/services/dataService.dart';
class TractionPage extends StatelessWidget {
Widget build(BuildContext context) {
final data = context.watch<DataService>();
return ListView.builder(
itemCount: data.traction.length,
itemBuilder: (context, index) {
final loco = data.traction[index];
return Card(
margin: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Padding(
padding: EdgeInsets.all(16),
child: Text('${loco.locoClass} ${loco.locoNumber}'),
),
);
},
);
}
}

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:dynamic_color/dynamic_color.dart'; import 'package:dynamic_color/dynamic_color.dart';
import 'package:mileograph_flutter/components/pages/traction.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:mileograph_flutter/components/pages/legs.dart'; import 'package:mileograph_flutter/components/pages/legs.dart';
@@ -125,7 +126,7 @@ class _MyHomePageState extends State<MyHomePage> {
Dashboard(), Dashboard(),
Center(child: Text("Calculator Page")), Center(child: Text("Calculator Page")),
LegsPage(), LegsPage(),
Center(child: Text("Traction Page")), TractionPage(),
Center(child: Text("Trips Page")), Center(child: Text("Trips Page")),
]; ];
@@ -148,6 +149,9 @@ class _MyHomePageState extends State<MyHomePage> {
if (data.legs.isEmpty) { if (data.legs.isEmpty) {
data.fetchLegs(); data.fetchLegs();
} }
if (data.traction.isEmpty) {
data.fetchHadTraction();
}
}); });
} }
} }
@@ -162,12 +166,7 @@ class _MyHomePageState extends State<MyHomePage> {
if (data.homepageStats != null) { if (data.homepageStats != null) {
currentPage = contentPages[pageIndex]; currentPage = contentPages[pageIndex];
} else { } else {
currentPage = Center( currentPage = Center(child: CircularProgressIndicator());
child: FilledButton(
onPressed: data.fetchHomepageStats,
child: CircularProgressIndicator(),
),
);
} }
// This method is rerun every time setState is called, for instance as done // This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above. // by the _incrementCounter method above.

View File

@@ -105,7 +105,7 @@ class LocoSummary {
locoType: json['loco_type'], locoType: json['loco_type'],
locoClass: json['loco_class'], locoClass: json['loco_class'],
locoNumber: json['loco_number'], locoNumber: json['loco_number'],
locoName: json['loco_name'], locoName: json['loco_name'] ?? "",
locoOperator: json['loco_operator'], locoOperator: json['loco_operator'],
locoNotes: json['loco_notes'], locoNotes: json['loco_notes'],
locoEvn: json['loco_evn'], locoEvn: json['loco_evn'],
@@ -224,4 +224,3 @@ class Leg {
.toList(), .toList(),
); );
} }

View File

@@ -13,6 +13,9 @@ class DataService extends ChangeNotifier {
List<Leg> _legs = []; List<Leg> _legs = [];
List<Leg> get legs => _legs; List<Leg> get legs => _legs;
List<LocoSummary> _traction = [];
List<LocoSummary> get traction => _traction;
bool _isHomepageLoading = false; bool _isHomepageLoading = false;
bool get isHomepageLoading => _isHomepageLoading; bool get isHomepageLoading => _isHomepageLoading;
@@ -31,23 +34,36 @@ class DataService extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
} }
Future<void> fetchLegs({
int offset = 0,
int limit = 100,
String sortBy = 'date',
int sortDirection = 0,
}) async {
final query = '?sort_direction=$sortDirection&sort_by=$sortBy&offset=$offset&limit=$limit';
final json = await api.get('/user/legs$query');
if (json is List) { Future<void> fetchLegs({
_legs = json.map((e) => Leg.fromJson(e)).toList(); int offset = 0,
notifyListeners(); int limit = 100,
} else { String sortBy = 'date',
throw Exception('Unexpected legs response: $json'); int sortDirection = 0,
}) async {
final query =
'?sort_direction=$sortDirection&sort_by=$sortBy&offset=$offset&limit=$limit';
final json = await api.get('/user/legs$query');
if (json is List) {
_legs = json.map((e) => Leg.fromJson(e)).toList();
notifyListeners();
} else {
throw Exception('Unexpected legs response: $json');
}
}
Future<void> fetchHadTraction({int offset = 0, int limit = 100}) async {
final query = '?offset=$offset&limit=$limit';
final json = await api.get('/loco/mileage$query');
if (json is List) {
_traction = json.map((e) => LocoSummary.fromJson(e)).toList();
notifyListeners();
} else {
throw Exception('Unexpected traction response: $json');
}
} }
}
void clear() { void clear() {
_homepageStats = null; _homepageStats = null;