Added ability to add custom teams and events.
This commit is contained in:
parent
84aaf9c527
commit
9327b6120f
8 changed files with 687 additions and 189 deletions
|
|
@ -1,14 +1,17 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:laserscouter/core/api.dart';
|
||||
import 'notespage.dart';
|
||||
import 'teamadder.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;
|
||||
final String eventName;
|
||||
final bool isCustomOnly;
|
||||
|
||||
const TeamPicker({super.key, required this.eventCode});
|
||||
const TeamPicker({super.key, required this.eventCode, required this.eventName,this.isCustomOnly = false});
|
||||
|
||||
@override
|
||||
State<TeamPicker> createState() => _TeamPickerState();
|
||||
|
|
@ -83,29 +86,89 @@ class _TeamPickerState extends State<TeamPicker> {
|
|||
|
||||
Future<void> _fetchTeams() async {
|
||||
try {
|
||||
final EventSearchResult result = await eventSearch(widget.eventCode);
|
||||
List<String> apiTeamNames = [];
|
||||
List<String> apiTeamCodes = [];
|
||||
|
||||
if (!widget.isCustomOnly) {
|
||||
final EventSearchResult apiResult = await eventSearch(widget.eventCode);
|
||||
apiTeamNames = apiResult.teamNames;
|
||||
apiTeamCodes = apiResult.teamCodes;
|
||||
}
|
||||
|
||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
final List<String> customTeamNames = prefs.getStringList('custom_team_names_${widget.eventName}') ?? [];
|
||||
final List<String> customTeamCodes = prefs.getStringList('custom_team_numbers_${widget.eventCode}') ?? [];
|
||||
|
||||
List<String> combinedNames = [];
|
||||
List<String> combinedCodes = [];
|
||||
for (int i = 0; i < apiTeamCodes.length; i++) {
|
||||
if (!combinedCodes.contains(apiTeamCodes[i])) {
|
||||
combinedCodes.add(apiTeamCodes[i]);
|
||||
if (i < apiTeamNames.length) {
|
||||
combinedNames.add(apiTeamNames[i]);
|
||||
} else {
|
||||
combinedNames.add('');
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < customTeamCodes.length; i++) {
|
||||
if (!combinedCodes.contains(customTeamCodes[i])) {
|
||||
combinedCodes.add(customTeamCodes[i]);
|
||||
if (i < customTeamNames.length) {
|
||||
combinedNames.add(customTeamNames[i]);
|
||||
} else {
|
||||
combinedNames.add('');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
teamNames = result.teamNames;
|
||||
teamCodes = result.teamCodes;
|
||||
teamNames = combinedNames.toList();
|
||||
teamCodes = combinedCodes.toList();
|
||||
isLoading = false;
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
errorMessage = "Failed to load teams. Please try again.";
|
||||
errorMessage = widget.isCustomOnly
|
||||
? "Could not load custom teams."
|
||||
: "Failed to load teams. Please try again.";
|
||||
isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Future<void> _refreshTeams() async {
|
||||
setState(() {
|
||||
isLoading = true;
|
||||
});
|
||||
await _fetchTeams();
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Teams'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: () async {
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => TeamAdder(eventCode: widget.eventCode,),
|
||||
),
|
||||
);
|
||||
_refreshTeams();
|
||||
},
|
||||
icon: Icon(Icons.add),
|
||||
)
|
||||
]
|
||||
),
|
||||
body: _buildBody(),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue