Rework data structure to map from list
This commit is contained in:
@@ -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 {
|
||||
|
@@ -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(
|
||||
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;
|
||||
});
|
||||
},
|
||||
),
|
||||
|
Reference in New Issue
Block a user