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 f05114a99e - Show all commits

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,
builder: (context, snapshot) {
final int count = (snapshot.hasData)
? snapshot.data!
: 0;
return QuickInfoTile(
width: constraints.maxWidth * 0.45, width: constraints.maxWidth * 0.45,
height: constraints.maxHeight * 0.15, height: constraints.maxHeight * 0.15,
title: 'Matches', title: 'Matches',
icon: Icons.groups_rounded, icon: Icons.groups_rounded,
value: count, value: matchCount,
);
},
), ),
SizedBox(width: constraints.maxWidth * 0.05), SizedBox(width: constraints.maxWidth * 0.05),
FutureBuilder<int>( QuickInfoTile(
future: _groupCountFuture,
builder: (context, snapshot) {
final int count =
(snapshot.connectionState == ConnectionState.done &&
snapshot.hasData)
? snapshot.data!
: 0;
return QuickInfoTile(
width: constraints.maxWidth * 0.45, width: constraints.maxWidth * 0.45,
height: constraints.maxHeight * 0.15, height: constraints.maxHeight * 0.15,
title: 'Groups', title: 'Groups',
icon: Icons.groups_rounded, icon: Icons.groups_rounded,
value: count, value: groupCount,
);
},
), ),
], ],
), ),
@@ -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.'),
sneeex marked this conversation as resolved Outdated

Punkt entfernen

Punkt entfernen
AsyncSnapshot<List<Match>> snapshot,
) {
if (snapshot.hasError) {
return const Center(
heightFactor: 4,
child: Text(
'Error while loading recent matches.',
), ),
); child: Column(
}
final List<Match> matches =
(isLoading
? skeletonData
: (snapshot.data ?? [])
..sort(
(a, b) => b.createdAt.compareTo(
a.createdAt,
),
))
.take(2)
.toList();
if (matches.isNotEmpty) {
return Column(
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
MatchTile( MatchTile(
matchTitle: matches[0].name, matchTitle: recentMatches[0].name,
game: 'Winner', game: 'Winner',
ruleset: 'Ruleset', ruleset: 'Ruleset',
players: _getPlayerText(matches[0]), players: _getPlayerText(recentMatches[0]),
winner: matches[0].winner == null winner: recentMatches[0].winner == null
? 'Match in progress...' ? 'Match in progress...'
: matches[0].winner!.name, : recentMatches[0].winner!.name,
), ),
const Padding( const Padding(
padding: EdgeInsets.symmetric( padding: EdgeInsets.symmetric(vertical: 8.0),
vertical: 8.0,
),
child: Divider(), child: Divider(),
), ),
if (matches.length > 1) ...[ if (recentMatches.length > 1) ...[
MatchTile( MatchTile(
matchTitle: matches[1].name, matchTitle: recentMatches[1].name,
game: 'Winner', game: 'Winner',
ruleset: 'Ruleset', ruleset: 'Ruleset',
players: _getPlayerText(matches[1]), players: _getPlayerText(recentMatches[1]),
winner: matches[1].winner == null winner: recentMatches[1].winner == null
? 'Game in progress...' ? 'Game in progress...'
: matches[1].winner!.name, : recentMatches[1].winner!.name,
), ),
const SizedBox(height: 8), const SizedBox(height: 8),
] else ...[ ] else ...[
const Center( const Center(
heightFactor: 4, heightFactor: 4,
child: Text( child: Text('No second game available.'),
sneeex marked this conversation as resolved
Review

Punkt entfernen

Punkt entfernen
'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,