add error message for game deletion and implement search functionality
This commit is contained in:
@@ -34,6 +34,7 @@
|
|||||||
"edit_match": "Gruppe bearbeiten",
|
"edit_match": "Gruppe bearbeiten",
|
||||||
"enter_results": "Ergebnisse eintragen",
|
"enter_results": "Ergebnisse eintragen",
|
||||||
"error_creating_group": "Fehler beim Erstellen der Gruppe, bitte erneut versuchen",
|
"error_creating_group": "Fehler beim Erstellen der Gruppe, bitte erneut versuchen",
|
||||||
|
"error_deleting_game": "Fehler beim Löschen der Spielvorlage, bitte erneut versuchen",
|
||||||
"error_deleting_group": "Fehler beim Löschen der Gruppe, bitte erneut versuchen",
|
"error_deleting_group": "Fehler beim Löschen der Gruppe, bitte erneut versuchen",
|
||||||
"error_editing_group": "Fehler beim Bearbeiten der Gruppe, bitte erneut versuchen",
|
"error_editing_group": "Fehler beim Bearbeiten der Gruppe, bitte erneut versuchen",
|
||||||
"error_reading_file": "Fehler beim Lesen der Datei",
|
"error_reading_file": "Fehler beim Lesen der Datei",
|
||||||
|
|||||||
@@ -104,6 +104,9 @@
|
|||||||
"@error_creating_group": {
|
"@error_creating_group": {
|
||||||
"description": "Error message when group creation fails"
|
"description": "Error message when group creation fails"
|
||||||
},
|
},
|
||||||
|
"@error_deleting_game": {
|
||||||
|
"description": "Error message when game deletion fails"
|
||||||
|
},
|
||||||
"@error_deleting_group": {
|
"@error_deleting_group": {
|
||||||
"description": "Error message when group deletion fails"
|
"description": "Error message when group deletion fails"
|
||||||
},
|
},
|
||||||
@@ -376,6 +379,7 @@
|
|||||||
"edit_match": "Edit Match",
|
"edit_match": "Edit Match",
|
||||||
"enter_results": "Enter Results",
|
"enter_results": "Enter Results",
|
||||||
"error_creating_group": "Error while creating group, please try again",
|
"error_creating_group": "Error while creating group, please try again",
|
||||||
|
"error_deleting_game": "Error while deleting game, please try again",
|
||||||
"error_deleting_group": "Error while deleting group, please try again",
|
"error_deleting_group": "Error while deleting group, please try again",
|
||||||
"error_editing_group": "Error while editing group, please try again",
|
"error_editing_group": "Error while editing group, please try again",
|
||||||
"error_reading_file": "Error reading file",
|
"error_reading_file": "Error reading file",
|
||||||
|
|||||||
@@ -296,6 +296,12 @@ abstract class AppLocalizations {
|
|||||||
/// **'Error while creating group, please try again'**
|
/// **'Error while creating group, please try again'**
|
||||||
String get error_creating_group;
|
String get error_creating_group;
|
||||||
|
|
||||||
|
/// Error message when game deletion fails
|
||||||
|
///
|
||||||
|
/// In en, this message translates to:
|
||||||
|
/// **'Error while deleting game, please try again'**
|
||||||
|
String get error_deleting_game;
|
||||||
|
|
||||||
/// Error message when group deletion fails
|
/// Error message when group deletion fails
|
||||||
///
|
///
|
||||||
/// In en, this message translates to:
|
/// In en, this message translates to:
|
||||||
|
|||||||
@@ -112,6 +112,10 @@ class AppLocalizationsDe extends AppLocalizations {
|
|||||||
String get error_creating_group =>
|
String get error_creating_group =>
|
||||||
'Fehler beim Erstellen der Gruppe, bitte erneut versuchen';
|
'Fehler beim Erstellen der Gruppe, bitte erneut versuchen';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get error_deleting_game =>
|
||||||
|
'Fehler beim Löschen der Spielvorlage, bitte erneut versuchen';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get error_deleting_group =>
|
String get error_deleting_group =>
|
||||||
'Fehler beim Löschen der Gruppe, bitte erneut versuchen';
|
'Fehler beim Löschen der Gruppe, bitte erneut versuchen';
|
||||||
|
|||||||
@@ -112,6 +112,10 @@ class AppLocalizationsEn extends AppLocalizations {
|
|||||||
String get error_creating_group =>
|
String get error_creating_group =>
|
||||||
'Error while creating group, please try again';
|
'Error while creating group, please try again';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get error_deleting_game =>
|
||||||
|
'Error while deleting game, please try again';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get error_deleting_group =>
|
String get error_deleting_group =>
|
||||||
'Error while deleting group, please try again';
|
'Error while deleting group, please try again';
|
||||||
|
|||||||
@@ -37,12 +37,18 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
/// Controller for the search bar
|
/// Controller for the search bar
|
||||||
final TextEditingController searchBarController = TextEditingController();
|
final TextEditingController searchBarController = TextEditingController();
|
||||||
|
|
||||||
/// Currently selected game index
|
/// Currently selected game id
|
||||||
late String selectedGameId;
|
late String selectedGameId;
|
||||||
|
|
||||||
|
/// Games filtered according to the current search query
|
||||||
|
late List<Game> filteredGames;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
selectedGameId = widget.initialSelectedGameId;
|
selectedGameId = widget.initialSelectedGameId;
|
||||||
|
|
||||||
|
// Start with all games visible
|
||||||
|
filteredGames = List<Game>.from(widget.games);
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,7 +69,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.add),
|
icon: const Icon(Icons.add),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
Game? newGame = await Navigator.push(
|
final result = await Navigator.push(
|
||||||
context,
|
context,
|
||||||
adaptivePageRoute(
|
adaptivePageRoute(
|
||||||
builder: (context) => CreateGameView(
|
builder: (context) => CreateGameView(
|
||||||
@@ -73,10 +79,11 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
if (newGame != null) {
|
if (result != null && result.game != null) {
|
||||||
setState(() {
|
setState(() {
|
||||||
widget.games.insert(0, newGame);
|
widget.games.insert(0, result.game);
|
||||||
});
|
});
|
||||||
|
_refreshFromSource();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -100,46 +107,61 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
child: CustomSearchBar(
|
child: CustomSearchBar(
|
||||||
controller: searchBarController,
|
controller: searchBarController,
|
||||||
hintText: loc.game_name,
|
hintText: loc.game_name,
|
||||||
|
onChanged: (value) {
|
||||||
|
_applySearchFilter(value);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 5),
|
const SizedBox(height: 5),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
itemCount: widget.games.length,
|
itemCount: filteredGames.length,
|
||||||
itemBuilder: (BuildContext context, int index) {
|
itemBuilder: (BuildContext context, int index) {
|
||||||
|
final game = filteredGames[index];
|
||||||
return TitleDescriptionListTile(
|
return TitleDescriptionListTile(
|
||||||
title: widget.games[index].name,
|
title: game.name,
|
||||||
description: widget.games[index].description,
|
description: game.description,
|
||||||
badgeText: translateRulesetToString(
|
badgeText: translateRulesetToString(game.ruleset, context),
|
||||||
widget.games[index].ruleset,
|
isHighlighted: selectedGameId == game.id,
|
||||||
context,
|
|
||||||
),
|
|
||||||
isHighlighted: selectedGameId == widget.games[index].id,
|
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
setState(() {
|
setState(() {
|
||||||
if (selectedGameId == widget.games[index].id) {
|
if (selectedGameId == game.id) {
|
||||||
selectedGameId = '';
|
selectedGameId = '';
|
||||||
} else {
|
} else {
|
||||||
selectedGameId = widget.games[index].id;
|
selectedGameId = game.id;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onLongPress: () async {
|
onLongPress: () async {
|
||||||
Game? newGame = await Navigator.push(
|
final result = await Navigator.push(
|
||||||
context,
|
context,
|
||||||
adaptivePageRoute(
|
adaptivePageRoute(
|
||||||
builder: (context) => CreateGameView(
|
builder: (context) => CreateGameView(
|
||||||
gameToEdit: widget.games[index],
|
gameToEdit: game,
|
||||||
onGameCreatedOrEdited: () {
|
onGameCreatedOrEdited: () {
|
||||||
widget.onGamesUpdated?.call();
|
widget.onGamesUpdated?.call();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
if (newGame != null) {
|
if (result != null && result.game != null) {
|
||||||
|
// Find the index in the original list to mutate
|
||||||
|
final originalIndex = widget.games.indexWhere(
|
||||||
|
(g) => g.id == game.id,
|
||||||
|
);
|
||||||
|
if (originalIndex == -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (result.delete) {
|
||||||
setState(() {
|
setState(() {
|
||||||
widget.games[index] = newGame;
|
widget.games.removeAt(originalIndex);
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
setState(() {
|
||||||
|
widget.games[originalIndex] = result.game;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_refreshFromSource();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -151,4 +173,28 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Applies the search filter to the games list based on [query].
|
||||||
|
void _applySearchFilter(String query) {
|
||||||
|
final q = query.toLowerCase().trim();
|
||||||
|
if (q.isEmpty) {
|
||||||
|
setState(() {
|
||||||
|
filteredGames = List<Game>.from(widget.games);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
filteredGames = widget.games.where((game) {
|
||||||
|
final name = game.name.toLowerCase();
|
||||||
|
final description = game.description.toLowerCase();
|
||||||
|
return name.contains(q) || description.contains(q);
|
||||||
|
}).toList();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Re-applies the current filter after the underlying games list changed.
|
||||||
|
void _refreshFromSource() {
|
||||||
|
_applySearchFilter(searchBarController.text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,9 +18,13 @@ import 'package:tallee/presentation/widgets/player_selection.dart';
|
|||||||
import 'package:tallee/presentation/widgets/text_input/text_input_field.dart';
|
import 'package:tallee/presentation/widgets/text_input/text_input_field.dart';
|
||||||
import 'package:tallee/presentation/widgets/tiles/choose_tile.dart';
|
import 'package:tallee/presentation/widgets/tiles/choose_tile.dart';
|
||||||
|
|
||||||
|
/// A stateful widget for creating or editing a match.
|
||||||
class CreateMatchView extends StatefulWidget {
|
class CreateMatchView extends StatefulWidget {
|
||||||
/// A view that allows creating a new match
|
/// Constructor for `CreateMatchView`.
|
||||||
/// [onWinnerChanged]: Optional callback invoked when the winner is changed
|
///
|
||||||
|
/// [onWinnerChanged] is an optional callback invoked when the winner is changed.
|
||||||
|
/// [matchToEdit] is an optional match to prefill the fields.
|
||||||
|
/// [onMatchUpdated] is an optional callback invoked when the match is updated.
|
||||||
const CreateMatchView({
|
const CreateMatchView({
|
||||||
super.key,
|
super.key,
|
||||||
this.onWinnerChanged,
|
this.onWinnerChanged,
|
||||||
@@ -28,47 +32,49 @@ class CreateMatchView extends StatefulWidget {
|
|||||||
this.onMatchUpdated,
|
this.onMatchUpdated,
|
||||||
});
|
});
|
||||||
|
|
||||||
/// Optional callback invoked when the winner is changed
|
/// Optional callback invoked when the winner is changed.
|
||||||
final VoidCallback? onWinnerChanged;
|
final VoidCallback? onWinnerChanged;
|
||||||
|
|
||||||
/// Optional callback invoked when the match is updated
|
/// Optional callback invoked when the match is updated.
|
||||||
final void Function(Match)? onMatchUpdated;
|
final void Function(Match)? onMatchUpdated;
|
||||||
|
|
||||||
/// An optional match to prefill the fields
|
/// An optional match to prefill the fields.
|
||||||
final Match? matchToEdit;
|
final Match? matchToEdit;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<CreateMatchView> createState() => _CreateMatchViewState();
|
State<CreateMatchView> createState() => _CreateMatchViewState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The state class for `CreateMatchView`, managing the UI and logic for creating or editing a match.
|
||||||
class _CreateMatchViewState extends State<CreateMatchView> {
|
class _CreateMatchViewState extends State<CreateMatchView> {
|
||||||
|
/// The database instance for accessing match data.
|
||||||
late final AppDatabase db;
|
late final AppDatabase db;
|
||||||
|
|
||||||
/// Controller for the match name input field
|
/// Controller for the match name input field.
|
||||||
final TextEditingController _matchNameController = TextEditingController();
|
final TextEditingController _matchNameController = TextEditingController();
|
||||||
|
|
||||||
/// Hint text for the match name input field
|
/// Hint text for the match name input field.
|
||||||
String? hintText;
|
String? hintText;
|
||||||
|
|
||||||
/// List of all groups from the database
|
/// List of all games from the database.
|
||||||
List<Game> gamesList = [];
|
List<Game> gamesList = [];
|
||||||
|
|
||||||
/// List of all groups from the database
|
/// List of all groups from the database.
|
||||||
List<Group> groupsList = [];
|
List<Group> groupsList = [];
|
||||||
|
|
||||||
/// List of all players from the database
|
/// List of all players from the database.
|
||||||
List<Player> playerList = [];
|
List<Player> playerList = [];
|
||||||
|
|
||||||
/// The currently selected group
|
/// The currently selected group.
|
||||||
Group? selectedGroup;
|
Group? selectedGroup;
|
||||||
|
|
||||||
/// The currently selected game
|
/// The currently selected game.
|
||||||
Game? selectedGame;
|
Game? selectedGame;
|
||||||
|
|
||||||
/// The currently selected players
|
/// The currently selected players.
|
||||||
List<Player> selectedPlayers = [];
|
List<Player> selectedPlayers = [];
|
||||||
|
|
||||||
/// GlobalKey for ScaffoldMessenger to show snackbars
|
/// GlobalKey for ScaffoldMessenger to show snackbars.
|
||||||
final _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
final _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -80,6 +86,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
|
|
||||||
db = Provider.of<AppDatabase>(context, listen: false);
|
db = Provider.of<AppDatabase>(context, listen: false);
|
||||||
|
|
||||||
|
// Load games, groups, and players from the database.
|
||||||
Future.wait([
|
Future.wait([
|
||||||
db.gameDao.getAllGames(),
|
db.gameDao.getAllGames(),
|
||||||
db.groupDao.getAllGroups(),
|
db.groupDao.getAllGroups(),
|
||||||
@@ -90,7 +97,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
groupsList = result[1] as List<Group>;
|
groupsList = result[1] as List<Group>;
|
||||||
playerList = result[2] as List<Player>;
|
playerList = result[2] as List<Player>;
|
||||||
|
|
||||||
// If a match is provided, prefill the fields
|
// If a match is provided, prefill the fields.
|
||||||
if (widget.matchToEdit != null) {
|
if (widget.matchToEdit != null) {
|
||||||
prefillMatchDetails();
|
prefillMatchDetails();
|
||||||
}
|
}
|
||||||
@@ -130,6 +137,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
|
// Match name input field.
|
||||||
Container(
|
Container(
|
||||||
margin: CustomTheme.tileMargin,
|
margin: CustomTheme.tileMargin,
|
||||||
child: TextInputField(
|
child: TextInputField(
|
||||||
@@ -138,6 +146,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
maxLength: Constants.MAX_MATCH_NAME_LENGTH,
|
maxLength: Constants.MAX_MATCH_NAME_LENGTH,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
// Game selection tile.
|
||||||
ChooseTile(
|
ChooseTile(
|
||||||
title: loc.game,
|
title: loc.game,
|
||||||
trailingText: selectedGame == null
|
trailingText: selectedGame == null
|
||||||
@@ -172,15 +181,13 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
// Group selection tile.
|
||||||
ChooseTile(
|
ChooseTile(
|
||||||
title: loc.group,
|
title: loc.group,
|
||||||
trailingText: selectedGroup == null
|
trailingText: selectedGroup == null
|
||||||
? loc.none_group
|
? loc.none_group
|
||||||
: selectedGroup!.name,
|
: selectedGroup!.name,
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
// Remove all players from the previously selected group from
|
|
||||||
// the selected players list, in case the user deselects the
|
|
||||||
// group or selects a different group.
|
|
||||||
selectedPlayers.removeWhere(
|
selectedPlayers.removeWhere(
|
||||||
(player) =>
|
(player) =>
|
||||||
selectedGroup?.members.any(
|
selectedGroup?.members.any(
|
||||||
@@ -207,6 +214,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
// Player selection widget.
|
||||||
Expanded(
|
Expanded(
|
||||||
child: PlayerSelection(
|
child: PlayerSelection(
|
||||||
key: ValueKey(selectedGroup?.id ?? 'no_group'),
|
key: ValueKey(selectedGroup?.id ?? 'no_group'),
|
||||||
@@ -219,6 +227,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
// Create or save button.
|
||||||
CustomWidthButton(
|
CustomWidthButton(
|
||||||
text: buttonText,
|
text: buttonText,
|
||||||
sizeRelativeToWidth: 0.95,
|
sizeRelativeToWidth: 0.95,
|
||||||
@@ -240,16 +249,16 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
///
|
///
|
||||||
/// Returns `true` if:
|
/// Returns `true` if:
|
||||||
/// - A ruleset is selected AND
|
/// - A ruleset is selected AND
|
||||||
/// - Either a group is selected OR at least 2 players are selected
|
/// - Either a group is selected OR at least 2 players are selected.
|
||||||
bool _enableCreateGameButton() {
|
bool _enableCreateGameButton() {
|
||||||
return (selectedGroup != null ||
|
return (selectedGroup != null ||
|
||||||
(selectedPlayers.length > 1) && selectedGame != null);
|
(selectedPlayers.length > 1) && selectedGame != null);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a match was provided to the view, it updates the match in the database
|
/// Handles navigation when the create or save button is pressed.
|
||||||
// and navigates back to the previous screen.
|
///
|
||||||
// If no match was provided, it creates a new match in the database and
|
/// If a match is being edited, updates the match in the database.
|
||||||
// navigates to the MatchResultView for the newly created match.
|
/// Otherwise, creates a new match and navigates to the MatchResultView.
|
||||||
void buttonNavigation(BuildContext context) async {
|
void buttonNavigation(BuildContext context) async {
|
||||||
if (widget.matchToEdit != null) {
|
if (widget.matchToEdit != null) {
|
||||||
await updateMatch();
|
await updateMatch();
|
||||||
@@ -274,8 +283,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates attributes of the existing match in the database based on the
|
/// Updates the existing match in the database.
|
||||||
/// changes made in the edit view.
|
|
||||||
Future<void> updateMatch() async {
|
Future<void> updateMatch() async {
|
||||||
final updatedMatch = Match(
|
final updatedMatch = Match(
|
||||||
id: widget.matchToEdit!.id,
|
id: widget.matchToEdit!.id,
|
||||||
@@ -312,7 +320,6 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add players who are in updatedMatch but not in the original match
|
|
||||||
for (var player in updatedMatch.players) {
|
for (var player in updatedMatch.players) {
|
||||||
if (!widget.matchToEdit!.players.any((p) => p.id == player.id)) {
|
if (!widget.matchToEdit!.players.any((p) => p.id == player.id)) {
|
||||||
await db.playerMatchDao.addPlayerToMatch(
|
await db.playerMatchDao.addPlayerToMatch(
|
||||||
@@ -322,7 +329,6 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove players who are in the original match but not in updatedMatch
|
|
||||||
for (var player in widget.matchToEdit!.players) {
|
for (var player in widget.matchToEdit!.players) {
|
||||||
if (!updatedMatch.players.any((p) => p.id == player.id)) {
|
if (!updatedMatch.players.any((p) => p.id == player.id)) {
|
||||||
await db.playerMatchDao.removePlayerFromMatch(
|
await db.playerMatchDao.removePlayerFromMatch(
|
||||||
@@ -338,8 +344,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
widget.onMatchUpdated?.call(updatedMatch);
|
widget.onMatchUpdated?.call(updatedMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new match and adds it to the database.
|
/// Creates a new match and adds it to the database.
|
||||||
// Returns the created match.
|
|
||||||
Future<Match> createMatch() async {
|
Future<Match> createMatch() async {
|
||||||
Match match = Match(
|
Match match = Match(
|
||||||
name: _matchNameController.text.isEmpty
|
name: _matchNameController.text.isEmpty
|
||||||
@@ -354,7 +359,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a match was provided to the view, this method prefills the input fields
|
/// Prefills the input fields if a match was provided to the view.
|
||||||
void prefillMatchDetails() {
|
void prefillMatchDetails() {
|
||||||
final match = widget.matchToEdit!;
|
final match = widget.matchToEdit!;
|
||||||
_matchNameController.text = match.name;
|
_matchNameController.text = match.name;
|
||||||
@@ -366,8 +371,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If none of the selected players are from the currently selected group,
|
/// Removes the group if none of its members are in the selected players list.
|
||||||
// the group is also deselected.
|
|
||||||
Future<void> removeGroupWhenNoMemberLeft() async {
|
Future<void> removeGroupWhenNoMemberLeft() async {
|
||||||
if (selectedGroup == null) return;
|
if (selectedGroup == null) return;
|
||||||
|
|
||||||
@@ -381,7 +385,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loads all games from the database and updates the gamesList.
|
/// Loads all games from the database and updates the state.
|
||||||
Future<void> loadGames() async {
|
Future<void> loadGames() async {
|
||||||
final result = await db.gameDao.getAllGames();
|
final result = await db.gameDao.getAllGames();
|
||||||
result.sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
result.sort((a, b) => b.createdAt.compareTo(a.createdAt));
|
||||||
|
|||||||
@@ -7,13 +7,18 @@ import 'package:tallee/core/custom_theme.dart';
|
|||||||
import 'package:tallee/core/enums.dart';
|
import 'package:tallee/core/enums.dart';
|
||||||
import 'package:tallee/data/db/database.dart';
|
import 'package:tallee/data/db/database.dart';
|
||||||
import 'package:tallee/data/dto/game.dart';
|
import 'package:tallee/data/dto/game.dart';
|
||||||
|
import 'package:tallee/data/dto/group.dart';
|
||||||
import 'package:tallee/l10n/generated/app_localizations.dart';
|
import 'package:tallee/l10n/generated/app_localizations.dart';
|
||||||
import 'package:tallee/presentation/views/main_menu/match_view/create_match/game_view/choose_color_view.dart';
|
import 'package:tallee/presentation/views/main_menu/match_view/create_match/game_view/choose_color_view.dart';
|
||||||
import 'package:tallee/presentation/views/main_menu/match_view/create_match/game_view/choose_ruleset_view.dart';
|
import 'package:tallee/presentation/views/main_menu/match_view/create_match/game_view/choose_ruleset_view.dart';
|
||||||
import 'package:tallee/presentation/widgets/buttons/custom_width_button.dart';
|
import 'package:tallee/presentation/widgets/buttons/custom_width_button.dart';
|
||||||
|
import 'package:tallee/presentation/widgets/custom_alert_dialog.dart';
|
||||||
import 'package:tallee/presentation/widgets/text_input/text_input_field.dart';
|
import 'package:tallee/presentation/widgets/text_input/text_input_field.dart';
|
||||||
import 'package:tallee/presentation/widgets/tiles/choose_tile.dart';
|
import 'package:tallee/presentation/widgets/tiles/choose_tile.dart';
|
||||||
|
|
||||||
|
/// A stateful widget for creating or editing a game.
|
||||||
|
/// - [gameToEdit] An optional game to prefill the fields
|
||||||
|
/// - [onGameCreatedOrEdited] Callback to invoke when the game is created or edited
|
||||||
class CreateGameView extends StatefulWidget {
|
class CreateGameView extends StatefulWidget {
|
||||||
const CreateGameView({
|
const CreateGameView({
|
||||||
super.key,
|
super.key,
|
||||||
@@ -32,16 +37,39 @@ class CreateGameView extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _CreateGameViewState extends State<CreateGameView> {
|
class _CreateGameViewState extends State<CreateGameView> {
|
||||||
|
/// GlobalKey for ScaffoldMessenger to show snackbars
|
||||||
|
final _scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
||||||
|
|
||||||
|
/// The database instance for accessing game data.
|
||||||
late final AppDatabase db;
|
late final AppDatabase db;
|
||||||
|
|
||||||
|
/// The currently selected ruleset for the game.
|
||||||
Ruleset? selectedRuleset;
|
Ruleset? selectedRuleset;
|
||||||
|
|
||||||
|
/// The index of the currently selected ruleset.
|
||||||
int selectedRulesetIndex = -1;
|
int selectedRulesetIndex = -1;
|
||||||
|
|
||||||
|
/// A list of available rulesets and their localized names.
|
||||||
late List<(Ruleset, String)> _rulesets;
|
late List<(Ruleset, String)> _rulesets;
|
||||||
|
|
||||||
|
/// The currently selected color for the game.
|
||||||
GameColor? selectedColor;
|
GameColor? selectedColor;
|
||||||
|
|
||||||
|
/// Controller for the game name input field.
|
||||||
final _gameNameController = TextEditingController();
|
final _gameNameController = TextEditingController();
|
||||||
|
|
||||||
|
/// Controller for the game description input field.
|
||||||
final _descriptionController = TextEditingController();
|
final _descriptionController = TextEditingController();
|
||||||
|
|
||||||
|
/// The ID of the currently selected group.
|
||||||
|
late String selectedGroupId;
|
||||||
|
|
||||||
|
/// A controller for the search bar input field.
|
||||||
|
final TextEditingController controller = TextEditingController();
|
||||||
|
|
||||||
|
/// A list of groups filtered based on the search query.
|
||||||
|
late final List<Group> filteredGroups;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
@@ -104,6 +132,51 @@ class _CreateGameViewState extends State<CreateGameView> {
|
|||||||
backgroundColor: CustomTheme.backgroundColor,
|
backgroundColor: CustomTheme.backgroundColor,
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text(isEditing ? loc.edit_game : loc.create_game),
|
title: Text(isEditing ? loc.edit_game : loc.create_game),
|
||||||
|
actions: widget.gameToEdit == null
|
||||||
|
? []
|
||||||
|
: [
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.delete),
|
||||||
|
onPressed: () async {
|
||||||
|
if (widget.gameToEdit != null) {
|
||||||
|
showDialog<bool>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => CustomAlertDialog(
|
||||||
|
title: loc.delete_game,
|
||||||
|
content: loc.this_cannot_be_undone,
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () =>
|
||||||
|
Navigator.of(context).pop(false),
|
||||||
|
child: Text(loc.cancel),
|
||||||
|
),
|
||||||
|
TextButton(
|
||||||
|
onPressed: () =>
|
||||||
|
Navigator.of(context).pop(true),
|
||||||
|
child: Text(loc.delete),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
).then((confirmed) async {
|
||||||
|
if (confirmed == true && context.mounted) {
|
||||||
|
bool success = await db.gameDao.deleteGame(
|
||||||
|
gameId: widget.gameToEdit!.id,
|
||||||
|
);
|
||||||
|
if (!context.mounted) return;
|
||||||
|
if (success) {
|
||||||
|
Navigator.of(
|
||||||
|
context,
|
||||||
|
).pop((game: widget.gameToEdit, delete: true));
|
||||||
|
} else {
|
||||||
|
if (!mounted) return;
|
||||||
|
showSnackbar(message: loc.error_deleting_game);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
body: SafeArea(
|
body: SafeArea(
|
||||||
child: Column(
|
child: Column(
|
||||||
@@ -196,7 +269,9 @@ class _CreateGameViewState extends State<CreateGameView> {
|
|||||||
}
|
}
|
||||||
widget.onGameCreatedOrEdited.call();
|
widget.onGameCreatedOrEdited.call();
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
Navigator.of(context).pop(newGame);
|
Navigator.of(
|
||||||
|
context,
|
||||||
|
).pop((game: newGame, delete: false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
@@ -209,6 +284,9 @@ class _CreateGameViewState extends State<CreateGameView> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handles updating an existing game in the database.
|
||||||
|
///
|
||||||
|
/// [newGame] The updated game object.
|
||||||
Future<void> handleGameUpdate(Game newGame) async {
|
Future<void> handleGameUpdate(Game newGame) async {
|
||||||
final oldGame = widget.gameToEdit!;
|
final oldGame = widget.gameToEdit!;
|
||||||
|
|
||||||
@@ -248,7 +326,26 @@ class _CreateGameViewState extends State<CreateGameView> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handles creating a new game in the database.
|
||||||
|
///
|
||||||
|
/// [newGame] The game object to be created.
|
||||||
Future<void> handleGameCreation(Game newGame) async {
|
Future<void> handleGameCreation(Game newGame) async {
|
||||||
await db.gameDao.addGame(game: newGame);
|
await db.gameDao.addGame(game: newGame);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Displays a snackbar with the given message and optional action.
|
||||||
|
///
|
||||||
|
/// [message] The message to display in the snackbar.
|
||||||
|
void showSnackbar({required String message}) {
|
||||||
|
final messenger = _scaffoldMessengerKey.currentState;
|
||||||
|
if (messenger != null) {
|
||||||
|
messenger.hideCurrentSnackBar();
|
||||||
|
messenger.showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(message, style: const TextStyle(color: Colors.white)),
|
||||||
|
backgroundColor: CustomTheme.boxColor,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import 'package:tallee/presentation/widgets/tiles/match_tile.dart';
|
|||||||
import 'package:tallee/presentation/widgets/top_centered_message.dart';
|
import 'package:tallee/presentation/widgets/top_centered_message.dart';
|
||||||
|
|
||||||
class MatchView extends StatefulWidget {
|
class MatchView extends StatefulWidget {
|
||||||
/// A view that displays a list of matches
|
/// A view that displays a list of matches.
|
||||||
const MatchView({super.key});
|
const MatchView({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -27,11 +27,14 @@ class MatchView extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MatchViewState extends State<MatchView> {
|
class _MatchViewState extends State<MatchView> {
|
||||||
|
/// Database instance used to access match data.
|
||||||
late final AppDatabase db;
|
late final AppDatabase db;
|
||||||
|
|
||||||
|
/// Indicates whether matches are currently being loaded.
|
||||||
bool isLoading = true;
|
bool isLoading = true;
|
||||||
|
|
||||||
/// Loaded matches from the database,
|
/// Loaded matches from the database,
|
||||||
/// initially filled with skeleton matches
|
/// initially filled with skeleton matches.
|
||||||
List<Match> matches = List.filled(
|
List<Match> matches = List.filled(
|
||||||
4,
|
4,
|
||||||
Match(
|
Match(
|
||||||
|
|||||||
Reference in New Issue
Block a user