Skip to content

Commit

Permalink
chore: little refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
forgotvas committed Mar 29, 2024
1 parent 8a6da8a commit 14be4c3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Course/Course/Presentation/Unit/CourseUnitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
63 changes: 35 additions & 28 deletions Course/Course/Presentation/Unit/CourseUnitViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,46 @@ 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 {
case next
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
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
9 changes: 5 additions & 4 deletions Course/Course/Presentation/Video/PlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,20 @@ 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(
seconds: 0.1,
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 }
Expand All @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions OpenEdX/DI/AppAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 24 additions & 31 deletions OpenEdX/Managers/PipManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 14be4c3

Please sign in to comment.