Skip to content

Commit

Permalink
YPLibraryView, YPAssetViewContainer and YPAssetZoomableView layout tr…
Browse files Browse the repository at this point in the history
…ansformed to code. Xib deleted.
  • Loading branch information
NikKovIos committed Nov 24, 2021
1 parent 5fb85d1 commit e092ccc
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 178 deletions.
75 changes: 39 additions & 36 deletions Source/Pages/Gallery/YPAssetViewContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import Stevia
import AVFoundation

/// The container for asset (video or image). It containts the YPGridView and YPAssetZoomableView.
class YPAssetViewContainer: UIView {
public var zoomableView: YPAssetZoomableView?
final class YPAssetViewContainer: UIView {
public var zoomableView: YPAssetZoomableView
public var itemOverlay: UIView?
public let curtain = UIView()
public let spinnerView = UIView()
public let squareCropButton = UIButton()
public let multipleSelectionButton = UIButton()
public let multipleSelectionButton: UIButton = {
let v = UIButton()
v.setImage(YPConfig.icons.multipleSelectionOffIcon, for: .normal)
return v
}()
public var onlySquare = YPConfig.library.onlySquare
public var isShown = true
public var spinnerIsShown = false
Expand All @@ -28,97 +32,96 @@ class YPAssetViewContainer: UIView {
private var isMultipleSelection = false

public var itemOverlayType = YPConfig.library.itemOverlayType

override func awakeFromNib() {
super.awakeFromNib()


init(frame: CGRect, zoomableView: YPAssetZoomableView) {
self.zoomableView = zoomableView
super.init(frame: frame)

self.zoomableView.zoomableViewDelegate = self

switch itemOverlayType {
case .grid:
itemOverlay = YPGridView()
default:
break
}

if let itemOverlay = itemOverlay {
addSubview(itemOverlay)
itemOverlay.frame = frame
clipsToBounds = true

itemOverlay.alpha = 0
}

for sv in subviews {
if let cv = sv as? YPAssetZoomableView {
zoomableView = cv
zoomableView?.myDelegate = self
}
}


let touchDownGR = UILongPressGestureRecognizer(target: self,
action: #selector(handleTouchDown))
touchDownGR.minimumPressDuration = 0
touchDownGR.delegate = self
addGestureRecognizer(touchDownGR)

// TODO: Add tap gesture to play/pause. Add double tap gesture to square/unsquare

subviews(
spinnerView.subviews(
spinner
),
curtain
)

spinner.centerInContainer()
spinnerView.fillContainer()
curtain.fillContainer()

spinner.startAnimating()
spinnerView.backgroundColor = UIColor.ypLabel.withAlphaComponent(0.3)
curtain.backgroundColor = UIColor.ypLabel.withAlphaComponent(0.7)
curtain.alpha = 0

if !onlySquare {
// Crop Button
squareCropButton.setImage(YPConfig.icons.cropIcon, for: .normal)
subviews(squareCropButton)
squareCropButton.size(42)
|-15-squareCropButton
squareCropButton.Bottom == zoomableView!.Bottom - 15
squareCropButton.Bottom == self.Bottom - 15
}

// Multiple selection button
subviews(multipleSelectionButton)
multipleSelectionButton.size(42)
multipleSelectionButton-15-|
multipleSelectionButton.setImage(YPConfig.icons.multipleSelectionOffIcon, for: .normal)
multipleSelectionButton.Bottom == zoomableView!.Bottom - 15

multipleSelectionButton.size(42).trailing(15)
multipleSelectionButton.Bottom == self.Bottom - 15
}


required init?(coder: NSCoder) {
zoomableView = YPAssetZoomableView()
super.init(coder: coder)
fatalError("Only code layout.")
}

// MARK: - Square button

@objc public func squareCropButtonTapped() {
if let zoomableView = zoomableView {
// if let zoomableView = zoomableView {
let z = zoomableView.zoomScale
shouldCropToSquare = (z >= 1 && z < zoomableView.squaredZoomScale)
}
zoomableView?.fitImage(shouldCropToSquare, animated: true)
// }
zoomableView.fitImage(shouldCropToSquare, animated: true)
}

public func refreshSquareCropButton() {
if onlySquare {
squareCropButton.isHidden = true
} else {
if let image = zoomableView?.assetImageView.image {
if let image = zoomableView.assetImageView.image {
let isImageASquare = image.size.width == image.size.height
squareCropButton.isHidden = isImageASquare
}
}

let shouldFit = YPConfig.library.onlySquare ? true : shouldCropToSquare
zoomableView?.fitImage(shouldFit)
zoomableView?.layoutSubviews()
zoomableView.fitImage(shouldFit)
zoomableView.layoutSubviews()
}

// MARK: - Multiple selection
Expand Down
21 changes: 13 additions & 8 deletions Source/Pages/Gallery/YPAssetZoomableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol YPAssetZoomableViewDelegate: AnyObject {
}

final class YPAssetZoomableView: UIScrollView {
public weak var myDelegate: YPAssetZoomableViewDelegate?
public weak var zoomableViewDelegate: YPAssetZoomableViewDelegate?
public var cropAreaDidChange = {}
public var isVideoMode = false
public var photoImageView = UIImageView()
Expand Down Expand Up @@ -92,7 +92,7 @@ final class YPAssetZoomableView: UIScrollView {

strongSelf.videoView.loadVideo(playerItem)
strongSelf.videoView.play()
strongSelf.myDelegate?.ypAssetZoomableViewDidLayoutSubviews(strongSelf)
strongSelf.zoomableViewDelegate?.ypAssetZoomableViewDidLayoutSubviews(strongSelf)
}
}

Expand Down Expand Up @@ -143,10 +143,10 @@ final class YPAssetZoomableView: UIScrollView {
photoImageView.removeFromSuperview()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = YPConfig.colors.assetViewBackgroundColor
frame.size = CGSize.zero
clipsToBounds = true
photoImageView.frame = CGRect(origin: CGPoint.zero, size: CGSize.zero)
videoView.frame = CGRect(origin: CGPoint.zero, size: CGSize.zero)
Expand All @@ -160,9 +160,14 @@ final class YPAssetZoomableView: UIScrollView {
isScrollEnabled = true
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
fatalError("Only code layout.")
}

override func layoutSubviews() {
super.layoutSubviews()
myDelegate?.ypAssetZoomableViewDidLayoutSubviews(self)
zoomableViewDelegate?.ypAssetZoomableViewDidLayoutSubviews(self)
}
}

Expand Down Expand Up @@ -253,7 +258,7 @@ extension YPAssetZoomableView: UIScrollViewDelegate {
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
myDelegate?.ypAssetZoomableViewScrollViewDidZoom()
zoomableViewDelegate?.ypAssetZoomableViewScrollViewDidZoom()

centerAssetView()
}
Expand All @@ -266,7 +271,7 @@ extension YPAssetZoomableView: UIScrollViewDelegate {
self.fitImage(true, animated: true)
}

myDelegate?.ypAssetZoomableViewScrollViewDidEndZooming()
zoomableViewDelegate?.ypAssetZoomableViewScrollViewDidEndZooming()
cropAreaDidChange()
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Pages/Gallery/YPLibrary+LibraryChange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ extension YPLibraryVC: PHPhotoLibraryChangeObserver {

public func photoLibraryDidChange(_ changeInstance: PHChange) {
guard let fetchResult = self.mediaManager.fetchResult,
let collectionChanges = changeInstance.changeDetails(for: fetchResult),
let collectionView = self.v.collectionView else {
let collectionChanges = changeInstance.changeDetails(for: fetchResult) else {
ypLog("Some problems there.")
return
}

DispatchQueue.main.async {
let collectionView = self.v.collectionView
self.mediaManager.fetchResult = collectionChanges.fetchResultAfterChanges
if !collectionChanges.hasIncrementalChanges || collectionChanges.hasMoves {
collectionView.reloadData()
Expand Down
1 change: 0 additions & 1 deletion Source/Pages/Gallery/YPLibraryVC+CollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ extension YPLibraryVC {
var isLimitExceeded: Bool { return selectedItems.count >= YPConfig.library.maxNumberOfItems }

func setupCollectionView() {
v.collectionView.backgroundColor = YPConfig.colors.libraryScreenBackgroundColor
v.collectionView.dataSource = self
v.collectionView.delegate = self
v.collectionView.register(YPLibraryViewCell.self, forCellWithReuseIdentifier: "YPLibraryViewCell")
Expand Down
6 changes: 4 additions & 2 deletions Source/Pages/Gallery/YPLibraryVC+PanGesture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {

// The height constraint of the view with main selected image
var topHeight: CGFloat {
get { return v.assetViewContainerConstraintTop.constant }
get {
return v.assetViewContainerConstraintTop?.constant ?? 0
}
set {
if newValue >= v.assetZoomableViewMinimalVisibleHeight - v.assetViewContainer.frame.height {
v.assetViewContainerConstraintTop.constant = newValue
v.assetViewContainerConstraintTop?.constant = newValue
}
}
}
Expand Down
51 changes: 23 additions & 28 deletions Source/Pages/Gallery/YPLibraryVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import UIKit
import Photos
import PhotosUI

internal class YPLibraryVC: UIViewController, YPPermissionCheckable {
internal final class YPLibraryVC: UIViewController, YPPermissionCheckable {
internal weak var delegate: YPLibraryViewDelegate?
internal var v: YPLibraryView!
internal var v = YPLibraryView(frame: .zero)
internal var isProcessing = false // true if video or image is in processing state
internal var selectedItems = [YPLibrarySelection]()
internal let mediaManager = LibraryMediaManager()
Expand All @@ -22,30 +22,20 @@ internal class YPLibraryVC: UIViewController, YPPermissionCheckable {
internal var isInitialized = false

// MARK: - Init

internal required init(items: [YPMediaItem]?) {

internal override func loadView() {
view = v
}

required init() {
super.init(nibName: nil, bundle: nil)
title = YPConfig.wordings.libraryTitle
}

internal convenience init() {
self.init(items: nil)
}


internal required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func setAlbum(_ album: YPAlbum) {
title = album.title
mediaManager.collection = album.collection
currentlySelectedIndex = 0
if !multipleSelectionEnabled {
selectedItems.removeAll()
}
refreshMediaRequest()
}

func initialize() {
guard isInitialized == false else {
return
Expand All @@ -54,7 +44,7 @@ internal class YPLibraryVC: UIViewController, YPPermissionCheckable {
defer {
isInitialized = true
}

mediaManager.initialize()
mediaManager.v = v

Expand Down Expand Up @@ -97,13 +87,18 @@ internal class YPLibraryVC: UIViewController, YPPermissionCheckable {
showMultipleSelection()
}
}

// MARK: - View Lifecycle

public override func loadView() {
v = YPLibraryView.xibView()
view = v

func setAlbum(_ album: YPAlbum) {
title = album.title
mediaManager.collection = album.collection
currentlySelectedIndex = 0
if !multipleSelectionEnabled {
selectedItems.removeAll()
}
refreshMediaRequest()
}

// MARK: - View Lifecycle

public override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -199,8 +194,8 @@ internal class YPLibraryVC: UIViewController, YPPermissionCheckable {
selectedItems = [
YPLibrarySelection(index: currentlySelectedIndex,
cropRect: v.currentCropRect(),
scrollViewContentOffset: v.assetZoomableView!.contentOffset,
scrollViewZoomScale: v.assetZoomableView!.zoomScale,
scrollViewContentOffset: v.assetZoomableView.contentOffset,
scrollViewZoomScale: v.assetZoomableView.zoomScale,
assetIdentifier: asset.localIdentifier)
]
}
Expand Down
Loading

0 comments on commit e092ccc

Please sign in to comment.