Skip to content

Commit

Permalink
Merge pull request #65 from Viacheslav-Radchenko/dev
Browse files Browse the repository at this point in the history
Extend PhotosInputViewDeelgate with permission callbacks
  • Loading branch information
diegosanchezr committed Mar 17, 2016
2 parents 4cc289f + 8d33c92 commit 5c28e92
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Diego Sánchez <diego.sanchezr@gmail.com>
Anton Schukin <a.p.schukin@gmail.com>
Zhao Wang <zhaowang.net@gmail.com>
Viacheslav Radchenko <viacheslav.radchenko@gmail.com>

&yet LLC
Badoo
10 changes: 10 additions & 0 deletions ChattoAdditions/Source/Input/Photos/PhotosChatInputItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import Foundation

@objc public class PhotosChatInputItem: NSObject {
public var photoInputHandler: ((UIImage) -> Void)?
public var cameraPermissionHandler: (() -> Void)?
public var photosPermissionHandler: (() -> Void)?
public weak var presentingController: UIViewController?
public init(presentingController: UIViewController?) {
self.presentingController = presentingController
Expand Down Expand Up @@ -86,4 +88,12 @@ extension PhotosChatInputItem: PhotosInputViewDelegate {
func inputView(inputView: PhotosInputViewProtocol, didSelectImage image: UIImage) {
self.photoInputHandler?(image)
}

func inputViewDidRequestCameraPermission(inputView: PhotosInputViewProtocol) {
self.cameraPermissionHandler?()
}

func inputViewDidRequestPhotoLibraryPermission(inputView: PhotosInputViewProtocol) {
self.photosPermissionHandler?()
}
}
30 changes: 24 additions & 6 deletions ChattoAdditions/Source/Input/Photos/PhotosInputView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ protocol PhotosInputViewProtocol {

protocol PhotosInputViewDelegate: class {
func inputView(inputView: PhotosInputViewProtocol, didSelectImage image: UIImage)
func inputViewDidRequestCameraPermission(inputView: PhotosInputViewProtocol)
func inputViewDidRequestPhotoLibraryPermission(inputView: PhotosInputViewProtocol)
}

class PhotosInputView: UIView, PhotosInputViewProtocol {
Expand All @@ -46,6 +48,14 @@ class PhotosInputView: UIView, PhotosInputViewProtocol {
private var dataProvider: PhotosInputDataProviderProtocol!
private var cellProvider: PhotosInputCellProviderProtocol!
private var itemSizeCalculator: PhotosInputViewItemSizeCalculator!

var cameraAuthorizationStatus: AVAuthorizationStatus {
return AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo)
}

var photoLibraryAuthorizationStatus: PHAuthorizationStatus {
return PHPhotoLibrary.authorizationStatus()
}

weak var delegate: PhotosInputViewDelegate?
override init(frame: CGRect) {
Expand Down Expand Up @@ -156,7 +166,7 @@ extension PhotosInputView: UICollectionViewDataSource {
var cell: UICollectionViewCell
if indexPath.item == Constants.liveCameraItemIndex {
let liveCameraCell = collectionView.dequeueReusableCellWithReuseIdentifier("bar", forIndexPath: indexPath) as! LiveCameraCell
liveCameraCell.updateWithAuthorizationStatus(AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo))
liveCameraCell.updateWithAuthorizationStatus(self.cameraAuthorizationStatus)
cell = liveCameraCell
} else {
cell = self.cellProvider.cellForItemAtIndexPath(indexPath)
Expand All @@ -168,14 +178,22 @@ extension PhotosInputView: UICollectionViewDataSource {
extension PhotosInputView: UICollectionViewDelegateFlowLayout {
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
if indexPath.item == Constants.liveCameraItemIndex {
self.cameraPicker.requestImage { image in
if let image = image {
self.delegate?.inputView(self, didSelectImage: image)
if self.cameraAuthorizationStatus != .Authorized {
self.delegate?.inputViewDidRequestCameraPermission(self)
} else {
self.cameraPicker.requestImage { image in
if let image = image {
self.delegate?.inputView(self, didSelectImage: image)
}
}
}
} else {
self.dataProvider.requestFullImageAtIndex(indexPath.item - 1) { image in
self.delegate?.inputView(self, didSelectImage: image)
if self.photoLibraryAuthorizationStatus != .Authorized {
self.delegate?.inputViewDidRequestPhotoLibraryPermission(self)
} else {
self.dataProvider.requestFullImageAtIndex(indexPath.item - 1) { image in
self.delegate?.inputView(self, didSelectImage: image)
}
}
}
}
Expand Down

0 comments on commit 5c28e92

Please sign in to comment.