add functionality to create/edit/select groups
Some checks failed
Pull Request Pipeline / test (pull_request) Successful in 38s
Pull Request Pipeline / lint (pull_request) Failing after 44s

This commit is contained in:
2026-03-08 14:49:48 +01:00
parent 9c92ded4fa
commit 69d9397ab3
3 changed files with 168 additions and 82 deletions

View File

@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:tallee/core/adaptive_page_route.dart'; import 'package:tallee/core/adaptive_page_route.dart';
import 'package:tallee/core/common.dart'; import 'package:tallee/core/common.dart';
import 'package:tallee/core/custom_theme.dart'; import 'package:tallee/core/custom_theme.dart';
import 'package:tallee/core/enums.dart';
import 'package:tallee/data/dto/game.dart'; import 'package:tallee/data/dto/game.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/create_game_view.dart'; import 'package:tallee/presentation/views/main_menu/match_view/create_match/game_view/create_game_view.dart';
@@ -11,19 +10,24 @@ import 'package:tallee/presentation/widgets/tiles/title_description_list_tile.da
class ChooseGameView extends StatefulWidget { class ChooseGameView extends StatefulWidget {
/// A view that allows the user to choose a game from a list of available games /// A view that allows the user to choose a game from a list of available games
/// - [games]: A list of tuples containing the game name, description and ruleset /// - [games]: The list of available games
/// - [initialGameIndex]: The index of the initially selected game /// - [initialSelectedGameId]: The id of the initially selected game
/// - [onGamesUpdated]: Optional callback invoked when the games are updated
const ChooseGameView({ const ChooseGameView({
super.key, super.key,
required this.games, required this.games,
required this.initialGameIndex, required this.initialSelectedGameId,
this.onGamesUpdated,
}); });
/// A list of tuples containing the game name, description and ruleset /// A list of tuples containing the game name, description and ruleset
final List<(String, String, Ruleset)> games; final List<Game> games;
/// The index of the initially selected game /// The index of the initially selected game
final int initialGameIndex; final String initialSelectedGameId;
/// Optional callback invoked when the games are updated
final VoidCallback? onGamesUpdated;
@override @override
State<ChooseGameView> createState() => _ChooseGameViewState(); State<ChooseGameView> createState() => _ChooseGameViewState();
@@ -34,11 +38,11 @@ class _ChooseGameViewState extends State<ChooseGameView> {
final TextEditingController searchBarController = TextEditingController(); final TextEditingController searchBarController = TextEditingController();
/// Currently selected game index /// Currently selected game index
late int selectedGameIndex; late String selectedGameId;
@override @override
void initState() { void initState() {
selectedGameIndex = widget.initialGameIndex; selectedGameId = widget.initialSelectedGameId;
super.initState(); super.initState();
} }
@@ -52,21 +56,28 @@ class _ChooseGameViewState extends State<ChooseGameView> {
leading: IconButton( leading: IconButton(
icon: const Icon(Icons.arrow_back_ios), icon: const Icon(Icons.arrow_back_ios),
onPressed: () { onPressed: () {
Navigator.of(context).pop(selectedGameIndex); Navigator.of(context).pop(selectedGameId);
}, },
), ),
actions: [ actions: [
IconButton( IconButton(
icon: const Icon(Icons.add), icon: const Icon(Icons.add),
onPressed: () async { onPressed: () async {
await Navigator.push( Game? newGame = await Navigator.push(
context, context,
adaptivePageRoute( adaptivePageRoute(
builder: (context) => CreateGameView( builder: (context) => CreateGameView(
callback: () {}, //TODO: implement callback onGameCreatedOrEdited: () {
widget.onGamesUpdated?.call();
},
), ),
), ),
); );
if (newGame != null) {
setState(() {
widget.games.insert(0, newGame);
});
}
}, },
), ),
], ],
@@ -80,7 +91,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
if (didPop) { if (didPop) {
return; return;
} }
Navigator.of(context).pop(selectedGameIndex); Navigator.of(context).pop(selectedGameId);
}, },
child: Column( child: Column(
children: [ children: [
@@ -97,40 +108,39 @@ class _ChooseGameViewState extends State<ChooseGameView> {
itemCount: widget.games.length, itemCount: widget.games.length,
itemBuilder: (BuildContext context, int index) { itemBuilder: (BuildContext context, int index) {
return TitleDescriptionListTile( return TitleDescriptionListTile(
title: widget.games[index].$1, title: widget.games[index].name,
description: widget.games[index].$2, description: widget.games[index].description,
badgeText: translateRulesetToString( badgeText: translateRulesetToString(
widget.games[index].$3, widget.games[index].ruleset,
context, context,
), ),
isHighlighted: selectedGameIndex == index, isHighlighted: selectedGameId == widget.games[index].id,
onTap: () async { onTap: () async {
setState(() { setState(() {
if (selectedGameIndex == index) { if (selectedGameId == widget.games[index].id) {
selectedGameIndex = -1; selectedGameId = '';
} else { } else {
selectedGameIndex = index; selectedGameId = widget.games[index].id;
} }
}); });
}, },
onLongPress: () async { onLongPress: () async {
await Navigator.push( Game? newGame = await Navigator.push(
context, context,
adaptivePageRoute( adaptivePageRoute(
builder: (context) => CreateGameView( builder: (context) => CreateGameView(
//TODO: implement callback & giving real game to create game view gameToEdit: widget.games[index],
gameToEdit: Game( onGameCreatedOrEdited: () {
name: 'Cabo', widget.onGamesUpdated?.call();
description: },
'Test Beschreibung mit sehr viel Inhalt',
ruleset: Ruleset.highestScore,
color: GameColor.blue,
icon: '',
),
callback: () {},
), ),
), ),
); );
if (newGame != null) {
setState(() {
widget.games[index] = newGame;
});
}
}, },
); );
}, },

View File

@@ -50,6 +50,9 @@ class _CreateMatchViewState extends State<CreateMatchView> {
/// 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<Game> gamesList = [];
/// List of all groups from the database /// List of all groups from the database
List<Group> groupsList = []; List<Group> groupsList = [];
@@ -59,9 +62,8 @@ class _CreateMatchViewState extends State<CreateMatchView> {
/// The currently selected group /// The currently selected group
Group? selectedGroup; Group? selectedGroup;
/// The index of the currently selected game in [games] to mark it in /// The currently selected game
/// the [ChooseGameView] Game? selectedGame;
int selectedGameIndex = -1;
/// The currently selected players /// The currently selected players
List<Player> selectedPlayers = []; List<Player> selectedPlayers = [];
@@ -79,11 +81,14 @@ class _CreateMatchViewState extends State<CreateMatchView> {
db = Provider.of<AppDatabase>(context, listen: false); db = Provider.of<AppDatabase>(context, listen: false);
Future.wait([ Future.wait([
db.gameDao.getAllGames(),
db.groupDao.getAllGroups(), db.groupDao.getAllGroups(),
db.playerDao.getAllPlayers(), db.playerDao.getAllPlayers(),
]).then((result) async { ]).then((result) async {
groupsList = result[0] as List<Group>; gamesList = result[0] as List<Game>;
playerList = result[1] as List<Player>; gamesList.sort((a, b) => b.createdAt.compareTo(a.createdAt));
groupsList = result[1] as List<Group>;
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) {
@@ -105,11 +110,6 @@ class _CreateMatchViewState extends State<CreateMatchView> {
hintText ??= loc.match_name; hintText ??= loc.match_name;
} }
List<(String, String, Ruleset)> games = [
('Example Game 1', 'This is a description', Ruleset.lowestScore),
('Example Game 2', '', Ruleset.singleWinner),
];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final loc = AppLocalizations.of(context); final loc = AppLocalizations.of(context);
@@ -140,21 +140,32 @@ class _CreateMatchViewState extends State<CreateMatchView> {
), ),
ChooseTile( ChooseTile(
title: loc.game, title: loc.game,
trailingText: selectedGameIndex == -1 trailingText: selectedGame == null
? loc.none ? loc.none
: games[selectedGameIndex].$1, : selectedGame!.name,
onPressed: () async { onPressed: () async {
selectedGameIndex = await Navigator.of(context).push( final String? selectedGameId = await Navigator.of(context)
adaptivePageRoute( .push(
builder: (context) => ChooseGameView( adaptivePageRoute(
games: games, builder: (context) => ChooseGameView(
initialGameIndex: selectedGameIndex, games: gamesList,
), initialSelectedGameId: selectedGame?.id ?? '',
), onGamesUpdated: loadGames,
); ),
),
);
try {
selectedGame = gamesList.firstWhere(
(g) => g.id == selectedGameId,
);
} catch (_) {
selectedGame = null;
}
setState(() { setState(() {
if (selectedGameIndex != -1) { if (selectedGame != null) {
hintText = games[selectedGameIndex].$1; hintText = selectedGame!.name;
} else { } else {
hintText = loc.match_name; hintText = loc.match_name;
} }
@@ -232,7 +243,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
/// - 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) && selectedGameIndex != -1); (selectedPlayers.length > 1) && selectedGame != null);
} }
// If a match was provided to the view, it updates the match in the database // If a match was provided to the view, it updates the match in the database
@@ -266,9 +277,6 @@ class _CreateMatchViewState extends State<CreateMatchView> {
/// Updates attributes of the existing match in the database based on the /// Updates attributes of the existing match in the database based on the
/// changes made in the edit view. /// changes made in the edit view.
Future<void> updateMatch() async { Future<void> updateMatch() async {
//TODO: Remove when Games implemented
final tempGame = await getTemporaryGame();
final updatedMatch = Match( final updatedMatch = Match(
id: widget.matchToEdit!.id, id: widget.matchToEdit!.id,
name: _matchNameController.text.isEmpty name: _matchNameController.text.isEmpty
@@ -276,7 +284,7 @@ class _CreateMatchViewState extends State<CreateMatchView> {
: _matchNameController.text.trim(), : _matchNameController.text.trim(),
group: selectedGroup, group: selectedGroup,
players: selectedPlayers, players: selectedPlayers,
game: tempGame, game: selectedGame!,
winner: widget.matchToEdit!.winner, winner: widget.matchToEdit!.winner,
createdAt: widget.matchToEdit!.createdAt, createdAt: widget.matchToEdit!.createdAt,
endedAt: widget.matchToEdit!.endedAt, endedAt: widget.matchToEdit!.endedAt,
@@ -297,6 +305,13 @@ class _CreateMatchViewState extends State<CreateMatchView> {
); );
} }
if (widget.matchToEdit!.game.id != updatedMatch.game.id) {
await db.matchDao.updateMatchGame(
matchId: widget.matchToEdit!.id,
gameId: updatedMatch.game.id,
);
}
// Add players who are in updatedMatch but not in the original match // 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)) {
@@ -326,8 +341,6 @@ class _CreateMatchViewState extends State<CreateMatchView> {
// Creates a new match and adds it to the database. // Creates a new match and adds it to the database.
// Returns the created match. // Returns the created match.
Future<Match> createMatch() async { Future<Match> createMatch() async {
final tempGame = await getTemporaryGame();
Match match = Match( Match match = Match(
name: _matchNameController.text.isEmpty name: _matchNameController.text.isEmpty
? (hintText ?? '') ? (hintText ?? '')
@@ -335,34 +348,18 @@ class _CreateMatchViewState extends State<CreateMatchView> {
createdAt: DateTime.now(), createdAt: DateTime.now(),
group: selectedGroup, group: selectedGroup,
players: selectedPlayers, players: selectedPlayers,
game: tempGame, game: selectedGame!,
); );
await db.matchDao.addMatch(match: match); await db.matchDao.addMatch(match: match);
return match; return match;
} }
// TODO: Remove when games fully implemented
Future<Game> getTemporaryGame() async {
Game? game;
final selectedGame = games[selectedGameIndex];
game = Game(
name: selectedGame.$1,
description: selectedGame.$2,
ruleset: selectedGame.$3,
color: GameColor.blue,
icon: '',
);
await db.gameDao.addGame(game: game);
return game;
}
// If a match was provided to the view, this method prefills the input fields // If a match was provided to the view, this method prefills the input fields
void prefillMatchDetails() { void prefillMatchDetails() {
final match = widget.matchToEdit!; final match = widget.matchToEdit!;
_matchNameController.text = match.name; _matchNameController.text = match.name;
selectedPlayers = match.players; selectedPlayers = match.players;
selectedGame = match.game;
if (match.group != null) { if (match.group != null) {
selectedGroup = match.group; selectedGroup = match.group;
@@ -383,4 +380,16 @@ class _CreateMatchViewState extends State<CreateMatchView> {
}); });
} }
} }
// Loads all games from the database and updates the gamesList.
Future<void> loadGames() async {
print("LOADING GAMES TRIGGERED");
final result = await db.gameDao.getAllGames();
result.sort((a, b) => b.createdAt.compareTo(a.createdAt));
setState(() {
gamesList = result;
});
print(gamesList.map((g) => g.name).join(', '));
print("GAMES LOADED");
}
} }

View File

@@ -1,9 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tallee/core/adaptive_page_route.dart'; import 'package:tallee/core/adaptive_page_route.dart';
import 'package:tallee/core/common.dart'; import 'package:tallee/core/common.dart';
import 'package:tallee/core/constants.dart'; import 'package:tallee/core/constants.dart';
import 'package:tallee/core/custom_theme.dart'; 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/dto/game.dart'; import 'package:tallee/data/dto/game.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';
@@ -13,17 +15,24 @@ 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';
class CreateGameView extends StatefulWidget { class CreateGameView extends StatefulWidget {
const CreateGameView({super.key, this.gameToEdit, required this.callback}); const CreateGameView({
super.key,
this.gameToEdit,
required this.onGameCreatedOrEdited,
});
/// An optional game to prefill the fields
final Game? gameToEdit; final Game? gameToEdit;
final VoidCallback callback; /// Callback to invoke when the game is created or edited
final VoidCallback onGameCreatedOrEdited;
@override @override
State<CreateGameView> createState() => _CreateGameViewState(); State<CreateGameView> createState() => _CreateGameViewState();
} }
class _CreateGameViewState extends State<CreateGameView> { class _CreateGameViewState extends State<CreateGameView> {
late final AppDatabase db;
Ruleset? selectedRuleset; Ruleset? selectedRuleset;
int selectedRulesetIndex = -1; int selectedRulesetIndex = -1;
late List<(Ruleset, String)> _rulesets; late List<(Ruleset, String)> _rulesets;
@@ -36,6 +45,7 @@ class _CreateGameViewState extends State<CreateGameView> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
db = Provider.of<AppDatabase>(context, listen: false);
_gameNameController.addListener(() => setState(() {})); _gameNameController.addListener(() => setState(() {}));
} }
@@ -171,9 +181,23 @@ class _CreateGameViewState extends State<CreateGameView> {
_gameNameController.text.trim().isNotEmpty && _gameNameController.text.trim().isNotEmpty &&
selectedRulesetIndex != -1 && selectedRulesetIndex != -1 &&
selectedColor != null selectedColor != null
? () { ? () async {
//TODO: Handle saving to db & updating game selection view Game newGame = Game(
Navigator.of(context).pop(); name: _gameNameController.text.trim(),
description: _descriptionController.text.trim(),
ruleset: selectedRuleset!,
color: selectedColor!,
icon: '',
);
if (isEditing) {
await handleGameUpdate(newGame);
} else {
await handleGameCreation(newGame);
}
widget.onGameCreatedOrEdited.call();
if (context.mounted) {
Navigator.of(context).pop(newGame);
}
} }
: null, : null,
), ),
@@ -184,4 +208,47 @@ class _CreateGameViewState extends State<CreateGameView> {
), ),
); );
} }
Future<void> handleGameUpdate(Game newGame) async {
final oldGame = widget.gameToEdit!;
if (oldGame.name != newGame.name) {
await db.gameDao.updateGameName(
gameId: oldGame.id,
newName: newGame.name,
);
}
if (oldGame.description != newGame.description) {
await db.gameDao.updateGameDescription(
gameId: oldGame.id,
newDescription: newGame.description,
);
}
if (oldGame.ruleset != newGame.ruleset) {
await db.gameDao.updateGameRuleset(
gameId: oldGame.id,
newRuleset: newGame.ruleset,
);
}
if (oldGame.color != newGame.color) {
await db.gameDao.updateGameColor(
gameId: oldGame.id,
newColor: newGame.color,
);
}
if (oldGame.icon != newGame.icon) {
await db.gameDao.updateGameIcon(
gameId: oldGame.id,
newIcon: newGame.icon,
);
}
}
Future<void> handleGameCreation(Game newGame) async {
await db.gameDao.addGame(game: newGame);
}
} }