From 700eaeb7bc0484b3c93d009b08b6cfdb837a14ba Mon Sep 17 00:00:00 2001 From: Vadim Kuznetsov Date: Tue, 9 Jul 2024 20:54:49 +0300 Subject: [PATCH 1/2] fix: row height changes for download states --- .../Extensions/UIApplicationExtension.swift | 12 +++++++---- Core/Core/View/Base/SnackBarView.swift | 2 +- .../Container/CourseContainerViewModel.swift | 20 +++++++++++++------ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Core/Core/Extensions/UIApplicationExtension.swift b/Core/Core/Extensions/UIApplicationExtension.swift index f95bff70b..d2a93bc21 100644 --- a/Core/Core/Extensions/UIApplicationExtension.swift +++ b/Core/Core/Extensions/UIApplicationExtension.swift @@ -10,12 +10,17 @@ import Theme public extension UIApplication { + var windows: [UIWindow]? { + let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene + return scene?.windows + } + var keyWindow: UIWindow? { - UIApplication.shared.windows.first { $0.isKeyWindow } + windows?.first { $0.isKeyWindow } } func endEditing(force: Bool = true) { - windows.forEach { $0.endEditing(force) } + windows?.forEach { $0.endEditing(force) } } class func topViewController( @@ -36,8 +41,7 @@ public extension UIApplication { } var windowInsets: UIEdgeInsets { - guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, - let window = windowScene.windows.first else { + guard let window = windows?.first else { return .zero } diff --git a/Core/Core/View/Base/SnackBarView.swift b/Core/Core/View/Base/SnackBarView.swift index 14ed079f4..fc61351de 100644 --- a/Core/Core/View/Base/SnackBarView.swift +++ b/Core/Core/View/Base/SnackBarView.swift @@ -14,7 +14,7 @@ public struct SnackBarView: View { var action: (() -> Void)? private var safeArea: CGFloat { - UIApplication.shared.windows.first { $0.isKeyWindow }?.safeAreaInsets.bottom ?? 0 + UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0 } private let minHeight: CGFloat = 50 diff --git a/Course/Course/Presentation/Container/CourseContainerViewModel.swift b/Course/Course/Presentation/Container/CourseContainerViewModel.swift index 8f95d25b9..3a43f4fed 100644 --- a/Course/Course/Presentation/Container/CourseContainerViewModel.swift +++ b/Course/Course/Presentation/Container/CourseContainerViewModel.swift @@ -176,6 +176,15 @@ public class CourseContainerViewModel: BaseCourseViewModel { ) } + @MainActor + func getCourseStructure(courseID: String) async throws -> CourseStructure? { + if isInternetAvaliable { + return try await interactor.getCourseBlocks(courseID: courseID) + } else { + return try await interactor.getLoadedCourseBlocks(courseID: courseID) + } + } + @MainActor func getCourseBlocks(courseID: String, withProgress: Bool = true) async { guard let courseStart, courseStart < Date() else { return } @@ -183,8 +192,10 @@ public class CourseContainerViewModel: BaseCourseViewModel { isShowProgress = withProgress isShowRefresh = !withProgress do { + let courseStructure = try await getCourseStructure(courseID: courseID) + await setDownloadsStates(courseStructure: courseStructure) + self.courseStructure = courseStructure if isInternetAvaliable { - courseStructure = try await interactor.getCourseBlocks(courseID: courseID) NotificationCenter.default.post(name: .getCourseDates, object: courseID) isShowProgress = false isShowRefresh = false @@ -194,11 +205,8 @@ public class CourseContainerViewModel: BaseCourseViewModel { courseStructure: courseStructure ) } - } else { - courseStructure = try await interactor.getLoadedCourseBlocks(courseID: courseID) } courseVideosStructure = interactor.getCourseVideoBlocks(fullStructure: courseStructure!) - await setDownloadsStates() isShowProgress = false isShowRefresh = false @@ -529,7 +537,7 @@ public class CourseContainerViewModel: BaseCourseViewModel { } @MainActor - func setDownloadsStates() async { + func setDownloadsStates(courseStructure: CourseStructure?) async { guard let course = courseStructure else { return } courseDownloadTasks = await manager.getDownloadTasksForCourse(course.id) downloadableVerticals = [] @@ -602,7 +610,7 @@ public class CourseContainerViewModel: BaseCourseViewModel { if case .progress = state { return } Task(priority: .background) { debugLog(state, "--- state ---") - await self.setDownloadsStates() + await self.setDownloadsStates(courseStructure: self.courseStructure) } } .store(in: &cancellables) From f7f58ffbecc62233835ae4779569c21435d292d9 Mon Sep 17 00:00:00 2001 From: Vadim Kuznetsov Date: Tue, 9 Jul 2024 21:06:19 +0300 Subject: [PATCH 2/2] fix: unit tests --- .../Container/CourseContainerViewModelTests.swift | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Course/CourseTests/Presentation/Container/CourseContainerViewModelTests.swift b/Course/CourseTests/Presentation/Container/CourseContainerViewModelTests.swift index 144614179..515e60143 100644 --- a/Course/CourseTests/Presentation/Container/CourseContainerViewModelTests.swift +++ b/Course/CourseTests/Presentation/Container/CourseContainerViewModelTests.swift @@ -481,7 +481,7 @@ final class CourseContainerViewModelTests: XCTestCase { coreAnalytics: CoreAnalyticsMock() ) viewModel.courseStructure = courseStructure - await viewModel.setDownloadsStates() + await viewModel.setDownloadsStates(courseStructure: courseStructure) await viewModel.onDownloadViewTap( chapter: chapter, @@ -607,7 +607,7 @@ final class CourseContainerViewModelTests: XCTestCase { coreAnalytics: CoreAnalyticsMock() ) viewModel.courseStructure = courseStructure - await viewModel.setDownloadsStates() + await viewModel.setDownloadsStates(courseStructure: courseStructure) await viewModel.onDownloadViewTap( chapter: chapter, @@ -733,7 +733,7 @@ final class CourseContainerViewModelTests: XCTestCase { coreAnalytics: CoreAnalyticsMock() ) viewModel.courseStructure = courseStructure - await viewModel.setDownloadsStates() + await viewModel.setDownloadsStates(courseStructure: courseStructure) await viewModel.onDownloadViewTap( chapter: chapter, @@ -860,7 +860,7 @@ final class CourseContainerViewModelTests: XCTestCase { coreAnalytics: CoreAnalyticsMock() ) viewModel.courseStructure = courseStructure - await viewModel.setDownloadsStates() + await viewModel.setDownloadsStates(courseStructure: courseStructure) let exp = expectation(description: "Task Starting") DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { @@ -995,7 +995,7 @@ final class CourseContainerViewModelTests: XCTestCase { coreAnalytics: CoreAnalyticsMock() ) viewModel.courseStructure = courseStructure - await viewModel.setDownloadsStates() + await viewModel.setDownloadsStates(courseStructure: courseStructure) let exp = expectation(description: "Task Starting") DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { @@ -1130,7 +1130,7 @@ final class CourseContainerViewModelTests: XCTestCase { coreAnalytics: CoreAnalyticsMock() ) viewModel.courseStructure = courseStructure - await viewModel.setDownloadsStates() + await viewModel.setDownloadsStates(courseStructure: courseStructure) let exp = expectation(description: "Task Starting") DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { @@ -1286,7 +1286,7 @@ final class CourseContainerViewModelTests: XCTestCase { coreAnalytics: CoreAnalyticsMock() ) viewModel.courseStructure = courseStructure - await viewModel.setDownloadsStates() + await viewModel.setDownloadsStates(courseStructure: courseStructure) let exp = expectation(description: "Task Starting") DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {