From f2e0ce1694ab91286ac87c547937f02773cc6919 Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Sat, 21 Oct 2023 11:14:28 +0200 Subject: [PATCH] Added provider for games --- lib/main.dart | 4 ++++ lib/models/game.dart | 6 +++-- lib/pages/games_page.dart | 39 ++++++++++++++++---------------- lib/services/db_helper.dart | 17 ++++++++++++++ lib/services/games_provider.dart | 19 ++++++++++++++++ lib/widgets/GamesButton.dart | 19 ++++++++++++++++ 6 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 lib/services/games_provider.dart diff --git a/lib/main.dart b/lib/main.dart index c937111..947b150 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -3,6 +3,7 @@ import 'package:maggs_victory_voyage/services/feed_provider.dart'; import 'package:provider/provider.dart'; import 'package:maggs_victory_voyage/services/db_helper.dart'; import 'pages/splash_page.dart'; +import 'services/games_provider.dart'; import 'services/profiles_provider.dart'; void main() async { @@ -16,6 +17,9 @@ void main() async { ), ChangeNotifierProvider( create: (_) => Profiles(), + ), + ChangeNotifierProvider( + create: (_) => Games(), ) ], child: const Application(), diff --git a/lib/models/game.dart b/lib/models/game.dart index 90974f5..1ce687c 100644 --- a/lib/models/game.dart +++ b/lib/models/game.dart @@ -1,8 +1,10 @@ class Game { final int id; final String name; + final List> rewards; - Game.fromMap(Map map) + Game.fromMap(Map map, Iterable> rewards) : id = map['id'], - name = map['name']; + name = map['name'], + rewards = rewards.toList(); } diff --git a/lib/pages/games_page.dart b/lib/pages/games_page.dart index 7af089a..d73a57b 100644 --- a/lib/pages/games_page.dart +++ b/lib/pages/games_page.dart @@ -1,8 +1,11 @@ import 'package:flutter/material.dart'; import 'package:maggs_victory_voyage/services/db_helper.dart'; +import 'package:maggs_victory_voyage/widgets/GamesButton.dart'; import 'package:provider/provider.dart'; +import '../models/game.dart'; import '../models/profile.dart'; +import '../services/games_provider.dart'; import '../services/profiles_provider.dart'; class GamesPage extends StatefulWidget { @@ -14,31 +17,17 @@ class GamesPage extends StatefulWidget { class _GamesPageState extends State { Profile? userProfile; - final List> games = [ - { - 'name': 'Mario Kart', - 'points': 4, - }, - { - 'name': 'Mario Party', - 'points': 6, - }, - { - 'name': 'Exploding Kittens', - 'points': 2, - }, - { - 'name': 'Twister', - 'points': 2, - } - ]; + List games = []; + @override Widget build(BuildContext context) { userProfile = Provider.of(context, listen: true).own; + games = Provider.of(context, listen: true).games; return Center( child: Column( - children: _getWidgets(games, context), + children: _getGames(), + // _getWidgets(games, context), ), ); } @@ -83,4 +72,16 @@ class _GamesPageState extends State { DbHelper.pushFeedEntry( '${userProfile!.username} just won at ${game['name']}'); } + + List _getGames() { + if (games.isEmpty) { + return [const CircularProgressIndicator()]; + } else { + final List list = []; + for (final game in games) { + list.add(const GamesButton()); + } + return list; + } + } } diff --git a/lib/services/db_helper.dart b/lib/services/db_helper.dart index d794d15..7e42050 100644 --- a/lib/services/db_helper.dart +++ b/lib/services/db_helper.dart @@ -1,4 +1,5 @@ import 'package:flutter/foundation.dart'; +import 'package:maggs_victory_voyage/models/game.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:supabase_flutter/supabase_flutter.dart'; @@ -93,6 +94,22 @@ class DbHelper { return items; } + static Future> fetchGames() async { + List games = []; + final res = await _supabase.from('games').select().eq('active', true); + final rewardsRes = + await _supabase.from('rewards').select>>(); + + for (final map in res) { + final rewards = + rewardsRes.where((element) => element['game_id'] == map['id']); + final game = Game.fromMap(map, rewards); + games.add(game); + } + + return games; + } + static Future pushFeedEntry(String text) async { await _supabase .from('feed') diff --git a/lib/services/games_provider.dart b/lib/services/games_provider.dart new file mode 100644 index 0000000..8e7cf58 --- /dev/null +++ b/lib/services/games_provider.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; +import 'package:maggs_victory_voyage/services/db_helper.dart'; + +import '../models/game.dart'; + +class Games extends ChangeNotifier { + final List _games = []; + + List get games { + if (_games.isEmpty) { + DbHelper.fetchGames().then((value) { + _games.clear(); + _games.addAll(value); + notifyListeners(); + }); + } + return _games; + } +} diff --git a/lib/widgets/GamesButton.dart b/lib/widgets/GamesButton.dart index e69de29..eb30213 100644 --- a/lib/widgets/GamesButton.dart +++ b/lib/widgets/GamesButton.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; + +class GamesButton extends StatelessWidget { + const GamesButton({super.key}); + + @override + Widget build(BuildContext context) { + return Expanded( + child: Card( + child: Row(children: [ + const Text('Title'), + TextButton( + child: const Text('I won'), + onPressed: () {}, + ) + ]), + )); + } +}