Skip to content

Commit

Permalink
Fixing collection view reference into a secure way
Browse files Browse the repository at this point in the history
  • Loading branch information
glassipbel committed Jun 29, 2018
1 parent 098ce0d commit 036b9e6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 32 deletions.
2 changes: 1 addition & 1 deletion BubblePictures.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'BubblePictures'
s.version = '1.1.5'
s.version = '1.1.6'
s.summary = 'Framework for showing bubbles like pictures.'
s.description = 'Framework for showing bubbles like pictures inside a collection View, the frameworks handles everything for you, you just need to pass the pictures with the titles and thats it.'
s.homepage = 'https://github.com/kbelter/BubblePictures'
Expand Down
51 changes: 28 additions & 23 deletions BubblePictures/Classes/BubblePictures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public class BubblePictures: NSObject {
self.configFiles = configFiles
self.collectionView = collectionView
self.layoutConfigurator = layoutConfigurator
self.collectionView.dataSource = nil
self.collectionView.delegate = nil
self.collectionView?.dataSource = nil
self.collectionView?.delegate = nil
super.init()
registerForNotifications()
registerCells()
setCollectionViewAlignment()
truncateCells(configFiles: configFiles)
assignAlignment()
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView?.dataSource = self
self.collectionView?.delegate = self
}

deinit {
Expand All @@ -33,22 +33,22 @@ public class BubblePictures: NSObject {
public weak var delegate: BPDelegate?

@objc internal func rotated() {
self.collectionView.delegate = nil
self.collectionView.dataSource = nil
self.collectionView?.delegate = nil
self.collectionView?.dataSource = nil

self.truncateCells(configFiles: configFiles)
self.assignAlignment()

self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.reloadData()
self.collectionView?.delegate = self
self.collectionView?.dataSource = self
self.collectionView?.reloadData()
}

private func setCollectionViewAlignment() {
if layoutConfigurator.direction == .rightToLeft {
collectionView.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
collectionView?.transform = CGAffineTransform(scaleX: -1.0, y: 1.0)
} else {
collectionView.transform = .identity
collectionView?.transform = .identity
}
}

Expand All @@ -58,7 +58,7 @@ public class BubblePictures: NSObject {

private func registerCells() {
let nib = UINib(nibName: BPCollectionViewCell.className, bundle: BubblePictures.bubblePicturesBundle)
self.collectionView.register(nib, forCellWithReuseIdentifier: BPCollectionViewCell.className)
self.collectionView?.register(nib, forCellWithReuseIdentifier: BPCollectionViewCell.className)
}

private func truncateCells(configFiles: [BPCellConfigFile]) {
Expand All @@ -84,40 +84,45 @@ public class BubblePictures: NSObject {
}

private func assignAlignment() {
let bubblesTotalWidth = (CGFloat(min(maxNumberOfBubbles, configFiles.count)) * (self.collectionView.bounds.height - negativeInsetWidth)) + negativeInsetWidth
let emptyWidthSpace = self.collectionView.bounds.width - bubblesTotalWidth - 0.01
let emptyWidthSpaceDivided = ((self.collectionView.bounds.width - bubblesTotalWidth) / 2.0) - 0.01
guard let collectionView = self.collectionView else { return }

let bubblesTotalWidth = (CGFloat(min(maxNumberOfBubbles, configFiles.count)) * (collectionView.bounds.height - negativeInsetWidth)) + negativeInsetWidth
let emptyWidthSpace = collectionView.bounds.width - bubblesTotalWidth - 0.01
let emptyWidthSpaceDivided = ((collectionView.bounds.width - bubblesTotalWidth) / 2.0) - 0.01
if emptyWidthSpace < 0.0 { return }

switch layoutConfigurator.alignment {
case .center:
self.collectionView.contentInset = UIEdgeInsets(top: 0.0, left: emptyWidthSpaceDivided, bottom: 0.0, right: emptyWidthSpaceDivided)
collectionView.contentInset = UIEdgeInsets(top: 0.0, left: emptyWidthSpaceDivided, bottom: 0.0, right: emptyWidthSpaceDivided)
case .left:
switch layoutConfigurator.direction {
case .leftToRight:
self.collectionView.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: emptyWidthSpace)
collectionView.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: emptyWidthSpace)
case .rightToLeft:
self.collectionView.contentInset = UIEdgeInsets(top: 0.0, left: emptyWidthSpace, bottom: 0.0, right: 0.0)
collectionView.contentInset = UIEdgeInsets(top: 0.0, left: emptyWidthSpace, bottom: 0.0, right: 0.0)
}
case .right:
switch layoutConfigurator.direction {
case .leftToRight:
self.collectionView.contentInset = UIEdgeInsets(top: 0.0, left: emptyWidthSpace, bottom: 0.0, right: 0.0)
collectionView.contentInset = UIEdgeInsets(top: 0.0, left: emptyWidthSpace, bottom: 0.0, right: 0.0)
case .rightToLeft:
self.collectionView.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: emptyWidthSpace)
collectionView.contentInset = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: emptyWidthSpace)
}
}
}

fileprivate weak var collectionView: UICollectionView!
fileprivate weak var collectionView: UICollectionView?
fileprivate var configFiles: [BPCellConfigFile]
fileprivate var configFilesTruncated: [BPCellConfigFile] = []
fileprivate var layoutConfigurator: BPLayoutConfigurator
fileprivate var negativeInsetWidth: CGFloat {
return layoutConfigurator.distanceInterBubbles ?? (self.collectionView.bounds.height / 3.0)
let height = self.collectionView?.bounds.height ?? 0.0
return layoutConfigurator.distanceInterBubbles ?? (height / 3.0)
}
fileprivate var maxNumberOfBubbles: Int {
let calculationMaxNumberOfBubbles = Int(floor((self.collectionView.bounds.width - negativeInsetWidth) / (self.collectionView.bounds.height - negativeInsetWidth)))
let width = self.collectionView?.bounds.width ?? 0.0
let height = self.collectionView?.bounds.height ?? 0.0
let calculationMaxNumberOfBubbles = Int(floor((width - negativeInsetWidth) / (height - negativeInsetWidth)))
guard let maxNumberPreferredByUser = layoutConfigurator.maxNumberOfBubbles else {
return calculationMaxNumberOfBubbles
}
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- BubblePictures (1.1.5):
- BubblePictures (1.1.6):
- SDWebImage
- SDWebImage (4.3.3):
- SDWebImage/Core (= 4.3.3)
Expand All @@ -17,7 +17,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
BubblePictures: 80ede658bfedc2becc6f3f5a2685fb6d232c194b
BubblePictures: 8fcf9326d4a2805009f5cbbe510132424c464a23
SDWebImage: de4d90b5bff3571eae7bd16202b1f43135409fa5

PODFILE CHECKSUM: 02208cc61d3dc992703211a50fcd1770f63e8932
Expand Down
4 changes: 2 additions & 2 deletions Example/Pods/Local Podspecs/BubblePictures.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 036b9e6

Please sign in to comment.