Files
Melo/lib/settings/settings_screen.dart
T
Hermes (Server)andClaude Haiku 4.5 323a67e3a2 🐛 Fix: INTERNET-Permission fehlte im Release-Manifest
ROOT CAUSE für die fehlenden Logs — und für den fehlschlagenden
Navidrome-Login: Flutter injiziert android.permission.INTERNET nur in
debug/ und profile/AndroidManifest.xml. In main/AndroidManifest.xml
stand sie nicht, das Release-APK hatte damit gar keinen Netzzugriff.
Jede http-Anfrage schlug sofort mit SocketException fehl.

- INTERNET + ACCESS_NETWORK_STATE in main/AndroidManifest.xml ergänzt
- Login-Versuch/-Erfolg/-Fehlschlag im Settings-Dialog explizit geloggt
  (URL + User + ob das Passwortfeld leer war; das Passwort selbst nicht)
- Batch-Schwelle 20 -> 5, damit INFO-Logs auch ohne vorherigen Fehler
  hochgeladen werden

Nicht geaendert: der Sofort-Upload bei ERROR war bereits implementiert
(error() ruft _uploadIfError -> _uploadLogs direkt auf) und war nicht
die Ursache.

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XJzQjUtnvYUtHdnTs3iCru
2026-08-19 22:28:36 +02:00

385 lines
14 KiB
Dart

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../library/database.dart';
import '../library/permissions.dart';
import '../library/playlist_service.dart';
import '../services/cache_manager.dart';
import '../services/logger_service.dart';
import '../services/navidrome_service.dart';
import '../services/offline_mode.dart';
import 'library_stats.dart';
/// Settings-Tab: Bibliotheks-Statistik, Berechtigungen, Navidrome, Über Melo.
class SettingsScreen extends StatefulWidget {
const SettingsScreen({super.key});
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
late final NavidromeService _navidrome = NavidromeService();
late final CacheManager _cache = CacheManager();
int _cacheSize = 0;
@override
void initState() {
super.initState();
_navidrome.ladeGespeicherteZugangsdaten();
_cache.init().then((_) => _updateCacheSize());
}
Future<void> _updateCacheSize() async {
final size = await _cache.getCacheSize();
if (mounted) setState(() => _cacheSize = size);
}
@override
Widget build(BuildContext context) {
final db = context.read<MeloDb>();
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: ListView(
children: [
const _SectionLabel('Bibliothek'),
StreamBuilder<List<Song>>(
stream: db.watchSongs(),
builder: (context, snapshot) {
final songs = snapshot.data ?? const [];
return Column(
children: [
ListTile(
leading: const Icon(Icons.library_music),
title: Text('${songs.length} Songs in der Bibliothek'),
),
ListTile(
leading: const Icon(Icons.schedule),
title:
Text('${formatTotalDuration(songs)} Gesamtspieldauer'),
),
],
);
},
),
const Divider(height: 1),
const _SectionLabel('Musikserver'),
ListTile(
leading: const Icon(Icons.cloud_circle),
title: const Text('🌐 Navidrome'),
subtitle: _navidrome.istVerbunden
? const Text('✅ Verbunden')
: const Text('Nicht verbunden'),
trailing: _navidrome.istVerbunden
? IconButton(
icon: const Icon(Icons.logout),
onPressed: () => _trennNavidrome(),
)
: IconButton(
icon: const Icon(Icons.login),
onPressed: () => _zeigeNavidromeDialog(),
),
),
if (!_navidrome.istVerbunden)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Text(
'💡 Tipp: Oder nutze Melo lokal ohne Server-Konto (nur deine Musik auf dem Gerät)',
style: TextStyle(fontSize: 12, color: Colors.white54),
),
),
ListTile(
leading: const Icon(Icons.wifi_off),
title: const Text('📴 Offline-Modus'),
subtitle: const Text('Nur gecachte Lieder abspielen'),
trailing: Consumer<OfflineMode>(
builder: (context, offlineMode, _) {
return Switch(
value: offlineMode.enabled,
onChanged: (value) => offlineMode.setEnabled(value),
);
},
),
),
ListTile(
leading: const Icon(Icons.storage),
title: const Text('🗄️ Cache-Speicher'),
subtitle: Text(_formatBytes(_cacheSize)),
trailing: _cacheSize > 0
? IconButton(
tooltip: 'Cache löschen',
icon: const Icon(Icons.delete),
onPressed: () => _clearCache(),
)
: null,
),
ListTile(
leading: const Icon(Icons.favorite),
title: const Text('🌟 Favoriten-Sync'),
subtitle: const Text('Favoriten vom Server importieren'),
trailing: IconButton(
tooltip: 'Vom Server laden',
icon: const Icon(Icons.cloud_download),
onPressed: () => _syncServerFavorites(),
),
),
ListTile(
leading: const Icon(Icons.playlist_play),
title: const Text('📋 Playlisten-Sync'),
subtitle: const Text('Server-Playlisten importieren'),
trailing: IconButton(
tooltip: 'Vom Server laden',
icon: const Icon(Icons.cloud_download),
onPressed: () => _syncServerPlaylists(),
),
),
const Divider(height: 1),
const _SectionLabel('Berechtigungen'),
ListTile(
leading: const Icon(Icons.mic_none),
title: const Text('Musik-Berechtigung'),
trailing: TextButton(
onPressed: openMusicPermissionSettings,
child: const Text('Einstellungen öffnen'),
),
),
const Divider(height: 1),
const _SectionLabel('Über Melo'),
const ListTile(
leading: Icon(Icons.info_outline),
title: Text('Melo'),
subtitle: Text('Deine Musik. Offline. Kein Abo.'),
),
],
),
);
}
void _zeigeNavidromeDialog() {
final urlCtrl = TextEditingController(text: 'https://musik.baka-net.de');
final passCtrl = TextEditingController();
String selectedUser = 'Dustin';
bool verbindet = false;
String? fehler;
showDialog(
context: context,
builder: (ctx) => StatefulBuilder(
builder: (ctx, setDialogState) => AlertDialog(
backgroundColor: Colors.grey.shade900,
title: const Text('🌐 Navidrome Login',
style: TextStyle(color: Colors.white, fontSize: 16)),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
controller: urlCtrl,
style: const TextStyle(color: Colors.white),
decoration: const InputDecoration(
labelText: 'Server-URL',
labelStyle: TextStyle(color: Colors.grey),
border: OutlineInputBorder(),
),
),
const SizedBox(height: 12),
const Text('Benutzer:', style: TextStyle(fontSize: 12, color: Colors.white70)),
const SizedBox(height: 8),
Row(
children: [
Expanded(
child: FilledButton(
onPressed: () => setDialogState(() => selectedUser = 'Baka'),
style: FilledButton.styleFrom(
backgroundColor: selectedUser == 'Baka'
? Colors.redAccent
: Colors.grey.shade700,
),
child: const Text('Baka'),
),
),
const SizedBox(width: 8),
Expanded(
child: FilledButton(
onPressed: () => setDialogState(() => selectedUser = 'Tinker'),
style: FilledButton.styleFrom(
backgroundColor: selectedUser == 'Tinker'
? Colors.redAccent
: Colors.grey.shade700,
),
child: const Text('Tinker'),
),
),
const SizedBox(width: 8),
Expanded(
child: FilledButton(
onPressed: () => setDialogState(() => selectedUser = 'Dustin'),
style: FilledButton.styleFrom(
backgroundColor: selectedUser == 'Dustin'
? Colors.redAccent
: Colors.grey.shade700,
),
child: const Text('Dustin'),
),
),
],
),
const SizedBox(height: 12),
TextField(
controller: passCtrl,
style: const TextStyle(color: Colors.white),
obscureText: true,
decoration: const InputDecoration(
labelText: 'Passwort',
labelStyle: TextStyle(color: Colors.grey),
border: OutlineInputBorder(),
),
),
if (fehler != null) ...[
const SizedBox(height: 8),
Text(fehler!,
style: const TextStyle(color: Colors.red, fontSize: 12)),
],
if (verbindet)
const Padding(
padding: EdgeInsets.only(top: 12),
child: SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2, color: Colors.redAccent),
),
),
],
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text('Abbrechen')),
TextButton(
onPressed: verbindet
? null
: () async {
setDialogState(() => verbindet = true);
logger.info(
'Login-Versuch: url=${urlCtrl.text} user=$selectedUser '
'pwLeer=${passCtrl.text.isEmpty}');
_navidrome.setCredentials(
urlCtrl.text, selectedUser, passCtrl.text);
final ok = await _navidrome.ping();
setDialogState(() => verbindet = false);
if (ok && ctx.mounted) {
logger.info(
'Login erfolgreich: url=${urlCtrl.text} user=$selectedUser');
await _navidrome.speichereZugangsdaten(
urlCtrl.text, selectedUser, passCtrl.text);
if (ctx.mounted) Navigator.pop(ctx);
if (mounted) setState(() {});
} else if (ctx.mounted) {
logger.error(
'Login fehlgeschlagen: url=${urlCtrl.text} '
'user=$selectedUser pwLeer=${passCtrl.text.isEmpty}');
setDialogState(() =>
fehler = '❌ Login fehlgeschlagen');
}
},
child: const Text('Login',
style: TextStyle(color: Colors.redAccent)),
),
],
),
),
).then((_) {
urlCtrl.dispose();
passCtrl.dispose();
});
}
Future<void> _trennNavidrome() async {
await _navidrome.loescheZugangsdaten();
if (mounted) setState(() {});
}
Future<void> _clearCache() async {
await _cache.clearCache();
if (mounted) {
setState(() => _cacheSize = 0);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Cache gelöscht')),
);
}
}
Future<void> _syncServerFavorites() async {
final playlistService = context.read<PlaylistService>();
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Lade Favoriten vom Server...')),
);
final count = await playlistService.syncFavoritesFromServer();
if (mounted) {
if (count == 0) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('❌ Server nicht verbunden oder keine Favoriten')),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('✅ $count Favoriten importiert')),
);
}
}
}
Future<void> _syncServerPlaylists() async {
final playlistService = context.read<PlaylistService>();
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Lade Playlisten vom Server...')),
);
final count = await playlistService.syncPlaylistsFromServer();
if (mounted) {
if (count == 0) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('❌ Server nicht verbunden oder keine Playlisten')),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('✅ $count Playlisten importiert')),
);
}
}
}
String _formatBytes(int bytes) {
if (bytes < 1024) return '$bytes B';
if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(1)} KB';
return '${(bytes / (1024 * 1024)).toStringAsFixed(1)} MB';
}
}
/// Kleine graue Überschrift über einer Einstellungs-Gruppe.
class _SectionLabel extends StatelessWidget {
const _SectionLabel(this.label);
final String label;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.fromLTRB(16, 16, 16, 4),
child: Text(
label,
style: const TextStyle(
color: Colors.white54,
fontSize: 12,
fontWeight: FontWeight.bold,
),
),
);
}
}