remove futurebuilder from match view and refactor
This commit is contained in:
@@ -24,16 +24,16 @@ class MatchView extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MatchViewState extends State<MatchView> {
|
||||
late Future<List<Match>> _gameListFuture;
|
||||
late final AppDatabase db;
|
||||
bool isLoading = true;
|
||||
|
||||
late final List<Match> skeletonData = List.filled(
|
||||
List<Match> matches = List.filled(
|
||||
4,
|
||||
Match(
|
||||
name: 'Skeleton Gamename',
|
||||
group: Group(
|
||||
name: 'Groupname',
|
||||
members: List.generate(5, (index) => Player(name: 'Player')),
|
||||
members: List.filled(5, Player(name: 'Player')),
|
||||
),
|
||||
winner: Player(name: 'Player'),
|
||||
players: [Player(name: 'Player')],
|
||||
@@ -44,10 +44,7 @@ class _MatchViewState extends State<MatchView> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
db = Provider.of<AppDatabase>(context, listen: false);
|
||||
_gameListFuture = Future.wait([
|
||||
db.matchDao.getAllMatches(),
|
||||
Future.delayed(minimumSkeletonDuration),
|
||||
]).then((results) => results[0] as List<Match>);
|
||||
loadGames();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -57,39 +54,17 @@ class _MatchViewState extends State<MatchView> {
|
||||
body: Stack(
|
||||
alignment: Alignment.center,
|
||||
children: [
|
||||
FutureBuilder<List<Match>>(
|
||||
future: _gameListFuture,
|
||||
builder:
|
||||
(BuildContext context, AsyncSnapshot<List<Match>> snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
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(
|
||||
AppSkeleton(
|
||||
enabled: isLoading,
|
||||
child: Visibility(
|
||||
visible: matches.isNotEmpty,
|
||||
replacement: const Center(
|
||||
child: TopCenteredMessage(
|
||||
icon: Icons.report,
|
||||
title: 'Info',
|
||||
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(
|
||||
padding: const EdgeInsets.only(bottom: 85),
|
||||
itemCount: matches.length + 1,
|
||||
@@ -107,7 +82,7 @@ class _MatchViewState extends State<MatchView> {
|
||||
fullscreenDialog: true,
|
||||
builder: (context) => GameResultView(
|
||||
match: matches[index],
|
||||
onWinnerChanged: refreshGameList,
|
||||
onWinnerChanged: loadGames,
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -116,8 +91,7 @@ class _MatchViewState extends State<MatchView> {
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
Positioned(
|
||||
bottom: MediaQuery.paddingOf(context).bottom,
|
||||
@@ -129,7 +103,7 @@ class _MatchViewState extends State<MatchView> {
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
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(() {
|
||||
_gameListFuture = db.matchDao.getAllMatches();
|
||||
isLoading = false;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user