NavidromeApp/iOS/App/NavidromePlayerApp.swift
Dallas Groot 7413163d57 Live lyrics system — full implementation
Companion API (4 new endpoints):
- GET /lyrics/search — proxy to LRCLIB, returns matches with sync status
- GET /lyrics/fetch — exact match by artist/title/duration, caches in SQLite
- GET /lyrics/get — checks embedded tags > .lrc sidecar > DB cache
- POST /lyrics/embed — writes LRC into audio file tags via mutagen + .lrc sidecar
- New lyrics table in companion database

iOS data layer (LyricsModels.swift):
- LyricLine/LyricWord structs with Codable conformance
- Full LRC parser (line-level + enhanced word-level timestamps)
- LRC serializer (toLRC) for saving edited timings
- LyricsCache with memory + disk tiers keyed by artist-title

iOS manager (LyricsManager.swift):
- Source pipeline: embedded > .lrc sidecar > local cache > LRCLIB auto-fetch
- Binary search line/word tracking (O(log n))
- Word-level progress for karaoke gradient fill
- Hooked into AudioPlayer time observer (0.5s sync) and pushWidgetState (song change)

iOS views:
- LyricsOverlayView: karaoke word-by-word highlighting via FlowLayout + KaraokeWordView
  gradient fill sweep, ScrollViewReader auto-scroll, tap any line to seek
- LyricsSearchSheet: LRCLIB search with debounced input, duration mismatch warnings,
  preview sheet, import to cache
- LyricsEditorView: tap-to-sync mode, per-line ±0.1s adjust buttons, global offset
  sheet, save to device or embed in file via Companion API

NowPlayingView integration:
- Lyrics toggle button (quote.bubble) in bottom controls bar
- Portrait layout swaps album art for lyrics overlay with animation
- Blur panel: .ultraThinMaterial with gradient mask so visualizer fades through
- Lyrics search/editor sheets wired with notification observer

Mini player lyric ticker:
- Both standard and compact mini player bars show current lyric line
- Accent pink with ♪ prefix, falls back to artist name when no lyrics
- Push transition animation on line changes

Bug fixes included:
- NavidromePlayerApp: removed unnecessary try on CompanionAPIService.shared
- Info.plist: added LSSupportsOpeningDocumentsInPlace for document type support
2026-04-13 09:01:30 -07:00

211 lines
8.8 KiB
Swift

import SwiftUI
import BackgroundTasks
// MARK: - App Delegate (Background Sessions + BGTask Registration)
class AppDelegate: NSObject, UIApplicationDelegate {
static let smartDJTaskId = "com.navidromeplayer.smartdj.refresh"
static let syncTaskId = "com.navidromeplayer.library.sync"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
// Register background tasks
BGTaskScheduler.shared.register(forTaskWithIdentifier: Self.smartDJTaskId, using: nil) { task in
self.handleSmartDJRefresh(task: task as! BGAppRefreshTask)
}
BGTaskScheduler.shared.register(forTaskWithIdentifier: Self.syncTaskId, using: nil) { task in
self.handleLibrarySync(task: task as! BGProcessingTask)
}
return true
}
/// Return a custom scene configuration so NavidromeSceneDelegate can provide
/// NavidromeHostingController giving us UIKit-level preferredStatusBarStyle
/// control without touching SwiftUI's colorScheme environment.
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
let config = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
config.delegateClass = NavidromeSceneDelegate.self
return config
}
func application(
_ application: UIApplication,
handleEventsForBackgroundURLSession identifier: String,
completionHandler: @escaping () -> Void
) {
if identifier == "com.navidromeplayer.batchupload" {
ZipImportManager.shared.setBackgroundCompletionHandler(completionHandler)
DebugLogger.shared.log("Background session woke app: \(identifier)", category: "Upload")
}
}
// MARK: - Smart DJ Background Refresh
private func handleSmartDJRefresh(task: BGAppRefreshTask) {
Self.scheduleSmartDJRefresh() // re-schedule next run immediately
let refreshTask = Task {
// Pre-fetch profiles using the shared singleton no per-song URLSession leak
if CompanionSettings.shared.isEnabled,
CompanionSettings.shared.smartDJEnabled {
let queue = AudioPlayer.shared.queue
let idx = AudioPlayer.shared.queueIndex
let upcoming = Array(queue.dropFirst(idx + 1).prefix(5))
let api = CompanionAPIService.shared
for song in upcoming {
guard let path = song.path else { continue }
_ = try? await api.fetchProfile(relativePath: path)
}
}
// Await the actual sync BGTask stays alive until work completes
await SyncEngine.shared.syncAndWait()
OptimisticActionQueue.shared.flush()
task.setTaskCompleted(success: true)
DebugLogger.shared.log("BGTask SmartDJ refresh completed", category: "Sync", level: .info)
}
task.expirationHandler = {
refreshTask.cancel()
task.setTaskCompleted(success: false)
DebugLogger.shared.log("BGTask SmartDJ refresh expired", category: "Sync", level: .warning)
}
}
private func handleLibrarySync(task: BGProcessingTask) {
let syncTask = Task {
// Await real completion previously fired setTaskCompleted immediately
// before any sync work started, causing iOS to kill the process (AUDIT-044)
await SyncEngine.shared.syncAndWait()
task.setTaskCompleted(success: true)
DebugLogger.shared.log("BGTask library sync completed", category: "Sync", level: .info)
}
task.expirationHandler = {
syncTask.cancel()
task.setTaskCompleted(success: false)
DebugLogger.shared.log("BGTask library sync expired", category: "Sync", level: .warning)
}
}
static func scheduleSmartDJRefresh() {
let request = BGAppRefreshTaskRequest(identifier: smartDJTaskId)
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60) // 15 min
try? BGTaskScheduler.shared.submit(request)
}
static func scheduleLibrarySync() {
let request = BGProcessingTaskRequest(identifier: syncTaskId)
request.requiresNetworkConnectivity = true
request.earliestBeginDate = Date(timeIntervalSinceNow: 60 * 60) // 1 hour
try? BGTaskScheduler.shared.submit(request)
}
}
@main
struct NavidromePlayerApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject private var serverManager = ServerManager.shared
@StateObject private var audioPlayer = AudioPlayer.shared
@StateObject private var offlineManager = OfflineManager.shared
// Initialize WatchConnectivity so sync works immediately
private let watchConnectivity = WatchConnectivityManager.shared
init() {
// Dismiss keyboard when scrolling any scroll view
UIScrollView.appearance().keyboardDismissMode = .interactive
}
var body: some Scene {
WindowGroup {
RootView()
.environmentObject(serverManager)
.environmentObject(audioPlayer)
.environmentObject(offlineManager)
.tint(Color(red: 1.0, green: 0.176, blue: 0.333))
}
}
}
struct RootView: View {
@EnvironmentObject var serverManager: ServerManager
var body: some View {
Group {
if serverManager.servers.isEmpty {
// No servers configured show login
LoginView()
} else {
// Servers exist show main UI immediately (connects in background)
MainTabView()
}
}
.dismissKeyboardOnTap()
.task {
ImageCache.shared.trimDiskCache()
AudioPreFetcher.shared.cleanOldPrefetches(keeping: AudioPlayer.shared.queue)
WidgetBridge.shared.start()
if serverManager.activeServer != nil {
await serverManager.connectToActive()
// Background sync fills LibraryCache so UI never waits for network
SyncEngine.shared.syncIfNeeded()
// Flush any pending optimistic actions (star/unstar that failed offline)
OptimisticActionQueue.shared.flush()
// Bulk-fetch all Smart DJ profiles in one request (~30KB gzipped).
// Populates SmartDJCache so crossfade/volume data is instant for every song.
if CompanionSettings.shared.smartDJEnabled {
SmartDJCache.shared.loadBulkCache() // disk memory (instant)
Task.detached(priority: .utility) {
do {
let api = CompanionAPIService.shared
let profiles = try await api.fetchAllProfiles()
SmartDJCache.shared.bulkImport(profiles)
} catch {
DebugLogger.shared.log(
"DJ profile bulk fetch failed: \(error.localizedDescription)",
category: "SmartDJ"
)
}
}
}
// Restore queue from last session if the app was killed by iOS.
// Only restore if no song is already playing (e.g. from a widget tap).
// Restoration starts paused at the saved position user taps Play.
if AudioPlayer.shared.currentSong == nil,
let saved = PlaybackStateStore.shared.load() {
let player = AudioPlayer.shared
// Load queue without starting playback
player.queue = saved.queue
player.queueIndex = saved.index
player.currentSong = saved.currentSong
player.currentTime = saved.currentTime
// Duration will populate when the player item loads
DebugLogger.shared.log(
"Restored queue: \(saved.queue.count) songs, index \(saved.index), t=\(Int(saved.currentTime))s",
category: "Audio", level: .info
)
}
}
// Connect Companion push client if enabled
if CompanionSettings.shared.isEnabled {
CompanionPushClient.shared.connect()
}
// Schedule background tasks
AppDelegate.scheduleSmartDJRefresh()
AppDelegate.scheduleLibrarySync()
}
}
}