From 1ec8fa1f0d572a5c6d5bf71c17ec37410663de6a Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 22 Feb 2025 15:07:29 +0100 Subject: [PATCH] More refactorings --- lib/main.dart | 185 +------------------------------------ lib/widgets/game_card.dart | 14 +-- 2 files changed, 8 insertions(+), 191 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 619e430..e952c6a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:gamer_updater/db.dart'; import 'package:gamer_updater/game.dart'; import 'package:gamer_updater/widgets/new_game_card.dart'; +import 'package:gamer_updater/widgets/game_card.dart'; void main() async { await DB.init(); @@ -120,187 +121,3 @@ class _MyHomePageState extends State { ); } } - -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 createState() => _GameCardState(); -} - -class _GameCardState extends State - 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 _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, - ), - ), - ), - ], - ), - ), - ); - } -} diff --git a/lib/widgets/game_card.dart b/lib/widgets/game_card.dart index ebb2e16..e8cffb7 100644 --- a/lib/widgets/game_card.dart +++ b/lib/widgets/game_card.dart @@ -106,13 +106,6 @@ class _GameCardState extends State with SingleTickerProviderStateMixin Row( mainAxisSize: MainAxisSize.min, children: [ - RotationTransition( - turns: _controller, - child: IconButton( - icon: const Icon(Icons.refresh), - onPressed: _isLoading ? null : _refreshVersion, - ), - ), if (widget.onDelete != null) IconButton( icon: Icon( @@ -121,6 +114,13 @@ class _GameCardState extends State with SingleTickerProviderStateMixin ), onPressed: _handleDeleteClick, ), + RotationTransition( + turns: _controller, + child: IconButton( + icon: const Icon(Icons.refresh), + onPressed: _isLoading ? null : _refreshVersion, + ), + ), ], ), ],