From 90bde336f1c1092dd210cf2e0f65540fb90f7d12 Mon Sep 17 00:00:00 2001 From: Vadim Kuznetsov Date: Fri, 3 May 2024 13:12:36 +0300 Subject: [PATCH] Fix wrong download state (#418) * fix: wrong download state * fix: incorect state of download all switch --- .../CourseVideoDownloadBarViewModel.swift | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Course/Course/Presentation/Subviews/CourseVideoDownloadBarView/CourseVideoDownloadBarViewModel.swift b/Course/Course/Presentation/Subviews/CourseVideoDownloadBarView/CourseVideoDownloadBarViewModel.swift index 463d8596e..5caa8ece4 100644 --- a/Course/Course/Presentation/Subviews/CourseVideoDownloadBarView/CourseVideoDownloadBarViewModel.swift +++ b/Course/Course/Presentation/Subviews/CourseVideoDownloadBarView/CourseVideoDownloadBarViewModel.swift @@ -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) { + 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 } @@ -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() @@ -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) }