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;
}
static Future<List<Game>> getAll() async {
static Future<Map<String, Game>> 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<String, Game>>({}, (map, game) => {...map, game.name: game});
}
static Future<void> delete(Game game) async {

View File

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