Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes for downloading #23

Merged
merged 9 commits into from
Jul 19, 2024
50 changes: 26 additions & 24 deletions Core/Core/Network/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ public class DownloadManager: DownloadManagerProtocol {

public func cancelDownloading(courseId: String, blocks: [CourseBlock]) async throws {
downloadRequest?.cancel()
let downloaded = await getDownloadTasksForCourse(courseId).filter { $0.state == .finished }
let downloaded = await getDownloadTasksForCourse(courseId)
let blocksForDelete = blocks.filter { block in
downloaded.first(where: { $0.blockId == block.id }) == nil
downloaded.first(where: { $0.blockId == block.id }) != nil
}
await deleteFile(blocks: blocksForDelete)
downloaded.forEach {
Expand All @@ -238,10 +238,10 @@ public class DownloadManager: DownloadManagerProtocol {
public func cancelDownloading(task: DownloadDataTask) async throws {
downloadRequest?.cancel()
do {
try await persistence.deleteDownloadDataTask(id: task.id)
if let fileUrl = await fileUrl(for: task.id) {
if let fileUrl = fileUrl(for: task.id) {
try FileManager.default.removeItem(at: fileUrl)
}
try await persistence.deleteDownloadDataTask(id: task.id)
currentDownloadEventPublisher.send(.canceled(task))
} catch {
NSLog("Error deleting file: \(error.localizedDescription)")
Expand All @@ -268,7 +268,8 @@ public class DownloadManager: DownloadManagerProtocol {
public func deleteFile(blocks: [CourseBlock]) async {
for block in blocks {
do {
if let fileURL = await fileUrl(for: block.id) {
if let fileURL = fileUrl(for: block.id),
FileManager.default.fileExists(atPath: fileURL.path) {
try FileManager.default.removeItem(at: fileURL)
}
try await persistence.deleteDownloadDataTask(id: block.id)
Expand All @@ -282,7 +283,7 @@ public class DownloadManager: DownloadManagerProtocol {
public func deleteAllFiles() async {
let downloadsData = await getDownloadTasks()
for downloadData in downloadsData {
if let fileURL = await fileUrl(for: downloadData.id) {
if let fileURL = fileUrl(for: downloadData.id) {
do {
try FileManager.default.removeItem(at: fileURL)
} catch {
Expand Down Expand Up @@ -333,7 +334,7 @@ public class DownloadManager: DownloadManagerProtocol {
}

private func downloadFileWithProgress(_ download: DownloadDataTask) throws {
guard let url = URL(string: download.url) else {
guard let url = URL(string: download.url), let folderURL = self.videosFolderUrl else {
return
}

Expand All @@ -343,10 +344,14 @@ public class DownloadManager: DownloadManagerProtocol {
resumeData: download.resumeData
)
self.isDownloadingInProgress = true
let destination: DownloadRequest.Destination = { _, _ in
let file = folderURL.appendingPathComponent(download.fileName)
return (file, [.createIntermediateDirectories, .removePreviousFile])
}
if let resumeData = download.resumeData {
downloadRequest = AF.download(resumingWith: resumeData)
downloadRequest = AF.download(resumingWith: resumeData, to: destination)
} else {
downloadRequest = AF.download(url)
downloadRequest = AF.download(url, to: destination)
}

downloadRequest?.downloadProgress { [weak self] prog in
Expand All @@ -359,20 +364,17 @@ public class DownloadManager: DownloadManagerProtocol {
debugLog(">>>>> Downloading", download.url, completed, "%")
}

downloadRequest?.responseData { [weak self] data in
downloadRequest?.responseData { [weak self] _ in
guard let self else { return }
if let data = data.value, let url = self.videosFolderUrl {
self.saveFile(fileName: download.fileName, data: data, folderURL: url)
self.persistence.updateDownloadState(
id: download.id,
state: .finished,
resumeData: nil
)
self.currentDownloadTask?.state = .finished
self.currentDownloadEventPublisher.send(.finished(download))
Task {
try? await self.newDownload()
}
self.persistence.updateDownloadState(
id: download.id,
state: .finished,
resumeData: nil
)
self.currentDownloadTask?.state = .finished
self.currentDownloadEventPublisher.send(.finished(download))
Task {
try? await self.newDownload()
}
}
}
Expand All @@ -393,10 +395,10 @@ public class DownloadManager: DownloadManagerProtocol {
private func cancel(tasks: [DownloadDataTask]) async {
for task in tasks {
do {
try await persistence.deleteDownloadDataTask(id: task.id)
if let fileUrl = await fileUrl(for: task.id) {
if let fileUrl = fileUrl(for: task.id) {
try FileManager.default.removeItem(at: fileUrl)
}
try await persistence.deleteDownloadDataTask(id: task.id)
} catch {
debugLog("Error deleting file: \(error.localizedDescription)")
}
Expand Down
Loading