Skip to content

Commit

Permalink
ui changes
Browse files Browse the repository at this point in the history
  • Loading branch information
davesegal committed Jul 16, 2018
1 parent 6be05b7 commit 4e88870
Show file tree
Hide file tree
Showing 6 changed files with 518 additions and 500 deletions.
128 changes: 69 additions & 59 deletions Sources/Images/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,86 @@ import Photos

/// Wrap a PHAsset
public class Image: Equatable {

public let asset: PHAsset

// MARK: - Initialization

init(asset: PHAsset) {
self.asset = asset
}

public let asset: PHAsset

public var progressHandler: PHAssetVideoProgressHandler?

// MARK: - Initialization

init(asset: PHAsset) {
self.asset = asset
}
}

// MARK: - UIImage

extension Image {

/// Resolve UIImage synchronously
///
/// - Parameter size: The target size
/// - Returns: The resolved UIImage, otherwise nil
public func resolve(completion: @escaping (UIImage?) -> Void) {
let options = PHImageRequestOptions()
options.isNetworkAccessAllowed = true
options.deliveryMode = .highQualityFormat

let targetSize = CGSize(
width: asset.pixelWidth,
height: asset.pixelHeight
)

PHImageManager.default().requestImage(
for: asset,
targetSize: targetSize,
contentMode: .default,
options: options) { (image, _) in
completion(image)

/// Resolve UIImage synchronously
///
/// - Parameter size: The target size
/// - Returns: The resolved UIImage, otherwise nil


public func resolve(completion: @escaping (UIImage?) -> Void) {
let options = PHImageRequestOptions()
options.isNetworkAccessAllowed = true
options.deliveryMode = .highQualityFormat
options.isSynchronous = true

if progressHandler != nil{
options.progressHandler = progressHandler
}

let targetSize = CGSize(
width: asset.pixelWidth,
height: asset.pixelHeight
)

PHImageManager.default().requestImage(
for: asset,
targetSize: targetSize,
contentMode: .default,
options: options) { (image, dict) in
completion(image)
}

}
}

/// Resolve an array of Image
///
/// - Parameters:
/// - images: The array of Image
/// - size: The target size for all images
/// - completion: Called when operations completion
public static func resolve(images: [Image], completion: @escaping ([UIImage?]) -> Void) {
let dispatchGroup = DispatchGroup()
var convertedImages = [Int: UIImage]()

for (index, image) in images.enumerated() {
dispatchGroup.enter()

image.resolve(completion: { resolvedImage in
if let resolvedImage = resolvedImage {
convertedImages[index] = resolvedImage

/// Resolve an array of Image
///
/// - Parameters:
/// - images: The array of Image
/// - size: The target size for all images
/// - completion: Called when operations completion
public static func resolve(images: [Image], completion: @escaping ([UIImage?]) -> Void) {
let dispatchGroup = DispatchGroup()
var convertedImages = [Int: UIImage]()

for (index, image) in images.enumerated() {
dispatchGroup.enter()

image.resolve(completion: { resolvedImage in
if let resolvedImage = resolvedImage {
convertedImages[index] = resolvedImage
}

dispatchGroup.leave()
})
}

dispatchGroup.leave()
})

dispatchGroup.notify(queue: .main, execute: {
let sortedImages = convertedImages
.sorted(by: { $0.key < $1.key })
.map({ $0.value })
completion(sortedImages)
})
}

dispatchGroup.notify(queue: .main, execute: {
let sortedImages = convertedImages
.sorted(by: { $0.key < $1.key })
.map({ $0.value })
completion(sortedImages)
})
}
}

// MARK: - Equatable

public func == (lhs: Image, rhs: Image) -> Bool {
return lhs.asset == rhs.asset
return lhs.asset == rhs.asset
}
5 changes: 4 additions & 1 deletion Sources/Images/ImagesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ class ImagesController: UIViewController {
func refreshView() {
let hasImages = !cart.images.isEmpty
gridView.bottomView.g_fade(visible: hasImages)
gridView.collectionView.g_updateBottomInset(hasImages ? gridView.bottomView.frame.size.height : 0)
UIView.animate(withDuration: 0.3) {
self.gridView.collectionView.g_updateBottomInset(hasImages ? self.gridView.bottomView.frame.size.height : 0)
}

}

// MARK: - Controls
Expand Down
221 changes: 111 additions & 110 deletions Sources/Utils/Pages/PageIndicator.swift
Original file line number Diff line number Diff line change
@@ -1,125 +1,126 @@
import UIKit

protocol PageIndicatorDelegate: class {
func pageIndicator(_ pageIndicator: PageIndicator, didSelect index: Int)
func pageIndicator(_ pageIndicator: PageIndicator, didSelect index: Int)
}

class PageIndicator: UIView {

let items: [(name: String, selected: String, unselected: String)]
var buttons: [UIButton]!

//lazy var indicator: UIImageView = self.makeIndicator()
weak var delegate: PageIndicatorDelegate?

// MARK: - Initialization

let items: [(name: String, selected: String, unselected: String)]
var buttons: [UIButton]!
//lazy var indicator: UIImageView = self.makeIndicator()
weak var delegate: PageIndicatorDelegate?
// MARK: - Initialization
required init(items: [(name: String, selected: String, unselected: String)]) {
self.items = items

super.init(frame: .zero)

setup()
}

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

// MARK: - Layout

override func layoutSubviews() {
super.layoutSubviews()

let width = bounds.size.width / CGFloat(buttons.count)

for (i, button) in buttons.enumerated() {

button.frame = CGRect(x: width * CGFloat(i),
y: 0,
width: width,
height: bounds.size.height)
self.items = items

super.init(frame: .zero)

setup()
}

// indicator.frame.size = CGSize(width: width / 1.5, height: 4)
// indicator.frame.origin.y = bounds.size.height - indicator.frame.size.height
//
// if indicator.frame.origin.x == 0 {
// select(index: 0)
// }
}

override func didMoveToSuperview() {
super.didMoveToSuperview()
}

// MARK: - Setup

func setup() {
buttons = items.map {
let button = self.makeButton($0)
addSubview(button)

return button

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

//addSubview(indicator)
}

// MARK: - Controls


// MARK: - Layout

override func layoutSubviews() {
super.layoutSubviews()

let width = bounds.size.width / CGFloat(buttons.count)

for (i, button) in buttons.enumerated() {

button.frame = CGRect(x: width * CGFloat(i),
y: 0,
width: width,
height: bounds.size.height)
}

// indicator.frame.size = CGSize(width: width / 1.5, height: 4)
// indicator.frame.origin.y = bounds.size.height - indicator.frame.size.height
//
// if indicator.frame.origin.x == 0 {
// select(index: 0)
// }
}

override func didMoveToSuperview() {
super.didMoveToSuperview()
}

// MARK: - Setup

func setup() {
buttons = items.map {
let button = self.makeButton($0)
addSubview(button)

return button
}

//addSubview(indicator)
}

// MARK: - Controls

func makeButton(_ title: (name:String, selected:String, unselected:String)) -> UIButton {
let button = UIButton(type: .custom)
//button.setTitle("", for: UIControlState())
let button = UIButton(type: .custom)
//button.setTitle("", for: UIControlState())
button.setImage(UIImage(named: title.unselected), for: .normal)
button.setImage(UIImage(named: title.selected), for: .selected)
//button.setTitleColor(Config.PageIndicator.textColor, for: UIControlState())
//button.setTitleColor(UIColor.gray, for: .highlighted)
button.backgroundColor = Config.PageIndicator.backgroundColor
button.addTarget(self, action: #selector(buttonTouched(_:)), for: .touchUpInside)
//button.titleLabel?.font = buttonFont(false)

return button
}

func makeIndicator() -> UIImageView {
let imageView = UIImageView(image: GalleryBundle.image("gallery_page_indicator"))

return imageView
}

// MARK: - Action

@objc func buttonTouched(_ button: UIButton) {
let index = buttons.index(of: button) ?? 0
delegate?.pageIndicator(self, didSelect: index)
select(index: index)
}

// MARK: - Logic

func select(index: Int, animated: Bool = true) {
for (i, b) in buttons.enumerated() {
//button.setTitleColor(Config.PageIndicator.textColor, for: UIControlState())
//button.setTitleColor(UIColor.gray, for: .highlighted)
button.backgroundColor = Config.PageIndicator.backgroundColor
button.addTarget(self, action: #selector(buttonTouched(_:)), for: .touchUpInside)
//button.titleLabel?.font = buttonFont(false)

let info = items[i]
b.setImage(UIImage(named: i == index ? info.selected : info.unselected), for: .normal)
//b.titleLabel?.font = buttonFont(i == index)
return button
}

func makeIndicator() -> UIImageView {
let imageView = UIImageView(image: GalleryBundle.image("gallery_page_indicator"))

return imageView
}

// MARK: - Action

@objc func buttonTouched(_ button: UIButton) {
let index = buttons.index(of: button) ?? 0
delegate?.pageIndicator(self, didSelect: index)
select(index: index)

UIView.animate(withDuration: 0.15, animations: {
button.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
}) { _ in
UIView.animate(withDuration: 0.15, animations: {
button.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
}, completion: { _ in
UIView.animate(withDuration: 0.075, animations: {
button.transform = .identity
})
})
}

}

// MARK: - Logic

func select(index: Int, animated: Bool = true) {
for (i, b) in buttons.enumerated() {
let info = items[i]
b.setImage(UIImage(named: i == index ? info.selected : info.unselected), for: .normal)
}
}

// MARK: - Helper

func buttonFont(_ selected: Bool) -> UIFont {
return selected ? Config.Font.Main.bold.withSize(14) : Config.Font.Main.regular.withSize(14)
}

// UIView.animate(withDuration: animated ? 0.25 : 0.0,
// delay: 0,
// usingSpringWithDamping: 0.7,
// initialSpringVelocity: 0.5,
// options: .beginFromCurrentState,
// animations: {
// self.indicator.center.x = self.buttons[index].center.x
// },
// completion: nil)
}

// MARK: - Helper

func buttonFont(_ selected: Bool) -> UIFont {
return selected ? Config.Font.Main.bold.withSize(14) : Config.Font.Main.regular.withSize(14)
}
}
Loading

0 comments on commit 4e88870

Please sign in to comment.