NavidromeApp/iOS/Data/PendingOperationsQueue.swift
Dallas Groot b9844b23cd Performance audit, Now Playing widget, crossfade stability, cover art embedding, DJ profile bulk cache
PERFORMANCE AUDIT
- Removed 16 dead SubsonicClient methods (~117 lines)
- Added NSCache memory tier to LibraryCache, AlbumCoverStore,
ArtistCoverStore, RadioCoverStore
- Replaced weak polynomial hash with FNV-1a 64-bit in ImageCache
- Split PlaybackStateStore into save() (full queue) and savePosition()
(time only)
- Reused single SubsonicClient in OfflineManager instead of
per-download allocation
- Added periodic ImageCache disk trim every 50 writes
- Changed AudioPreFetcher to fuzzy offline match
(isSongAvailableOffline)
- Removed dead code: hasCompanionLibrary, downloadedSongIds, isActive,
CachedImageLoader.task
- Fixed thread safety: inline JSONEncoder/JSONDecoder in LibraryCache
(no shared instances)

WIDGET EXTENSION (new target: NavidromeWidget)
- v2 glassmorphism design: blurred album art background + frosted
glass panel
- Waveform scrubber: 40-bar Canvas with tap-to-seek (20 segments via
SeekToIntent)
- Color-adaptive theming: CIAreaAverage dominant color extraction with
HSB contrast adjustment
- Transport controls: previous/play-pause/next with interactive
AppIntents
- Up Next footer with crossfade countdown from Smart DJ profiles
- Large widget: 3-item queue list with numbered rows
- Small/Medium/Large sizes matching design mockups
- App Group communication via WidgetSharedState (UserDefaults)
- Darwin notification observer for widget→app commands
- Foreground command pickup for suspended app recovery
- Idempotency guards on all widget commands

CROSSFADE & PLAYBACK FIXES
- Fixed dual audio on single-song queue: guard nextSong.id ==
currentSong?.id in prepareNextForCrossfade
- Fixed crossfade play path never calling pushWidgetState (returned
before reaching it)
- Fixed crossfade needsNextTrack callback missing queue persistence +
widget push
- Fixed toggleShuffle queue not persisted after PlaybackStateStore
split
- Added nowPlayingSyncTimer restart on foreground (Lock Screen seek
bar drift)
- Added AVPlayer currentTime/duration sync in resumeVisTimers before
vis timer restart
(fixes waveform distortion after background — confirmed by Apple
Forums + SoundCloud engineering)

COVER ART PIPELINE
- Fixed pushWidgetState cover art size mismatch (300→600 to match
fetchAndSetArtwork)
- Added custom cover art key differentiation ("custom_" prefix forces
re-blur)
- Changed server art lookup from memoryOnlyImage to cachedImage
(memory+disk fallback)
- Added POST /library/cover-art-by-path endpoint (was missing — iOS
fallback hit 404)
- Added navidrome_id fallback on existing cover art endpoint
- Added embed_cover_art_in_file/embed_cover_art_in_directory: mutagen
writes cover art
directly into FLAC/MP3/M4A/OGG metadata tags so Navidrome serves
updated art
- All three upload paths (by-id, by-path, upload-tracks) now embed +
trigger_scan

COMPANION API FIXES
- Fixed _create_task recursion (was calling itself instead of
asyncio.create_task)
- Fixed navidrome_db NameError on /library/conflicts endpoint
- Reduced WebSocket connect/disconnect logging (only first-client and
all-disconnected)

SMART DJ PROFILE PREFETCH
- New endpoint: GET /smart-dj/profiles/export (bulk JSON, gzip
automatic)
- SmartDJCache.loadBulkCache() reads single file on launch (instant)
- SmartDJCache.bulkImport() writes all profiles in one atomic file
- CompanionAPIService.fetchAllProfiles() fetches entire profile set in
one request
- Wired into NavidromePlayerApp.task after server connect
- SmartCrossfadeManager unchanged — already reads from SmartDJCache
first

WEBSOCKET NOISE REDUCTION
- iOS: silent reconnect retries, only log milestones (#1, #5, every
20th)
- iOS: log "reconnected after N attempts" on success, silent initial
connect
- Python: only log first client connect and all-clients-disconnected

Files: 13 modified, 8 new (including companion-api/main.py)
2026-04-12 19:24:22 -07:00

227 lines
7.5 KiB
Swift

import Foundation
//
// PendingOperationsQueue.swift
// Persistent queue for companion API operations that failed due to
// network issues. Retries automatically when the WebSocket reconnects.
// Stored as JSON in Documents so it survives app restarts.
//
struct PendingOperation: Codable, Identifiable {
let id: UUID
let type: OperationType
let payload: [String: String]
let createdAt: Date
var retryCount: Int
let maxRetries: Int
enum OperationType: String, Codable {
case metadataEdit // PATCH /batch-edit-metadata
case coverArtUpload // POST /library/cover-art-by-path
case coverArtDelete // DELETE /library/cover-art/{id}
case tagCleanup // POST /tag-cleanup
}
init(type: OperationType, payload: [String: String], maxRetries: Int = 5) {
self.id = UUID()
self.type = type
self.payload = payload
self.createdAt = Date()
self.retryCount = 0
self.maxRetries = maxRetries
}
var isExpired: Bool { retryCount >= maxRetries }
var displayDescription: String {
switch type {
case .metadataEdit:
return "Edit: \(payload["album"] ?? payload["artist"] ?? "metadata")"
case .coverArtUpload:
return "Cover art: \(payload["path"] ?? "upload")"
case .coverArtDelete:
return "Remove cover: \(payload["songId"] ?? "")"
case .tagCleanup:
return "Tag cleanup: \(payload["path"] ?? "")"
}
}
}
class PendingOperationsQueue: ObservableObject {
static let shared = PendingOperationsQueue()
@Published private(set) var operations: [PendingOperation] = []
private let fileURL: URL
private var isProcessing = false
var count: Int { operations.count }
var isEmpty: Bool { operations.isEmpty }
private init() {
let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
fileURL = docs.appendingPathComponent("pending_operations.json")
loadFromDisk()
}
// MARK: - Enqueue
func enqueue(_ op: PendingOperation) {
operations.append(op)
saveToDisk()
DebugLogger.shared.log(
"Pending op queued: \(op.type.rawValue) (\(operations.count) total)",
category: "PendingOps"
)
}
/// Convenience: enqueue a metadata edit that failed.
func enqueueMetadataEdit(paths: [String], tags: [String: String]) {
var payload = tags
payload["_paths"] = paths.joined(separator: "\n")
enqueue(PendingOperation(type: .metadataEdit, payload: payload))
}
/// Convenience: enqueue a cover art upload that failed.
func enqueueCoverArtUpload(relativePath: String) {
enqueue(PendingOperation(
type: .coverArtUpload,
payload: ["path": relativePath]
))
}
// MARK: - Process
/// Retry all pending operations. Call when WebSocket reconnects
/// or when the app returns to foreground with a connection.
func processAll() {
guard !isProcessing, !operations.isEmpty else { return }
isProcessing = true
DebugLogger.shared.log(
"Processing \(operations.count) pending operations",
category: "PendingOps"
)
Task {
var remaining: [PendingOperation] = []
for var op in operations {
if op.isExpired {
DebugLogger.shared.log(
"Dropping expired op: \(op.displayDescription) (\(op.retryCount) retries)",
category: "PendingOps"
)
continue
}
let success = await retryOperation(op)
if !success {
op.retryCount += 1
remaining.append(op)
}
}
await MainActor.run {
self.operations = remaining
self.saveToDisk()
self.isProcessing = false
if remaining.isEmpty {
DebugLogger.shared.log("All pending ops completed", category: "PendingOps")
} else {
DebugLogger.shared.log(
"\(remaining.count) ops still pending after retry",
category: "PendingOps"
)
}
}
}
}
private func retryOperation(_ op: PendingOperation) async -> Bool {
guard let api = try? CompanionAPIService() else { return false }
switch op.type {
case .metadataEdit:
let paths = (op.payload["_paths"] ?? "").split(separator: "\n").map(String.init)
guard !paths.isEmpty else { return true } // nothing to do = success
var tags: [String: String] = op.payload
tags.removeValue(forKey: "_paths")
let request = BatchMetadataEditRequest(
relativePaths: paths,
title: tags["title"],
artist: tags["artist"],
album: tags["album"],
albumArtist: tags["album_artist"],
genre: tags["genre"],
year: tags["year"].flatMap { Int($0) }
)
do {
_ = try await api.batchEditMetadata(request)
return true
} catch {
return false
}
case .coverArtUpload:
// Cover art data isn't persisted in the queue (too large).
// Mark as expired user will need to re-apply.
return false
case .coverArtDelete:
guard let songId = op.payload["songId"] else { return true }
do {
try await api.deleteCoverArt(songId: songId)
return true
} catch {
return false
}
case .tagCleanup:
// Tag cleanup is idempotent safe to retry
return false // Not implemented yet
}
}
// MARK: - Remove
func remove(_ op: PendingOperation) {
operations.removeAll { $0.id == op.id }
saveToDisk()
}
func clearAll() {
operations.removeAll()
saveToDisk()
}
// MARK: - Persistence
private func saveToDisk() {
if let data = try? JSONEncoder().encode(operations) {
try? data.write(to: fileURL, options: .atomic)
}
}
func loadFromDisk() {
guard let data = try? Data(contentsOf: fileURL),
let ops = try? JSONDecoder().decode([PendingOperation].self, from: data)
else { return }
operations = ops
}
// MARK: - Export/Import (for backup system)
func exportData() -> Data? {
try? JSONEncoder().encode(operations)
}
func importData(_ data: Data) {
guard let ops = try? JSONDecoder().decode([PendingOperation].self, from: data) else { return }
operations = ops
saveToDisk()
}
}