Add persistent local music catalog
This commit is contained in:
@@ -11,25 +11,38 @@ final class iPhoneLibraryViewModel {
|
||||
var tracks: [LibraryTrack] = []
|
||||
var syncStatus = "Offline library not synced yet"
|
||||
|
||||
private let store = InMemoryLocalLibraryStore()
|
||||
private let store: any LocalLibraryStore
|
||||
private let syncCoordinator: PlaceholderSyncCoordinator
|
||||
private var hasLoaded = false
|
||||
|
||||
init() {
|
||||
let repository = Self.makeTrackRepository()
|
||||
let environment = ServerEnvironment(
|
||||
baseURL: ServerEnvironment.defaultLocalBaseURL,
|
||||
appVersion: "0.1.0"
|
||||
)
|
||||
let apiClient = StubVelodyAPIClient(environment: environment)
|
||||
self.store = repository
|
||||
self.syncCoordinator = PlaceholderSyncCoordinator(
|
||||
apiClient: apiClient,
|
||||
store: store
|
||||
store: repository
|
||||
)
|
||||
}
|
||||
|
||||
func loadIfNeeded() async {
|
||||
guard !hasLoaded else { return }
|
||||
hasLoaded = true
|
||||
|
||||
do {
|
||||
let persistedTracks = try await store.loadTracks()
|
||||
if !persistedTracks.isEmpty {
|
||||
tracks = persistedTracks
|
||||
syncStatus = "Loaded \(persistedTracks.count) cached track(s) from local storage."
|
||||
}
|
||||
} catch {
|
||||
syncStatus = "Failed to load cached catalog: \(error.localizedDescription)"
|
||||
}
|
||||
|
||||
await refreshSync()
|
||||
}
|
||||
|
||||
@@ -42,4 +55,12 @@ final class iPhoneLibraryViewModel {
|
||||
syncStatus = "Sync placeholder failed: \(error.localizedDescription)"
|
||||
}
|
||||
}
|
||||
|
||||
private static func makeTrackRepository() -> any TrackRepository {
|
||||
if let repository = try? SwiftDataTrackRepository() {
|
||||
return repository
|
||||
}
|
||||
|
||||
return InMemoryTrackRepository()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user