165 lines
4.6 KiB
Dart
165 lines
4.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:laserscouter/core/api.dart';
|
|
import 'notespage.dart';
|
|
import 'package:to_csv/to_csv.dart' as csv_export;
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
|
class TeamPicker extends StatefulWidget {
|
|
final String eventCode;
|
|
|
|
const TeamPicker({super.key, required this.eventCode});
|
|
|
|
@override
|
|
State<TeamPicker> createState() => _TeamPickerState();
|
|
}
|
|
|
|
class _TeamPickerState extends State<TeamPicker> {
|
|
List<String> teamNames = [];
|
|
List<String> teamCodes = [];
|
|
bool isLoading = true;
|
|
String? errorMessage;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_fetchTeams();
|
|
}
|
|
|
|
Future<void> _exportData() async {
|
|
List<String> header = [];
|
|
header.add('Team Number');
|
|
header.add('Drivetrain Type');
|
|
header.add('Has Vision');
|
|
header.add('Climb Level');
|
|
header.add('Trenchable');
|
|
header.add('Fuel Capacity');
|
|
header.add('Bot Position');
|
|
header.add('Auton Rundown');
|
|
header.add('General Observations');
|
|
|
|
List<List<String>> data = [];
|
|
for (int i = 0; i < teamCodes.length; i++) {
|
|
String generateKey(String field) {
|
|
return '${teamCodes[i]}_${widget.eventCode}_$field';
|
|
}
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
String? autonRundown = prefs.getString(generateKey('autonRundown'));
|
|
String? botPosition = prefs.getString(generateKey('botPosition'));
|
|
String? generalObservations = prefs.getString(generateKey('generalObservations'));
|
|
String? driveTrainType = prefs.getString(generateKey('driveTrainType'));
|
|
String? hasVision = prefs.getBool(generateKey('hasVision')).toString();
|
|
String? climbLevel = prefs.getDouble(generateKey('climbLevel')).toString();
|
|
String? trenchable = prefs.getBool(generateKey('trenchable')).toString();
|
|
String? fuelCapacity = prefs.getDouble(generateKey('fuelCapacity')).toString();
|
|
|
|
if (hasVision == 'null') {
|
|
hasVision = '';
|
|
}
|
|
if (climbLevel == 'null') {
|
|
climbLevel = '';
|
|
}
|
|
if (trenchable == 'null') {
|
|
trenchable = '';
|
|
}
|
|
if (fuelCapacity == 'null') {
|
|
fuelCapacity = '';
|
|
}
|
|
|
|
List<String> teamData = [];
|
|
teamData.add(teamCodes[i]);
|
|
teamData.add(driveTrainType ?? '');
|
|
teamData.add(hasVision);
|
|
teamData.add(climbLevel);
|
|
teamData.add(trenchable);
|
|
teamData.add(fuelCapacity);
|
|
teamData.add(botPosition ?? '');
|
|
teamData.add(autonRundown ?? '');
|
|
teamData.add(generalObservations ?? '');
|
|
data.add(teamData);
|
|
}
|
|
csv_export.myCSV(header, data, setHeadersInFirstRow: true, emptyRowsConfig: {1: 1}, fileName: 'laserscouter_${widget.eventCode}.csv');
|
|
}
|
|
|
|
Future<void> _fetchTeams() async {
|
|
try {
|
|
final EventSearchResult result = await eventSearch(widget.eventCode);
|
|
if (mounted) {
|
|
setState(() {
|
|
teamNames = result.teamNames;
|
|
teamCodes = result.teamCodes;
|
|
isLoading = false;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
setState(() {
|
|
errorMessage = "Failed to load teams. Please try again.";
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Teams'),
|
|
),
|
|
body: _buildBody(),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: _exportData,
|
|
child: const Icon(Icons.share),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildBody() {
|
|
if (isLoading) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
if (errorMessage != null) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16.0),
|
|
child: Text(
|
|
errorMessage!,
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
|
color: Colors.red,
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
if (teamCodes.isEmpty) {
|
|
return const Center(
|
|
child: Text('No teams found for this event.'),
|
|
);
|
|
}
|
|
|
|
return ListView.builder(
|
|
itemCount: teamCodes.length,
|
|
itemBuilder: (context, index) {
|
|
return ListTile(
|
|
title: Text('${teamCodes[index]} - ${teamNames[index]}'),
|
|
onTap: () {
|
|
Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => NotesPage(
|
|
teamName: teamNames[index],
|
|
eventCode: widget.eventCode,
|
|
teamCode: teamCodes[index],
|
|
),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|