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 wrong download state #418

Merged
merged 2 commits into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,17 @@ final class CourseVideoDownloadBarViewModel: ObservableObject {

// MARK: - Private intents

private func toggleStateIsOn() {
let totalCount = courseViewModel.downloadableVerticals.count
let availableCount = courseViewModel.downloadableVerticals.filter { $0.state == .available }.count
let finishedCount = courseViewModel.downloadableVerticals.filter { $0.state == .finished }.count
let downloadingCount = courseViewModel.downloadableVerticals.filter { $0.state == .downloading }.count
private func toggleStateIsOn(downloadableVerticals: Set<VerticalsDownloadState>) {
let totalCount = downloadableVerticals.count
let availableCount = downloadableVerticals.filter { $0.state == .available }.count
let finishedCount = downloadableVerticals.filter { $0.state == .finished }.count
let downloadingCount = downloadableVerticals.filter { $0.state == .downloading }.count

if downloadingCount == totalCount {
if downloadingCount == totalCount, totalCount > 0 {
self.isOn = true
return
}
if totalCount == finishedCount {
if totalCount == finishedCount, totalCount > 0 {
self.isOn = true
return
}
Expand All @@ -217,18 +217,18 @@ final class CourseVideoDownloadBarViewModel: ObservableObject {
return
}

let isOn = totalCount - finishedCount == downloadingCount
let isOn = totalCount - finishedCount == downloadingCount && totalCount > 0
self.isOn = isOn
}

private func observers() {
currentDownloadTask = courseViewModel.manager.currentDownloadTask
toggleStateIsOn()
toggleStateIsOn(downloadableVerticals: courseViewModel.downloadableVerticals)
courseViewModel.$downloadableVerticals
.sink { [weak self] _ in
.sink { [weak self] value in
guard let self else { return }
self.currentDownloadTask = self.courseViewModel.manager.currentDownloadTask
self.toggleStateIsOn()
self.toggleStateIsOn(downloadableVerticals: value)
}
.store(in: &cancellables)
courseViewModel.manager.eventPublisher()
Expand All @@ -237,7 +237,7 @@ final class CourseVideoDownloadBarViewModel: ObservableObject {
if case .progress = state {
self.currentDownloadTask = self.courseViewModel.manager.currentDownloadTask
}
self.toggleStateIsOn()
self.toggleStateIsOn(downloadableVerticals: self.courseViewModel.downloadableVerticals)
}
.store(in: &cancellables)
}
Expand Down
Loading