Skip to content

Commit

Permalink
fix: animation
Browse files Browse the repository at this point in the history
set next and prev animation in unit content to be horizontal if COURSE_UNIT_PROGRESS_ENABLED key is true
  • Loading branch information
forgotvas committed Jan 23, 2024
1 parent 9154c48 commit ee9723e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
7 changes: 2 additions & 5 deletions Course/Course/Presentation/Unit/CourseNavigationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ struct CourseNavigationView: View {
} else {
if viewModel.selectedLesson() == viewModel.verticals[viewModel.verticalIndex].childs.last {
if viewModel.selectedLesson() != viewModel.verticals[viewModel.verticalIndex].childs.first {
UnitButtonView(type: .previous, action: {
playerStateSubject.send(VideoPlayerState.pause)
viewModel.select(move: .previous)
})
prevButton
}
lastButton
} else {
Expand Down Expand Up @@ -82,7 +79,7 @@ struct CourseNavigationView: View {
playerStateSubject.send(VideoPlayerState.pause)
viewModel.select(move: .previous)
}
)
)
}

private var lastButton: some View {
Expand Down
26 changes: 16 additions & 10 deletions Course/Course/Presentation/Unit/CourseUnitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct CourseUnitView: View {
}
}
}
@State var offsetView: CGFloat = 0
@State var offsetView: CGPoint = .zero
@State var showDiscussion: Bool = false
@Environment(\.isPresented) private var isPresented
@Environment(\.isHorizontal) private var isHorizontal
Expand Down Expand Up @@ -159,7 +159,7 @@ public struct CourseUnitView: View {

// swiftlint:disable function_body_length
private func content(reader: GeometryProxy) -> some View {
LazyVStack(alignment: .leading, spacing: 0) {
LazyHStack(alignment: .top, spacing: 0) {
let data = Array(viewModel.verticals[viewModel.verticalIndex].childs.enumerated())
ForEach(data, id: \.offset) { index, block in
VStack(spacing: 0) {
Expand Down Expand Up @@ -282,40 +282,40 @@ public struct CourseUnitView: View {
.id(index)
}
}
.offset(y: offsetView)
.offset(x: offsetView.x, y: offsetView.y)
.clipped()
.onAppear {
offsetView = -(reader.size.height * CGFloat(viewModel.index))
offsetView = viewOffset(for: viewModel.index, with: reader.size)
}
.onAppear {
NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification,
object: nil, queue: .main) { _ in
offsetView = -(reader.size.height * CGFloat(viewModel.index))
offsetView = viewOffset(for: viewModel.index, with: reader.size)
}
NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidShowNotification,
object: nil, queue: .main) { _ in
offsetView = -(reader.size.height * CGFloat(viewModel.index))
offsetView = viewOffset(for: viewModel.index, with: reader.size)
}
NotificationCenter.default.addObserver(forName: UIResponder.keyboardDidHideNotification,
object: nil, queue: .main) { _ in
offsetView = -(reader.size.height * CGFloat(viewModel.index))
offsetView = viewOffset(for: viewModel.index, with: reader.size)
}
}
.onChange(of: UIDevice.current.orientation, perform: { _ in
offsetView = -(reader.size.height * CGFloat(viewModel.index))
offsetView = viewOffset(for: viewModel.index, with: reader.size)
})
.onChange(of: viewModel.verticalIndex, perform: { index in
DispatchQueue.main.async {
withAnimation(Animation.easeInOut(duration: 0.2)) {
offsetView = -(reader.size.height * CGFloat(index))
offsetView = viewOffset(for: index, with: reader.size)
}
}

})
.onChange(of: viewModel.index, perform: { index in
DispatchQueue.main.async {
withAnimation(Animation.easeInOut(duration: 0.2)) {
offsetView = -(reader.size.height * CGFloat(index))
offsetView = viewOffset(for: index, with: reader.size)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
showDiscussion = viewModel.selectedLesson().type == .discussion
}
Expand All @@ -336,6 +336,12 @@ public struct CourseUnitView: View {
}
// swiftlint:enable function_body_length

private func viewOffset(for index: Int, with size: CGSize) -> CGPoint {
let x: CGFloat = viewModel.courseUnitProgressEnabled ? -(size.width * CGFloat(index)) : 0
let y: CGFloat = viewModel.courseUnitProgressEnabled ? 0 : -(size.height * CGFloat(index))
return CGPoint(x: x, y: y)
}

private func dropdown(block: CourseBlock) -> some View {
HStack {
if block.type == .video {
Expand Down

0 comments on commit ee9723e

Please sign in to comment.