delete games
parent
46ea9d10d3
commit
2dbc0f630f
@ -0,0 +1,275 @@
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../../core/status.dart';
|
||||
import '../../shared/providers.dart';
|
||||
import '../data/game_model.dart';
|
||||
import '../data/game_repository.dart';
|
||||
|
||||
class GamesDetailScreen extends ConsumerStatefulWidget {
|
||||
final Game game;
|
||||
const GamesDetailScreen({super.key, required this.game});
|
||||
|
||||
@override
|
||||
ConsumerState<GamesDetailScreen> createState() => _GamesDetailScreenState();
|
||||
}
|
||||
|
||||
class _GamesDetailScreenState extends ConsumerState<GamesDetailScreen> {
|
||||
late Game _game;
|
||||
late ItemStatus _status;
|
||||
late ItemStatus _origStatus;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_game = widget.game;
|
||||
_status = widget.game.status;
|
||||
_origStatus = widget.game.status;
|
||||
}
|
||||
|
||||
bool get _dirty => _status != _origStatus;
|
||||
|
||||
Future<void> _save() async {
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
try {
|
||||
final backend = ref.read(backendApiProvider);
|
||||
await backend.setGameStatus(
|
||||
igdbId: _game.igdbId,
|
||||
name: _game.name,
|
||||
originalName: _game.originalName,
|
||||
status: _status.index,
|
||||
lang: 'de',
|
||||
title: _game.localizedTitle ?? _game.name,
|
||||
summary: _game.summary,
|
||||
coverUrl: _game.coverUrl,
|
||||
releaseYear: _game.releaseYear,
|
||||
);
|
||||
if (!mounted) return;
|
||||
setState(() => _origStatus = _status);
|
||||
// ignore: unused_result
|
||||
ref.invalidate(gamesStreamProvider);
|
||||
// ignore: unused_result
|
||||
ref.invalidate(gamesProvider);
|
||||
messenger.showSnackBar(const SnackBar(content: Text('Gespeichert')));
|
||||
} catch (e) {
|
||||
messenger.showSnackBar(SnackBar(content: Text('Speichern fehlgeschlagen: $e')));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final g = _game;
|
||||
final cover = g.coverUrl;
|
||||
return Scaffold(
|
||||
extendBodyBehindAppBar: true,
|
||||
appBar: AppBar(
|
||||
backgroundColor: Colors.transparent,
|
||||
elevation: 0,
|
||||
title: Text(g.displayName),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.sync),
|
||||
tooltip: 'IGDB aktualisieren',
|
||||
onPressed: _refreshFromIgdb,
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: _dirty
|
||||
? FloatingActionButton.extended(
|
||||
onPressed: _save,
|
||||
icon: const Icon(Icons.save),
|
||||
label: const Text('Speichern'),
|
||||
)
|
||||
: null,
|
||||
body: Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: Container(
|
||||
decoration: cover != null && cover.isNotEmpty
|
||||
? BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: CachedNetworkImageProvider(cover),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
Positioned.fill(
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
Colors.black.withOpacity(0.25),
|
||||
Colors.black.withOpacity(0.65),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Positioned.fill(
|
||||
child: SingleChildScrollView(
|
||||
padding: EdgeInsets.only(
|
||||
top: MediaQuery.of(context).padding.top + kToolbarHeight + 12,
|
||||
left: 12,
|
||||
right: 12,
|
||||
bottom: 24,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_header(context, g),
|
||||
const SizedBox(height: 16),
|
||||
if ((g.summary ?? '').isNotEmpty) _summaryCard(context, g.summary!),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _header(BuildContext context, Game g) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black.withOpacity(0.35),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (g.coverUrl != null && g.coverUrl!.isNotEmpty)
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: g.coverUrl!,
|
||||
width: 120,
|
||||
height: 170,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
if (g.coverUrl != null && g.coverUrl!.isNotEmpty) const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: DefaultTextStyle(
|
||||
style: const TextStyle(color: Colors.white),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
g.releaseYear != null ? '${g.displayName} (${g.releaseYear})' : g.displayName,
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(color: Colors.white),
|
||||
),
|
||||
if (g.originalName != null && g.originalName!.isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
g.originalName!,
|
||||
style: const TextStyle(color: Colors.white70, fontStyle: FontStyle.italic),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 12),
|
||||
_statusSelector(_status),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _summaryCard(BuildContext context, String summary) {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black.withOpacity(0.35),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: DefaultTextStyle(
|
||||
style: const TextStyle(color: Colors.white),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Beschreibung', style: Theme.of(context).textTheme.titleMedium?.copyWith(color: Colors.white)),
|
||||
const SizedBox(height: 6),
|
||||
Text(summary),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _statusSelector(ItemStatus current) {
|
||||
return Wrap(
|
||||
spacing: 8,
|
||||
children: [
|
||||
for (final s in ItemStatus.values)
|
||||
ChoiceChip(
|
||||
label: Text(s.name),
|
||||
selected: current == s,
|
||||
onSelected: (sel) {
|
||||
if (sel) setState(() => _status = s);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _refreshFromIgdb() async {
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
try {
|
||||
final igdb = ref.read(igdbApiProvider);
|
||||
final data = await igdb.getGameDetails(_game.igdbId, lang: 'de');
|
||||
final cover = (data['cover'] is Map && data['cover']['image_id'] != null)
|
||||
? 'https://images.igdb.com/igdb/image/upload/t_cover_big/${data['cover']['image_id']}.jpg'
|
||||
: _game.coverUrl;
|
||||
final summary = data['summary'] as String? ?? _game.summary;
|
||||
final title = data['name'] as String? ?? _game.name;
|
||||
final firstRelease = data['first_release_date'];
|
||||
int? year;
|
||||
if (firstRelease is num) {
|
||||
year = DateTime.fromMillisecondsSinceEpoch(firstRelease.toInt() * 1000).year;
|
||||
}
|
||||
|
||||
await ref.read(backendApiProvider).setGameStatus(
|
||||
igdbId: _game.igdbId,
|
||||
name: title,
|
||||
originalName: _game.originalName,
|
||||
status: _status.index,
|
||||
lang: 'de',
|
||||
title: title,
|
||||
summary: summary,
|
||||
coverUrl: cover,
|
||||
releaseYear: year,
|
||||
);
|
||||
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_game = Game(
|
||||
id: _game.id,
|
||||
igdbId: _game.igdbId,
|
||||
name: title,
|
||||
localizedTitle: title,
|
||||
originalName: _game.originalName,
|
||||
status: _status,
|
||||
note: _game.note,
|
||||
summary: summary,
|
||||
coverUrl: cover,
|
||||
releaseYear: year ?? _game.releaseYear,
|
||||
);
|
||||
});
|
||||
// ignore: unused_result
|
||||
ref.invalidate(gamesStreamProvider);
|
||||
// ignore: unused_result
|
||||
ref.invalidate(gamesProvider);
|
||||
messenger.showSnackBar(const SnackBar(content: Text('IGDB aktualisiert')));
|
||||
} catch (e) {
|
||||
messenger.showSnackBar(SnackBar(content: Text('IGDB Update fehlgeschlagen: $e')));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue