FutureBuilder-/Skeleton-/Futurelogik verbessern #90

Merged
sneeex merged 24 commits from enhancement/80-FutureBuilder-Logik-bei-Interaktion-mit-DB-verbessern into development 2025-12-29 17:21:49 +00:00
Showing only changes of commit 7732c6ceb9 - Show all commits

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,39 +54,17 @@ 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(
icon: Icons.report,
title: 'Error',
message: 'Game data could not be loaded',
),
);
}
if (snapshot.connectionState == ConnectionState.done &&
(!snapshot.hasData || snapshot.data!.isEmpty)) {
return const Center(
child: TopCenteredMessage( child: TopCenteredMessage(
icon: Icons.report, icon: Icons.report,
title: 'Info', title: 'Info',
message: 'No games created yet', message: 'No games created yet',
), ),
); ),
}
final bool isLoading =
snapshot.connectionState == ConnectionState.waiting;
final List<Match> matches =
(isLoading ? skeletonData : (snapshot.data ?? [])
..sort(
(a, b) => b.createdAt.compareTo(a.createdAt),
))
.toList();
return AppSkeleton(
enabled: isLoading,
child: ListView.builder( child: ListView.builder(
padding: const EdgeInsets.only(bottom: 85), padding: const EdgeInsets.only(bottom: 85),
itemCount: matches.length + 1, itemCount: matches.length + 1,
@@ -107,7 +82,7 @@ class _MatchViewState extends State<MatchView> {
fullscreenDialog: true, fullscreenDialog: true,
builder: (context) => GameResultView( builder: (context) => GameResultView(
match: matches[index], match: matches[index],
onWinnerChanged: refreshGameList, onWinnerChanged: loadGames,
), ),
), ),
); );
@@ -116,8 +91,7 @@ class _MatchViewState extends State<MatchView> {
); );
}, },
), ),
); ),
},
), ),
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() {
Future.wait([
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(() { setState(() {
_gameListFuture = db.matchDao.getAllMatches(); isLoading = false;
});
}); });
} }
} }