add games
parent
975b704553
commit
46ea9d10d3
@ -0,0 +1,37 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../core/config.dart';
|
||||
|
||||
class IgdbApi {
|
||||
final Dio _dio;
|
||||
|
||||
IgdbApi()
|
||||
: _dio = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: AppConfig.backendBaseUrl,
|
||||
headers: const {'Accept': 'application/json'},
|
||||
),
|
||||
);
|
||||
|
||||
Future<List<Map<String, dynamic>>> searchGames(String query,
|
||||
{int limit = 20, String lang = 'de'}) async {
|
||||
final res = await _dio.get(
|
||||
'',
|
||||
queryParameters: {
|
||||
'action': 'search',
|
||||
'query': query,
|
||||
'limit': limit,
|
||||
'lang': lang,
|
||||
'check_external': 1,
|
||||
},
|
||||
);
|
||||
if (res.statusCode != 200) {
|
||||
throw Exception('IGDB search HTTP ${res.statusCode}');
|
||||
}
|
||||
final data = res.data;
|
||||
if (data is Map && data['results'] is List) {
|
||||
return (data['results'] as List).cast<Map<String, dynamic>>();
|
||||
}
|
||||
throw Exception('IGDB search: unerwartetes Antwortformat');
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
import '../../../core/status.dart';
|
||||
|
||||
class Game {
|
||||
final int id; // DB id
|
||||
final int igdbId;
|
||||
final String name;
|
||||
final String? localizedTitle;
|
||||
final String? originalName;
|
||||
final ItemStatus status;
|
||||
final String? note;
|
||||
final String? summary;
|
||||
final String? coverUrl;
|
||||
|
||||
Game({
|
||||
required this.id,
|
||||
required this.igdbId,
|
||||
required this.name,
|
||||
this.localizedTitle,
|
||||
this.originalName,
|
||||
this.status = ItemStatus.Init,
|
||||
this.note,
|
||||
this.summary,
|
||||
this.coverUrl,
|
||||
});
|
||||
|
||||
factory Game.fromJson(Map<String, dynamic> j) {
|
||||
return Game(
|
||||
id: (j['id'] as num).toInt(),
|
||||
igdbId: (j['igdb_id'] as num).toInt(),
|
||||
name: (j['name'] ?? '') as String,
|
||||
localizedTitle: j['loc_title'] as String?,
|
||||
originalName: j['original_name'] as String?,
|
||||
status: j['status'] != null ? statusFromString(j['status'] as String) : ItemStatus.Init,
|
||||
note: j['note'] as String?,
|
||||
summary: j['loc_summary'] as String?,
|
||||
coverUrl: j['cover_url'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
String get displayName => (localizedTitle != null && localizedTitle!.isNotEmpty) ? localizedTitle! : name;
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../../core/status.dart';
|
||||
import '../../shared/providers.dart';
|
||||
import 'game_model.dart';
|
||||
|
||||
final gameFilterProvider = StateProvider<ItemStatus?>((_) => null);
|
||||
|
||||
final gamesProvider = FutureProvider.autoDispose<List<Game>>((ref) async {
|
||||
final backend = ref.watch(backendApiProvider);
|
||||
final st = ref.watch(gameFilterProvider);
|
||||
const pageSize = 500;
|
||||
var offset = 0;
|
||||
final all = <Map<String, dynamic>>[];
|
||||
while (true) {
|
||||
final page = await backend.getGames(status: st?.name, offset: offset, limit: pageSize, lang: 'de');
|
||||
all.addAll(page);
|
||||
if (page.length < pageSize) break;
|
||||
offset += pageSize;
|
||||
}
|
||||
return all.map(Game.fromJson).toList();
|
||||
});
|
||||
|
||||
final gamesStreamProvider = StreamProvider.autoDispose<List<Game>>((ref) async* {
|
||||
final backend = ref.watch(backendApiProvider);
|
||||
final st = ref.watch(gameFilterProvider);
|
||||
const pageSize = 300;
|
||||
var offset = 0;
|
||||
var agg = <Game>[];
|
||||
while (true) {
|
||||
final page = await backend.getGames(status: st?.name, offset: offset, limit: pageSize, lang: 'de');
|
||||
final mapped = page.map(Game.fromJson).toList();
|
||||
if (mapped.isEmpty) {
|
||||
if (agg.isEmpty) yield const <Game>[];
|
||||
break;
|
||||
}
|
||||
agg = [...agg, ...mapped];
|
||||
yield agg;
|
||||
if (mapped.length < pageSize) break;
|
||||
offset += pageSize;
|
||||
}
|
||||
});
|
||||
@ -0,0 +1,194 @@
|
||||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../shared/providers.dart';
|
||||
import '../data/game_repository.dart';
|
||||
|
||||
class GamesAddScreen extends ConsumerStatefulWidget {
|
||||
const GamesAddScreen({super.key});
|
||||
|
||||
@override
|
||||
ConsumerState<GamesAddScreen> createState() => _GamesAddScreenState();
|
||||
}
|
||||
|
||||
class _GamesAddScreenState extends ConsumerState<GamesAddScreen> {
|
||||
final _queryCtrl = TextEditingController();
|
||||
bool _loading = false;
|
||||
List<Map<String, dynamic>> _results = const [];
|
||||
final Set<int> _selected = {};
|
||||
|
||||
Future<void> _search() async {
|
||||
final q = _queryCtrl.text.trim();
|
||||
if (q.isEmpty) return;
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_results = const [];
|
||||
_selected.clear();
|
||||
});
|
||||
try {
|
||||
final igdb = ref.read(igdbApiProvider);
|
||||
final items = await igdb.searchGames(q, limit: 30, lang: 'de');
|
||||
setState(() => _results = items);
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context)
|
||||
.showSnackBar(SnackBar(content: Text('Suche fehlgeschlagen: $e')));
|
||||
}
|
||||
} finally {
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _addSelected() async {
|
||||
if (_selected.isEmpty) return;
|
||||
setState(() => _loading = true);
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
try {
|
||||
final backend = ref.read(backendApiProvider);
|
||||
int ok = 0;
|
||||
for (final id in _selected) {
|
||||
final game = _results.firstWhere((g) => (g['id'] as num).toInt() == id);
|
||||
final name = (game['name'] ?? '') as String;
|
||||
final original = (game['original_name'] ?? '') as String;
|
||||
final summary = (game['summary'] ?? '') as String;
|
||||
final title = (game['name'] ?? '') as String;
|
||||
final cover = game['cover_url'] as String?;
|
||||
if (name.isEmpty) continue;
|
||||
await backend.setGameStatus(
|
||||
igdbId: id,
|
||||
name: name,
|
||||
originalName: original.isNotEmpty ? original : null,
|
||||
status: 0,
|
||||
note: null,
|
||||
lang: 'de',
|
||||
title: title.isNotEmpty ? title : null,
|
||||
summary: summary.isNotEmpty ? summary : null,
|
||||
coverUrl: cover,
|
||||
);
|
||||
ok++;
|
||||
}
|
||||
// ignore: unused_result
|
||||
ref.invalidate(gamesStreamProvider);
|
||||
// ignore: unused_result
|
||||
ref.invalidate(gamesProvider);
|
||||
messenger.showSnackBar(SnackBar(content: Text('$ok Spiel(e) hinzugefügt')));
|
||||
if (mounted) Navigator.of(context).pop();
|
||||
} catch (e) {
|
||||
messenger.showSnackBar(SnackBar(content: Text('Hinzufügen fehlgeschlagen: $e')));
|
||||
} finally {
|
||||
if (mounted) setState(() => _loading = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Neue Spiele hinzufügen (IGDB)'),
|
||||
actions: [
|
||||
if (_selected.isNotEmpty)
|
||||
TextButton.icon(
|
||||
onPressed: _loading ? null : _addSelected,
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text('Hinzufügen (${_selected.length})'),
|
||||
)
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _queryCtrl,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Spiele suchen …',
|
||||
prefixIcon: Icon(Icons.search),
|
||||
),
|
||||
onSubmitted: (_) => _search(),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
FilledButton.icon(
|
||||
onPressed: _loading ? null : _search,
|
||||
icon: const Icon(Icons.search),
|
||||
label: const Text('Suchen'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (_loading) const LinearProgressIndicator(),
|
||||
const SizedBox(height: 8),
|
||||
Expanded(
|
||||
child: _results.isEmpty
|
||||
? const Center(child: Text('Keine Ergebnisse'))
|
||||
: ListView.separated(
|
||||
itemCount: _results.length,
|
||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||
itemBuilder: (_, i) {
|
||||
final g = _results[i];
|
||||
final id = (g['id'] as num).toInt();
|
||||
final sel = _selected.contains(id);
|
||||
final title = (g['name'] ?? '') as String;
|
||||
final summary = (g['summary'] ?? '') as String;
|
||||
final cover = g['cover_url'] as String?;
|
||||
return ListTile(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
if (sel) {
|
||||
_selected.remove(id);
|
||||
} else {
|
||||
_selected.add(id);
|
||||
}
|
||||
});
|
||||
},
|
||||
leading: cover != null
|
||||
? ClipRRect(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: cover,
|
||||
width: 60,
|
||||
height: 80,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
)
|
||||
: const SizedBox(width: 60, height: 80),
|
||||
title: Text(title),
|
||||
subtitle: summary.isNotEmpty
|
||||
? Text(
|
||||
summary,
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
)
|
||||
: null,
|
||||
isThreeLine: summary.isNotEmpty,
|
||||
trailing: Checkbox(
|
||||
value: sel,
|
||||
onChanged: (v) {
|
||||
setState(() {
|
||||
if (v == true) {
|
||||
_selected.add(id);
|
||||
} else {
|
||||
_selected.remove(id);
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: _selected.isEmpty || _loading ? null : _addSelected,
|
||||
icon: const Icon(Icons.add),
|
||||
label: Text('Hinzufügen (${_selected.length})'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,110 @@
|
||||
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 '../data/game_repository.dart';
|
||||
import 'games_add_screen.dart';
|
||||
import '../../movies/presentation/widgets/status_chip.dart';
|
||||
|
||||
class GamesScreen extends ConsumerWidget {
|
||||
const GamesScreen({super.key});
|
||||
|
||||
Color _statusBg(ItemStatus status, BuildContext context) {
|
||||
switch (status) {
|
||||
case ItemStatus.Progress:
|
||||
return Theme.of(context).colorScheme.secondaryContainer.withOpacity(0.3);
|
||||
case ItemStatus.Done:
|
||||
return Theme.of(context).colorScheme.primaryContainer.withOpacity(0.35);
|
||||
case ItemStatus.Init:
|
||||
default:
|
||||
return Colors.transparent;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final filter = ref.watch(gameFilterProvider);
|
||||
final gamesAsync = ref.watch(gamesStreamProvider);
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
StatusChip(
|
||||
selected: filter,
|
||||
onChanged: (f) => ref.read(gameFilterProvider.notifier).state = f,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: TextButton.icon(
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('Neue Spiele hinzufügen'),
|
||||
onPressed: () {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const GamesAddScreen()),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Expanded(
|
||||
child: gamesAsync.when(
|
||||
data: (items) {
|
||||
final sorted = [...items]
|
||||
..sort((a, b) => a.displayName.toLowerCase().compareTo(b.displayName.toLowerCase()));
|
||||
if (sorted.isEmpty) {
|
||||
return const Center(child: Text('Keine Spiele in der Sammlung'));
|
||||
}
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
// ignore: unused_result
|
||||
ref.invalidate(gamesStreamProvider);
|
||||
// ignore: unused_result
|
||||
ref.invalidate(gamesProvider);
|
||||
},
|
||||
child: ListView.separated(
|
||||
itemCount: sorted.length,
|
||||
separatorBuilder: (_, __) => const Divider(height: 1),
|
||||
itemBuilder: (_, i) {
|
||||
final g = sorted[i];
|
||||
return ListTile(
|
||||
tileColor: _statusBg(g.status, context),
|
||||
leading: g.coverUrl != null && g.coverUrl!.isNotEmpty
|
||||
? ClipRRect(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: g.coverUrl!,
|
||||
width: 50,
|
||||
height: 70,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
)
|
||||
: CircleAvatar(
|
||||
backgroundColor: Theme.of(context).colorScheme.secondaryContainer,
|
||||
child: const Icon(Icons.sports_esports),
|
||||
),
|
||||
title: Text(g.displayName),
|
||||
subtitle: Text(
|
||||
(g.summary?.isNotEmpty ?? false)
|
||||
? g.summary!
|
||||
: (g.note ?? ''),
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
trailing: Text(g.status.name),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
error: (e, st) => Center(child: Text('Fehler: $e')),
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,8 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../core/api/backend_api.dart';
|
||||
import '../../core/api/tmdb_api.dart';
|
||||
import '../../core/api/igdb_api.dart';
|
||||
|
||||
final backendApiProvider = Provider<BackendApi>((ref) => BackendApi());
|
||||
final tmdbApiProvider = Provider<TmdbApi>((ref) => TmdbApi());
|
||||
final tmdbApiProvider = Provider<TmdbApi>((ref) => TmdbApi());
|
||||
final igdbApiProvider = Provider<IgdbApi>((ref) => IgdbApi());
|
||||
|
||||
Loading…
Reference in New Issue