fix: sorted games
This commit is contained in:
@@ -51,6 +51,9 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
/// Games filtered according to the current search query
|
/// Games filtered according to the current search query
|
||||||
late List<Game> filteredGames;
|
late List<Game> filteredGames;
|
||||||
|
|
||||||
|
List<Game> get games =>
|
||||||
|
widget.games..sort((a, b) => a.name.compareTo(b.name));
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
db = Provider.of<AppDatabase>(context, listen: false);
|
db = Provider.of<AppDatabase>(context, listen: false);
|
||||||
@@ -59,7 +62,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
selectedGameId = widget.initialGameId;
|
selectedGameId = widget.initialGameId;
|
||||||
|
|
||||||
// Start with all games visible
|
// Start with all games visible
|
||||||
filteredGames = List<Game>.from(widget.games);
|
filteredGames = List<Game>.from(games);
|
||||||
|
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
@@ -77,9 +80,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
Navigator.of(context).pop(
|
Navigator.of(context).pop(
|
||||||
selectedGameId == ''
|
selectedGameId == ''
|
||||||
? null
|
? null
|
||||||
: widget.games.firstWhere(
|
: games.firstWhere((game) => game.id == selectedGameId),
|
||||||
(game) => game.id == selectedGameId,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@@ -99,7 +100,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
);
|
);
|
||||||
if (result != null && result.game != null) {
|
if (result != null && result.game != null) {
|
||||||
setState(() {
|
setState(() {
|
||||||
widget.games.insert(0, result.game);
|
games.insert(0, result.game);
|
||||||
});
|
});
|
||||||
_refreshFromSource();
|
_refreshFromSource();
|
||||||
}
|
}
|
||||||
@@ -139,7 +140,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
child: Visibility(
|
child: Visibility(
|
||||||
visible: filteredGames.isNotEmpty,
|
visible: filteredGames.isNotEmpty,
|
||||||
replacement: Visibility(
|
replacement: Visibility(
|
||||||
visible: widget.games.isNotEmpty,
|
visible: games.isNotEmpty,
|
||||||
replacement: TopCenteredMessage(
|
replacement: TopCenteredMessage(
|
||||||
icon: Icons.info,
|
icon: Icons.info,
|
||||||
title: loc.info,
|
title: loc.info,
|
||||||
@@ -190,7 +191,7 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
);
|
);
|
||||||
if (result != null && result.game != null) {
|
if (result != null && result.game != null) {
|
||||||
// Find the index in the original list to mutate
|
// Find the index in the original list to mutate
|
||||||
final originalIndex = widget.games.indexWhere(
|
final originalIndex = games.indexWhere(
|
||||||
(g) => g.id == game.id,
|
(g) => g.id == game.id,
|
||||||
);
|
);
|
||||||
if (originalIndex == -1) {
|
if (originalIndex == -1) {
|
||||||
@@ -202,12 +203,12 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
if (selectedGameId == game.id) {
|
if (selectedGameId == game.id) {
|
||||||
selectedGameId = '';
|
selectedGameId = '';
|
||||||
}
|
}
|
||||||
widget.games.removeAt(originalIndex);
|
games.removeAt(originalIndex);
|
||||||
widget.onGamesUpdated?.call();
|
widget.onGamesUpdated?.call();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
setState(() {
|
setState(() {
|
||||||
widget.games[originalIndex] = result.game;
|
games[originalIndex] = result.game;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
_refreshFromSource();
|
_refreshFromSource();
|
||||||
@@ -229,13 +230,13 @@ class _ChooseGameViewState extends State<ChooseGameView> {
|
|||||||
final q = query.toLowerCase().trim();
|
final q = query.toLowerCase().trim();
|
||||||
if (q.isEmpty) {
|
if (q.isEmpty) {
|
||||||
setState(() {
|
setState(() {
|
||||||
filteredGames = List<Game>.from(widget.games);
|
filteredGames = List<Game>.from(games);
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
setState(() {
|
setState(() {
|
||||||
filteredGames = widget.games.where((game) {
|
filteredGames = games.where((game) {
|
||||||
final name = game.name.toLowerCase();
|
final name = game.name.toLowerCase();
|
||||||
final description = game.description.toLowerCase();
|
final description = game.description.toLowerCase();
|
||||||
return name.contains(q) || description.contains(q);
|
return name.contains(q) || description.contains(q);
|
||||||
|
|||||||
Reference in New Issue
Block a user