Fixed issues with match.players including group.members, added callback

This commit is contained in:
2026-03-06 16:49:56 +01:00
parent 8bd251ac7d
commit b68c570d47

View File

@@ -19,18 +19,18 @@ import 'package:tallee/presentation/widgets/tiles/text_icon_tile.dart';
class MatchDetailView extends StatefulWidget { class MatchDetailView extends StatefulWidget {
/// A view that displays the profile of a match /// A view that displays the profile of a match
/// - [match]: The match to display /// - [match]: The match to display
/// - [callback]: Callback to refresh the match list /// - [onMatchUpdate]: Callback to refresh the match list
const MatchDetailView({ const MatchDetailView({
super.key, super.key,
required this.match, required this.match,
required this.callback, required this.onMatchUpdate,
}); });
/// The match to display /// The match to display
final Match match; final Match match;
/// Callback to refresh the match list /// Callback to refresh the match list
final VoidCallback callback; final VoidCallback onMatchUpdate;
@override @override
State<MatchDetailView> createState() => _MatchDetailViewState(); State<MatchDetailView> createState() => _MatchDetailViewState();
@@ -41,15 +41,14 @@ class _MatchDetailViewState extends State<MatchDetailView> {
late Player? currentWinner; late Player? currentWinner;
/// All players who participated in the match late Match match;
late final List<Player> allPlayers;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
db = Provider.of<AppDatabase>(context, listen: false); db = Provider.of<AppDatabase>(context, listen: false);
allPlayers = _getAllPlayers();
currentWinner = widget.match.winner; currentWinner = widget.match.winner;
match = widget.match;
} }
@override @override
@@ -90,10 +89,10 @@ class _MatchDetailViewState extends State<MatchDetailView> {
), ),
).then((confirmed) async { ).then((confirmed) async {
if (confirmed! && context.mounted) { if (confirmed! && context.mounted) {
await db.matchDao.deleteMatch(matchId: widget.match.id); await db.matchDao.deleteMatch(matchId: match.id);
if (!context.mounted) return; if (!context.mounted) return;
Navigator.pop(context); Navigator.pop(context);
widget.callback.call(); widget.onMatchUpdate.call();
} }
}); });
}, },
@@ -121,7 +120,7 @@ class _MatchDetailViewState extends State<MatchDetailView> {
), ),
const SizedBox(height: 10), const SizedBox(height: 10),
Text( Text(
widget.match.name, match.name,
style: const TextStyle( style: const TextStyle(
fontSize: 28, fontSize: 28,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
@@ -131,7 +130,7 @@ class _MatchDetailViewState extends State<MatchDetailView> {
), ),
const SizedBox(height: 5), const SizedBox(height: 5),
Text( Text(
'${loc.created_on} ${DateFormat.yMMMd(Localizations.localeOf(context).toString()).format(widget.match.createdAt)}', '${loc.created_on} ${DateFormat.yMMMd(Localizations.localeOf(context).toString()).format(match.createdAt)}',
style: const TextStyle( style: const TextStyle(
fontSize: 12, fontSize: 12,
color: CustomTheme.textColor, color: CustomTheme.textColor,
@@ -139,7 +138,7 @@ class _MatchDetailViewState extends State<MatchDetailView> {
textAlign: TextAlign.center, textAlign: TextAlign.center,
), ),
const SizedBox(height: 10), const SizedBox(height: 10),
if (widget.match.group != null) ...[ if (match.group != null) ...[
Row( Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
@@ -147,7 +146,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
'${widget.match.group!.name} ${widget.match.players != null ? '+ ${widget.match.players!.length}' : ''}', '${match.group!.name}${getExtraPlayerCount()}',
style: const TextStyle(fontWeight: FontWeight.bold), style: const TextStyle(fontWeight: FontWeight.bold),
), ),
], ],
@@ -163,7 +162,7 @@ class _MatchDetailViewState extends State<MatchDetailView> {
crossAxisAlignment: WrapCrossAlignment.start, crossAxisAlignment: WrapCrossAlignment.start,
spacing: 12, spacing: 12,
runSpacing: 8, runSpacing: 8,
children: allPlayers.map((player) { children: match.players.map((player) {
return TextIconTile( return TextIconTile(
text: player.name, text: player.name,
iconEnabled: false, iconEnabled: false,
@@ -197,7 +196,7 @@ class _MatchDetailViewState extends State<MatchDetailView> {
style: TextStyle( style: TextStyle(
fontSize: 16, fontSize: 16,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
color: widget.match.winner != null color: match.winner != null
? CustomTheme.primaryColor ? CustomTheme.primaryColor
: CustomTheme.textColor, : CustomTheme.textColor,
), ),
@@ -227,8 +226,10 @@ class _MatchDetailViewState extends State<MatchDetailView> {
context, context,
adaptivePageRoute( adaptivePageRoute(
fullscreenDialog: true, fullscreenDialog: true,
builder: (context) => builder: (context) => CreateMatchView(
CreateMatchView(match: widget.match), match: match,
onMatchUpdated: onMatchUpdated,
),
), ),
), ),
), ),
@@ -242,9 +243,9 @@ class _MatchDetailViewState extends State<MatchDetailView> {
adaptivePageRoute( adaptivePageRoute(
fullscreenDialog: true, fullscreenDialog: true,
builder: (context) => MatchResultView( builder: (context) => MatchResultView(
match: widget.match, match: match,
onWinnerChanged: () { onWinnerChanged: () {
widget.callback.call(); widget.onMatchUpdate.call();
setState(() {}); setState(() {});
}, },
), ),
@@ -261,25 +262,32 @@ class _MatchDetailViewState extends State<MatchDetailView> {
); );
} }
/// Gets all players who participated in the match (from group and individual players) /// Counts how many players in the match are not part of the group
List<Player> _getAllPlayers() { /// Returns the count as a string, or an empty string if there is no group
final List<Player> players = []; String getExtraPlayerCount() {
int count = 0;
// Add group members if group exists final groupMembers = match.group!.members;
if (widget.match.group != null) { final players = match.players;
players.addAll(widget.match.group!.members);
}
// Add individual players for (var player in players) {
if (widget.match.players != null) { if (!groupMembers.any((member) => member.id == player.id)) {
for (var player in widget.match.players!) { count++;
// Avoid duplicates
if (!players.any((p) => p.id == player.id)) {
players.add(player);
}
} }
} }
return players; 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) {
setState(() {
match = editedMatch;
});
widget.onMatchUpdate.call();
} }
} }