Rework data structure to map from list

This commit is contained in:
2025-02-22 15:31:23 +01:00
parent c94a8f8926
commit fef5f199c3
2 changed files with 25 additions and 21 deletions

View File

@@ -86,12 +86,14 @@ last_updated = excluded.last_updated
return game; return game;
} }
static Future<List<Game>> getAll() async { static Future<Map<String, Game>> getAll() async {
final db = DB.db; final db = DB.db;
final games = await db.rawQuery( final games = await db.rawQuery(
'SELECT name, actual_version, last_played, rss_feed_url, version_regex, last_updated FROM games', '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<String, Game>>({}, (map, game) => {...map, game.name: game});
} }
static Future<void> delete(Game game) async { static Future<void> delete(Game game) async {

View File

@@ -53,7 +53,7 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
late List<Game> games = []; late Map<String, Game> games = {};
@override @override
void initState() { void initState() {
@@ -86,31 +86,33 @@ class _MyHomePageState extends State<MyHomePage> {
spacing: 4, spacing: 4,
runSpacing: 4, runSpacing: 4,
children: [ children: [
...games.map((game) => SizedBox( ...games.values.map(
(game) => SizedBox(
width: 400, width: 400,
child: GameCard( child: GameCard(
game: game, game: game,
onGameUpdated: (game) async { onGameUpdated: (game) async {
game = await GameRepository.upsert(game); game = await GameRepository.upsert(game);
setState(() { setState(() {
games[games.indexOf(game)] = game; games[game.name] = game;
}); });
}, },
onDelete: () async { onDelete: () async {
await GameRepository.delete(game); await GameRepository.delete(game);
setState(() { setState(() {
games.remove(game); games.remove(game.name);
}); });
}, },
), ),
)), ),
),
SizedBox( SizedBox(
width: 400, width: 400,
child: NewGameCard( child: NewGameCard(
onGameCreated: (game) async { onGameCreated: (game) async {
game = await GameRepository.upsert(game); game = await GameRepository.upsert(game);
setState(() { setState(() {
games.add(game); games[game.name] = game;
}); });
}, },
), ),