v2.40 — Issue #9: Progress-Notification für Download/Upload (flutter_local_notifications)

- flutter_local_notifications ^22.2.0 hinzugefügt
- Notification-Channel 'de.baka.melo.downloads' in main() initialisiert
- DownloadService._showProgress(): Fortschritts-Notification mit ProgressBar
- DownloadService._completeNotification(): Abschluss-Notification (Erfolg/Fehler/Abbruch)
- Integration in downloadBatch(): Fortschritt pro Song, Abschluss nach Batch
- CloudScreen._upload(): Upload-Fortschritt + Abschluss-Notification
This commit is contained in:
Dustin
2026-08-02 16:04:09 +02:00
parent f23dc685cb
commit b8c206e58f
7 changed files with 228 additions and 1 deletions
+26
View File
@@ -4,6 +4,7 @@ import 'dart:io' show Platform;
import 'package:audio_service/audio_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'database/db_helper.dart';
import 'services/favoriten_service.dart';
import 'services/auth_service.dart';
@@ -15,12 +16,37 @@ import 'utils/farb_theme.dart';
import 'screens/home_screen.dart';
import 'screens/login_screen.dart';
/// Globaler Notification-Plugin (wird in main() initialisiert)
final FlutterLocalNotificationsPlugin notificationsPlugin =
FlutterLocalNotificationsPlugin();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Logger startet sofort zeichnet ALLES auf
MeloLogger().init('2.35');
// ── Notifications initialisieren (Issue #9) ──
const androidInit = AndroidInitializationSettings('@mipmap/ic_launcher');
const initSettings = InitializationSettings(android: androidInit);
await notificationsPlugin.initialize(
settings: initSettings,
);
// Notification-Channel für Downloads (Fortschritt + Abschluss)
const downloadChannel = AndroidNotificationChannel(
'de.baka.melo.downloads',
'Melo Downloads',
description: 'Download-Fortschritt und Abschluss',
importance: Importance.low,
playSound: false,
enableVibration: false,
);
await notificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(downloadChannel);
try {
await DbHelper().db;
await FavoritenService().init();
+56 -1
View File
@@ -3,11 +3,13 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import '../utils/farb_theme.dart';
import '../services/cloud_service.dart';
import '../services/auth_service.dart';
import '../database/db_helper.dart';
import '../services/melo_logger.dart';
import '../main.dart'; // notificationsPlugin
/// Melo Cloud Sync Screen v3 — vollständiges Sync-System
/// Playlisten, Favoriten, Auto-Sync, Persistent Login, Benutzerdefinierte Namen
@@ -398,6 +400,9 @@ class _CloudScreenState extends State<CloudScreen> {
// ─── Upload / Download ───
static const String _cloudChannelId = 'de.baka.melo.downloads';
static const int _cloudNotifyId = 200;
Future<void> _upload() async {
setState(() => _ladt = true);
_setzeStatus('Suche lokale Songs...');
@@ -413,12 +418,59 @@ class _CloudScreenState extends State<CloudScreen> {
}
final files = dir.listSync().whereType<File>().where(
(f) => f.path.endsWith('.mp3') || f.path.endsWith('.m4a'));
final fileList = files.toList();
int count = 0;
for (final f in files) {
for (int i = 0; i < fileList.length; i++) {
final f = fileList[i];
_setzeStatus('Upload: ${f.path.split('/').last}...');
// Progress-Notification
if (Platform.isAndroid) {
notificationsPlugin.show(
id: _cloudNotifyId,
title: 'Melo Cloud Upload',
body: '${i + 1} / ${fileList.length}: ${f.path.split('/').last}',
notificationDetails: NotificationDetails(
android: AndroidNotificationDetails(
_cloudChannelId,
'Melo Downloads',
channelDescription: 'Cloud Upload-Fortschritt',
importance: Importance.low,
priority: Priority.low,
onlyAlertOnce: true,
showProgress: true,
maxProgress: fileList.length,
progress: i,
ongoing: true,
autoCancel: false,
),
),
);
}
final sid = await widget.cloud.upload(f.path, f.path.split('/').last);
if (sid != null) count++;
}
// Abschluss-Notification
if (Platform.isAndroid) {
notificationsPlugin.cancel(id: _cloudNotifyId);
final ok = count > 0;
notificationsPlugin.show(
id: _cloudNotifyId + 1,
title: ok ? 'Cloud Upload fertig' : 'Cloud Upload fehlgeschlagen',
body: ok
? '$count / ${fileList.length} Songs hochgeladen ✅'
: 'Kein Song konnte hochgeladen werden ❌',
notificationDetails: NotificationDetails(
android: AndroidNotificationDetails(
_cloudChannelId,
'Melo Downloads',
channelDescription: 'Cloud Upload-Abschluss',
importance: Importance.defaultImportance,
priority: Priority.defaultPriority,
autoCancel: true,
),
),
);
}
await _ladeStatus();
if (mounted) {
setState(() => _ladt = false);
@@ -427,6 +479,9 @@ class _CloudScreenState extends State<CloudScreen> {
}
} catch (e) {
MeloLogger().fehler('cloud_upload_path', e);
if (Platform.isAndroid) {
notificationsPlugin.cancel(id: _cloudNotifyId);
}
if (mounted) {
setState(() {
_ladt = false;
+70
View File
@@ -4,14 +4,17 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import '../models/song.dart';
import '../database/db_helper.dart';
import '../utils/audio_validator.dart';
import 'melo_logger.dart';
import '../config/app_config.dart';
import '../main.dart'; // für notificationsPlugin
/// Download-Service: Lädt YouTube-Audio über den yt-proxy herunter.
/// Nutzt ChangeNotifier für UI-Updates via ListenableBuilder.
/// Issue #9: Fortschritt per System-Notification (ProgressBar).
class DownloadService extends ChangeNotifier {
static final http.Client _client = http.Client();
@@ -23,6 +26,10 @@ class DownloadService extends ChangeNotifier {
static String get _apiKey => AppConfig.ytProxyApiKey;
static Map<String, String> get _authHeader => {'X-API-Key': _apiKey};
static const String _channelId = 'de.baka.melo.downloads';
static const int _notifyProgressId = 100;
static const int _notifyCompleteId = 101;
final DbHelper _db = DbHelper();
bool _ladt = false;
@@ -91,12 +98,14 @@ class DownloadService extends ChangeNotifier {
int erfolgreich = 0;
_aktuellerTitel = '0/${urls.length} Starte...';
notifyListeners();
_showProgress(0, urls.length);
for (int i = 0; i < urls.length; i++) {
if (sollAbbrechen) break;
_aktuellerTitel = '${i + 1}/${urls.length} ${urls[i]._titelKurz()}';
notifyListeners();
_showProgress(i, urls.length, titel: urls[i]._titelKurz());
MeloLogger().zustand('download_einzeln_start', {
'index': i,
@@ -137,6 +146,7 @@ class DownloadService extends ChangeNotifier {
_aktuellerTitel = '$erfolgreich/${urls.length} Songs geladen';
}
notifyListeners();
_completeNotification(erfolgreich, urls.length, abgebrochen: sollAbbrechen);
MeloLogger().zustand('download_batch_ende', {
'erfolgreich': erfolgreich,
'gesamt': urls.length,
@@ -458,6 +468,66 @@ class DownloadService extends ChangeNotifier {
return _downloadEinzeln(song.ytUrl!);
}
// ─── #9: Notification-Progress ─────────────────
/// Zeigt oder aktualisiert eine Fortschritts-Notification.
/// [current] = aktueller Index (1-basiert), [total] = Gesamtanzahl.
void _showProgress(int current, int total, {String? titel}) {
if (!Platform.isAndroid) return;
notificationsPlugin.show(
id: _notifyProgressId,
title: 'Melo lädt herunter',
body: titel ?? '$current / $total Songs',
notificationDetails: NotificationDetails(
android: AndroidNotificationDetails(
_channelId,
'Melo Downloads',
channelDescription: 'Download-Fortschritt',
importance: Importance.low,
priority: Priority.low,
onlyAlertOnce: true,
showProgress: true,
maxProgress: total,
progress: current,
ongoing: true,
autoCancel: false,
),
),
);
}
/// Zeigt Abschluss-Notification (Erfolg oder Fehler).
void _completeNotification(int erfolgreich, int gesamt, {bool abgebrochen = false}) {
if (!Platform.isAndroid) return;
// Fortschritts-Notification abbrechen
notificationsPlugin.cancel(id: _notifyProgressId);
final titel = abgebrochen
? 'Download abgebrochen'
: (erfolgreich > 0 ? 'Download fertig' : 'Download fehlgeschlagen');
final body = abgebrochen
? '$erfolgreich / $gesamt Songs geladen'
: (erfolgreich > 0
? '$erfolgreich / $gesamt Songs geladen ✅'
: 'Kein Song konnte geladen werden ❌');
notificationsPlugin.show(
id: _notifyCompleteId,
title: titel,
body: body,
notificationDetails: NotificationDetails(
android: AndroidNotificationDetails(
_channelId,
'Melo Downloads',
channelDescription: 'Download-Abschluss',
importance: Importance.defaultImportance,
priority: Priority.defaultPriority,
autoCancel: true,
),
),
);
}
@override
void dispose() {
abbrechen();