From fef5f199c399e27ff1e7c6569e72dfea43e2e49b Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 22 Feb 2025 15:31:23 +0100 Subject: [PATCH] Rework data structure to map from list --- lib/game.dart | 6 ++++-- lib/main.dart | 40 +++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/game.dart b/lib/game.dart index 7ca3cd7..27015b7 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -86,12 +86,14 @@ last_updated = excluded.last_updated return game; } - static Future> getAll() async { + static Future> getAll() async { final db = DB.db; final games = await db.rawQuery( 'SELECT name, actual_version, last_played, rss_feed_url, version_regex, last_updated FROM games', ); - return games.map((e) => Game.fromMap(e)).toList(); + return games + .map((e) => Game.fromMap(e)) + .fold>({}, (map, game) => {...map, game.name: game}); } static Future delete(Game game) async { diff --git a/lib/main.dart b/lib/main.dart index 35d2ac1..0c4f951 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -53,7 +53,7 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { - late List games = []; + late Map games = {}; @override void initState() { @@ -86,31 +86,33 @@ class _MyHomePageState extends State { spacing: 4, runSpacing: 4, children: [ - ...games.map((game) => SizedBox( - width: 400, - child: GameCard( - game: game, - onGameUpdated: (game) async { - game = await GameRepository.upsert(game); - setState(() { - games[games.indexOf(game)] = game; - }); - }, - onDelete: () async { - await GameRepository.delete(game); - setState(() { - games.remove(game); - }); - }, + ...games.values.map( + (game) => SizedBox( + width: 400, + child: GameCard( + game: game, + onGameUpdated: (game) async { + game = await GameRepository.upsert(game); + setState(() { + games[game.name] = game; + }); + }, + onDelete: () async { + await GameRepository.delete(game); + setState(() { + games.remove(game.name); + }); + }, + ), ), - )), + ), SizedBox( width: 400, child: NewGameCard( onGameCreated: (game) async { game = await GameRepository.upsert(game); setState(() { - games.add(game); + games[game.name] = game; }); }, ),