Skip to content

Commit

Permalink
Merge pull request hyperoslo#257 from richardtop/feature/per-instance…
Browse files Browse the repository at this point in the history
…-configuration

Per-instance based configuration
  • Loading branch information
onmyway133 authored Feb 9, 2017
2 parents 599b43b + 824bb5d commit be431ea
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 88 deletions.
30 changes: 19 additions & 11 deletions Source/BottomView/BottomContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ open class BottomContainerView: UIView {
static let height: CGFloat = 101
}

var configuration = Configuration()

lazy var pickerButton: ButtonPicker = { [unowned self] in
let pickerButton = ButtonPicker()
pickerButton.setTitleColor(UIColor.white, for: UIControlState())
Expand All @@ -34,8 +36,8 @@ open class BottomContainerView: UIView {

open lazy var doneButton: UIButton = { [unowned self] in
let button = UIButton()
button.setTitle(Configuration.cancelButtonTitle, for: UIControlState())
button.titleLabel?.font = Configuration.doneButton
button.setTitle(self.configuration.cancelButtonTitle, for: UIControlState())
button.titleLabel?.font = self.configuration.doneButton
button.addTarget(self, action: #selector(doneButtonDidPress(_:)), for: .touchUpInside)

return button
Expand All @@ -45,7 +47,7 @@ open class BottomContainerView: UIView {

lazy var topSeparator: UIView = { [unowned self] in
let view = UIView()
view.backgroundColor = Configuration.backgroundColor
view.backgroundColor = self.configuration.backgroundColor

return view
}()
Expand All @@ -62,29 +64,35 @@ open class BottomContainerView: UIView {

// MARK: Initializers

public override init(frame: CGRect) {
super.init(frame: frame)
public init(configuration: Configuration? = nil) {
if let configuration = configuration {
self.configuration = configuration
}
super.init(frame: .zero)
configure()
}

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

func configure() {
[borderPickerButton, pickerButton, doneButton, stackView, topSeparator].forEach {
addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
}

backgroundColor = Configuration.backgroundColor
backgroundColor = configuration.backgroundColor
stackView.accessibilityLabel = "Image stack"
stackView.addGestureRecognizer(tapGestureRecognizer)

setupConstraints()
}

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

// MARK: - Action methods

func doneButtonDidPress(_ button: UIButton) {
if button.currentTitle == Configuration.cancelButtonTitle {
if button.currentTitle == configuration.cancelButtonTitle {
delegate?.cancelButtonDidPress()
} else {
delegate?.doneButtonDidPress()
Expand Down
15 changes: 14 additions & 1 deletion Source/BottomView/ButtonPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class ButtonPicker: UIButton {
static let buttonBorderSize: CGFloat = 68
}

var configuration = Configuration()

lazy var numberLabel: UILabel = { [unowned self] in
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = Configuration.numberLabelFont
label.font = self.configuration.numberLabelFont

return label
}()
Expand All @@ -25,9 +27,20 @@ class ButtonPicker: UIButton {

// MARK: - Initializers

public init(configuration: Configuration? = nil) {
if let configuration = configuration {
self.configuration = configuration
}
super.init(frame: .zero)
configure()
}

override init(frame: CGRect) {
super.init(frame: frame)
configure()
}

func configure() {
addSubview(numberLabel)

subscribe()
Expand Down
34 changes: 24 additions & 10 deletions Source/CameraView/CameraView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ protocol CameraViewDelegate: class {

class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate {

var configuration = Configuration()

lazy var blurView: UIVisualEffectView = { [unowned self] in
let effect = UIBlurEffect(style: .dark)
let blurView = UIVisualEffectView(effect: effect)
Expand Down Expand Up @@ -45,26 +47,26 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate

lazy var noCameraLabel: UILabel = { [unowned self] in
let label = UILabel()
label.font = Configuration.noCameraFont
label.textColor = Configuration.noCameraColor
label.text = Configuration.noCameraTitle
label.font = self.configuration.noCameraFont
label.textColor = self.configuration.noCameraColor
label.text = self.configuration.noCameraTitle
label.sizeToFit()

return label
}()

lazy var noCameraButton: UIButton = { [unowned self] in
let button = UIButton(type: .system)
let title = NSAttributedString(string: Configuration.settingsTitle,
let title = NSAttributedString(string: self.configuration.settingsTitle,
attributes: [
NSFontAttributeName : Configuration.settingsFont,
NSForegroundColorAttributeName : Configuration.settingsColor,
NSFontAttributeName : self.configuration.settingsFont,
NSForegroundColorAttributeName : self.configuration.settingsColor,
])

button.setAttributedTitle(title, for: UIControlState())
button.contentEdgeInsets = UIEdgeInsets(top: 5.0, left: 10.0, bottom: 5.0, right: 10.0)
button.sizeToFit()
button.layer.borderColor = Configuration.settingsColor.cgColor
button.layer.borderColor = self.configuration.settingsColor.cgColor
button.layer.borderWidth = 1
button.layer.cornerRadius = 4
button.addTarget(self, action: #selector(settingsButtonDidTap), for: .touchUpInside)
Expand All @@ -87,14 +89,26 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate
var locationManager: LocationManager?
var startOnFrontCamera: Bool = false


public init(configuration: Configuration? = nil) {
if let configuration = configuration {
self.configuration = configuration
}
super.init(nibName: nil, bundle: nil)
}

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

override func viewDidLoad() {
super.viewDidLoad()

if Configuration.recordLocation {
if configuration.recordLocation {
locationManager = LocationManager()
}

view.backgroundColor = Configuration.mainColor
view.backgroundColor = configuration.mainColor

view.addSubview(containerView)
containerView.addSubview(blurView)
Expand Down Expand Up @@ -124,7 +138,7 @@ class CameraView: UIViewController, CLLocationManagerDelegate, CameraManDelegate
func setupPreviewLayer() {
guard let layer = AVCaptureVideoPreviewLayer(session: cameraMan.session) else { return }

layer.backgroundColor = Configuration.mainColor.cgColor
layer.backgroundColor = configuration.mainColor.cgColor
layer.autoreverses = true
layer.videoGravity = AVLayerVideoGravityResizeAspectFill

Expand Down
60 changes: 31 additions & 29 deletions Source/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,55 @@ public struct Configuration {

// MARK: Colors

public static var backgroundColor = UIColor(red: 0.15, green: 0.19, blue: 0.24, alpha: 1)
public static var gallerySeparatorColor = UIColor.black.withAlphaComponent(0.6)
public static var mainColor = UIColor(red: 0.09, green: 0.11, blue: 0.13, alpha: 1)
public static var noImagesColor = UIColor(red: 0.86, green: 0.86, blue: 0.86, alpha: 1)
public static var noCameraColor = UIColor(red: 0.86, green: 0.86, blue: 0.86, alpha: 1)
public static var settingsColor = UIColor.white
public static var bottomContainerColor = UIColor(red: 0.09, green: 0.11, blue: 0.13, alpha: 1)
public var backgroundColor = UIColor(red: 0.15, green: 0.19, blue: 0.24, alpha: 1)
public var gallerySeparatorColor = UIColor.black.withAlphaComponent(0.6)
public var mainColor = UIColor(red: 0.09, green: 0.11, blue: 0.13, alpha: 1)
public var noImagesColor = UIColor(red: 0.86, green: 0.86, blue: 0.86, alpha: 1)
public var noCameraColor = UIColor(red: 0.86, green: 0.86, blue: 0.86, alpha: 1)
public var settingsColor = UIColor.white
public var bottomContainerColor = UIColor(red: 0.09, green: 0.11, blue: 0.13, alpha: 1)

// MARK: Fonts

public static var numberLabelFont = UIFont(name: "HelveticaNeue-Bold", size: 19)!
public static var doneButton = UIFont(name: "HelveticaNeue-Medium", size: 19)!
public static var flashButton = UIFont(name: "HelveticaNeue-Medium", size: 12)!
public static var noImagesFont = UIFont(name: "HelveticaNeue-Medium", size: 18)!
public static var noCameraFont = UIFont(name: "HelveticaNeue-Medium", size: 18)!
public static var settingsFont = UIFont(name: "HelveticaNeue-Medium", size: 16)!
public var numberLabelFont = UIFont(name: "HelveticaNeue-Bold", size: 19)!
public var doneButton = UIFont(name: "HelveticaNeue-Medium", size: 19)!
public var flashButton = UIFont(name: "HelveticaNeue-Medium", size: 12)!
public var noImagesFont = UIFont(name: "HelveticaNeue-Medium", size: 18)!
public var noCameraFont = UIFont(name: "HelveticaNeue-Medium", size: 18)!
public var settingsFont = UIFont(name: "HelveticaNeue-Medium", size: 16)!

// MARK: Titles

public static var OKButtonTitle = "OK"
public static var cancelButtonTitle = "Cancel"
public static var doneButtonTitle = "Done"
public static var noImagesTitle = "No images available"
public static var noCameraTitle = "Camera is not available"
public static var settingsTitle = "Settings"
public static var requestPermissionTitle = "Permission denied"
public static var requestPermissionMessage = "Please, allow the application to access to your photo library."
public var OKButtonTitle = "OK"
public var cancelButtonTitle = "Cancel"
public var doneButtonTitle = "Done"
public var noImagesTitle = "No images available"
public var noCameraTitle = "Camera is not available"
public var settingsTitle = "Settings"
public var requestPermissionTitle = "Permission denied"
public var requestPermissionMessage = "Please, allow the application to access to your photo library."

// MARK: Dimensions

public static var cellSpacing: CGFloat = 2
public static var indicatorWidth: CGFloat = 41
public static var indicatorHeight: CGFloat = 8
public var cellSpacing: CGFloat = 2
public var indicatorWidth: CGFloat = 41
public var indicatorHeight: CGFloat = 8

// MARK: Custom behaviour

public static var canRotateCamera = true
public static var collapseCollectionViewWhileShot = true
public static var recordLocation = true
public static var allowMultiplePhotoSelection = true
public var canRotateCamera = true
public var collapseCollectionViewWhileShot = true
public var recordLocation = true
public var allowMultiplePhotoSelection = true

// MARK: Images
public static var indicatorView: UIView = {
public var indicatorView: UIView = {
let view = UIView()
view.backgroundColor = UIColor.white.withAlphaComponent(0.6)
view.layer.cornerRadius = 4
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()

public init() {}
}
2 changes: 1 addition & 1 deletion Source/Extensions/ConstraintsSetup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ extension TopView {
relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,
multiplier: 1, constant: 55))

if Configuration.canRotateCamera {
if configuration.canRotateCamera {
addConstraint(NSLayoutConstraint(item: rotateCamera, attribute: .right,
relatedBy: .equal, toItem: self, attribute: .right,
multiplier: 1, constant: Dimensions.rightOffset))
Expand Down
45 changes: 29 additions & 16 deletions Source/ImageGallery/ImageGalleryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ open class ImageGalleryView: UIView {
static let galleryBarHeight: CGFloat = 24
}

var configuration = Configuration()

lazy open var collectionView: UICollectionView = { [unowned self] in
let collectionView = UICollectionView(frame: CGRect.zero,
collectionViewLayout: self.collectionViewLayout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.backgroundColor = Configuration.mainColor
collectionView.backgroundColor = self.configuration.mainColor
collectionView.showsHorizontalScrollIndicator = false
collectionView.dataSource = self
collectionView.delegate = self
Expand All @@ -41,7 +43,7 @@ open class ImageGalleryView: UIView {
lazy var collectionViewLayout: UICollectionViewLayout = { [unowned self] in
let layout = ImageGalleryLayout()
layout.scrollDirection = .horizontal
layout.minimumInteritemSpacing = Configuration.cellSpacing
layout.minimumInteritemSpacing = self.configuration.cellSpacing
layout.minimumLineSpacing = 2
layout.sectionInset = UIEdgeInsets.zero

Expand All @@ -52,7 +54,7 @@ open class ImageGalleryView: UIView {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.addGestureRecognizer(self.panGestureRecognizer)
view.backgroundColor = Configuration.gallerySeparatorColor
view.backgroundColor = self.configuration.gallerySeparatorColor

return view
}()
Expand All @@ -66,9 +68,9 @@ open class ImageGalleryView: UIView {

open lazy var noImagesLabel: UILabel = { [unowned self] in
let label = UILabel()
label.font = Configuration.noImagesFont
label.textColor = Configuration.noImagesColor
label.text = Configuration.noImagesTitle
label.font = self.configuration.noImagesFont
label.textColor = self.configuration.noImagesColor
label.text = self.configuration.noImagesTitle
label.alpha = 0
label.sizeToFit()
self.addSubview(label)
Expand All @@ -88,26 +90,37 @@ open class ImageGalleryView: UIView {

// MARK: - Initializers

public init(configuration: Configuration? = nil) {
if let configuration = configuration {
self.configuration = configuration
}
super.init(frame: .zero)
configure()
}

override init(frame: CGRect) {
super.init(frame: frame)
configure()
}

backgroundColor = Configuration.mainColor
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func configure() {
backgroundColor = configuration.mainColor

collectionView.register(ImageGalleryViewCell.self,
forCellWithReuseIdentifier: CollectionView.reusableIdentifier)
forCellWithReuseIdentifier: CollectionView.reusableIdentifier)

[collectionView, topSeparator].forEach { addSubview($0) }

topSeparator.addSubview(Configuration.indicatorView)
topSeparator.addSubview(configuration.indicatorView)

imagesBeforeLoading = 0
fetchPhotos()
}

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

// MARK: - Layout

open override func layoutSubviews() {
Expand All @@ -121,8 +134,8 @@ open class ImageGalleryView: UIView {
let collectionFrame = frame.height == Dimensions.galleryBarHeight ? 100 + Dimensions.galleryBarHeight : frame.height
topSeparator.frame = CGRect(x: 0, y: 0, width: totalWidth, height: Dimensions.galleryBarHeight)
topSeparator.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleWidth]
Configuration.indicatorView.frame = CGRect(x: (totalWidth - Configuration.indicatorWidth) / 2, y: (topSeparator.frame.height - Configuration.indicatorHeight) / 2,
width: Configuration.indicatorWidth, height: Configuration.indicatorHeight)
configuration.indicatorView.frame = CGRect(x: (totalWidth - configuration.indicatorWidth) / 2, y: (topSeparator.frame.height - configuration.indicatorHeight) / 2,
width: configuration.indicatorWidth, height: configuration.indicatorHeight)
collectionView.frame = CGRect(x: 0, y: topSeparator.frame.height, width: totalWidth, height: collectionFrame - topSeparator.frame.height)
collectionSize = CGSize(width: collectionView.frame.height, height: collectionView.frame.height)

Expand Down Expand Up @@ -200,7 +213,7 @@ extension ImageGalleryView: UICollectionViewDelegate {
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let cell = collectionView.cellForItem(at: indexPath)
as? ImageGalleryViewCell else { return }
if Configuration.allowMultiplePhotoSelection == false {
if configuration.allowMultiplePhotoSelection == false {
// Clear selected photos array
for asset in self.selectedStack.assets {
self.selectedStack.dropAsset(asset)
Expand Down
Loading

0 comments on commit be431ea

Please sign in to comment.