From a8ade294b574e5348a9a3b31324daf9d3cfa3664 Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Fri, 6 Mar 2026 21:53:46 +0100 Subject: [PATCH] Added common.dart --- lib/core/common.dart | 45 +++++++++++++++++++ lib/core/enums.dart | 28 +++--------- .../create_match/choose_game_view.dart | 1 + .../match_view/match_detail_view.dart | 23 +--------- .../widgets/tiles/match_tile.dart | 30 +++---------- 5 files changed, 61 insertions(+), 66 deletions(-) create mode 100644 lib/core/common.dart diff --git a/lib/core/common.dart b/lib/core/common.dart new file mode 100644 index 0000000..a27daf0 --- /dev/null +++ b/lib/core/common.dart @@ -0,0 +1,45 @@ +import 'package:flutter/cupertino.dart'; +import 'package:tallee/core/enums.dart'; +import 'package:tallee/data/dto/match.dart'; +import 'package:tallee/l10n/generated/app_localizations.dart'; + +/// Translates a [Ruleset] enum value to its corresponding localized string. +String translateRulesetToString(Ruleset ruleset, BuildContext context) { + final loc = AppLocalizations.of(context); + switch (ruleset) { + case Ruleset.highestScore: + return loc.highest_score; + case Ruleset.lowestScore: + return loc.lowest_score; + case Ruleset.singleWinner: + return loc.single_winner; + case Ruleset.singleLoser: + return loc.single_loser; + case Ruleset.multipleWinners: + return loc.multiple_winners; + } +} + +/// Counts how many players in the match are not part of the group +/// Returns the count as a string, or an empty string if there is no group +String getExtraPlayerCount(Match match) { + int count = 0; + + if (match.group == null) { + return ''; + } + + final groupMembers = match.group!.members; + final players = match.players; + + for (var player in players) { + if (!groupMembers.any((member) => member.id == player.id)) { + count++; + } + } + + if (count == 0) { + return ''; + } + return ' + ${count.toString()}'; +} diff --git a/lib/core/enums.dart b/lib/core/enums.dart index d3e0610..6b33124 100644 --- a/lib/core/enums.dart +++ b/lib/core/enums.dart @@ -1,6 +1,3 @@ -import 'package:flutter/material.dart'; -import 'package:tallee/l10n/generated/app_localizations.dart'; - /// Button types used for styling the [CustomWidthButton] /// - [ButtonType.primary]: Primary button style. /// - [ButtonType.secondary]: Secondary button style. @@ -35,7 +32,13 @@ enum ExportResult { success, canceled, unknownException } /// - [Ruleset.singleWinner]: The match is won by a single player. /// - [Ruleset.singleLoser]: The match has a single loser. /// - [Ruleset.multipleWinners]: Multiple players can be winners. -enum Ruleset { highestScore, lowestScore, singleWinner, singleLoser, multipleWinners } +enum Ruleset { + highestScore, + lowestScore, + singleWinner, + singleLoser, + multipleWinners, +} /// Different colors available for games /// - [GameColor.red]: Red color @@ -47,20 +50,3 @@ enum Ruleset { highestScore, lowestScore, singleWinner, singleLoser, multipleWin /// - [GameColor.pink]: Pink color /// - [GameColor.teal]: Teal color enum GameColor { red, blue, green, yellow, purple, orange, pink, teal } - -/// Translates a [Ruleset] enum value to its corresponding localized string. -String translateRulesetToString(Ruleset ruleset, BuildContext context) { - final loc = AppLocalizations.of(context); - switch (ruleset) { - case Ruleset.highestScore: - return loc.highest_score; - case Ruleset.lowestScore: - return loc.lowest_score; - case Ruleset.singleWinner: - return loc.single_winner; - case Ruleset.singleLoser: - return loc.single_loser; - case Ruleset.multipleWinners: - return loc.multiple_winners; - } -} diff --git a/lib/presentation/views/main_menu/match_view/create_match/choose_game_view.dart b/lib/presentation/views/main_menu/match_view/create_match/choose_game_view.dart index 447b9c5..d4d7f4d 100644 --- a/lib/presentation/views/main_menu/match_view/create_match/choose_game_view.dart +++ b/lib/presentation/views/main_menu/match_view/create_match/choose_game_view.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:tallee/core/common.dart'; import 'package:tallee/core/custom_theme.dart'; import 'package:tallee/core/enums.dart'; import 'package:tallee/l10n/generated/app_localizations.dart'; diff --git a/lib/presentation/views/main_menu/match_view/match_detail_view.dart b/lib/presentation/views/main_menu/match_view/match_detail_view.dart index 4ce66da..d5791a5 100644 --- a/lib/presentation/views/main_menu/match_view/match_detail_view.dart +++ b/lib/presentation/views/main_menu/match_view/match_detail_view.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import 'package:tallee/core/adaptive_page_route.dart'; +import 'package:tallee/core/common.dart'; import 'package:tallee/core/custom_theme.dart'; import 'package:tallee/data/db/database.dart'; import 'package:tallee/data/dto/match.dart'; @@ -146,7 +147,7 @@ class _MatchDetailViewState extends State { const SizedBox(width: 8), Text( // TODO: Update after DB changes - '${match.group!.name}${getExtraPlayerCount()}', + '${match.group!.name}${getExtraPlayerCount(match)}', style: const TextStyle(fontWeight: FontWeight.bold), ), ], @@ -262,26 +263,6 @@ class _MatchDetailViewState extends State { ); } - /// Counts how many players in the match are not part of the group - /// Returns the count as a string, or an empty string if there is no group - String getExtraPlayerCount() { - int count = 0; - - final groupMembers = match.group!.members; - final players = match.players; - - for (var player in players) { - if (!groupMembers.any((member) => member.id == player.id)) { - count++; - } - } - - if (count == 0) { - return ''; - } - return ' + ${count.toString()}'; - } - /// Callback for when the match is updated in the edit view, /// updates the match in this view onMatchUpdated(editedMatch) { diff --git a/lib/presentation/widgets/tiles/match_tile.dart b/lib/presentation/widgets/tiles/match_tile.dart index 6ac382a..2f990cb 100644 --- a/lib/presentation/widgets/tiles/match_tile.dart +++ b/lib/presentation/widgets/tiles/match_tile.dart @@ -2,6 +2,7 @@ import 'dart:core' hide Match; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; +import 'package:tallee/core/common.dart'; import 'package:tallee/core/custom_theme.dart'; import 'package:tallee/data/dto/match.dart'; import 'package:tallee/data/dto/player.dart'; @@ -51,6 +52,7 @@ class _MatchTileState extends State { @override Widget build(BuildContext context) { + final match = widget.match; final group = widget.match.group; final winner = widget.match.winner; final loc = AppLocalizations.of(context); @@ -70,7 +72,7 @@ class _MatchTileState extends State { children: [ Expanded( child: Text( - widget.match.name, + match.name, style: const TextStyle( fontSize: 18, fontWeight: FontWeight.bold, @@ -79,7 +81,7 @@ class _MatchTileState extends State { ), ), Text( - _formatDate(widget.match.createdAt, context), + _formatDate(match.createdAt, context), style: const TextStyle(fontSize: 12, color: Colors.grey), ), ], @@ -94,7 +96,7 @@ class _MatchTileState extends State { const SizedBox(width: 6), Expanded( child: Text( - '${group.name}${getExtraPlayerCount()}', + '${match.group!.name}${getExtraPlayerCount(match)}', style: const TextStyle(fontSize: 14, color: Colors.grey), overflow: TextOverflow.ellipsis, ), @@ -109,7 +111,7 @@ class _MatchTileState extends State { const SizedBox(width: 6), Expanded( child: Text( - '${widget.match.players.length} ${loc.players}', + '${match.players.length} ${loc.players}', style: const TextStyle(fontSize: 14, color: Colors.grey), overflow: TextOverflow.ellipsis, ), @@ -236,24 +238,4 @@ class _MatchTileState extends State { return '${loc.created_on} ${DateFormat.yMMMd(Localizations.localeOf(context).toString()).format(dateTime)}'; } } - - /// Counts how many players in the match are not part of the group - /// Returns the count as a string, or an empty string if there is no group - String getExtraPlayerCount() { - int count = 0; - - final groupMembers = widget.match.group!.members; - final players = widget.match.players; - - for (var player in players) { - if (!groupMembers.any((member) => member.id == player.id)) { - count++; - } - } - - if (count == 0) { - return ''; - } - return ' + ${count.toString()}'; - } }