More refactorings

This commit is contained in:
2025-02-22 15:07:29 +01:00
parent ef97b1eb30
commit 1ec8fa1f0d
2 changed files with 8 additions and 191 deletions

View File

@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:gamer_updater/db.dart'; 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';
void main() async { void main() async {
await DB.init(); await DB.init();
@@ -120,187 +121,3 @@ class _MyHomePageState extends State<MyHomePage> {
); );
} }
} }
class GameCard extends StatefulWidget {
final Game game;
final Function(Game) onGameUpdated;
final VoidCallback onDelete;
const GameCard({
super.key,
required this.game,
required this.onGameUpdated,
required this.onDelete,
});
@override
State<GameCard> createState() => _GameCardState();
}
class _GameCardState extends State<GameCard>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
bool _isLoading = false;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: const Duration(seconds: 1),
vsync: this,
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _refreshVersion() async {
setState(() => _isLoading = true);
_controller.repeat();
final updatedGame = widget.game;
await updatedGame.updateActualVersion();
widget.onGameUpdated(updatedGame);
_controller.stop();
_controller.reset();
setState(() => _isLoading = false);
}
@override
Widget build(BuildContext context) {
final isUpToDate = widget.game.actualVersion == widget.game.lastPlayed;
return Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
widget.game.name,
style: Theme.of(context).textTheme.titleLarge,
),
RotationTransition(
turns: _controller,
child: IconButton(
icon: const Icon(Icons.refresh),
onPressed: _isLoading ? null : _refreshVersion,
),
),
],
),
const SizedBox(height: 8),
Row(
children: [
SizedBox(
width: 120,
child: Text(
'Version:',
style: Theme.of(context).textTheme.bodyLarge,
),
),
Text(
widget.game.actualVersion.isEmpty
? 'Unknown'
: widget.game.actualVersion,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: isUpToDate ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
],
),
Row(
children: [
SizedBox(
width: 120,
child: Text(
'Last Updated:',
style: Theme.of(context).textTheme.bodyLarge,
),
),
Text(
widget.game.lastUpdated.isEmpty
? 'Never'
: DateTime.parse(widget.game.lastUpdated).toString(),
style: Theme.of(context).textTheme.bodyMedium,
),
],
),
Row(
children: [
SizedBox(
width: 120,
child: Text(
'Last Played:',
style: Theme.of(context).textTheme.bodyLarge,
),
),
Text(
widget.game.lastPlayed,
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
color: isUpToDate ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
],
),
const Divider(),
TextField(
controller: TextEditingController(text: widget.game.versionRegex),
decoration: const InputDecoration(labelText: 'Version Regex'),
onChanged:
(value) => widget.onGameUpdated(
Game(
name: widget.game.name,
versionRegex: value,
lastPlayed: widget.game.lastPlayed,
rssFeedUrl: widget.game.rssFeedUrl,
actualVersion: widget.game.actualVersion,
lastUpdated: widget.game.lastUpdated,
),
),
),
TextField(
controller: TextEditingController(text: widget.game.rssFeedUrl),
decoration: const InputDecoration(labelText: 'RSS Feed URL'),
onChanged:
(value) => widget.onGameUpdated(
Game(
name: widget.game.name,
versionRegex: widget.game.versionRegex,
lastPlayed: widget.game.lastPlayed,
rssFeedUrl: value,
actualVersion: widget.game.actualVersion,
lastUpdated: widget.game.lastUpdated,
),
),
),
TextField(
controller: TextEditingController(text: widget.game.lastPlayed),
decoration: const InputDecoration(labelText: 'Last Played'),
onChanged:
(value) => widget.onGameUpdated(
Game(
name: widget.game.name,
versionRegex: widget.game.versionRegex,
lastPlayed: value,
rssFeedUrl: widget.game.rssFeedUrl,
actualVersion: widget.game.actualVersion,
lastUpdated: widget.game.lastUpdated,
),
),
),
],
),
),
);
}
}

View File

@@ -106,13 +106,6 @@ class _GameCardState extends State<GameCard> with SingleTickerProviderStateMixin
Row( Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
RotationTransition(
turns: _controller,
child: IconButton(
icon: const Icon(Icons.refresh),
onPressed: _isLoading ? null : _refreshVersion,
),
),
if (widget.onDelete != null) if (widget.onDelete != null)
IconButton( IconButton(
icon: Icon( icon: Icon(
@@ -121,6 +114,13 @@ class _GameCardState extends State<GameCard> with SingleTickerProviderStateMixin
), ),
onPressed: _handleDeleteClick, onPressed: _handleDeleteClick,
), ),
RotationTransition(
turns: _controller,
child: IconButton(
icon: const Icon(Icons.refresh),
onPressed: _isLoading ? null : _refreshVersion,
),
),
], ],
), ),
], ],