v2.52 — Favoriten-Sync-Fix: bidirektionaler Merge (lokal↔Server), Toggle-Push sofort, cloud_id-Verdrahtung
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../database/db_helper.dart';
|
||||
import '../models/song.dart';
|
||||
@@ -11,6 +12,11 @@ class FavoritenService {
|
||||
final DbHelper _db = DbHelper();
|
||||
int? _favoritenPlaylistId;
|
||||
|
||||
/// Wird nach jedem erfolgreichen lokalen Toggle aufgerufen (Feuer-und-
|
||||
/// Vergessen → Server-Push). Wird in `main()` verdrahtet; Fehler werden
|
||||
/// beim nächsten vollständigen Sync korrigiert (favoritenMerge-Vereinigung).
|
||||
Future<void> Function(int songId, bool neuerStatus)? onStatusGeaendert;
|
||||
|
||||
int? get favoritenId => _favoritenPlaylistId;
|
||||
|
||||
Future<void> init() async {
|
||||
@@ -32,18 +38,32 @@ class FavoritenService {
|
||||
/// Schaltet den Favoriten-Status um und gibt den NEUEN Status zurück
|
||||
/// (true = jetzt Favorit). Bei Fehlern (z. B. DB-Race) wird der Zustand
|
||||
/// aus der DB neu geladen statt blind geflippt — kein unhandled Throw.
|
||||
/// Nach dem lokalen Toggle wird [onStatusGeaendert] Feuer-und-Vergessen
|
||||
/// aufgerufen (Server-Push; Retry = nächster vollständiger Sync).
|
||||
Future<bool> umschalten(int songId) async {
|
||||
if (_favoritenPlaylistId == null) return false;
|
||||
try {
|
||||
bool neuerStatus;
|
||||
if (await istFavorit(songId)) {
|
||||
await _db.songAusPlaylistEntfernen(_favoritenPlaylistId!, songId);
|
||||
return false;
|
||||
neuerStatus = false;
|
||||
} else {
|
||||
final songs = await _db.songsDerPlaylist(_favoritenPlaylistId!);
|
||||
// INSERT OR IGNORE in songZurPlaylist fängt Doppel-Tap-Races ab
|
||||
await _db.songZurPlaylist(_favoritenPlaylistId!, songId, songs.length);
|
||||
return true;
|
||||
neuerStatus = true;
|
||||
}
|
||||
final cb = onStatusGeaendert;
|
||||
if (cb != null) {
|
||||
unawaited(() async {
|
||||
try {
|
||||
await cb(songId, neuerStatus);
|
||||
} catch (e) {
|
||||
MeloLogger().fehler('favoriten_server_push', e);
|
||||
}
|
||||
}());
|
||||
}
|
||||
return neuerStatus;
|
||||
} catch (e) {
|
||||
debugPrint('Favoriten-Toggle fehlgeschlagen: $e');
|
||||
MeloLogger().fehler('favoriten_toggle', e);
|
||||
@@ -57,6 +77,31 @@ class FavoritenService {
|
||||
return _db.songsDerPlaylist(_favoritenPlaylistId!);
|
||||
}
|
||||
|
||||
/// Cloud-IDs aller lokalen ⭐-Favoriten (Push-Richtung lokal → Server).
|
||||
/// Songs ohne cloud_id (nicht hochgeladen) werden übersprungen.
|
||||
Future<Set<String>> favoritenCloudIds() async {
|
||||
if (_favoritenPlaylistId == null) return {};
|
||||
final songs = await _db.songsDerPlaylist(_favoritenPlaylistId!);
|
||||
return songs
|
||||
.map((s) => s.cloudId)
|
||||
.whereType<String>()
|
||||
.where((c) => c.isNotEmpty)
|
||||
.toSet();
|
||||
}
|
||||
|
||||
/// Markiert einen Server-Favoriten (cloud_id) lokal als ⭐-Favorit
|
||||
/// (Richtung Server → lokal). Fehlt der Song lokal, passiert nichts —
|
||||
/// der Favorit bleibt in `server_favorites` fürs Dashboard erhalten.
|
||||
Future<void> merkeCloudFavorit(String cloudId) async {
|
||||
if (_favoritenPlaylistId == null) return;
|
||||
final song = await _db.songNachCloudId(cloudId);
|
||||
final id = song?.id;
|
||||
if (id == null) return;
|
||||
if (await istFavorit(id)) return;
|
||||
final songs = await _db.songsDerPlaylist(_favoritenPlaylistId!);
|
||||
await _db.songZurPlaylist(_favoritenPlaylistId!, id, songs.length);
|
||||
}
|
||||
|
||||
Future<int> anzahlFavoriten() async {
|
||||
if (_favoritenPlaylistId == null) await init();
|
||||
if (_favoritenPlaylistId == null) return 0;
|
||||
|
||||
Reference in New Issue
Block a user