Build APK / build (push) Successful in 3m6s
- Écran Mods (recherche Modrinth + liste installée) accessible depuis le détail d_un serveur Minecraft - Ajout/retrait réservés aux admins (bouton masqué/averti sinon), via l_API gamepanel - Service API : role(), mods(), searchMods(), addMod(), removeMod() + modèle ModHit
260 lines
7.6 KiB
Dart
260 lines
7.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../models.dart';
|
|
import '../services/api_service.dart';
|
|
|
|
/// Gestion des mods d'un serveur via Modrinth.
|
|
/// Lecture pour tous ceux qui ont accès ; ajout/retrait réservés aux admins
|
|
/// (le backend renvoie 403 sinon).
|
|
class ModsScreen extends StatefulWidget {
|
|
const ModsScreen({super.key, required this.api, required this.serverId, required this.serverName});
|
|
|
|
final ApiService api;
|
|
final String serverId;
|
|
final String serverName;
|
|
|
|
@override
|
|
State<ModsScreen> createState() => _ModsScreenState();
|
|
}
|
|
|
|
class _ModsScreenState extends State<ModsScreen> {
|
|
bool _loading = true;
|
|
String? _error;
|
|
bool _isAdmin = false;
|
|
List<String> _installed = [];
|
|
|
|
final _searchCtrl = TextEditingController();
|
|
bool _searching = false;
|
|
List<ModHit> _results = [];
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_load();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_searchCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
setState(() {
|
|
_loading = true;
|
|
_error = null;
|
|
});
|
|
try {
|
|
final role = await widget.api.role().catchError((_) => 'member');
|
|
final mods = await widget.api.mods(widget.serverId);
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_isAdmin = role == 'admin';
|
|
_installed = mods;
|
|
_loading = false;
|
|
});
|
|
} catch (e) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_error = '$e';
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
Future<void> _search() async {
|
|
final q = _searchCtrl.text.trim();
|
|
setState(() => _searching = true);
|
|
try {
|
|
final hits = await widget.api.searchMods(q, widget.serverId);
|
|
if (!mounted) return;
|
|
setState(() => _results = hits);
|
|
} catch (e) {
|
|
_snack('$e');
|
|
} finally {
|
|
if (mounted) setState(() => _searching = false);
|
|
}
|
|
}
|
|
|
|
Future<void> _add(ModHit hit) async {
|
|
try {
|
|
await widget.api.addMod(widget.serverId, hit.slug);
|
|
_snack('« ${hit.title} » ajouté — le serveur redémarre (téléchargement au boot).');
|
|
await _load();
|
|
} catch (e) {
|
|
_snack('$e');
|
|
}
|
|
}
|
|
|
|
Future<void> _remove(String slug) async {
|
|
final ok = await showDialog<bool>(
|
|
context: context,
|
|
builder: (c) => AlertDialog(
|
|
title: Text('Retirer $slug ?'),
|
|
content: const Text('Le serveur redémarrera pour appliquer le changement.'),
|
|
actions: [
|
|
TextButton(onPressed: () => Navigator.pop(c, false), child: const Text('Annuler')),
|
|
FilledButton(onPressed: () => Navigator.pop(c, true), child: const Text('Retirer')),
|
|
],
|
|
),
|
|
);
|
|
if (ok != true) return;
|
|
try {
|
|
await widget.api.removeMod(widget.serverId, slug);
|
|
_snack('« $slug » retiré — le serveur redémarre.');
|
|
await _load();
|
|
} catch (e) {
|
|
_snack('$e');
|
|
}
|
|
}
|
|
|
|
void _snack(String m) {
|
|
if (mounted) ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(m)));
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(title: Text('Mods — ${widget.serverName}')),
|
|
body: _loading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: _error != null
|
|
? _ErrorState(message: _error!, onRetry: _load)
|
|
: RefreshIndicator(
|
|
onRefresh: _load,
|
|
child: ListView(
|
|
padding: const EdgeInsets.all(16),
|
|
children: [
|
|
_InstalledSection(
|
|
installed: _installed,
|
|
canRemove: _isAdmin,
|
|
onRemove: _remove,
|
|
),
|
|
const SizedBox(height: 24),
|
|
if (_isAdmin) ..._searchSection() else _memberNote(),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _memberNote() => const Card(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(16),
|
|
child: Text('La gestion des mods est réservée aux administrateurs.'),
|
|
),
|
|
);
|
|
|
|
List<Widget> _searchSection() {
|
|
return [
|
|
Text('Ajouter depuis Modrinth', style: Theme.of(context).textTheme.titleMedium),
|
|
const SizedBox(height: 8),
|
|
TextField(
|
|
controller: _searchCtrl,
|
|
textInputAction: TextInputAction.search,
|
|
onSubmitted: (_) => _search(),
|
|
decoration: InputDecoration(
|
|
hintText: 'Rechercher un mod (compatible avec ce serveur)…',
|
|
prefixIcon: const Icon(Icons.search),
|
|
suffixIcon: IconButton(icon: const Icon(Icons.arrow_forward), onPressed: _search),
|
|
border: const OutlineInputBorder(),
|
|
),
|
|
),
|
|
const SizedBox(height: 12),
|
|
if (_searching)
|
|
const Padding(padding: EdgeInsets.all(24), child: Center(child: CircularProgressIndicator()))
|
|
else if (_results.isEmpty)
|
|
const Padding(
|
|
padding: EdgeInsets.symmetric(vertical: 24),
|
|
child: Center(child: Text('Lance une recherche pour trouver des mods.')),
|
|
)
|
|
else
|
|
..._results.map(_resultTile),
|
|
];
|
|
}
|
|
|
|
Widget _resultTile(ModHit hit) {
|
|
final already = _installed.contains(hit.slug);
|
|
return Card(
|
|
child: ListTile(
|
|
leading: hit.iconUrl != null
|
|
? ClipRRect(
|
|
borderRadius: BorderRadius.circular(8),
|
|
child: Image.network(hit.iconUrl!, width: 40, height: 40,
|
|
errorBuilder: (_, __, ___) => const Icon(Icons.extension)),
|
|
)
|
|
: const Icon(Icons.extension),
|
|
title: Text(hit.title),
|
|
subtitle: Text(
|
|
hit.description ?? hit.slug,
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
trailing: already
|
|
? const Chip(label: Text('Installé'))
|
|
: FilledButton.tonal(onPressed: () => _add(hit), child: const Text('Ajouter')),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _InstalledSection extends StatelessWidget {
|
|
const _InstalledSection({required this.installed, required this.canRemove, required this.onRemove});
|
|
|
|
final List<String> installed;
|
|
final bool canRemove;
|
|
final void Function(String slug) onRemove;
|
|
|
|
static const _core = {'fabric-api', 'fabricloader', 'forge', 'neoforge'};
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text('Mods installés (${installed.length})', style: Theme.of(context).textTheme.titleMedium),
|
|
const SizedBox(height: 8),
|
|
if (installed.isEmpty)
|
|
const Text('Aucun mod.')
|
|
else
|
|
Wrap(
|
|
spacing: 8,
|
|
runSpacing: 8,
|
|
children: [
|
|
for (final slug in installed)
|
|
InputChip(
|
|
label: Text(slug),
|
|
onDeleted: canRemove && !_core.contains(slug) ? () => onRemove(slug) : null,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ErrorState extends StatelessWidget {
|
|
const _ErrorState({required this.message, required this.onRetry});
|
|
final String message;
|
|
final VoidCallback onRetry;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
const Icon(Icons.error_outline, size: 40),
|
|
const SizedBox(height: 12),
|
|
Text(message, textAlign: TextAlign.center),
|
|
const SizedBox(height: 16),
|
|
FilledButton(onPressed: onRetry, child: const Text('Réessayer')),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|