Skip to content

Commit e092ccc

Browse files
committed
YPLibraryView, YPAssetViewContainer and YPAssetZoomableView layout transformed to code. Xib deleted.
1 parent 5fb85d1 commit e092ccc

8 files changed

+219
-178
lines changed

Source/Pages/Gallery/YPAssetViewContainer.swift

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ import Stevia
1212
import AVFoundation
1313

1414
/// The container for asset (video or image). It containts the YPGridView and YPAssetZoomableView.
15-
class YPAssetViewContainer: UIView {
16-
public var zoomableView: YPAssetZoomableView?
15+
final class YPAssetViewContainer: UIView {
16+
public var zoomableView: YPAssetZoomableView
1717
public var itemOverlay: UIView?
1818
public let curtain = UIView()
1919
public let spinnerView = UIView()
2020
public let squareCropButton = UIButton()
21-
public let multipleSelectionButton = UIButton()
21+
public let multipleSelectionButton: UIButton = {
22+
let v = UIButton()
23+
v.setImage(YPConfig.icons.multipleSelectionOffIcon, for: .normal)
24+
return v
25+
}()
2226
public var onlySquare = YPConfig.library.onlySquare
2327
public var isShown = true
2428
public var spinnerIsShown = false
@@ -28,97 +32,96 @@ class YPAssetViewContainer: UIView {
2832
private var isMultipleSelection = false
2933

3034
public var itemOverlayType = YPConfig.library.itemOverlayType
31-
32-
override func awakeFromNib() {
33-
super.awakeFromNib()
34-
35+
36+
init(frame: CGRect, zoomableView: YPAssetZoomableView) {
37+
self.zoomableView = zoomableView
38+
super.init(frame: frame)
39+
40+
self.zoomableView.zoomableViewDelegate = self
41+
3542
switch itemOverlayType {
3643
case .grid:
3744
itemOverlay = YPGridView()
3845
default:
3946
break
4047
}
41-
48+
4249
if let itemOverlay = itemOverlay {
4350
addSubview(itemOverlay)
4451
itemOverlay.frame = frame
4552
clipsToBounds = true
46-
53+
4754
itemOverlay.alpha = 0
4855
}
49-
50-
for sv in subviews {
51-
if let cv = sv as? YPAssetZoomableView {
52-
zoomableView = cv
53-
zoomableView?.myDelegate = self
54-
}
55-
}
56-
56+
5757
let touchDownGR = UILongPressGestureRecognizer(target: self,
5858
action: #selector(handleTouchDown))
5959
touchDownGR.minimumPressDuration = 0
6060
touchDownGR.delegate = self
6161
addGestureRecognizer(touchDownGR)
62-
62+
6363
// TODO: Add tap gesture to play/pause. Add double tap gesture to square/unsquare
64-
64+
6565
subviews(
6666
spinnerView.subviews(
6767
spinner
6868
),
6969
curtain
7070
)
71-
71+
7272
spinner.centerInContainer()
7373
spinnerView.fillContainer()
7474
curtain.fillContainer()
75-
75+
7676
spinner.startAnimating()
7777
spinnerView.backgroundColor = UIColor.ypLabel.withAlphaComponent(0.3)
7878
curtain.backgroundColor = UIColor.ypLabel.withAlphaComponent(0.7)
7979
curtain.alpha = 0
80-
80+
8181
if !onlySquare {
8282
// Crop Button
8383
squareCropButton.setImage(YPConfig.icons.cropIcon, for: .normal)
8484
subviews(squareCropButton)
8585
squareCropButton.size(42)
8686
|-15-squareCropButton
87-
squareCropButton.Bottom == zoomableView!.Bottom - 15
87+
squareCropButton.Bottom == self.Bottom - 15
8888
}
89-
89+
9090
// Multiple selection button
9191
subviews(multipleSelectionButton)
92-
multipleSelectionButton.size(42)
93-
multipleSelectionButton-15-|
94-
multipleSelectionButton.setImage(YPConfig.icons.multipleSelectionOffIcon, for: .normal)
95-
multipleSelectionButton.Bottom == zoomableView!.Bottom - 15
96-
92+
multipleSelectionButton.size(42).trailing(15)
93+
multipleSelectionButton.Bottom == self.Bottom - 15
9794
}
98-
95+
96+
required init?(coder: NSCoder) {
97+
zoomableView = YPAssetZoomableView()
98+
super.init(coder: coder)
99+
fatalError("Only code layout.")
100+
}
101+
99102
// MARK: - Square button
100103

101104
@objc public func squareCropButtonTapped() {
102-
if let zoomableView = zoomableView {
105+
// if let zoomableView = zoomableView {
103106
let z = zoomableView.zoomScale
104107
shouldCropToSquare = (z >= 1 && z < zoomableView.squaredZoomScale)
105-
}
106-
zoomableView?.fitImage(shouldCropToSquare, animated: true)
108+
// }
109+
zoomableView.fitImage(shouldCropToSquare, animated: true)
107110
}
108111

109112
public func refreshSquareCropButton() {
110113
if onlySquare {
111114
squareCropButton.isHidden = true
112115
} else {
113-
if let image = zoomableView?.assetImageView.image {
116+
if let image = zoomableView.assetImageView.image {
114117
let isImageASquare = image.size.width == image.size.height
115118
squareCropButton.isHidden = isImageASquare
116119
}
117120
}
118121

119122
let shouldFit = YPConfig.library.onlySquare ? true : shouldCropToSquare
120-
zoomableView?.fitImage(shouldFit)
121-
zoomableView?.layoutSubviews()
123+
zoomableView.fitImage(shouldFit)
124+
zoomableView.layoutSubviews()
122125
}
123126

124127
// MARK: - Multiple selection

Source/Pages/Gallery/YPAssetZoomableView.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protocol YPAssetZoomableViewDelegate: AnyObject {
1717
}
1818

1919
final class YPAssetZoomableView: UIScrollView {
20-
public weak var myDelegate: YPAssetZoomableViewDelegate?
20+
public weak var zoomableViewDelegate: YPAssetZoomableViewDelegate?
2121
public var cropAreaDidChange = {}
2222
public var isVideoMode = false
2323
public var photoImageView = UIImageView()
@@ -92,7 +92,7 @@ final class YPAssetZoomableView: UIScrollView {
9292

9393
strongSelf.videoView.loadVideo(playerItem)
9494
strongSelf.videoView.play()
95-
strongSelf.myDelegate?.ypAssetZoomableViewDidLayoutSubviews(strongSelf)
95+
strongSelf.zoomableViewDelegate?.ypAssetZoomableViewDidLayoutSubviews(strongSelf)
9696
}
9797
}
9898

@@ -143,10 +143,10 @@ final class YPAssetZoomableView: UIScrollView {
143143
photoImageView.removeFromSuperview()
144144
}
145145

146-
required init?(coder aDecoder: NSCoder) {
147-
super.init(coder: aDecoder)!
146+
override init(frame: CGRect) {
147+
super.init(frame: frame)
148+
148149
backgroundColor = YPConfig.colors.assetViewBackgroundColor
149-
frame.size = CGSize.zero
150150
clipsToBounds = true
151151
photoImageView.frame = CGRect(origin: CGPoint.zero, size: CGSize.zero)
152152
videoView.frame = CGRect(origin: CGPoint.zero, size: CGSize.zero)
@@ -160,9 +160,14 @@ final class YPAssetZoomableView: UIScrollView {
160160
isScrollEnabled = true
161161
}
162162

163+
required init?(coder aDecoder: NSCoder) {
164+
super.init(coder: aDecoder)!
165+
fatalError("Only code layout.")
166+
}
167+
163168
override func layoutSubviews() {
164169
super.layoutSubviews()
165-
myDelegate?.ypAssetZoomableViewDidLayoutSubviews(self)
170+
zoomableViewDelegate?.ypAssetZoomableViewDidLayoutSubviews(self)
166171
}
167172
}
168173

@@ -253,7 +258,7 @@ extension YPAssetZoomableView: UIScrollViewDelegate {
253258
}
254259

255260
func scrollViewDidZoom(_ scrollView: UIScrollView) {
256-
myDelegate?.ypAssetZoomableViewScrollViewDidZoom()
261+
zoomableViewDelegate?.ypAssetZoomableViewScrollViewDidZoom()
257262

258263
centerAssetView()
259264
}
@@ -266,7 +271,7 @@ extension YPAssetZoomableView: UIScrollViewDelegate {
266271
self.fitImage(true, animated: true)
267272
}
268273

269-
myDelegate?.ypAssetZoomableViewScrollViewDidEndZooming()
274+
zoomableViewDelegate?.ypAssetZoomableViewScrollViewDidEndZooming()
270275
cropAreaDidChange()
271276
}
272277

Source/Pages/Gallery/YPLibrary+LibraryChange.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ extension YPLibraryVC: PHPhotoLibraryChangeObserver {
1616

1717
public func photoLibraryDidChange(_ changeInstance: PHChange) {
1818
guard let fetchResult = self.mediaManager.fetchResult,
19-
let collectionChanges = changeInstance.changeDetails(for: fetchResult),
20-
let collectionView = self.v.collectionView else {
19+
let collectionChanges = changeInstance.changeDetails(for: fetchResult) else {
2120
ypLog("Some problems there.")
2221
return
2322
}
2423

2524
DispatchQueue.main.async {
25+
let collectionView = self.v.collectionView
2626
self.mediaManager.fetchResult = collectionChanges.fetchResultAfterChanges
2727
if !collectionChanges.hasIncrementalChanges || collectionChanges.hasMoves {
2828
collectionView.reloadData()

Source/Pages/Gallery/YPLibraryVC+CollectionView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ extension YPLibraryVC {
1212
var isLimitExceeded: Bool { return selectedItems.count >= YPConfig.library.maxNumberOfItems }
1313

1414
func setupCollectionView() {
15-
v.collectionView.backgroundColor = YPConfig.colors.libraryScreenBackgroundColor
1615
v.collectionView.dataSource = self
1716
v.collectionView.delegate = self
1817
v.collectionView.register(YPLibraryViewCell.self, forCellWithReuseIdentifier: "YPLibraryViewCell")

Source/Pages/Gallery/YPLibraryVC+PanGesture.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ public class PanGestureHelper: NSObject, UIGestureRecognizerDelegate {
2121

2222
// The height constraint of the view with main selected image
2323
var topHeight: CGFloat {
24-
get { return v.assetViewContainerConstraintTop.constant }
24+
get {
25+
return v.assetViewContainerConstraintTop?.constant ?? 0
26+
}
2527
set {
2628
if newValue >= v.assetZoomableViewMinimalVisibleHeight - v.assetViewContainer.frame.height {
27-
v.assetViewContainerConstraintTop.constant = newValue
29+
v.assetViewContainerConstraintTop?.constant = newValue
2830
}
2931
}
3032
}

Source/Pages/Gallery/YPLibraryVC.swift

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import UIKit
1010
import Photos
1111
import PhotosUI
1212

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

2424
// MARK: - Init
25-
26-
internal required init(items: [YPMediaItem]?) {
25+
26+
internal override func loadView() {
27+
view = v
28+
}
29+
30+
required init() {
2731
super.init(nibName: nil, bundle: nil)
2832
title = YPConfig.wordings.libraryTitle
2933
}
30-
31-
internal convenience init() {
32-
self.init(items: nil)
33-
}
34-
34+
3535
internal required init?(coder aDecoder: NSCoder) {
3636
fatalError("init(coder:) has not been implemented")
3737
}
3838

39-
func setAlbum(_ album: YPAlbum) {
40-
title = album.title
41-
mediaManager.collection = album.collection
42-
currentlySelectedIndex = 0
43-
if !multipleSelectionEnabled {
44-
selectedItems.removeAll()
45-
}
46-
refreshMediaRequest()
47-
}
48-
4939
func initialize() {
5040
guard isInitialized == false else {
5141
return
@@ -54,7 +44,7 @@ internal class YPLibraryVC: UIViewController, YPPermissionCheckable {
5444
defer {
5545
isInitialized = true
5646
}
57-
47+
5848
mediaManager.initialize()
5949
mediaManager.v = v
6050

@@ -97,13 +87,18 @@ internal class YPLibraryVC: UIViewController, YPPermissionCheckable {
9787
showMultipleSelection()
9888
}
9989
}
100-
101-
// MARK: - View Lifecycle
102-
103-
public override func loadView() {
104-
v = YPLibraryView.xibView()
105-
view = v
90+
91+
func setAlbum(_ album: YPAlbum) {
92+
title = album.title
93+
mediaManager.collection = album.collection
94+
currentlySelectedIndex = 0
95+
if !multipleSelectionEnabled {
96+
selectedItems.removeAll()
97+
}
98+
refreshMediaRequest()
10699
}
100+
101+
// MARK: - View Lifecycle
107102

108103
public override func viewDidLoad() {
109104
super.viewDidLoad()
@@ -199,8 +194,8 @@ internal class YPLibraryVC: UIViewController, YPPermissionCheckable {
199194
selectedItems = [
200195
YPLibrarySelection(index: currentlySelectedIndex,
201196
cropRect: v.currentCropRect(),
202-
scrollViewContentOffset: v.assetZoomableView!.contentOffset,
203-
scrollViewZoomScale: v.assetZoomableView!.zoomScale,
197+
scrollViewContentOffset: v.assetZoomableView.contentOffset,
198+
scrollViewZoomScale: v.assetZoomableView.zoomScale,
204199
assetIdentifier: asset.localIdentifier)
205200
]
206201
}

0 commit comments

Comments
 (0)