From dbef735a821564485989f16a0101534e9ec08ad3 Mon Sep 17 00:00:00 2001 From: gelbeinhalb Date: Sun, 1 Feb 2026 18:13:03 +0100 Subject: [PATCH] add color enum --- lib/core/enums.dart | 11 ++++++++ lib/data/dao/game_dao.dart | 12 ++++----- lib/data/dao/match_dao.dart | 2 +- lib/data/dto/game.dart | 6 ++--- lib/data/dto/match.dart | 2 +- .../views/main_menu/home_view.dart | 2 +- .../create_match/create_match_view.dart | 4 +-- .../main_menu/match_view/match_view.dart | 2 +- lib/services/data_transfer_service.dart | 2 +- test/db_tests/game_test.dart | 26 +++++++++---------- test/db_tests/match_test.dart | 2 +- test/db_tests/player_match_test.dart | 2 +- test/db_tests/score_test.dart | 2 +- test/db_tests/team_test.dart | 4 +-- 14 files changed, 45 insertions(+), 34 deletions(-) diff --git a/lib/core/enums.dart b/lib/core/enums.dart index a6bf274..d3e0610 100644 --- a/lib/core/enums.dart +++ b/lib/core/enums.dart @@ -37,6 +37,17 @@ enum ExportResult { success, canceled, unknownException } /// - [Ruleset.multipleWinners]: Multiple players can be winners. enum Ruleset { highestScore, lowestScore, singleWinner, singleLoser, multipleWinners } +/// Different colors available for games +/// - [GameColor.red]: Red color +/// - [GameColor.blue]: Blue color +/// - [GameColor.green]: Green color +/// - [GameColor.yellow]: Yellow color +/// - [GameColor.purple]: Purple color +/// - [GameColor.orange]: Orange color +/// - [GameColor.pink]: Pink color +/// - [GameColor.teal]: Teal color +enum GameColor { red, blue, green, yellow, purple, orange, pink, teal } + /// Translates a [Ruleset] enum value to its corresponding localized string. String translateRulesetToString(Ruleset ruleset, BuildContext context) { final loc = AppLocalizations.of(context); diff --git a/lib/data/dao/game_dao.dart b/lib/data/dao/game_dao.dart index b656266..8e658ed 100644 --- a/lib/data/dao/game_dao.dart +++ b/lib/data/dao/game_dao.dart @@ -21,7 +21,7 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { name: row.name, ruleset: Ruleset.values.firstWhere((e) => e.name == row.ruleset), description: row.description, - color: row.color, + color: GameColor.values.firstWhere((e) => e.name == row.color), icon: row.icon, createdAt: row.createdAt, ), @@ -38,7 +38,7 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { name: result.name, ruleset: Ruleset.values.firstWhere((e) => e.name == result.ruleset), description: result.description, - color: result.color, + color: GameColor.values.firstWhere((e) => e.name == result.color), icon: result.icon, createdAt: result.createdAt, ); @@ -55,7 +55,7 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { name: game.name, ruleset: game.ruleset.name, description: game.description, - color: game.color, + color: game.color.name, icon: game.icon, createdAt: game.createdAt, ), @@ -81,7 +81,7 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { name: game.name, ruleset: game.ruleset.name, description: game.description, - color: game.color, + color: game.color.name, icon: game.icon, createdAt: game.createdAt, ), @@ -135,10 +135,10 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { } /// Updates the color of the game with the given [gameId]. - Future updateGameColor({required String gameId, required String newColor}) async { + Future updateGameColor({required String gameId, required GameColor newColor}) async { await (update( gameTable, - )..where((g) => g.id.equals(gameId))).write(GameTableCompanion(color: Value(newColor))); + )..where((g) => g.id.equals(gameId))).write(GameTableCompanion(color: Value(newColor.name))); } /// Updates the icon of the game with the given [gameId]. diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index 3f5a7a1..c2e9d72 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -126,7 +126,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { name: game.name, ruleset: game.ruleset.name, description: game.description, - color: game.color, + color: game.color.name, icon: game.icon, createdAt: game.createdAt, ), diff --git a/lib/data/dto/game.dart b/lib/data/dto/game.dart index b463a82..437d98e 100644 --- a/lib/data/dto/game.dart +++ b/lib/data/dto/game.dart @@ -8,7 +8,7 @@ class Game { final String name; final Ruleset ruleset; final String description; - final String color; + final GameColor color; final String icon; Game({ @@ -34,7 +34,7 @@ class Game { name = json['name'], ruleset = Ruleset.values.firstWhere((e) => e.name == json['ruleset']), description = json['description'], - color = json['color'], + color = GameColor.values.firstWhere((e) => e.name == json['color']), icon = json['icon']; /// Converts the Game instance to a JSON object. @@ -44,7 +44,7 @@ class Game { 'name': name, 'ruleset': ruleset.name, 'description': description, - 'color': color, + 'color': color.name, 'icon': icon, }; } diff --git a/lib/data/dto/match.dart b/lib/data/dto/match.dart index 4f919b4..559914e 100644 --- a/lib/data/dto/match.dart +++ b/lib/data/dto/match.dart @@ -41,7 +41,7 @@ class Match { createdAt = DateTime.parse(json['createdAt']), endedAt = json['endedAt'] != null ? DateTime.parse(json['endedAt']) : null, name = json['name'], - game = Game(name: '', ruleset: Ruleset.singleWinner, description: '', color: '', icon: ''), // Populated during import via DataTransferService + game = Game(name: '', ruleset: Ruleset.singleWinner, description: '', color: GameColor.blue, icon: ''), // Populated during import via DataTransferService group = null, // Populated during import via DataTransferService players = [], // Populated during import via DataTransferService notes = json['notes'] ?? ''; diff --git a/lib/presentation/views/main_menu/home_view.dart b/lib/presentation/views/main_menu/home_view.dart index f8c39f8..a7f5cfa 100644 --- a/lib/presentation/views/main_menu/home_view.dart +++ b/lib/presentation/views/main_menu/home_view.dart @@ -42,7 +42,7 @@ class _HomeViewState extends State { 2, Match( name: 'Skeleton Match', - game: Game(name: '', ruleset: Ruleset.singleWinner, description: '', color: '', icon: ''), + game: Game(name: '', ruleset: Ruleset.singleWinner, description: '', color: GameColor.blue, icon: ''), group: Group( name: 'Skeleton Group', description: '', diff --git a/lib/presentation/views/main_menu/match_view/create_match/create_match_view.dart b/lib/presentation/views/main_menu/match_view/create_match/create_match_view.dart index 35bffc4..163ebd2 100644 --- a/lib/presentation/views/main_menu/match_view/create_match/create_match_view.dart +++ b/lib/presentation/views/main_menu/match_view/create_match/create_match_view.dart @@ -202,7 +202,7 @@ class _CreateMatchViewState extends State { name: selectedGame.$1, description: selectedGame.$2, ruleset: selectedGame.$3, - color: '0xFF000000', + color: GameColor.blue, icon: '', ); } else { @@ -212,7 +212,7 @@ class _CreateMatchViewState extends State { name: selectedGame.$1, description: selectedGame.$2, ruleset: selectedGame.$3, - color: '0xFF000000', + color: GameColor.blue, icon: '', ); } diff --git a/lib/presentation/views/main_menu/match_view/match_view.dart b/lib/presentation/views/main_menu/match_view/match_view.dart index b2ef367..1f50342 100644 --- a/lib/presentation/views/main_menu/match_view/match_view.dart +++ b/lib/presentation/views/main_menu/match_view/match_view.dart @@ -38,7 +38,7 @@ class _MatchViewState extends State { 4, Match( name: 'Skeleton match name', - game: Game(name: '', ruleset: Ruleset.singleWinner, description: '', color: '', icon: ''), + game: Game(name: '', ruleset: Ruleset.singleWinner, description: '', color: GameColor.blue, icon: ''), group: Group( name: 'Group name', description: '', diff --git a/lib/services/data_transfer_service.dart b/lib/services/data_transfer_service.dart index fb69e12..4995c77 100644 --- a/lib/services/data_transfer_service.dart +++ b/lib/services/data_transfer_service.dart @@ -208,7 +208,7 @@ class DataTransferService { return Match( id: map['id'] as String, name: map['name'] as String, - game: game ?? Game(name: 'Unknown', ruleset: Ruleset.singleWinner, description: '', color: '', icon: ''), + game: game ?? Game(name: 'Unknown', ruleset: Ruleset.singleWinner, description: '', color: GameColor.blue, icon: ''), group: group, players: players.isNotEmpty ? players : null, createdAt: DateTime.parse(map['createdAt'] as String), diff --git a/test/db_tests/game_test.dart b/test/db_tests/game_test.dart index 27133ec..6038690 100644 --- a/test/db_tests/game_test.dart +++ b/test/db_tests/game_test.dart @@ -27,7 +27,7 @@ void main() { name: 'Chess', ruleset: Ruleset.singleWinner, description: 'A classic strategy game', - color: '0xFF0000FF', + color: GameColor.blue, icon: 'chess_icon', ); testGame2 = Game( @@ -35,7 +35,7 @@ void main() { name: 'Poker', ruleset: Ruleset.multipleWinners, description: 'Card game with multiple winners', - color: '0xFFFF0000', + color: GameColor.red, icon: 'poker_icon', ); testGame3 = Game( @@ -43,7 +43,7 @@ void main() { name: 'Monopoly', ruleset: Ruleset.highestScore, description: 'A board game about real estate', - color: '0xFF000000', + color: GameColor.orange, icon: '', ); }); @@ -134,7 +134,7 @@ void main() { // Verifies that a game with null optional fields can be added and retrieved. test('addGame handles game with null optional fields', () async { - final gameWithNulls = Game(name: 'Simple Game', ruleset: Ruleset.lowestScore, description: 'A simple game', color: '0xFF000000', icon: ''); + final gameWithNulls = Game(name: 'Simple Game', ruleset: Ruleset.lowestScore, description: 'A simple game', color: GameColor.green, icon: ''); final result = await database.gameDao.addGame(game: gameWithNulls); expect(result, true); @@ -143,7 +143,7 @@ void main() { ); expect(fetchedGame.name, 'Simple Game'); expect(fetchedGame.description, 'A simple game'); - expect(fetchedGame.color, '0xFF000000'); + expect(fetchedGame.color, GameColor.green); expect(fetchedGame.icon, isNull); }); @@ -340,20 +340,20 @@ void main() { await database.gameDao.updateGameColor( gameId: testGame1.id, - newColor: '0xFF00FF00', + newColor: GameColor.green, ); final updatedGame = await database.gameDao.getGameById( gameId: testGame1.id, ); - expect(updatedGame.color, '0xFF00FF00'); + expect(updatedGame.color, GameColor.green); }); // Verifies that updateGameColor does nothing when game doesn't exist. test('updateGameColor does nothing for non-existent game', () async { await database.gameDao.updateGameColor( gameId: 'non-existent-id', - newColor: '0xFF00FF00', + newColor: GameColor.green, ); final allGames = await database.gameDao.getAllGames(); @@ -460,7 +460,7 @@ void main() { name: 'Game\'s & "Special" ', ruleset: Ruleset.multipleWinners, description: 'Description with émojis 🎮🎲', - color: '0xFF000000', + color: GameColor.purple, icon: '', ); await database.gameDao.addGame(game: specialGame); @@ -479,7 +479,7 @@ void main() { ruleset: Ruleset.singleWinner, description: '', icon: '', - color: '0xFF000000', + color: GameColor.red, ); await database.gameDao.addGame(game: emptyGame); @@ -499,7 +499,7 @@ void main() { name: longString, description: longString, ruleset: Ruleset.multipleWinners, - color: '0xFF000000', + color: GameColor.yellow, icon: '', ); await database.gameDao.addGame(game: longGame); @@ -522,7 +522,7 @@ void main() { ); await database.gameDao.updateGameColor( gameId: testGame1.id, - newColor: '0xFF123456', + newColor: GameColor.teal, ); await database.gameDao.updateGameDescription( gameId: testGame1.id, @@ -533,7 +533,7 @@ void main() { gameId: testGame1.id, ); expect(updatedGame.name, 'Updated Name'); - expect(updatedGame.color, '0xFF123456'); + expect(updatedGame.color, GameColor.teal); expect(updatedGame.description, 'Updated Description'); expect(updatedGame.ruleset, testGame1.ruleset); expect(updatedGame.icon, testGame1.icon); diff --git a/test/db_tests/match_test.dart b/test/db_tests/match_test.dart index 2544ccc..4935f81 100644 --- a/test/db_tests/match_test.dart +++ b/test/db_tests/match_test.dart @@ -51,7 +51,7 @@ void main() { description: '', members: [testPlayer4, testPlayer5], ); - testGame = Game(name: 'Test Game', ruleset: Ruleset.singleWinner, description: 'A test game', color: '0xFF000000', icon: ''); + testGame = Game(name: 'Test Game', ruleset: Ruleset.singleWinner, description: 'A test game', color: GameColor.blue, icon: ''); testMatch1 = Match( name: 'First Test Match', game: testGame, diff --git a/test/db_tests/player_match_test.dart b/test/db_tests/player_match_test.dart index 164dbf9..ddc36d2 100644 --- a/test/db_tests/player_match_test.dart +++ b/test/db_tests/player_match_test.dart @@ -48,7 +48,7 @@ void main() { description: '', members: [testPlayer1, testPlayer2, testPlayer3], ); - testGame = Game(name: 'Test Game', ruleset: Ruleset.singleWinner, description: 'A test game', color: '0xFF000000', icon: ''); + testGame = Game(name: 'Test Game', ruleset: Ruleset.singleWinner, description: 'A test game', color: GameColor.blue, icon: ''); testMatchOnlyGroup = Match( name: 'Test Match with Group', game: testGame, diff --git a/test/db_tests/score_test.dart b/test/db_tests/score_test.dart index 1490dc9..2ad4671 100644 --- a/test/db_tests/score_test.dart +++ b/test/db_tests/score_test.dart @@ -32,7 +32,7 @@ void main() { testPlayer1 = Player(name: 'Alice', description: ''); testPlayer2 = Player(name: 'Bob', description: ''); testPlayer3 = Player(name: 'Charlie', description: ''); - testGame = Game(name: 'Test Game', ruleset: Ruleset.singleWinner, description: 'A test game', color: '0xFF000000', icon: ''); + testGame = Game(name: 'Test Game', ruleset: Ruleset.singleWinner, description: 'A test game', color: GameColor.blue, icon: ''); testMatch1 = Match( name: 'Test Match 1', game: testGame, diff --git a/test/db_tests/team_test.dart b/test/db_tests/team_test.dart index 8092d02..17ceff9 100644 --- a/test/db_tests/team_test.dart +++ b/test/db_tests/team_test.dart @@ -49,8 +49,8 @@ void main() { name: 'Team Gamma', members: [testPlayer1, testPlayer3], ); - testGame1 = Game(name: 'Game 1', ruleset: Ruleset.singleWinner, description: 'Test game 1', color: '0xFF000000', icon: ''); - testGame2 = Game(name: 'Game 2', ruleset: Ruleset.highestScore, description: 'Test game 2', color: '0xFF000000', icon: ''); + testGame1 = Game(name: 'Game 1', ruleset: Ruleset.singleWinner, description: 'Test game 1', color: GameColor.blue, icon: ''); + testGame2 = Game(name: 'Game 2', ruleset: Ruleset.highestScore, description: 'Test game 2', color: GameColor.red, icon: ''); }); await database.playerDao.addPlayersAsList(