dth-pingpong-mobileapp/lib/pages/views/leaderboard.dart

140 lines
3.8 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
2024-12-23 21:48:34 +01:00
import '../../globals.dart';
class LeaderboardPage extends StatefulWidget {
@override
_LeaderboardPageState createState() => _LeaderboardPageState();
}
class _LeaderboardPageState extends State<LeaderboardPage> {
List<dynamic> _leaderboard = [];
bool _isLoading = true;
bool _sortByOskMu = false;
2024-12-23 21:48:34 +01:00
final String _leaderboardApi = '$apiurl/leaderboards';
Future<void> _fetchLeaderboard() async {
setState(() {
_isLoading = true;
});
try {
2024-12-23 21:48:34 +01:00
final response = await http.get(Uri.parse(_leaderboardApi));
if (response.statusCode == 200) {
List<dynamic> data = json.decode(response.body);
setState(() {
_leaderboard = data;
_sortLeaderboard();
_isLoading = false;
});
} else {
setState(() {
_isLoading = false;
});
_showError('Failed to load data');
}
} catch (e) {
setState(() {
_isLoading = false;
});
_showError('Error: $e');
}
}
void _sortLeaderboard() {
setState(() {
if (_sortByOskMu) {
_leaderboard.sort((a, b) => b['osk_mu'].compareTo(a['osk_mu']));
} else {
_leaderboard.sort((a, b) => b['elo_rating'].compareTo(a['elo_rating']));
}
});
}
void _toggleSort() {
setState(() {
_sortByOskMu = !_sortByOskMu;
_sortLeaderboard();
});
}
void _showError(String message) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Error'),
content: Text(message),
actions: [
TextButton(
child: Text('OK'),
onPressed: () => Navigator.pop(context),
),
],
);
},
);
}
@override
void initState() {
super.initState();
2024-12-23 21:48:34 +01:00
_fetchLeaderboard();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Leaderboard'),
actions: [
IconButton(
icon: Icon(Icons.sort),
onPressed: _toggleSort,
tooltip: _sortByOskMu ? 'Sort by Elo' : 'Sort by Osk_Mu',
),
],
),
body: _isLoading
2024-12-23 21:48:34 +01:00
? Center(child: CircularProgressIndicator())
: RefreshIndicator(
2024-12-23 21:48:34 +01:00
onRefresh: _fetchLeaderboard,
child: ListView.builder(
itemCount: _leaderboard.length,
itemBuilder: (context, index) {
var player = _leaderboard[index];
String truncatedOskMu = player['osk_mu'].toStringAsFixed(3);
String assetName = _sortByOskMu
? 'assets/player_${index}_dp.png'
: 'assets/player_${index}.png';
2024-12-23 21:48:34 +01:00
return Card(
margin: EdgeInsets.all(8),
child: ListTile(
contentPadding: EdgeInsets.all(10),
leading: CircleAvatar(
child: Text(player['player_name'][0].toUpperCase())),
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(player['player_name']),
if (index < 3)
Image.asset(assetName, width: 45, height: 45),
],
2024-12-23 21:48:34 +01:00
),
subtitle: Text(
'Elo: ${player['elo_rating']} | TSC: $truncatedOskMu'),
trailing: Text('UID: ${player['friend_code']}'),
2024-12-23 21:48:34 +01:00
),
);
},
),
2024-12-23 21:48:34 +01:00
),
);
}
}