diff --git a/Course/Course/Presentation/Unit/CourseUnitView.swift b/Course/Course/Presentation/Unit/CourseUnitView.swift index 2b00da51b..dff0a0ea9 100644 --- a/Course/Course/Presentation/Unit/CourseUnitView.swift +++ b/Course/Course/Presentation/Unit/CourseUnitView.swift @@ -119,7 +119,7 @@ public struct CourseUnitView: View { offsetY: isHorizontal ? landscapeTopSpacing : portraitTopSpacing, showDropdown: $showDropdown ) { [weak viewModel] vertical in - let data = viewModel?.dataFor(blockId: vertical.childs.first?.id) + let data = VerticalData.dataFor(blockId: vertical.childs.first?.id, in: viewModel?.chapters ?? []) viewModel?.route(to: data) playerStateSubject.send(VideoPlayerState.kill) } diff --git a/Course/Course/Presentation/Unit/CourseUnitViewModel.swift b/Course/Course/Presentation/Unit/CourseUnitViewModel.swift index 221c8942e..8f4be45b8 100644 --- a/Course/Course/Presentation/Unit/CourseUnitViewModel.swift +++ b/Course/Course/Presentation/Unit/CourseUnitViewModel.swift @@ -54,6 +54,39 @@ public enum LessonType: Equatable { } } +public struct VerticalData: Equatable { + public var chapterIndex: Int + public var sequentialIndex: Int + public var verticalIndex: Int + public var blockIndex: Int + + public init(chapterIndex: Int, sequentialIndex: Int, verticalIndex: Int, blockIndex: Int) { + self.chapterIndex = chapterIndex + self.sequentialIndex = sequentialIndex + self.verticalIndex = verticalIndex + self.blockIndex = blockIndex + } + + public static func dataFor(blockId: String?, in chapters: [CourseChapter]) -> VerticalData? { + guard let blockId = blockId else { return nil } + for (chapterIndex, chapter) in chapters.enumerated() { + for (sequentialIndex, sequential) in chapter.childs.enumerated() { + for (verticalIndex, vertical) in sequential.childs.enumerated() { + for (blockIndex, block) in vertical.childs.enumerated() where block.id.contains(blockId) { + return VerticalData( + chapterIndex: chapterIndex, + sequentialIndex: sequentialIndex, + verticalIndex: verticalIndex, + blockIndex: blockIndex + ) + } + } + } + } + return nil + } +} + public class CourseUnitViewModel: ObservableObject { enum LessonAction { @@ -61,13 +94,6 @@ public class CourseUnitViewModel: ObservableObject { case previous } - struct VerticalData: Equatable { - var chapterIndex: Int - var sequentialIndex: Int - var verticalIndex: Int - var blockIndex: Int - } - var verticals: [CourseVertical] var verticalIndex: Int var courseName: String @@ -310,7 +336,7 @@ public class CourseUnitViewModel: ObservableObject { let block = blockFor(index: data.blockIndex, in: vertical) { router.replaceCourseUnit( courseName: courseName, - blockId: block.id, + blockId: block.blockId, courseID: courseID, verticalIndex: data.verticalIndex, chapters: chapters, @@ -322,29 +348,10 @@ public class CourseUnitViewModel: ObservableObject { } public func route(to blockId: String?) { - guard let data = dataFor(blockId: blockId) else { return } + guard let data = VerticalData.dataFor(blockId: blockId, in: chapters) else { return } route(to: data, animated: true) } - func dataFor(blockId: String?) -> VerticalData? { - guard let blockId = blockId else { return nil } - for (chapterIndex, chapter) in chapters.enumerated() { - for (sequentialIndex, sequential) in chapter.childs.enumerated() { - for (verticalIndex, vertical) in sequential.childs.enumerated() { - for (blockIndex, block) in vertical.childs.enumerated() where block.id.contains(blockId) { - return VerticalData( - chapterIndex: chapterIndex, - sequentialIndex: sequentialIndex, - verticalIndex: verticalIndex, - blockIndex: blockIndex - ) - } - } - } - } - return nil - } - public var currentCourseId: String { courseID } diff --git a/Course/Course/Presentation/Video/PlayerViewController.swift b/Course/Course/Presentation/Video/PlayerViewController.swift index 5ac17bcc5..0bb477635 100644 --- a/Course/Course/Presentation/Video/PlayerViewController.swift +++ b/Course/Course/Presentation/Video/PlayerViewController.swift @@ -97,11 +97,12 @@ struct PlayerViewController: UIViewControllerRepresentable { } func setPlayer(_ player: AVPlayer?, currentProgress: @escaping ((Float, Double) -> Void)) { - guard let player = player else { return } cancellations.removeAll() if let observer = observer { currentPlayer?.removeTimeObserver(observer) - currentPlayer?.pause() + if currentHolder?.isPlayingInPip == false { + currentPlayer?.pause() + } } let interval = CMTime( @@ -109,7 +110,7 @@ struct PlayerViewController: UIViewControllerRepresentable { preferredTimescale: CMTimeScale(NSEC_PER_SEC) ) - observer = player.addPeriodicTimeObserver(forInterval: interval, queue: .main) {[weak player] time in + observer = player?.addPeriodicTimeObserver(forInterval: interval, queue: .main) {[weak player] time in var progress: Float = .zero let currentSeconds = CMTimeGetSeconds(time) guard let duration = player?.currentItem?.duration else { return } @@ -118,7 +119,7 @@ struct PlayerViewController: UIViewControllerRepresentable { currentProgress(progress, currentSeconds) } - player.publisher(for: \.rate) + player?.publisher(for: \.rate) .sink {[weak self] rate in guard rate > 0 else { return } self?.currentHolder?.pausePipIfNeed() diff --git a/OpenEdX/DI/AppAssembly.swift b/OpenEdX/DI/AppAssembly.swift index 4998e62f1..12ee2d514 100644 --- a/OpenEdX/DI/AppAssembly.swift +++ b/OpenEdX/DI/AppAssembly.swift @@ -127,10 +127,6 @@ class AppAssembly: Assembly { r.resolve(Router.self)! }.inObjectScope(.container) - container.register(DeepLinkRouter.self) { r in - r.resolve(Router.self)! - }.inObjectScope(.container) - container.register(ConfigProtocol.self) { _ in Config() }.inObjectScope(.container) diff --git a/OpenEdX/Managers/PipManager.swift b/OpenEdX/Managers/PipManager.swift index 63a6d4498..8720ae03f 100644 --- a/OpenEdX/Managers/PipManager.swift +++ b/OpenEdX/Managers/PipManager.swift @@ -133,21 +133,16 @@ public class PipManager: PipManagerProtocol { if holder.selectedCourseTab == CourseTab.videos.rawValue { courseStructure = courseInteractor.getCourseVideoBlocks(fullStructure: courseStructure) } - for (chapterIndex, chapter) in courseStructure.childs.enumerated() { - for (sequentialIndex, sequential) in chapter.childs.enumerated() { - for (verticalIndex, vertical) in sequential.childs.enumerated() { - for block in vertical.childs where block.id == holder.blockID { - return router.getVerticalController( - courseID: holder.courseID, - courseName: courseStructure.displayName, - title: courseStructure.childs[chapterIndex].childs[sequentialIndex].displayName, - chapters: courseStructure.childs, - chapterIndex: chapterIndex, - sequentialIndex: sequentialIndex - ) - } - } - } + + if let data = VerticalData.dataFor(blockId: holder.blockID, in: courseStructure.childs) { + return router.getVerticalController( + courseID: holder.courseID, + courseName: courseStructure.displayName, + title: courseStructure.childs[data.chapterIndex].childs[data.sequentialIndex].displayName, + chapters: courseStructure.childs, + chapterIndex: data.chapterIndex, + sequentialIndex: data.sequentialIndex + ) } throw PipManagerError.cantCreateCourseVerticalView @@ -162,22 +157,20 @@ public class PipManager: PipManagerProtocol { if holder.selectedCourseTab == CourseTab.videos.rawValue { courseStructure = courseInteractor.getCourseVideoBlocks(fullStructure: courseStructure) } - for (chapterIndex, chapter) in courseStructure.childs.enumerated() { - for (sequentialIndex, sequential) in chapter.childs.enumerated() { - for (verticalIndex, vertical) in sequential.childs.enumerated() { - for block in vertical.childs where block.id == holder.blockID { - return router.getUnitController( - courseName: courseStructure.displayName, - blockId: block.blockId, - courseID: courseStructure.id, - verticalIndex: verticalIndex, - chapters: courseStructure.childs, - chapterIndex: chapterIndex, - sequentialIndex: sequentialIndex - ) - } - } - } + if let data = VerticalData.dataFor(blockId: holder.blockID, in: courseStructure.childs) { + let chapter = courseStructure.childs[data.chapterIndex] + let sequential = chapter.childs[data.sequentialIndex] + let vertical = sequential.childs[data.verticalIndex] + let block = vertical.childs[data.blockIndex] + return router.getUnitController( + courseName: courseStructure.displayName, + blockId: block.id, + courseID: courseStructure.id, + verticalIndex: data.verticalIndex, + chapters: courseStructure.childs, + chapterIndex: data.chapterIndex, + sequentialIndex: data.sequentialIndex + ) } throw PipManagerError.cantCreateCourseUnitView