2 Commits

Author SHA1 Message Date
2616f7c113 Refactor FutureBuilder logic in HomeView to handle empty game lists and improve code formatting
Some checks failed
Pull Request Pipeline / test (pull_request) Successful in 2m11s
Pull Request Pipeline / lint (pull_request) Failing after 2m12s
2025-11-23 18:08:03 +01:00
963edaf1d1 Update game status text and player count label in HomeView 2025-11-23 17:51:55 +01:00

View File

@@ -138,29 +138,40 @@ class _HomeViewState extends State<HomeView> {
padding: const EdgeInsets.symmetric(horizontal: 40.0), padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: FutureBuilder( child: FutureBuilder(
future: _recentGamesFuture, future: _recentGamesFuture,
builder: (context, snapshot) { builder:
(
BuildContext context,
AsyncSnapshot<List<Game>> snapshot,
) {
if (snapshot.hasError) { if (snapshot.hasError) {
return const Center( return const Center(
heightFactor: 4, heightFactor: 4,
child: Text('Error while loading recent games.'), child: Text(
'Error while loading recent games.',
),
); );
} }
if (snapshot.connectionState == if (snapshot.connectionState ==
ConnectionState.done && ConnectionState.done &&
(!snapshot.hasData || snapshot.data!.isEmpty)) { (!snapshot.hasData ||
snapshot.data!.isEmpty)) {
return const Center( return const Center(
heightFactor: 4, heightFactor: 4,
child: Text('No recent games available.'), child: Text('No recent games available.'),
); );
} }
final List<Game> games = final List<Game> games =
(isLoading ? skeletonData : (snapshot.data ?? []) (isLoading
? skeletonData
: (snapshot.data ?? [])
..sort( ..sort(
(a, b) => (a, b) => b.createdAt.compareTo(
b.createdAt.compareTo(a.createdAt), a.createdAt,
),
)) ))
.take(2) .take(2)
.toList(); .toList();
if (games.length > 0)
return Column( return Column(
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
@@ -171,32 +182,41 @@ class _HomeViewState extends State<HomeView> {
ruleset: 'Ruleset', ruleset: 'Ruleset',
players: _getPlayerText(games[0]), players: _getPlayerText(games[0]),
winner: games[0].winner == null winner: games[0].winner == null
? 'No winner set.' ? 'Game in progress...'
: games[0].winner!.name, : games[0].winner!.name,
), ),
const Padding( const Padding(
padding: EdgeInsets.symmetric(vertical: 8.0), padding: EdgeInsets.symmetric(
vertical: 8.0,
),
child: Divider(), child: Divider(),
), ),
if (games.length >= 2) ...[ if (games.length > 1) ...[
GameTile( GameTile(
gameTitle: games[1].name, gameTitle: games[1].name,
gameType: 'Winner', gameType: 'Winner',
ruleset: 'Ruleset', ruleset: 'Ruleset',
players: _getPlayerText(games[1]), players: _getPlayerText(games[1]),
winner: games[1].winner == null winner: games[1].winner == null
? 'No winner set.' ? 'Game in progress...'
: games[1].winner!.name, : games[1].winner!.name,
), ),
const SizedBox(height: 8), const SizedBox(height: 8),
] else ...[ ] else ...[
const Center( const Center(
heightFactor: 4, heightFactor: 4,
child: Text('No second game available.'), child: Text(
'No second game available.',
),
), ),
], ],
], ],
); );
else
return const Center(
heightFactor: 4,
child: Text('No recent games available.'),
);
}, },
), ),
), ),
@@ -262,7 +282,7 @@ class _HomeViewState extends State<HomeView> {
String _getPlayerText(Game game) { String _getPlayerText(Game game) {
if (game.group == null) { if (game.group == null) {
final playerCount = game.players?.length ?? 0; final playerCount = game.players?.length ?? 0;
return '$playerCount Player(s)'; return '$playerCount Players';
} }
if (game.players == null || game.players!.isEmpty) { if (game.players == null || game.players!.isEmpty) {
return game.group!.name; return game.group!.name;