Skip to content

Commit

Permalink
chore: cancel downloading if log out and title fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eyatsenkoperpetio committed Feb 16, 2024
1 parent 6414890 commit 6688cd3
Show file tree
Hide file tree
Showing 15 changed files with 1,987 additions and 291 deletions.
358 changes: 318 additions & 40 deletions Authorization/AuthorizationTests/AuthorizationMock.generated.swift

Large diffs are not rendered by default.

59 changes: 39 additions & 20 deletions Core/Core/Network/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,24 @@ public protocol DownloadManagerProtocol {
func publisher() -> AnyPublisher<Int, Never>
func eventPublisher() -> AnyPublisher<DownloadManagerEvent, Never>

func addToDownloadQueue(blocks: [CourseBlock]) throws

func getDownloadTasks() async -> [DownloadDataTask]
func getDownloadTasksForCourse(_ courseId: String) async -> [DownloadDataTask]

func cancelDownloading(courseId: String, blocks: [CourseBlock]) async throws
func cancelDownloading(task: DownloadDataTask) async throws
func cancelDownloading(courseId: String) async throws
func cancelAllDownloading() async throws

func deleteFile(blocks: [CourseBlock]) async
func deleteAllFiles() async

func fileUrl(for blockId: String) async -> URL?

func addToDownloadQueue(blocks: [CourseBlock]) throws
func isLargeVideosSize(blocks: [CourseBlock]) -> Bool
func resumeDownloading() throws
func fileUrl(for blockId: String) -> URL?
func isLargeVideosSize(blocks: [CourseBlock]) -> Bool
}

public enum DownloadManagerEvent {
Expand All @@ -127,8 +132,9 @@ public enum DownloadManagerEvent {
case progress(Double, DownloadDataTask)
case paused(DownloadDataTask)
case canceled(DownloadDataTask)
case finished(DownloadDataTask)
case courseCanceled(String)
case allCanceled
case finished(DownloadDataTask)
case deletedFile(String)
case clearedAll
}
Expand Down Expand Up @@ -224,13 +230,10 @@ 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 blocksForDelete = blocks
.filter {
block in downloaded.first(where: { $0.blockId == block.id }) == nil
}

let blocksForDelete = blocks.filter { block in
downloaded.first(where: { $0.blockId == block.id }) == nil
}
await deleteFile(blocks: blocksForDelete)
downloaded.forEach {
currentDownloadEventPublisher.send(.canceled($0))
Expand All @@ -254,21 +257,20 @@ public class DownloadManager: DownloadManagerProtocol {

public func cancelDownloading(courseId: String) async throws {
let tasks = await getDownloadTasksForCourse(courseId)
for task in tasks {
do {
try persistence.deleteDownloadDataTask(id: task.id)
if let fileUrl = await fileUrl(for: task.id) {
try FileManager.default.removeItem(at: fileUrl)
}
} catch {
debugLog("Error deleting file: \(error.localizedDescription)")
}
}
await cancel(tasks: tasks)
currentDownloadEventPublisher.send(.courseCanceled(courseId))
downloadRequest?.cancel()
try newDownload()
}

public func cancelAllDownloading() async throws {
let tasks = await getDownloadTasks().filter { $0.state != .finished }
await cancel(tasks: tasks)
currentDownloadEventPublisher.send(.allCanceled)
downloadRequest?.cancel()
try newDownload()
}

public func deleteFile(blocks: [CourseBlock]) async {
for block in blocks {
do {
Expand Down Expand Up @@ -320,6 +322,8 @@ public class DownloadManager: DownloadManagerProtocol {
return path?.appendingPathComponent(fileName)
}

// MARK: - Private Intents

private func newDownload() throws {
guard userCanDownload() else {
throw NoWiFiError()
Expand Down Expand Up @@ -405,7 +409,18 @@ public class DownloadManager: DownloadManagerProtocol {
}
}

// MARK: - Private Intents
private func cancel(tasks: [DownloadDataTask]) async {
for task in tasks {
do {
try persistence.deleteDownloadDataTask(id: task.id)
if let fileUrl = await fileUrl(for: task.id) {
try FileManager.default.removeItem(at: fileUrl)
}
} catch {
debugLog("Error deleting file: \(error.localizedDescription)")
}
}
}

private func backgroundTask() {
backgroundTaskProvider.eventPublisher()
Expand Down Expand Up @@ -598,6 +613,10 @@ public class DownloadManagerMock: DownloadManagerProtocol {

}

public func cancelAllDownloading() async throws {

}

public func resumeDownloading() {

}
Expand Down
19 changes: 1 addition & 18 deletions Course/Course/Presentation/Container/CourseContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public struct CourseContainerView: View {
}
.navigationBarHidden(false)
.navigationBarBackButtonHidden(false)
.navigationTitle(titleBar())
.navigationTitle(title)
.onChange(of: selection, perform: didSelect)
.background(Theme.Colors.background)
}
Expand Down Expand Up @@ -211,23 +211,6 @@ public struct CourseContainerView: View {
)
}
}

private func titleBar() -> String {
switch CourseTab(rawValue: selection) {
case .course:
return self.title
case .videos:
return self.title
case .dates:
return CourseLocalization.CourseContainer.dates
case .discussion:
return DiscussionLocalization.title
case .handounds:
return CourseLocalization.CourseContainer.handouts
default:
return ""
}
}
}

#if DEBUG
Expand Down
Loading

0 comments on commit 6688cd3

Please sign in to comment.