wrap up carousel view

pull/782/head
Ryan Zhao 2 years ago
parent 838969efa6
commit 7e164ecf04

@ -55,28 +55,7 @@ extension MediaInfoVC {
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
guard let attachment = coder.decodeObject(forKey: "attachment") as? Attachment else { preconditionFailure("Use init(attachment:) instead.")
print("No attachment")
return nil
}
guard let isOutgoing = coder.decodeObject(forKey: "isOutgoing") as? Bool else {
print("No isOutgoing")
return nil
}
self.attachment = attachment
self.isOutgoing = isOutgoing
super.init(coder: coder)
self.accessibilityLabel = "Media info"
setUpViewHierarchy()
}
override func encode(with coder: NSCoder) {
super.encode(with: coder)
// coder.encode(self.attachment, forKey: "attachment")
coder.encode(self.isOutgoing, forKey: "isOutgoing")
} }
private func setUpViewHierarchy() { private func setUpViewHierarchy() {
@ -94,8 +73,14 @@ extension MediaInfoVC {
} }
// MARK: - Interaction // MARK: - Interaction
@objc func showMediaFullScreen() { @objc func showMediaFullScreen() {
} }
// MARK: - Copy
func copyView() -> MediaPreviewView {
return MediaPreviewView(attachment: self.attachment, isOutgoing: self.isOutgoing)
}
} }
} }

@ -13,20 +13,27 @@ final class MediaInfoVC: BaseVC {
// MARK: - UI // MARK: - UI
private lazy var mediaInfoView: MediaInfoView = MediaInfoView(attachment: nil) private lazy var mediaInfoView: MediaInfoView = MediaInfoView(attachment: nil)
private lazy var mediaCarouselView: SessionCarouselView = { private lazy var mediaCarouselView: SessionCarouselView = {
let slices: [MediaPreviewView] = self.attachments.map {
MediaPreviewView(
attachment: $0,
isOutgoing: self.isOutgoing
)
}
let result: SessionCarouselView = SessionCarouselView( let result: SessionCarouselView = SessionCarouselView(
info: SessionCarouselView.Info( info: SessionCarouselView.Info(
slices: self.attachments.map { slices: slices,
MediaPreviewView( copyOfFirstSlice: slices.first?.copyView(),
attachment: $0, copyOfLastSlice: slices.last?.copyView(),
isOutgoing: self.isOutgoing
)
},
sliceSize: CGSize( sliceSize: CGSize(
width: Self.mediaSize, width: Self.mediaSize,
height: Self.mediaSize height: Self.mediaSize
), ),
shouldShowPageControl: true, shouldShowPageControl: true,
pageControlHeight: 10, pageControlStyle: SessionCarouselView.PageControlStyle(
size: .medium,
backgroundColor: .init(white: 0, alpha: 0.4),
bottomInset: Values.mediumSpacing
),
shouldShowArrows: true, shouldShowArrows: true,
arrowsSize: CGSize( arrowsSize: CGSize(
width: 20, width: 20,

@ -7,11 +7,12 @@ import SessionUtilitiesKit
extension SessionCarouselView { extension SessionCarouselView {
public struct Info { public struct Info {
let slices: [UIView] let slices: [UIView]
let copyOfFirstSlice: UIView?
let copyOfLastSlice: UIView?
let sliceSize: CGSize let sliceSize: CGSize
let sliceCount: Int let sliceCount: Int
let shouldShowPageControl: Bool let shouldShowPageControl: Bool
let pageControlHeight: CGFloat let pageControlStyle: PageControlStyle
let pageControlScale: CGFloat // This is to control the size of the dots
let shouldShowArrows: Bool let shouldShowArrows: Bool
let arrowsSize: CGSize let arrowsSize: CGSize
@ -19,21 +20,50 @@ extension SessionCarouselView {
init( init(
slices: [UIView] = [], slices: [UIView] = [],
copyOfFirstSlice: UIView? = nil,
copyOfLastSlice: UIView? = nil,
sliceSize: CGSize = .zero, sliceSize: CGSize = .zero,
shouldShowPageControl: Bool = true, shouldShowPageControl: Bool = true,
pageControlHeight: CGFloat = 0, pageControlStyle: PageControlStyle,
pageControlScale: CGFloat = 1,
shouldShowArrows: Bool = true, shouldShowArrows: Bool = true,
arrowsSize: CGSize = .zero arrowsSize: CGSize = .zero
) { ) {
self.slices = slices self.slices = slices
self.copyOfFirstSlice = copyOfFirstSlice
self.copyOfLastSlice = copyOfLastSlice
self.sliceSize = sliceSize self.sliceSize = sliceSize
self.sliceCount = slices.count self.sliceCount = slices.count
self.shouldShowPageControl = shouldShowPageControl && (self.sliceCount > 1) self.shouldShowPageControl = shouldShowPageControl && (self.sliceCount > 1)
self.pageControlHeight = pageControlHeight self.pageControlStyle = pageControlStyle
self.pageControlScale = pageControlScale
self.shouldShowArrows = shouldShowArrows && (self.sliceCount > 1) self.shouldShowArrows = shouldShowArrows && (self.sliceCount > 1)
self.arrowsSize = arrowsSize self.arrowsSize = arrowsSize
} }
} }
public struct PageControlStyle {
enum DotSize: CGFloat {
case mini = 0.5
case medium = 0.8
case original = 1
}
let height: CGFloat?
let size: DotSize
let backgroundColor: UIColor
let bottomInset: CGFloat
// MARK: - Initialization
init(
height: CGFloat? = nil,
size: DotSize = .original,
backgroundColor: UIColor = .clear,
bottomInset: CGFloat = 0
) {
self.height = height
self.size = size
self.backgroundColor = backgroundColor
self.bottomInset = bottomInset
}
}
} }

@ -28,8 +28,10 @@ final class SessionCarouselView: UIView, UIScrollViewDelegate {
result.numberOfPages = self.info.sliceCount result.numberOfPages = self.info.sliceCount
result.currentPage = 0 result.currentPage = 0
result.isHidden = !self.info.shouldShowPageControl result.isHidden = !self.info.shouldShowPageControl
result.set(.height, to: self.info.pageControlHeight) result.transform = CGAffineTransform(
result.transform = CGAffineTransform(scaleX: self.info.pageControlScale, y: self.info.pageControlScale) scaleX: self.info.pageControlStyle.size.rawValue,
y: self.info.pageControlStyle.size.rawValue
)
return result return result
}() }()
@ -59,11 +61,12 @@ final class SessionCarouselView: UIView, UIScrollViewDelegate {
}() }()
// MARK: - Lifecycle // MARK: - Lifecycle
init(info: SessionCarouselView.Info) { init(info: SessionCarouselView.Info) {
self.info = info self.info = info
if self.info.sliceCount > 1, if self.info.sliceCount > 1,
let copyOfFirstSlice: UIView = self.info.slices.first?.copyView(), let copyOfFirstSlice: UIView = self.info.copyOfFirstSlice,
let copyOfLastSlice: UIView = self.info.slices.last?.copyView() let copyOfLastSlice: UIView = self.info.copyOfLastSlice
{ {
self.slicesForLoop = [copyOfLastSlice] self.slicesForLoop = [copyOfLastSlice]
.appending(contentsOf: self.info.slices) .appending(contentsOf: self.info.slices)
@ -120,6 +123,7 @@ final class SessionCarouselView: UIView, UIScrollViewDelegate {
} }
// MARK: - UIScrollViewDelegate // MARK: - UIScrollViewDelegate
func scrollViewDidScroll(_ scrollView: UIScrollView) { func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex: Int = { let pageIndex: Int = {
let maybeCurrentPageIndex: Int = Int(round(scrollView.contentOffset.x/self.info.sliceSize.width)) let maybeCurrentPageIndex: Int = Int(round(scrollView.contentOffset.x/self.info.sliceSize.width))
@ -139,6 +143,14 @@ final class SessionCarouselView: UIView, UIScrollViewDelegate {
} }
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
setCorrectCotentOffsetIfNeeded(scrollView)
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
setCorrectCotentOffsetIfNeeded(scrollView)
}
private func setCorrectCotentOffsetIfNeeded(_ scrollView: UIScrollView) {
if pageControl.currentPage == 0 { if pageControl.currentPage == 0 {
scrollView.setContentOffset( scrollView.setContentOffset(
CGPoint( CGPoint(
@ -162,6 +174,7 @@ final class SessionCarouselView: UIView, UIScrollViewDelegate {
} }
// MARK: - Interaction // MARK: - Interaction
@objc func scrollToNextSlice() { @objc func scrollToNextSlice() {
self.scrollView.setContentOffset( self.scrollView.setContentOffset(
CGPoint( CGPoint(

@ -72,14 +72,4 @@ public extension UIView {
return result return result
} }
func copyView<T: UIView>() -> T? {
do {
return try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(NSKeyedArchiver.archivedData(withRootObject:self, requiringSecureCoding:false)) as? T
} catch {
print("\(error)")
return nil
}
}
} }

Loading…
Cancel
Save