Implement some sort of filtering

This commit is contained in:
2025-02-26 21:02:07 +01:00
parent 795060a05b
commit 336cb87a06
2 changed files with 113 additions and 44 deletions

View File

@@ -55,10 +55,17 @@ class Game {
throw Exception('No version found for $name'); throw Exception('No version found for $name');
} }
actualVersion = version; actualVersion = version;
try {
// Some sites use weird ass dogshit fucking formats
// We cannot really reliably parse every single one of them
// So - fuck it
lastUpdated = lastUpdated =
parseRfc822Date( parseRfc822Date(
response.headers['last-modified'] ?? '', response.headers['last-modified'] ?? '',
).toIso8601String(); ).toIso8601String();
} catch (e) {
lastUpdated = DateTime.now().toIso8601String();
}
} }
} }

View File

@@ -3,6 +3,7 @@ import 'package:gamer_updater/db.dart';
import 'package:gamer_updater/game.dart'; import 'package:gamer_updater/game.dart';
import 'package:gamer_updater/widgets/new_game_card.dart'; import 'package:gamer_updater/widgets/new_game_card.dart';
import 'package:gamer_updater/widgets/game_card.dart'; import 'package:gamer_updater/widgets/game_card.dart';
import 'dart:async';
void main() async { void main() async {
await DB.init(); await DB.init();
@@ -54,6 +55,9 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
late Map<String, Game> games = {}; late Map<String, Game> games = {};
String searchQuery = "";
final TextEditingController _searchController = TextEditingController();
Timer? _debounce;
@override @override
void initState() { void initState() {
@@ -61,13 +65,20 @@ class _MyHomePageState extends State<MyHomePage> {
_refreshGames(); _refreshGames();
} }
@override
void dispose() {
_searchController.dispose();
_debounce?.cancel();
super.dispose();
}
Future<void> _refreshGames() async { Future<void> _refreshGames() async {
final games = await GameRepository.getAll(); final games = await GameRepository.getAll();
games.forEach((key, game) { games.forEach((key, game) {
game.updateActualVersion().then((_) { game.updateActualVersion().then((_) {
GameRepository.upsert(game); GameRepository.upsert(game);
setState(() { setState(() {
games[game.name] = game; this.games[game.name] = game;
}); });
}); });
}); });
@@ -76,6 +87,24 @@ class _MyHomePageState extends State<MyHomePage> {
}); });
} }
List<Game> get filteredGames {
if (searchQuery.isEmpty) {
return games.values.toList();
}
return games.values.where((game) =>
game.name.toLowerCase().contains(searchQuery.toLowerCase())
).toList();
}
void _onSearchChanged(String value) {
if (_debounce?.isActive ?? false) _debounce!.cancel();
_debounce = Timer(const Duration(milliseconds: 100), () {
setState(() {
searchQuery = value;
});
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@@ -86,7 +115,35 @@ class _MyHomePageState extends State<MyHomePage> {
IconButton(icon: const Icon(Icons.refresh), onPressed: _refreshGames), IconButton(icon: const Icon(Icons.refresh), onPressed: _refreshGames),
], ],
), ),
body: RefreshIndicator( body: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: _searchController,
decoration: InputDecoration(
hintText: 'Search games...',
prefixIcon: const Icon(Icons.search),
suffixIcon: searchQuery.isNotEmpty
? IconButton(
icon: const Icon(Icons.clear),
onPressed: () {
_searchController.clear();
setState(() {
searchQuery = "";
});
},
)
: null,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
onChanged: _onSearchChanged,
),
),
Expanded(
child: RefreshIndicator(
onRefresh: _refreshGames, onRefresh: _refreshGames,
child: SingleChildScrollView( child: SingleChildScrollView(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
@@ -94,10 +151,12 @@ class _MyHomePageState extends State<MyHomePage> {
spacing: 8, spacing: 8,
runSpacing: 8, runSpacing: 8,
children: [ children: [
...games.values.map( ...filteredGames.map(
(game) => SizedBox( (game) => SizedBox(
key: ValueKey(game.name),
width: (MediaQuery.of(context).size.width - 24) / 2, width: (MediaQuery.of(context).size.width - 24) / 2,
child: GameCard( child: GameCard(
key: ValueKey('card_${game.name}'),
game: game, game: game,
onGameUpdated: (game) async { onGameUpdated: (game) async {
game = await GameRepository.upsert(game); game = await GameRepository.upsert(game);
@@ -129,6 +188,9 @@ class _MyHomePageState extends State<MyHomePage> {
), ),
), ),
), ),
),
],
),
); );
} }
} }