import 'package:flutter/material.dart'; import 'package:gamer_updater/game.dart'; class GameCard extends StatefulWidget { final Game game; final Function(Game) onGameUpdated; final Function()? onDelete; final bool isNameEditable; const GameCard({ super.key, required this.game, required this.onGameUpdated, this.onDelete, this.isNameEditable = false, }); @override State createState() => _GameCardState(); } class _GameCardState extends State with SingleTickerProviderStateMixin { late final AnimationController _controller; bool _isLoading = false; int _deleteClickCount = 0; late TextEditingController _nameController; late TextEditingController _versionRegexController; late TextEditingController _rssFeedUrlController; late TextEditingController _lastPlayedController; late FocusNode _nameFocus; late FocusNode _versionRegexFocus; late FocusNode _rssFeedUrlFocus; late FocusNode _lastPlayedFocus; @override void initState() { super.initState(); _controller = AnimationController( duration: const Duration(seconds: 1), vsync: this, ); _nameController = TextEditingController(text: widget.game.name); _versionRegexController = TextEditingController( text: widget.game.versionRegex, ); _rssFeedUrlController = TextEditingController(text: widget.game.rssFeedUrl); _lastPlayedController = TextEditingController(text: widget.game.lastPlayed); _nameFocus = FocusNode(); _versionRegexFocus = FocusNode(); _rssFeedUrlFocus = FocusNode(); _lastPlayedFocus = FocusNode(); _setupFocusListeners(); } void _setupFocusListeners() { void updateGame() { var name = widget.isNameEditable ? _nameController.text : widget.game.name; if (name.isNotEmpty) { widget.onGameUpdated( Game( name: name, versionRegex: _versionRegexController.text, lastPlayed: _lastPlayedController.text, rssFeedUrl: _rssFeedUrlController.text, actualVersion: widget.game.actualVersion, lastUpdated: widget.game.lastUpdated, ), ); } } _nameFocus.addListener(() { if (!_nameFocus.hasFocus) updateGame(); }); _versionRegexFocus.addListener(() { if (!_versionRegexFocus.hasFocus) updateGame(); }); _rssFeedUrlFocus.addListener(() { if (!_rssFeedUrlFocus.hasFocus) updateGame(); }); _lastPlayedFocus.addListener(() { if (!_lastPlayedFocus.hasFocus) updateGame(); }); } @override void dispose() { _controller.dispose(); _nameController.dispose(); _versionRegexController.dispose(); _rssFeedUrlController.dispose(); _lastPlayedController.dispose(); _nameFocus.dispose(); _versionRegexFocus.dispose(); _rssFeedUrlFocus.dispose(); _lastPlayedFocus.dispose(); super.dispose(); } Future _refreshVersion() async { setState(() => _isLoading = true); _controller.repeat(); final updatedGame = widget.game; try { await updatedGame.updateActualVersion(); } catch (e) { if (mounted) { ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text(e.toString()))); } } widget.onGameUpdated(updatedGame); _controller.stop(); _controller.reset(); setState(() => _isLoading = false); } void _handleDeleteClick() { setState(() { _deleteClickCount++; if (_deleteClickCount >= 2) { widget.onDelete?.call(); _deleteClickCount = 0; } }); // Reset click count after a delay Future.delayed(const Duration(seconds: 1), () { if (mounted) { setState(() => _deleteClickCount = 0); } }); } @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: [ SizedBox( height: 40, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( child: TextField( controller: _nameController, focusNode: _nameFocus, style: Theme.of(context).textTheme.titleLarge, enabled: widget.isNameEditable, decoration: const InputDecoration.collapsed( hintText: 'New Game', ), onSubmitted: (_) => _nameFocus.unfocus(), ), ), Row( mainAxisSize: MainAxisSize.min, children: [ if (widget.onDelete != null) IconButton( icon: Icon( Icons.delete, color: _deleteClickCount > 0 ? Colors.red : null, ), onPressed: _handleDeleteClick, ), if (!widget.isNameEditable) 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: _versionRegexController, focusNode: _versionRegexFocus, decoration: const InputDecoration(labelText: 'Version Regex'), onSubmitted: (_) => _versionRegexFocus.unfocus(), ), TextField( controller: _rssFeedUrlController, focusNode: _rssFeedUrlFocus, decoration: const InputDecoration(labelText: 'RSS Feed URL'), onSubmitted: (_) => _rssFeedUrlFocus.unfocus(), ), TextField( controller: _lastPlayedController, focusNode: _lastPlayedFocus, decoration: const InputDecoration(labelText: 'Last Played'), onSubmitted: (_) => _lastPlayedFocus.unfocus(), ), ], ), ), ); } }