Implement remote library metadata sync for iPhone
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import Foundation
|
||||
import VelodyDomain
|
||||
|
||||
public actor RemoteLibrarySyncService {
|
||||
private let repository: any RemoteLibraryRepository
|
||||
|
||||
public init(repository: any RemoteLibraryRepository) {
|
||||
self.repository = repository
|
||||
}
|
||||
|
||||
public func loadCachedRemoteTracks() async throws -> [RemoteTrack] {
|
||||
try await repository.loadCachedRemoteTracks()
|
||||
}
|
||||
|
||||
public func syncRemoteLibrary(deviceId: String) async throws -> [RemoteTrack] {
|
||||
try await repository.syncRemoteTracks(deviceId: deviceId)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user