remove futurebuilder from match view and refactor
All checks were successful
Pull Request Pipeline / test (pull_request) Successful in 2m10s
Pull Request Pipeline / lint (pull_request) Successful in 2m10s

This commit is contained in:
2025-12-23 23:17:27 +01:00
parent a747d91c5d
commit 7732c6ceb9

View File

@@ -24,16 +24,16 @@ class MatchView extends StatefulWidget {
} }
class _MatchViewState extends State<MatchView> { class _MatchViewState extends State<MatchView> {
late Future<List<Match>> _gameListFuture;
late final AppDatabase db; late final AppDatabase db;
bool isLoading = true;
late final List<Match> skeletonData = List.filled( List<Match> matches = List.filled(
4, 4,
Match( Match(
name: 'Skeleton Gamename', name: 'Skeleton Gamename',
group: Group( group: Group(
name: 'Groupname', name: 'Groupname',
members: List.generate(5, (index) => Player(name: 'Player')), members: List.filled(5, Player(name: 'Player')),
), ),
winner: Player(name: 'Player'), winner: Player(name: 'Player'),
players: [Player(name: 'Player')], players: [Player(name: 'Player')],
@@ -44,10 +44,7 @@ class _MatchViewState extends State<MatchView> {
void initState() { void initState() {
super.initState(); super.initState();
db = Provider.of<AppDatabase>(context, listen: false); db = Provider.of<AppDatabase>(context, listen: false);
_gameListFuture = Future.wait([ loadGames();
db.matchDao.getAllMatches(),
Future.delayed(minimumSkeletonDuration),
]).then((results) => results[0] as List<Match>);
} }
@override @override
@@ -57,67 +54,44 @@ class _MatchViewState extends State<MatchView> {
body: Stack( body: Stack(
alignment: Alignment.center, alignment: Alignment.center,
children: [ children: [
FutureBuilder<List<Match>>( AppSkeleton(
future: _gameListFuture, enabled: isLoading,
builder: child: Visibility(
(BuildContext context, AsyncSnapshot<List<Match>> snapshot) { visible: matches.isNotEmpty,
if (snapshot.hasError) { replacement: const Center(
return const Center( child: TopCenteredMessage(
child: TopCenteredMessage( icon: Icons.report,
icon: Icons.report, title: 'Info',
title: 'Error', message: 'No games created yet',
message: 'Game data could not be loaded', ),
), ),
child: ListView.builder(
padding: const EdgeInsets.only(bottom: 85),
itemCount: matches.length + 1,
itemBuilder: (BuildContext context, int index) {
if (index == matches.length) {
return SizedBox(
height: MediaQuery.paddingOf(context).bottom - 80,
); );
} }
if (snapshot.connectionState == ConnectionState.done && return GameHistoryTile(
(!snapshot.hasData || snapshot.data!.isEmpty)) { onTap: () async {
return const Center( Navigator.push(
child: TopCenteredMessage( context,
icon: Icons.report, CupertinoPageRoute(
title: 'Info', fullscreenDialog: true,
message: 'No games created yet', builder: (context) => GameResultView(
), match: matches[index],
); onWinnerChanged: loadGames,
} ),
final bool isLoading = ),
snapshot.connectionState == ConnectionState.waiting; );
final List<Match> matches = },
(isLoading ? skeletonData : (snapshot.data ?? []) match: matches[index],
..sort(
(a, b) => b.createdAt.compareTo(a.createdAt),
))
.toList();
return AppSkeleton(
enabled: isLoading,
child: ListView.builder(
padding: const EdgeInsets.only(bottom: 85),
itemCount: matches.length + 1,
itemBuilder: (BuildContext context, int index) {
if (index == matches.length) {
return SizedBox(
height: MediaQuery.paddingOf(context).bottom - 80,
);
}
return GameHistoryTile(
onTap: () async {
Navigator.push(
context,
CupertinoPageRoute(
fullscreenDialog: true,
builder: (context) => GameResultView(
match: matches[index],
onWinnerChanged: refreshGameList,
),
),
);
},
match: matches[index],
);
},
),
); );
}, },
),
),
), ),
Positioned( Positioned(
bottom: MediaQuery.paddingOf(context).bottom, bottom: MediaQuery.paddingOf(context).bottom,
@@ -129,7 +103,7 @@ class _MatchViewState extends State<MatchView> {
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => builder: (context) =>
CreateMatchView(onWinnerChanged: refreshGameList), CreateMatchView(onWinnerChanged: loadGames),
), ),
); );
}, },
@@ -140,9 +114,17 @@ class _MatchViewState extends State<MatchView> {
); );
} }
void refreshGameList() { void loadGames() {
setState(() { Future.wait([
_gameListFuture = db.matchDao.getAllMatches(); db.matchDao.getAllMatches(),
Future.delayed(minimumSkeletonDuration),
]).then((results) {
final loadedMatches = results[0] as List<Match>;
matches = loadedMatches
..sort((a, b) => b.createdAt.compareTo(a.createdAt));
setState(() {
isLoading = false;
});
}); });
} }
} }