Added common.dart
This commit is contained in:
45
lib/core/common.dart
Normal file
45
lib/core/common.dart
Normal file
@@ -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()}';
|
||||||
|
}
|
||||||
@@ -1,6 +1,3 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:tallee/l10n/generated/app_localizations.dart';
|
|
||||||
|
|
||||||
/// Button types used for styling the [CustomWidthButton]
|
/// Button types used for styling the [CustomWidthButton]
|
||||||
/// - [ButtonType.primary]: Primary button style.
|
/// - [ButtonType.primary]: Primary button style.
|
||||||
/// - [ButtonType.secondary]: Secondary 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.singleWinner]: The match is won by a single player.
|
||||||
/// - [Ruleset.singleLoser]: The match has a single loser.
|
/// - [Ruleset.singleLoser]: The match has a single loser.
|
||||||
/// - [Ruleset.multipleWinners]: Multiple players can be winners.
|
/// - [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
|
/// Different colors available for games
|
||||||
/// - [GameColor.red]: Red color
|
/// - [GameColor.red]: Red color
|
||||||
@@ -47,20 +50,3 @@ enum Ruleset { highestScore, lowestScore, singleWinner, singleLoser, multipleWin
|
|||||||
/// - [GameColor.pink]: Pink color
|
/// - [GameColor.pink]: Pink color
|
||||||
/// - [GameColor.teal]: Teal color
|
/// - [GameColor.teal]: Teal color
|
||||||
enum GameColor { red, blue, green, yellow, purple, orange, pink, teal }
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:tallee/core/common.dart';
|
||||||
import 'package:tallee/core/custom_theme.dart';
|
import 'package:tallee/core/custom_theme.dart';
|
||||||
import 'package:tallee/core/enums.dart';
|
import 'package:tallee/core/enums.dart';
|
||||||
import 'package:tallee/l10n/generated/app_localizations.dart';
|
import 'package:tallee/l10n/generated/app_localizations.dart';
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:tallee/core/adaptive_page_route.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/core/custom_theme.dart';
|
||||||
import 'package:tallee/data/db/database.dart';
|
import 'package:tallee/data/db/database.dart';
|
||||||
import 'package:tallee/data/dto/match.dart';
|
import 'package:tallee/data/dto/match.dart';
|
||||||
@@ -146,7 +147,7 @@ class _MatchDetailViewState extends State<MatchDetailView> {
|
|||||||
const SizedBox(width: 8),
|
const SizedBox(width: 8),
|
||||||
Text(
|
Text(
|
||||||
// TODO: Update after DB changes
|
// TODO: Update after DB changes
|
||||||
'${match.group!.name}${getExtraPlayerCount()}',
|
'${match.group!.name}${getExtraPlayerCount(match)}',
|
||||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -262,26 +263,6 @@ class _MatchDetailViewState extends State<MatchDetailView> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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,
|
/// Callback for when the match is updated in the edit view,
|
||||||
/// updates the match in this view
|
/// updates the match in this view
|
||||||
onMatchUpdated(editedMatch) {
|
onMatchUpdated(editedMatch) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'dart:core' hide Match;
|
|||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
|
import 'package:tallee/core/common.dart';
|
||||||
import 'package:tallee/core/custom_theme.dart';
|
import 'package:tallee/core/custom_theme.dart';
|
||||||
import 'package:tallee/data/dto/match.dart';
|
import 'package:tallee/data/dto/match.dart';
|
||||||
import 'package:tallee/data/dto/player.dart';
|
import 'package:tallee/data/dto/player.dart';
|
||||||
@@ -51,6 +52,7 @@ class _MatchTileState extends State<MatchTile> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final match = widget.match;
|
||||||
final group = widget.match.group;
|
final group = widget.match.group;
|
||||||
final winner = widget.match.winner;
|
final winner = widget.match.winner;
|
||||||
final loc = AppLocalizations.of(context);
|
final loc = AppLocalizations.of(context);
|
||||||
@@ -70,7 +72,7 @@ class _MatchTileState extends State<MatchTile> {
|
|||||||
children: [
|
children: [
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
widget.match.name,
|
match.name,
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
fontSize: 18,
|
fontSize: 18,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
@@ -79,7 +81,7 @@ class _MatchTileState extends State<MatchTile> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
Text(
|
Text(
|
||||||
_formatDate(widget.match.createdAt, context),
|
_formatDate(match.createdAt, context),
|
||||||
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@@ -94,7 +96,7 @@ class _MatchTileState extends State<MatchTile> {
|
|||||||
const SizedBox(width: 6),
|
const SizedBox(width: 6),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
'${group.name}${getExtraPlayerCount()}',
|
'${match.group!.name}${getExtraPlayerCount(match)}',
|
||||||
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
),
|
),
|
||||||
@@ -109,7 +111,7 @@ class _MatchTileState extends State<MatchTile> {
|
|||||||
const SizedBox(width: 6),
|
const SizedBox(width: 6),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
'${widget.match.players.length} ${loc.players}',
|
'${match.players.length} ${loc.players}',
|
||||||
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
),
|
),
|
||||||
@@ -236,24 +238,4 @@ class _MatchTileState extends State<MatchTile> {
|
|||||||
return '${loc.created_on} ${DateFormat.yMMMd(Localizations.localeOf(context).toString()).format(dateTime)}';
|
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()}';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user