Skip to content

Commit

Permalink
fix: size in DownloadActionView
Browse files Browse the repository at this point in the history
  • Loading branch information
forgotvas committed Jan 23, 2025
1 parent f850eb7 commit 78392dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ public final class CourseContainerViewModel: BaseCourseViewModel {
view: DownloadActionView(
actionType: .confirmDownloadCellular,
sequentials: sequentials,
downloadedSize: courseHelper.sizeFor(sequentials: sequentials),
action: { [weak self] in
guard let self else { return }
if !self.isEnoughSpace(for: totalFileSize) {
Expand Down Expand Up @@ -608,6 +609,7 @@ public final class CourseContainerViewModel: BaseCourseViewModel {
view: DownloadActionView(
actionType: .confirmDownload,
sequentials: sequentials,
downloadedSize: courseHelper.sizeFor(sequentials: sequentials),
action: { [weak self] in
guard let self else { return }
if !self.isEnoughSpace(for: totalFileSize) {
Expand Down Expand Up @@ -636,6 +638,7 @@ public final class CourseContainerViewModel: BaseCourseViewModel {
view: DownloadActionView(
actionType: .remove,
sequentials: sequentials,
downloadedSize: courseHelper.sizeFor(sequentials: sequentials),
action: { [weak self] in
guard let self else { return }
if let courseID = self.courseStructure?.id {
Expand Down Expand Up @@ -878,6 +881,7 @@ public final class CourseContainerViewModel: BaseCourseViewModel {
view: DownloadActionView(
actionType: .remove,
courseBlocks: [block],
downloadedSize: courseHelper.sizeFor(blocks: [block]),
action: { [weak self] in
guard let self else { return }
withAnimation(.linear(duration: 0.3)) {
Expand Down
19 changes: 18 additions & 1 deletion Course/Course/Presentation/Container/CourseDownloadHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,29 @@ public final class CourseDownloadHelper: @unchecked Sendable {
)
}

private func sizeFor(block: CourseBlock) -> Int? {
func sizeFor(block: CourseBlock) -> Int? {
if block.type == .video {
return block.encodedVideo?.video(downloadQuality: videoQuality)?.fileSize
}
return block.fileSize
}

func sizeFor(blocks: [CourseBlock]) -> Int {
let filteredBlocks = blocks.filter { $0.isDownloadable }
let sizes = filteredBlocks.compactMap { sizeFor(block: $0) }
return sizes.reduce(0, +)
}

func sizeFor(sequential: CourseSequential) -> Int {
let blocks = sequential.childs.flatMap({ $0.childs })
return sizeFor(blocks: blocks)
}

func sizeFor(sequentials: [CourseSequential]) -> Int {
let sizes = sequentials.map { sizeFor(sequential: $0) }
return sizes.reduce(0, +)
}

// swiftlint:disable function_body_length
private func enumerate(
tasks: [DownloadDataTask],
Expand Down

0 comments on commit 78392dd

Please sign in to comment.