Added provider for games
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:maggs_victory_voyage/services/feed_provider.dart';
|
|||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:maggs_victory_voyage/services/db_helper.dart';
|
import 'package:maggs_victory_voyage/services/db_helper.dart';
|
||||||
import 'pages/splash_page.dart';
|
import 'pages/splash_page.dart';
|
||||||
|
import 'services/games_provider.dart';
|
||||||
import 'services/profiles_provider.dart';
|
import 'services/profiles_provider.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
@@ -16,6 +17,9 @@ void main() async {
|
|||||||
),
|
),
|
||||||
ChangeNotifierProvider(
|
ChangeNotifierProvider(
|
||||||
create: (_) => Profiles(),
|
create: (_) => Profiles(),
|
||||||
|
),
|
||||||
|
ChangeNotifierProvider(
|
||||||
|
create: (_) => Games(),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
child: const Application(),
|
child: const Application(),
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
class Game {
|
class Game {
|
||||||
final int id;
|
final int id;
|
||||||
final String name;
|
final String name;
|
||||||
|
final List<Map<String, dynamic>> rewards;
|
||||||
|
|
||||||
Game.fromMap(Map<String, dynamic> map)
|
Game.fromMap(Map<String, dynamic> map, Iterable<Map<String, dynamic>> rewards)
|
||||||
: id = map['id'],
|
: id = map['id'],
|
||||||
name = map['name'];
|
name = map['name'],
|
||||||
|
rewards = rewards.toList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:maggs_victory_voyage/services/db_helper.dart';
|
import 'package:maggs_victory_voyage/services/db_helper.dart';
|
||||||
|
import 'package:maggs_victory_voyage/widgets/GamesButton.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
import '../models/game.dart';
|
||||||
import '../models/profile.dart';
|
import '../models/profile.dart';
|
||||||
|
import '../services/games_provider.dart';
|
||||||
import '../services/profiles_provider.dart';
|
import '../services/profiles_provider.dart';
|
||||||
|
|
||||||
class GamesPage extends StatefulWidget {
|
class GamesPage extends StatefulWidget {
|
||||||
@@ -14,31 +17,17 @@ class GamesPage extends StatefulWidget {
|
|||||||
|
|
||||||
class _GamesPageState extends State<GamesPage> {
|
class _GamesPageState extends State<GamesPage> {
|
||||||
Profile? userProfile;
|
Profile? userProfile;
|
||||||
final List<Map<String, dynamic>> games = [
|
List<Game> games = [];
|
||||||
{
|
|
||||||
'name': 'Mario Kart',
|
|
||||||
'points': 4,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'name': 'Mario Party',
|
|
||||||
'points': 6,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'name': 'Exploding Kittens',
|
|
||||||
'points': 2,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
'name': 'Twister',
|
|
||||||
'points': 2,
|
|
||||||
}
|
|
||||||
];
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
userProfile = Provider.of<Profiles>(context, listen: true).own;
|
userProfile = Provider.of<Profiles>(context, listen: true).own;
|
||||||
|
games = Provider.of<Games>(context, listen: true).games;
|
||||||
|
|
||||||
return Center(
|
return Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
children: _getWidgets(games, context),
|
children: _getGames(),
|
||||||
|
// _getWidgets(games, context),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -83,4 +72,16 @@ class _GamesPageState extends State<GamesPage> {
|
|||||||
DbHelper.pushFeedEntry(
|
DbHelper.pushFeedEntry(
|
||||||
'${userProfile!.username} just won at ${game['name']}');
|
'${userProfile!.username} just won at ${game['name']}');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Widget> _getGames() {
|
||||||
|
if (games.isEmpty) {
|
||||||
|
return [const CircularProgressIndicator()];
|
||||||
|
} else {
|
||||||
|
final List<Widget> list = [];
|
||||||
|
for (final game in games) {
|
||||||
|
list.add(const GamesButton());
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:maggs_victory_voyage/models/game.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:supabase_flutter/supabase_flutter.dart';
|
import 'package:supabase_flutter/supabase_flutter.dart';
|
||||||
|
|
||||||
@@ -93,6 +94,22 @@ class DbHelper {
|
|||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<List<Game>> fetchGames() async {
|
||||||
|
List<Game> games = [];
|
||||||
|
final res = await _supabase.from('games').select().eq('active', true);
|
||||||
|
final rewardsRes =
|
||||||
|
await _supabase.from('rewards').select<List<Map<String, dynamic>>>();
|
||||||
|
|
||||||
|
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<void> pushFeedEntry(String text) async {
|
static Future<void> pushFeedEntry(String text) async {
|
||||||
await _supabase
|
await _supabase
|
||||||
.from('feed')
|
.from('feed')
|
||||||
|
|||||||
19
lib/services/games_provider.dart
Normal file
19
lib/services/games_provider.dart
Normal file
@@ -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<Game> _games = [];
|
||||||
|
|
||||||
|
List<Game> get games {
|
||||||
|
if (_games.isEmpty) {
|
||||||
|
DbHelper.fetchGames().then((value) {
|
||||||
|
_games.clear();
|
||||||
|
_games.addAll(value);
|
||||||
|
notifyListeners();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return _games;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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: () {},
|
||||||
|
)
|
||||||
|
]),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user