Skip to content

Commit

Permalink
Update song download states via main actor task in harmonykit
Browse files Browse the repository at this point in the history
Fixes a bunch of weird bugs when the song download state is changed

Signed-off-by: Claudio Cambra <[email protected]>
  • Loading branch information
claucambra committed Sep 27, 2024
1 parent c4c3ff9 commit 32533a7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions HarmonyKit/Backend/Files/FilesBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public class FilesBackend: NSObject, Backend {
guard fileManager.isUbiquitousItem(at: song.url) else { return }
do {
Logger.filesBackend.debug("Fetching iCloud song: \(song.url)")
song.downloadState = DownloadState.downloading.rawValue
Task { @MainActor in song.downloadState = DownloadState.downloading.rawValue }
await startPollingDownloadState(forSong: song, endState: .downloaded)
try fileManager.startDownloadingUbiquitousItem(at: song.url)
} catch let error {
Expand Down Expand Up @@ -283,13 +283,13 @@ public class FilesBackend: NSObject, Backend {
// download
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
if self.ubiquitousFileIsDownloaded(url: song.url) {
song.downloadState = DownloadState.downloaded.rawValue
Task { @MainActor in song.downloadState = DownloadState.downloaded.rawValue }
if endState == URLUbiquitousItemDownloadingStatus.downloaded ||
endState == URLUbiquitousItemDownloadingStatus.current {
timer.invalidate()
}
} else {
song.downloadState = DownloadState.notDownloaded.rawValue
Task { @MainActor in song.downloadState = DownloadState.notDownloaded.rawValue }
if endState == URLUbiquitousItemDownloadingStatus.notDownloaded {
timer.invalidate()
}
Expand Down
12 changes: 8 additions & 4 deletions HarmonyKit/Backend/Nextcloud/NextcloudBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -512,17 +512,21 @@ public class NextcloudBackend: NSObject, Backend, NKCommonDelegate, URLSessionWe
serverUrlFileName: song.url,
fileNameLocalPath: localPath,
progressHandler: { progress in
song.downloadState = DownloadState.downloading.rawValue
song.downloadProgress = progress.fractionCompleted
Task { @MainActor in
song.downloadState = DownloadState.downloading.rawValue
song.downloadProgress = progress.fractionCompleted
}
},
completionHandler: { account, etag, date, length, allHeaderFields, nkError in
guard nkError == .success else {
Logger.ncBackend.error("Download error: \(nkError.errorDescription)")
song.downloadState = DownloadState.notDownloaded.rawValue
Task { @MainActor in
song.downloadState = DownloadState.notDownloaded.rawValue
}
continuation.resume()
return
}
song.downloadState = DownloadState.downloaded.rawValue
Task { @MainActor in song.downloadState = DownloadState.downloaded.rawValue }
Logger.ncBackend.debug("Successfully downloaded \(song.url) to \(localUrl)")
continuation.resume()
})
Expand Down

0 comments on commit 32533a7

Please sign in to comment.