removed futurebuilder logic in groups_view

This commit is contained in:
2025-12-21 15:50:39 +01:00
parent d96494f608
commit f05114a99e

View File

@@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:game_tracker/core/constants.dart';
import 'package:game_tracker/data/db/database.dart'; import 'package:game_tracker/data/db/database.dart';
import 'package:game_tracker/data/dto/group.dart'; import 'package:game_tracker/data/dto/group.dart';
import 'package:game_tracker/data/dto/match.dart'; import 'package:game_tracker/data/dto/match.dart';
@@ -18,12 +19,10 @@ class HomeView extends StatefulWidget {
} }
class _HomeViewState extends State<HomeView> { class _HomeViewState extends State<HomeView> {
late Future<int> _matchCountFuture;
late Future<int> _groupCountFuture;
late Future<List<Match>> _recentMatchesFuture;
bool isLoading = true; bool isLoading = true;
int matchCount = 0;
late final List<Match> skeletonData = List.filled( int groupCount = 0;
List<Match> recentMatches = List.filled(
2, 2,
Match( Match(
name: 'Skeleton Match', name: 'Skeleton Match',
@@ -39,19 +38,24 @@ class _HomeViewState extends State<HomeView> {
); );
@override @override
initState() { void initState() {
super.initState(); super.initState();
final db = Provider.of<AppDatabase>(context, listen: false); final db = Provider.of<AppDatabase>(context, listen: false);
_matchCountFuture = db.matchDao.getMatchCount();
_groupCountFuture = db.groupDao.getGroupCount();
_recentMatchesFuture = db.matchDao.getAllMatches();
Future.wait([ Future.wait([
_matchCountFuture, db.matchDao.getMatchCount(),
_groupCountFuture, db.groupDao.getGroupCount(),
_recentMatchesFuture, db.matchDao.getAllMatches(),
]).then((_) async { Future.delayed(minimumSkeletonDuration),
await Future.delayed(const Duration(milliseconds: 250)); ]).then((results) {
matchCount = results[0] as int;
groupCount = results[1] as int;
recentMatches = results[2] as List<Match>;
recentMatches =
(recentMatches..sort((a, b) => b.createdAt.compareTo(a.createdAt)))
.take(2)
.toList();
if (mounted) { if (mounted) {
setState(() { setState(() {
isLoading = false; isLoading = false;
@@ -73,38 +77,20 @@ class _HomeViewState extends State<HomeView> {
Row( Row(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
FutureBuilder<int>( QuickInfoTile(
future: _matchCountFuture, width: constraints.maxWidth * 0.45,
builder: (context, snapshot) { height: constraints.maxHeight * 0.15,
final int count = (snapshot.hasData) title: 'Matches',
? snapshot.data! icon: Icons.groups_rounded,
: 0; value: matchCount,
return QuickInfoTile(
width: constraints.maxWidth * 0.45,
height: constraints.maxHeight * 0.15,
title: 'Matches',
icon: Icons.groups_rounded,
value: count,
);
},
), ),
SizedBox(width: constraints.maxWidth * 0.05), SizedBox(width: constraints.maxWidth * 0.05),
FutureBuilder<int>( QuickInfoTile(
future: _groupCountFuture, width: constraints.maxWidth * 0.45,
builder: (context, snapshot) { height: constraints.maxHeight * 0.15,
final int count = title: 'Groups',
(snapshot.connectionState == ConnectionState.done && icon: Icons.groups_rounded,
snapshot.hasData) value: groupCount,
? snapshot.data!
: 0;
return QuickInfoTile(
width: constraints.maxWidth * 0.45,
height: constraints.maxHeight * 0.15,
title: 'Groups',
icon: Icons.groups_rounded,
value: count,
);
},
), ),
], ],
), ),
@@ -116,80 +102,48 @@ class _HomeViewState extends State<HomeView> {
icon: Icons.timer, icon: Icons.timer,
content: Padding( content: Padding(
padding: const EdgeInsets.symmetric(horizontal: 40.0), padding: const EdgeInsets.symmetric(horizontal: 40.0),
child: FutureBuilder( child: Visibility(
future: _recentMatchesFuture, visible: !isLoading,
builder: replacement: const Center(
( heightFactor: 12,
BuildContext context, child: Text('No recent games available.'),
AsyncSnapshot<List<Match>> snapshot, ),
) { child: Column(
if (snapshot.hasError) { mainAxisAlignment: MainAxisAlignment.start,
return const Center( crossAxisAlignment: CrossAxisAlignment.start,
heightFactor: 4, children: [
child: Text( MatchTile(
'Error while loading recent matches.', matchTitle: recentMatches[0].name,
), game: 'Winner',
); ruleset: 'Ruleset',
} players: _getPlayerText(recentMatches[0]),
final List<Match> matches = winner: recentMatches[0].winner == null
(isLoading ? 'Match in progress...'
? skeletonData : recentMatches[0].winner!.name,
: (snapshot.data ?? []) ),
..sort( const Padding(
(a, b) => b.createdAt.compareTo( padding: EdgeInsets.symmetric(vertical: 8.0),
a.createdAt, child: Divider(),
), ),
)) if (recentMatches.length > 1) ...[
.take(2) MatchTile(
.toList(); matchTitle: recentMatches[1].name,
if (matches.isNotEmpty) { game: 'Winner',
return Column( ruleset: 'Ruleset',
mainAxisAlignment: MainAxisAlignment.start, players: _getPlayerText(recentMatches[1]),
crossAxisAlignment: CrossAxisAlignment.start, winner: recentMatches[1].winner == null
children: [ ? 'Game in progress...'
MatchTile( : recentMatches[1].winner!.name,
matchTitle: matches[0].name, ),
game: 'Winner', const SizedBox(height: 8),
ruleset: 'Ruleset', ] else ...[
players: _getPlayerText(matches[0]), const Center(
winner: matches[0].winner == null heightFactor: 4,
? 'Match in progress...' child: Text('No second game available.'),
: matches[0].winner!.name, ),
), ],
const Padding( ],
padding: EdgeInsets.symmetric( ),
vertical: 8.0,
),
child: Divider(),
),
if (matches.length > 1) ...[
MatchTile(
matchTitle: matches[1].name,
game: 'Winner',
ruleset: 'Ruleset',
players: _getPlayerText(matches[1]),
winner: matches[1].winner == null
? 'Game in progress...'
: matches[1].winner!.name,
),
const SizedBox(height: 8),
] else ...[
const Center(
heightFactor: 4,
child: Text(
'No second game available.',
),
),
],
],
);
} else {
return const Center(
heightFactor: 12,
child: Text('No recent games available.'),
);
}
},
), ),
), ),
), ),
@@ -199,7 +153,6 @@ class _HomeViewState extends State<HomeView> {
title: 'Quick Create', title: 'Quick Create',
icon: Icons.add_box_rounded, icon: Icons.add_box_rounded,
content: Column( content: Column(
spacing: 8,
children: [ children: [
Row( Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly, mainAxisAlignment: MainAxisAlignment.spaceEvenly,