diff --git a/lib/game.dart b/lib/game.dart index 583e2c9..4ea6f19 100644 --- a/lib/game.dart +++ b/lib/game.dart @@ -21,6 +21,17 @@ class Game { this.lastUpdated = '', }) : _internalVersionRegex = RegExp(versionRegex); + factory Game.fromMap(Map map) { + return Game( + name: map['name'], + versionRegex: map['version_regex'], + lastPlayed: map['last_played'], + rssFeedUrl: map['rss_feed_url'], + actualVersion: map['actual_version'], + lastUpdated: map['last_updated'], + ); + } + Future updateActualVersion() async { final response = await http.get(Uri.parse(rssFeedUrl)); final document = RssFeed.parse(response.body); @@ -74,6 +85,14 @@ last_updated = excluded.last_updated ); return game; } + + 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(); + } } //CREATE TABLE IF NOT EXISTS games ( diff --git a/lib/main.dart b/lib/main.dart index cc9fbc7..268e411 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,16 +5,6 @@ import 'package:gamer_updater/game.dart'; void main() async { await DB.init(); - var game = Game( - name: 'Rimworld', - versionRegex: r'(\d+\.\d+\.\d+)', - lastPlayed: '1.4.3704', - rssFeedUrl: - 'https://store.steampowered.com/feeds/news/app/294100/?cc=HR&l=english', - ); - await game.updateActualVersion(); - await GameRepository.upsert(game); - runApp(const MyApp()); } @@ -55,6 +45,22 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { + late List games = []; + + @override + void initState() { + super.initState(); + GameRepository.getAll().then((games) { + setState(() { + this.games = games; + }); + for (var e in games) { + e.updateActualVersion(); + GameRepository.upsert(e); + } + }); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -65,7 +71,10 @@ class _MyHomePageState extends State { body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, - children: [const Text('Hello cyka')], + children: [ + Text(games.map((e) => e.name).join('\n')), + Text(games.map((e) => e.actualVersion).join('\n')), + ], ), ), );