Herz-Tipp meldet den Favoriten sofort deterministisch an die Cloud

This commit is contained in:
Hermes (Server)
2026-08-27 11:23:38 +02:00
parent 5dc80931e2
commit 8d84a88a9c
3 changed files with 167 additions and 3 deletions
+30 -2
View File
@@ -1,4 +1,5 @@
import 'package:flutter/foundation.dart';
import '../services/melo_cloud_service.dart';
import '../services/navidrome_service.dart';
import 'database.dart';
@@ -6,11 +7,18 @@ import 'database.dart';
/// nach jeder Mutation (für Feedback wie SnackBars — die Listen selbst
/// beobachten UIs direkt über die watch()-Streams von [MeloDb]).
class PlaylistService extends ChangeNotifier {
PlaylistService(this.db, {NavidromeService? navidrome})
: _navidrome = navidrome ?? NavidromeService();
// `this._cloud` würde den öffentlichen Parameternamen `cloud` zu `_cloud`
// (privat) ändern und die API brechen.
PlaylistService(this.db, {NavidromeService? navidrome, MeloCloudService? cloud})
: _navidrome = navidrome ?? NavidromeService(),
// ignore: prefer_initializing_formals
_cloud = cloud;
final MeloDb db;
final NavidromeService _navidrome;
/// Optional: ohne Cloud-Zugang entfällt der Sofort-Push stillschweigend.
final MeloCloudService? _cloud;
Future<String> createPlaylist(String name, {String? description}) async {
final id = await db.createPlaylist(name, description: description);
notifyListeners();
@@ -45,6 +53,26 @@ class PlaylistService extends ChangeNotifier {
Future<void> toggleFavorite(String songId) async {
await db.toggleFavorite(songId);
notifyListeners();
await _meldeFavorit(songId);
}
/// Meldet den neuen Favoriten-Stand sofort an die Melo-Cloud.
///
/// Deterministisch (`set:true/false`), nicht als Umschalten: ein
/// abweichender Server-Stand darf den Wunsch nicht invertieren. Nur für
/// Titel mit cloudId, und Fehler werden still geschluckt — der nächste
/// Abgleich pusht additiv nach.
Future<void> _meldeFavorit(String songId) async {
final cloud = _cloud;
if (cloud == null || !cloud.istAngemeldet) return;
final cloudId = (await db.songById(songId))?.cloudId;
if (cloudId == null) return;
final gesetzt = await db.watchIsFavorite(songId).first;
try {
await cloud.setzeFavorit(cloudId, gesetzt);
} catch (e) {
debugPrint('Favorit nicht gemeldet: $e');
}
}
/// Lädt alle Favoriten vom Navidrome-Server und importiert sie lokal.
+6 -1
View File
@@ -51,7 +51,6 @@ Future<void> main() async {
await logger.init();
_db = MeloDb();
_library = LibraryService(_db);
_playlists = PlaylistService(_db);
_offlineMode = OfflineMode();
await _offlineMode.init();
_categories = CategoryService(_db);
@@ -83,6 +82,12 @@ Future<void> main() async {
cloud: MeloCloudService(auth: _bakaAuth),
);
await _sync.laden();
// Erst hier: der Sofort-Push braucht den angemeldeten Cloud-Zugang, und
// _bakaAuth entsteht weiter oben.
_playlists = PlaylistService(
_db,
cloud: MeloCloudService(auth: _bakaAuth),
);
final navidrome = NavidromeService();
await navidrome.ladeGespeicherteZugangsdaten();
_downloads = DownloadService(db: _db, navidrome: navidrome);