diff --git a/furumi_macos/CatalogService.swift b/furumi_macos/CatalogService.swift index 161c963..a4d15a7 100644 --- a/furumi_macos/CatalogService.swift +++ b/furumi_macos/CatalogService.swift @@ -190,6 +190,12 @@ struct CatalogService { _ = try await post(path: "api/player/devices/command", body: body) } + func lastfmNowPlaying(trackId: Int64) async throws { + struct Body: Encodable { let trackId: Int64 } + let body = try Self.deviceEncoder.encode(Body(trackId: trackId)) + _ = try await post(path: "api/player/lastfm/now-playing", body: body) + } + func recordHistory(trackId: Int64, startedAt: Int64?, durationListened: Int32, completed: Bool) async throws { struct Body: Encodable { let trackId: Int64 diff --git a/furumi_macos/ContentView.swift b/furumi_macos/ContentView.swift index c816c3a..22fbc76 100644 --- a/furumi_macos/ContentView.swift +++ b/furumi_macos/ContentView.swift @@ -106,6 +106,9 @@ struct ContentView: View { Task { await authManager.loadLikedIds() } deviceManager.start(service: service, player: player) let am = authManager + player.onTrackStarted = { trackId in + try? await am.catalogService?.lastfmNowPlaying(trackId: trackId) + } player.onTrackFinished = { trackId, startedAt, durationListened, completed in guard let svc = am.catalogService else { return } try? await svc.recordHistory(trackId: trackId, startedAt: startedAt, durationListened: durationListened, completed: completed) diff --git a/furumi_macos/PlayerManager.swift b/furumi_macos/PlayerManager.swift index a6a013c..cd7bafc 100644 --- a/furumi_macos/PlayerManager.swift +++ b/furumi_macos/PlayerManager.swift @@ -37,6 +37,7 @@ final class PlayerManager { var canGoPrevious: Bool { currentIndex > 0 } var formattedCurrentTime: String { formatTime(currentTime) } + var onTrackStarted: ((Int64) async -> Void)? var onTrackFinished: ((Int64, Int64?, Int32, Bool) async -> Void)? private var player: AVPlayer? @@ -102,6 +103,8 @@ final class PlayerManager { addObserver() updateNowPlayingInfo(track: track) + let cb = onTrackStarted + Task { await cb?(track.id) } } // MARK: - Public playback