velody/packages/apple/VelodySync/Sources/VelodySync/RemoteLibraryRepository.swift

34 lines
1.0 KiB
Swift

import Foundation
import VelodyDomain
import VelodyNetworking
import VelodyPersistence
public protocol RemoteLibraryRepository: Actor {
func loadCachedRemoteTracks() async throws -> [RemoteTrack]
func syncRemoteTracks(deviceId: String) async throws -> [RemoteTrack]
}
public actor DefaultRemoteLibraryRepository: RemoteLibraryRepository {
private let apiClient: any VelodyAPIClient
private let store: any RemoteLibraryStore
public init(
apiClient: any VelodyAPIClient,
store: any RemoteLibraryStore
) {
self.apiClient = apiClient
self.store = store
}
public func loadCachedRemoteTracks() async throws -> [RemoteTrack] {
try await store.loadRemoteTracks()
}
public func syncRemoteTracks(deviceId: String) async throws -> [RemoteTrack] {
let response = try await apiClient.fetchRemoteLibrary(deviceId: deviceId)
let tracks = response.tracks.map(\.remoteTrack)
try await store.replaceRemoteTracks(tracks)
return tracks
}
}