diff --git a/lib/models/game.dart b/lib/models/game.dart index 1ce687c..85b5a74 100644 --- a/lib/models/game.dart +++ b/lib/models/game.dart @@ -1,10 +1,12 @@ +import 'reward.dart'; + class Game { final int id; final String name; - final List> rewards; + final List rewards; Game.fromMap(Map map, Iterable> rewards) : id = map['id'], name = map['name'], - rewards = rewards.toList(); + rewards = rewards.map((e) => Reward.fromMap(e)).toList(); } diff --git a/lib/models/reward.dart b/lib/models/reward.dart new file mode 100644 index 0000000..45497b2 --- /dev/null +++ b/lib/models/reward.dart @@ -0,0 +1,8 @@ +class Reward { + final int placement; + final int reward; + + Reward.fromMap(Map map) + : placement = map['placement'], + reward = map['reward']; +} diff --git a/lib/pages/games_page.dart b/lib/pages/games_page.dart index 17874da..b24d901 100644 --- a/lib/pages/games_page.dart +++ b/lib/pages/games_page.dart @@ -33,47 +33,6 @@ class _GamesPageState extends State { ); } - List _getWidgets( - List> games, BuildContext context) { - final width = MediaQuery.of(context).size.width * 0.8; - List widgets = []; - for (final game in games) { - widgets.add( - Expanded( - child: SizedBox( - width: width, - child: Padding( - padding: const EdgeInsets.symmetric(vertical: 10), - child: _getGamesButton(context, game)), - ), - ), - ); - } - return widgets; - } - - Text _getText(String text, BuildContext context) { - return Text( - text, - style: Theme.of(context).textTheme.headlineSmall, - ); - } - - ElevatedButton _getGamesButton( - BuildContext context, Map game) { - return ElevatedButton( - style: ElevatedButton.styleFrom(shape: const BeveledRectangleBorder()), - 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']}'); - } - List _getGames() { if (games.isEmpty) { return [const CircularProgressIndicator()]; @@ -89,7 +48,7 @@ class _GamesPageState extends State { builder: (context) => GamesPopup( withPlacements: game.rewards.length > 1, game: game, - onSubmitted: (_) {}, + onSubmitted: (placement) => _winConfirmed(placement, game), ), ); }, @@ -99,4 +58,26 @@ class _GamesPageState extends State { return list; } } + + _winConfirmed(int placement, Game game) { + DbHelper.pushGameWin( + userProfile!.points, + game.rewards + .singleWhere((element) => element.placement == placement) + .reward); + + String feedText = ''; + if (placement == 1) { + feedText = '${userProfile!.username} just won at ${game.name}'; + } else if (placement == 2) { + feedText = '${userProfile!.username} just got 2nd place in ${game.name}'; + } else if (placement == 3) { + feedText = '${userProfile!.username} just got 3rd place in ${game.name}'; + } else { + feedText = + '${userProfile!.username} just got ${placement}th place in ${game.name}'; + } + + DbHelper.pushFeedEntry(feedText); + } } diff --git a/lib/widgets/games_popup.dart b/lib/widgets/games_popup.dart index 0c9d0df..138d57d 100644 --- a/lib/widgets/games_popup.dart +++ b/lib/widgets/games_popup.dart @@ -4,7 +4,7 @@ import '../models/game.dart'; typedef IntCallback = Function(int); -class GamesPopup extends StatelessWidget { +class GamesPopup extends StatefulWidget { const GamesPopup({ super.key, this.withPlacements = false, @@ -15,30 +15,79 @@ class GamesPopup extends StatelessWidget { final IntCallback onSubmitted; final Game game; + @override + State createState() => _GamesPopupState(); +} + +class _GamesPopupState extends State { + int selectedPlacement = 0; + + @override + void initState() { + super.initState(); + if (!widget.withPlacements) { + selectedPlacement = 1; + } + } + @override Widget build(BuildContext context) { - return SizedBox( - height: 400, - width: 400, - child: AlertDialog( - title: _getTitle(withPlacements), - content: _getContent(withPlacements), + return AlertDialog( + title: _getTitle(widget.withPlacements), + content: SizedBox( + width: 400, + child: _getContent(widget.withPlacements), ), + actions: [ + TextButton( + onPressed: () => Navigator.of(context).pop(), + child: const Text('Cancel'), + ), + TextButton( + onPressed: _onConfirmPressed, + child: const Text('Confirm'), + ), + ], ); } Widget _getTitle(bool withPlacements) { - if (withPlacements) { - return const Text('Which place did you achieve?'); - } - return Text('Confirm you won at ${game.name}'); + return Text('Congratulations,\nyou won at ${widget.game.name}!'); } Widget _getContent(bool withPlacements) { if (withPlacements) { - return const Text(''); - } else { - return const Text(''); + return Column( + mainAxisSize: MainAxisSize.min, + children: [ + const Text('Which placement did you achieve?'), + const Padding(padding: EdgeInsets.symmetric(vertical: 10)), + DropdownMenu( + dropdownMenuEntries: widget.game.rewards + .map( + (e) => DropdownMenuEntry( + value: e.placement, + label: e.placement.toString(), + ), + ) + .toList(), + onSelected: (value) => setState(() { + selectedPlacement = value ?? 0; + }), + enableSearch: true, + enableFilter: false, + ), + ], + ); } + return const Text('Do you want to confirm your win?'); + } + + void _onConfirmPressed() { + if (selectedPlacement == 0) { + return; + } + widget.onSubmitted(selectedPlacement); + Navigator.of(context).pop(); } }