Implement some sort of filtering
This commit is contained in:
@@ -55,10 +55,17 @@ class Game {
|
|||||||
throw Exception('No version found for $name');
|
throw Exception('No version found for $name');
|
||||||
}
|
}
|
||||||
actualVersion = version;
|
actualVersion = version;
|
||||||
lastUpdated =
|
try {
|
||||||
parseRfc822Date(
|
// Some sites use weird ass dogshit fucking formats
|
||||||
response.headers['last-modified'] ?? '',
|
// We cannot really reliably parse every single one of them
|
||||||
).toIso8601String();
|
// So - fuck it
|
||||||
|
lastUpdated =
|
||||||
|
parseRfc822Date(
|
||||||
|
response.headers['last-modified'] ?? '',
|
||||||
|
).toIso8601String();
|
||||||
|
} catch (e) {
|
||||||
|
lastUpdated = DateTime.now().toIso8601String();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
142
lib/main.dart
142
lib/main.dart
@@ -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,48 +115,81 @@ 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(
|
||||||
onRefresh: _refreshGames,
|
children: [
|
||||||
child: SingleChildScrollView(
|
Padding(
|
||||||
padding: const EdgeInsets.all(8),
|
padding: const EdgeInsets.all(8.0),
|
||||||
child: Wrap(
|
child: TextField(
|
||||||
spacing: 8,
|
controller: _searchController,
|
||||||
runSpacing: 8,
|
decoration: InputDecoration(
|
||||||
children: [
|
hintText: 'Search games...',
|
||||||
...games.values.map(
|
prefixIcon: const Icon(Icons.search),
|
||||||
(game) => SizedBox(
|
suffixIcon: searchQuery.isNotEmpty
|
||||||
width: (MediaQuery.of(context).size.width - 24) / 2,
|
? IconButton(
|
||||||
child: GameCard(
|
icon: const Icon(Icons.clear),
|
||||||
game: game,
|
onPressed: () {
|
||||||
onGameUpdated: (game) async {
|
_searchController.clear();
|
||||||
game = await GameRepository.upsert(game);
|
setState(() {
|
||||||
setState(() {
|
searchQuery = "";
|
||||||
games[game.name] = game;
|
});
|
||||||
});
|
},
|
||||||
},
|
)
|
||||||
onDelete: () async {
|
: null,
|
||||||
await GameRepository.delete(game);
|
border: OutlineInputBorder(
|
||||||
setState(() {
|
borderRadius: BorderRadius.circular(10),
|
||||||
games.remove(game.name);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
SizedBox(
|
onChanged: _onSearchChanged,
|
||||||
width: (MediaQuery.of(context).size.width - 24) / 2,
|
),
|
||||||
child: NewGameCard(
|
|
||||||
onGameCreated: (game) async {
|
|
||||||
game = await GameRepository.upsert(game);
|
|
||||||
setState(() {
|
|
||||||
games[game.name] = game;
|
|
||||||
});
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
Expanded(
|
||||||
|
child: RefreshIndicator(
|
||||||
|
onRefresh: _refreshGames,
|
||||||
|
child: SingleChildScrollView(
|
||||||
|
padding: const EdgeInsets.all(8),
|
||||||
|
child: Wrap(
|
||||||
|
spacing: 8,
|
||||||
|
runSpacing: 8,
|
||||||
|
children: [
|
||||||
|
...filteredGames.map(
|
||||||
|
(game) => SizedBox(
|
||||||
|
key: ValueKey(game.name),
|
||||||
|
width: (MediaQuery.of(context).size.width - 24) / 2,
|
||||||
|
child: GameCard(
|
||||||
|
key: ValueKey('card_${game.name}'),
|
||||||
|
game: game,
|
||||||
|
onGameUpdated: (game) async {
|
||||||
|
game = await GameRepository.upsert(game);
|
||||||
|
setState(() {
|
||||||
|
games[game.name] = game;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
onDelete: () async {
|
||||||
|
await GameRepository.delete(game);
|
||||||
|
setState(() {
|
||||||
|
games.remove(game.name);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(
|
||||||
|
width: (MediaQuery.of(context).size.width - 24) / 2,
|
||||||
|
child: NewGameCard(
|
||||||
|
onGameCreated: (game) async {
|
||||||
|
game = await GameRepository.upsert(game);
|
||||||
|
setState(() {
|
||||||
|
games[game.name] = game;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user