From c951458d4c420bb28fa6653e091941dfb0df3069 Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Sat, 21 Oct 2023 01:30:04 +0200 Subject: [PATCH] winning games pushes feed --- lib/pages/games_page.dart | 59 ++++++++++++++++++++++------- lib/services/db_helper.dart | 11 ++++++ lib/services/profiles_provider.dart | 10 +++++ 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/lib/pages/games_page.dart b/lib/pages/games_page.dart index ed093fd..7af089a 100644 --- a/lib/pages/games_page.dart +++ b/lib/pages/games_page.dart @@ -1,15 +1,41 @@ import 'package:flutter/material.dart'; +import 'package:maggs_victory_voyage/services/db_helper.dart'; +import 'package:provider/provider.dart'; -class GamesPage extends StatelessWidget { +import '../models/profile.dart'; +import '../services/profiles_provider.dart'; + +class GamesPage extends StatefulWidget { const GamesPage({super.key}); + + @override + State createState() => _GamesPageState(); +} + +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, + } + ]; @override Widget build(BuildContext context) { - final games = [ - 'Mario Kart', - 'Mario Party', - 'Exploding Kittens', - 'Twister', - ]; + userProfile = Provider.of(context, listen: true).own; + return Center( child: Column( children: _getWidgets(games, context), @@ -17,17 +43,18 @@ class GamesPage extends StatelessWidget { ); } - List _getWidgets(List list, BuildContext context) { + List _getWidgets( + List> games, BuildContext context) { final width = MediaQuery.of(context).size.width * 0.8; List widgets = []; - for (final game in list) { + for (final game in games) { widgets.add( Expanded( child: SizedBox( width: width, child: Padding( padding: const EdgeInsets.symmetric(vertical: 10), - child: _getGamesButton(() {}, game, context)), + child: _getGamesButton(context, game)), ), ), ); @@ -43,11 +70,17 @@ class GamesPage extends StatelessWidget { } ElevatedButton _getGamesButton( - void Function()? onPressed, String title, BuildContext context) { + BuildContext context, Map game) { return ElevatedButton( style: ElevatedButton.styleFrom(shape: const BeveledRectangleBorder()), - onPressed: onPressed, - child: _getText(title, context), + onPressed: () => _onPressed(game), + child: _getText(game['name'], context), ); } + + void _onPressed(Map game) { + DbHelper.pushGameWin(userProfile!.points, game['points']); + DbHelper.pushFeedEntry( + '${userProfile!.username} just won at ${game['name']}'); + } } diff --git a/lib/services/db_helper.dart b/lib/services/db_helper.dart index 1d95b31..f866be8 100644 --- a/lib/services/db_helper.dart +++ b/lib/services/db_helper.dart @@ -90,6 +90,17 @@ class DbHelper { return items; } + static Future pushFeedEntry(String text) async { + await _supabase + .from('feed') + .insert({'text': text, 'timestamp': DateTime.now().toString()}); + } + + static Future pushGameWin(int currentPoints, int pointsWon) async { + await _supabase.from('profiles').update( + {'points': currentPoints + pointsWon}).eq('id', currentUser!.id); + } + static Stream>> get feedStream => _supabase.from('feed').stream(primaryKey: ['timestamp']); diff --git a/lib/services/profiles_provider.dart b/lib/services/profiles_provider.dart index 67f0fc1..a7bc0f2 100644 --- a/lib/services/profiles_provider.dart +++ b/lib/services/profiles_provider.dart @@ -21,6 +21,16 @@ class Profiles extends ChangeNotifier { return _profiles; } + Profile? get own { + final list = + profiles.where((element) => element.id == DbHelper.currentUser!.id); + if (list.isNotEmpty) { + return list.first; + } else { + return null; + } + } + _updateProfile(List> data) { final List profiles = data.map((e) => Profile.fromMap(e)).toList();