diff --git a/lib/data/dao/game_dao.dart b/lib/data/dao/game_dao.dart index 4b99dfa..1d8af98 100644 --- a/lib/data/dao/game_dao.dart +++ b/lib/data/dao/game_dao.dart @@ -20,7 +20,7 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { name: row.name, ruleset: row.ruleset, description: row.description, - color: row.color != null ? int.tryParse(row.color!) : null, + color: row.color, icon: row.icon, createdAt: row.createdAt, ), @@ -37,7 +37,7 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { name: result.name, ruleset: result.ruleset, description: result.description, - color: result.color != null ? int.tryParse(result.color!) : null, + color: result.color, icon: result.icon, createdAt: result.createdAt, ); @@ -54,7 +54,7 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { name: game.name, ruleset: game.ruleset ?? '', description: Value(game.description), - color: Value(game.color?.toString()), + color: game.color, icon: Value(game.icon), createdAt: game.createdAt, ), @@ -80,7 +80,7 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { name: game.name, ruleset: game.ruleset ?? '', description: Value(game.description), - color: Value(game.color?.toString()), + color: game.color, icon: Value(game.icon), createdAt: game.createdAt, ), @@ -142,10 +142,10 @@ class GameDao extends DatabaseAccessor with _$GameDaoMixin { /// Updates the color of the game with the given [gameId]. Future updateGameColor({ required String gameId, - required int? newColor, + required String newColor, }) async { await (update(gameTable)..where((g) => g.id.equals(gameId))).write( - GameTableCompanion(color: Value(newColor?.toString())), + GameTableCompanion(color: Value(newColor)), ); } diff --git a/lib/data/dao/match_dao.dart b/lib/data/dao/match_dao.dart index 7c67e08..af6d602 100644 --- a/lib/data/dao/match_dao.dart +++ b/lib/data/dao/match_dao.dart @@ -129,7 +129,7 @@ class MatchDao extends DatabaseAccessor with _$MatchDaoMixin { name: game.name, ruleset: game.ruleset ?? '', description: Value(game.description), - color: Value(game.color?.toString()), + color: game.color, icon: Value(game.icon), createdAt: game.createdAt, ), diff --git a/lib/data/db/database.g.dart b/lib/data/db/database.g.dart index fbc76f0..a2d99c1 100644 --- a/lib/data/db/database.g.dart +++ b/lib/data/db/database.g.dart @@ -682,9 +682,9 @@ class $GameTableTable extends GameTable late final GeneratedColumn color = GeneratedColumn( 'color', aliasedName, - true, + false, type: DriftSqlType.string, - requiredDuringInsert: false, + requiredDuringInsert: true, ); static const VerificationMeta _iconMeta = const VerificationMeta('icon'); @override @@ -763,6 +763,8 @@ class $GameTableTable extends GameTable _colorMeta, color.isAcceptableOrUnknown(data['color']!, _colorMeta), ); + } else if (isInserting) { + context.missing(_colorMeta); } if (data.containsKey('icon')) { context.handle( @@ -806,7 +808,7 @@ class $GameTableTable extends GameTable color: attachedDatabase.typeMapping.read( DriftSqlType.string, data['${effectivePrefix}color'], - ), + )!, icon: attachedDatabase.typeMapping.read( DriftSqlType.string, data['${effectivePrefix}icon'], @@ -829,7 +831,7 @@ class GameTableData extends DataClass implements Insertable { final String name; final String ruleset; final String? description; - final String? color; + final String color; final String? icon; final DateTime createdAt; const GameTableData({ @@ -837,7 +839,7 @@ class GameTableData extends DataClass implements Insertable { required this.name, required this.ruleset, this.description, - this.color, + required this.color, this.icon, required this.createdAt, }); @@ -850,9 +852,7 @@ class GameTableData extends DataClass implements Insertable { if (!nullToAbsent || description != null) { map['description'] = Variable(description); } - if (!nullToAbsent || color != null) { - map['color'] = Variable(color); - } + map['color'] = Variable(color); if (!nullToAbsent || icon != null) { map['icon'] = Variable(icon); } @@ -868,9 +868,7 @@ class GameTableData extends DataClass implements Insertable { description: description == null && nullToAbsent ? const Value.absent() : Value(description), - color: color == null && nullToAbsent - ? const Value.absent() - : Value(color), + color: Value(color), icon: icon == null && nullToAbsent ? const Value.absent() : Value(icon), createdAt: Value(createdAt), ); @@ -886,7 +884,7 @@ class GameTableData extends DataClass implements Insertable { name: serializer.fromJson(json['name']), ruleset: serializer.fromJson(json['ruleset']), description: serializer.fromJson(json['description']), - color: serializer.fromJson(json['color']), + color: serializer.fromJson(json['color']), icon: serializer.fromJson(json['icon']), createdAt: serializer.fromJson(json['createdAt']), ); @@ -899,7 +897,7 @@ class GameTableData extends DataClass implements Insertable { 'name': serializer.toJson(name), 'ruleset': serializer.toJson(ruleset), 'description': serializer.toJson(description), - 'color': serializer.toJson(color), + 'color': serializer.toJson(color), 'icon': serializer.toJson(icon), 'createdAt': serializer.toJson(createdAt), }; @@ -910,7 +908,7 @@ class GameTableData extends DataClass implements Insertable { String? name, String? ruleset, Value description = const Value.absent(), - Value color = const Value.absent(), + String? color, Value icon = const Value.absent(), DateTime? createdAt, }) => GameTableData( @@ -918,7 +916,7 @@ class GameTableData extends DataClass implements Insertable { name: name ?? this.name, ruleset: ruleset ?? this.ruleset, description: description.present ? description.value : this.description, - color: color.present ? color.value : this.color, + color: color ?? this.color, icon: icon.present ? icon.value : this.icon, createdAt: createdAt ?? this.createdAt, ); @@ -971,7 +969,7 @@ class GameTableCompanion extends UpdateCompanion { final Value name; final Value ruleset; final Value description; - final Value color; + final Value color; final Value icon; final Value createdAt; final Value rowid; @@ -990,13 +988,14 @@ class GameTableCompanion extends UpdateCompanion { required String name, required String ruleset, this.description = const Value.absent(), - this.color = const Value.absent(), + required String color, this.icon = const Value.absent(), required DateTime createdAt, this.rowid = const Value.absent(), }) : id = Value(id), name = Value(name), ruleset = Value(ruleset), + color = Value(color), createdAt = Value(createdAt); static Insertable custom({ Expression? id, @@ -1025,7 +1024,7 @@ class GameTableCompanion extends UpdateCompanion { Value? name, Value? ruleset, Value? description, - Value? color, + Value? color, Value? icon, Value? createdAt, Value? rowid, @@ -3665,7 +3664,7 @@ typedef $$GameTableTableCreateCompanionBuilder = required String name, required String ruleset, Value description, - Value color, + required String color, Value icon, required DateTime createdAt, Value rowid, @@ -3676,7 +3675,7 @@ typedef $$GameTableTableUpdateCompanionBuilder = Value name, Value ruleset, Value description, - Value color, + Value color, Value icon, Value createdAt, Value rowid, @@ -3910,7 +3909,7 @@ class $$GameTableTableTableManager Value name = const Value.absent(), Value ruleset = const Value.absent(), Value description = const Value.absent(), - Value color = const Value.absent(), + Value color = const Value.absent(), Value icon = const Value.absent(), Value createdAt = const Value.absent(), Value rowid = const Value.absent(), @@ -3930,7 +3929,7 @@ class $$GameTableTableTableManager required String name, required String ruleset, Value description = const Value.absent(), - Value color = const Value.absent(), + required String color, Value icon = const Value.absent(), required DateTime createdAt, Value rowid = const Value.absent(), diff --git a/lib/data/db/tables/game_table.dart b/lib/data/db/tables/game_table.dart index 2074ae7..eaa3e1b 100644 --- a/lib/data/db/tables/game_table.dart +++ b/lib/data/db/tables/game_table.dart @@ -5,7 +5,7 @@ class GameTable extends Table { TextColumn get name => text()(); TextColumn get ruleset => text()(); TextColumn get description => text().nullable()(); - TextColumn get color => text().nullable()(); + TextColumn get color => text()(); TextColumn get icon => text().nullable()(); DateTimeColumn get createdAt => dateTime()(); diff --git a/lib/data/dto/game.dart b/lib/data/dto/game.dart index 9e271e5..5afbd9b 100644 --- a/lib/data/dto/game.dart +++ b/lib/data/dto/game.dart @@ -7,7 +7,7 @@ class Game { final String name; final String? ruleset; final String? description; - final int? color; + final String color; final String? icon; Game({ @@ -16,7 +16,7 @@ class Game { required this.name, this.ruleset, this.description, - this.color, + required this.color, this.icon, }) : id = id ?? const Uuid().v4(), createdAt = createdAt ?? clock.now(); 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 eb630df..31e232f 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,6 +202,7 @@ class _CreateMatchViewState extends State { name: selectedGame.$1, description: selectedGame.$2, ruleset: selectedGame.$3.name, + color: '0xFF000000', ); } else { // Use the selected game from the list @@ -210,6 +211,7 @@ class _CreateMatchViewState extends State { name: selectedGame.$1, description: selectedGame.$2, ruleset: selectedGame.$3.name, + color: '0xFF000000', ); } // Add the game to the database if it doesn't exist diff --git a/test/db_tests/game_test.dart b/test/db_tests/game_test.dart index a1d8c1f..250d67a 100644 --- a/test/db_tests/game_test.dart +++ b/test/db_tests/game_test.dart @@ -26,7 +26,7 @@ void main() { name: 'Chess', ruleset: 'winner.single', description: 'A classic strategy game', - color: 0xFF0000FF, + color: '0xFF0000FF', icon: 'chess_icon', ); testGame2 = Game( @@ -34,13 +34,14 @@ void main() { name: 'Poker', ruleset: 'Texas Hold\'em rules', description: 'winner.multiple', - color: 0xFFFF0000, + color: '0xFFFF0000', icon: 'poker_icon', ); testGame3 = Game( id: 'game3', name: 'Monopoly', description: 'A board game about real estate', + color: '0xFF000000', ); }); }); @@ -130,7 +131,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'); + final gameWithNulls = Game(name: 'Simple Game', color: '0xFF000000'); final result = await database.gameDao.addGame(game: gameWithNulls); expect(result, true); @@ -139,7 +140,7 @@ void main() { ); expect(fetchedGame.name, 'Simple Game'); expect(fetchedGame.description, isNull); - expect(fetchedGame.color, isNull); + expect(fetchedGame.color, '0xFF000000'); expect(fetchedGame.icon, isNull); }); @@ -336,35 +337,20 @@ void main() { await database.gameDao.updateGameColor( gameId: testGame1.id, - newColor: 0xFF00FF00, + newColor: '0xFF00FF00', ); final updatedGame = await database.gameDao.getGameById( gameId: testGame1.id, ); - expect(updatedGame.color, 0xFF00FF00); - }); - - // Verifies that updateGameColor can set the color to null. - test('updateGameColor can set color to null', () async { - await database.gameDao.addGame(game: testGame1); - - await database.gameDao.updateGameColor( - gameId: testGame1.id, - newColor: null, - ); - - final updatedGame = await database.gameDao.getGameById( - gameId: testGame1.id, - ); - expect(updatedGame.color, isNull); + expect(updatedGame.color, '0xFF00FF00'); }); // 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: '0xFF00FF00', ); final allGames = await database.gameDao.getAllGames(); @@ -470,6 +456,7 @@ void main() { final specialGame = Game( name: 'Game\'s & "Special" ', description: 'Description with émojis 🎮🎲', + color: '0xFF000000', ); await database.gameDao.addGame(game: specialGame); @@ -487,6 +474,7 @@ void main() { ruleset: '', description: '', icon: '', + color: '0xFF000000', ); await database.gameDao.addGame(game: emptyGame); @@ -506,6 +494,7 @@ void main() { name: longString, description: longString, ruleset: longString, + color: '0xFF000000', ); await database.gameDao.addGame(game: longGame); @@ -527,7 +516,7 @@ void main() { ); await database.gameDao.updateGameColor( gameId: testGame1.id, - newColor: 0xFF123456, + newColor: '0xFF123456', ); await database.gameDao.updateGameDescription( gameId: testGame1.id, @@ -538,7 +527,7 @@ void main() { gameId: testGame1.id, ); expect(updatedGame.name, 'Updated Name'); - expect(updatedGame.color, 0xFF123456); + expect(updatedGame.color, '0xFF123456'); 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 5941523..055efad 100644 --- a/test/db_tests/match_test.dart +++ b/test/db_tests/match_test.dart @@ -48,7 +48,7 @@ void main() { name: 'Test Group 2', members: [testPlayer4, testPlayer5], ); - testGame = Game(name: 'Test Game'); + testGame = Game(name: 'Test Game', color: '0xFF000000'); 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 8c68395..899857e 100644 --- a/test/db_tests/player_match_test.dart +++ b/test/db_tests/player_match_test.dart @@ -46,7 +46,7 @@ void main() { name: 'Test Group', members: [testPlayer1, testPlayer2, testPlayer3], ); - testGame = Game(name: 'Test Game'); + testGame = Game(name: 'Test Game', color: '0xFF000000'); 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 109febf..5ddaade 100644 --- a/test/db_tests/score_test.dart +++ b/test/db_tests/score_test.dart @@ -31,7 +31,7 @@ void main() { testPlayer1 = Player(name: 'Alice'); testPlayer2 = Player(name: 'Bob'); testPlayer3 = Player(name: 'Charlie'); - testGame = Game(name: 'Test Game'); + testGame = Game(name: 'Test Game', color: '0xFF000000'); 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 df2c93c..72c1adc 100644 --- a/test/db_tests/team_test.dart +++ b/test/db_tests/team_test.dart @@ -48,8 +48,8 @@ void main() { name: 'Team Gamma', members: [testPlayer1, testPlayer3], ); - testGame1 = Game(name: 'Game 1'); - testGame2 = Game(name: 'Game 2'); + testGame1 = Game(name: 'Game 1', color: '0xFF000000'); + testGame2 = Game(name: 'Game 2', color: '0xFF000000'); }); await database.playerDao.addPlayersAsList(