Skip to content

Commit

Permalink
Merge branch 'High5Apps-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaoj committed Mar 1, 2020
2 parents b575fc6 + 2868657 commit fca8944
Show file tree
Hide file tree
Showing 13 changed files with 266 additions and 106 deletions.
8 changes: 8 additions & 0 deletions BSImagePicker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
55CDB45F22347D640050D572 /* ZoomInteractionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55CDB45E22347D640050D572 /* ZoomInteractionController.swift */; };
55F67B77222EEB2500805134 /* VideoCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55F67B76222EEB2500805134 /* VideoCollectionViewCell.swift */; };
55F67B7B222F088500805134 /* GradientView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55F67B7A222F088500805134 /* GradientView.swift */; };
C2DC13CA23F75BDB0035FD13 /* NumberView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2DC13C923F75BDA0035FD13 /* NumberView.swift */; };
C2DC13CC23F75BE40035FD13 /* SelectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2DC13CB23F75BE40035FD13 /* SelectionView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -124,6 +126,8 @@
55DAB07F23145A8A00982A5B /* .travis.yml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.yaml; path = .travis.yml; sourceTree = "<group>"; };
55F67B76222EEB2500805134 /* VideoCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VideoCollectionViewCell.swift; sourceTree = "<group>"; };
55F67B7A222F088500805134 /* GradientView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientView.swift; sourceTree = "<group>"; };
C2DC13C923F75BDA0035FD13 /* NumberView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NumberView.swift; sourceTree = "<group>"; };
C2DC13CB23F75BE40035FD13 /* SelectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SelectionView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -208,7 +212,9 @@
55BCF8BD21D52C1000386752 /* AssetsCollectionViewDataSource.swift */,
55BCF8C121D52C1000386752 /* AssetCollectionViewCell.swift */,
55F67B76222EEB2500805134 /* VideoCollectionViewCell.swift */,
C2DC13CB23F75BE40035FD13 /* SelectionView.swift */,
55BCF8C321D52C1000386752 /* CheckmarkView.swift */,
C2DC13C923F75BDA0035FD13 /* NumberView.swift */,
55BCF8C521D52C1000386752 /* CameraCollectionViewCell.swift */,
55F67B7A222F088500805134 /* GradientView.swift */,
);
Expand Down Expand Up @@ -458,10 +464,12 @@
559DB80F21E655D000CD58B4 /* ImagePickerControllerDelegate.swift in Sources */,
559DB81721E6AFD800CD58B4 /* ImagePickerController+Assets.swift in Sources */,
55CDB45B223435420050D572 /* PlayerView.swift in Sources */,
C2DC13CC23F75BE40035FD13 /* SelectionView.swift in Sources */,
559DB81921E6AFF300CD58B4 /* ImagePickerController+Albums.swift in Sources */,
55BCF8D721D52C1000386752 /* CheckmarkView.swift in Sources */,
555472AE21E538B000B90CA5 /* AssetsViewController.swift in Sources */,
5543942D232A4EB500DB51B7 /* LivePreviewViewController.swift in Sources */,
C2DC13CA23F75BDB0035FD13 /* NumberView.swift in Sources */,
55BCF8DC21D52C1000386752 /* PreviewViewController.swift in Sources */,
55BCF8CF21D52C1000386752 /* Settings.swift in Sources */,
55CDB45D2234523C0050D572 /* PreviewTitleBuilder.swift in Sources */,
Expand Down
3 changes: 0 additions & 3 deletions BSImagePicker.xcworkspace/contents.xcworkspacedata

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

2 changes: 2 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class ViewController: UIViewController {
@IBAction func showImagePicker(_ sender: UIButton) {
let imagePicker = ImagePickerController()
imagePicker.settings.selection.max = 5
imagePicker.settings.theme.selectionStyle = .numbered
imagePicker.settings.fetch.assets.supportedMediaTypes = [.image, .video]
imagePicker.settings.selection.unselectOnReachingMax = true

let start = Date()
self.presentImagePicker(imagePicker, select: { (asset) in
Expand Down
13 changes: 2 additions & 11 deletions Sources/Controller/ImagePickerController+Assets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ import Photos

extension ImagePickerController: AssetsViewControllerDelegate {
func assetsViewController(_ assetsViewController: AssetsViewController, didSelectAsset asset: PHAsset) {
if settings.selection.unselectOnReachingMax && assetStore.count >= settings.selection.max {
if settings.selection.unselectOnReachingMax && assetStore.count > settings.selection.max {
if let first = assetStore.removeFirst() {
assetsViewController.unselect(asset:first)
imagePickerDelegate?.imagePicker(self, didDeselectAsset: first)
}
}
assetStore.append(asset)
updatedDoneButton()
imagePickerDelegate?.imagePicker(self, didSelectAsset: asset)
}

func assetsViewController(_ assetsViewController: AssetsViewController, didDeselectAsset asset: PHAsset) {
assetStore.remove(asset)
updatedDoneButton()
imagePickerDelegate?.imagePicker(self, didDeselectAsset: asset)
}
Expand All @@ -49,12 +48,4 @@ extension ImagePickerController: AssetsViewControllerDelegate {

pushViewController(previewViewController, animated: true)
}

func shouldSelect(in assetsViewController: AssetsViewController) -> Bool {
return assetStore.count < settings.selection.max || settings.selection.unselectOnReachingMax
}

func selectedAssets() -> [PHAsset] {
return assetStore.assets
}
}
11 changes: 10 additions & 1 deletion Sources/Controller/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ImagePickerController: UINavigationController {
var onCancel: ((_ assets: [PHAsset]) -> Void)?
var onFinish: ((_ assets: [PHAsset]) -> Void)?

let assetsViewController = AssetsViewController()
let assetsViewController: AssetsViewController
let albumsViewController = AlbumsViewController()
let dropdownTransitionDelegate = DropdownTransitionDelegate()
let zoomTransitionDelegate = ZoomTransitionDelegate()
Expand All @@ -64,6 +64,15 @@ public class ImagePickerController: UINavigationController {
}
}()

public init() {
assetsViewController = AssetsViewController(store: assetStore)
super.init(nibName: nil, bundle: nil)
}

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

public override func viewDidLoad() {
super.viewDidLoad()

Expand Down
4 changes: 4 additions & 0 deletions Sources/Model/AssetStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ public class AssetStore {
func removeFirst() -> PHAsset? {
return assets.removeFirst()
}

func index(of asset: PHAsset) -> Int? {
return assets.firstIndex(of: asset)
}
}
21 changes: 15 additions & 6 deletions Sources/Model/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ public class Settings {
/// What color to fill the circle with
public lazy var selectionFillColor: UIColor = UIView().tintColor

/// Color for the actual checkmark
/// Color for the actual selection icon
public lazy var selectionStrokeColor: UIColor = .white

/// Shadow color for the circle
public lazy var selectionShadowColor: UIColor = .black

public enum SelectionStyle {
case checked
case numbered
}

/// The icon to display inside the selection oval
public lazy var selectionStyle: SelectionStyle = .checked

public lazy var previewTitleAttributes : [NSAttributedString.Key: Any] = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16),
NSAttributedString.Key.foregroundColor: UIColor.black
Expand Down Expand Up @@ -101,13 +109,14 @@ public class Settings {
}()

/// Fetch results for asset collections you want to present to the user
/// Some other fetch results that you might wanna use:
/// PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumFavorites, options: options),
/// PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: options),
/// PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumSelfPortraits, options: options),
/// PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumPanoramas, options: options),
/// PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumVideos, options: options),
public lazy var fetchResults: [PHFetchResult<PHAssetCollection>] = [
PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: options),
PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumFavorites, options: options),
PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: options),
PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumSelfPortraits, options: options),
PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumPanoramas, options: options),
PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumVideos, options: options),
]
}

Expand Down
24 changes: 14 additions & 10 deletions Sources/Scene/Assets/AssetCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ The photo cell.
class AssetCollectionViewCell: UICollectionViewCell {
let imageView: UIImageView = UIImageView(frame: .zero)
var settings: Settings! {
didSet { checkmarkView.settings = settings }
didSet { selectionView.settings = settings }
}
var selectionIndex: Int? {
didSet { selectionView.selectionIndex = selectionIndex }
}

override var isSelected: Bool {
Expand Down Expand Up @@ -57,7 +60,7 @@ class AssetCollectionViewCell: UICollectionViewCell {
}

private let selectionOverlayView: UIView = UIView(frame: .zero)
private let checkmarkView: CheckmarkView = CheckmarkView(frame: .zero)
private let selectionView: SelectionView = SelectionView(frame: .zero)

override init(frame: CGRect) {
super.init(frame: frame)
Expand All @@ -68,10 +71,10 @@ class AssetCollectionViewCell: UICollectionViewCell {
imageView.clipsToBounds = true
selectionOverlayView.backgroundColor = UIColor.lightGray
selectionOverlayView.translatesAutoresizingMaskIntoConstraints = false
checkmarkView.translatesAutoresizingMaskIntoConstraints = false
selectionView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(imageView)
contentView.addSubview(selectionOverlayView)
contentView.addSubview(checkmarkView)
contentView.addSubview(selectionView)

// Add constraints
NSLayoutConstraint.activate([
Expand All @@ -83,10 +86,10 @@ class AssetCollectionViewCell: UICollectionViewCell {
selectionOverlayView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
selectionOverlayView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
selectionOverlayView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
checkmarkView.heightAnchor.constraint(equalToConstant: 25),
checkmarkView.widthAnchor.constraint(equalToConstant: 25),
checkmarkView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -4),
checkmarkView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -4)
selectionView.heightAnchor.constraint(equalToConstant: 25),
selectionView.widthAnchor.constraint(equalToConstant: 25),
selectionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -4),
selectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -4)
])

updateAlpha(isSelected)
Expand All @@ -100,6 +103,7 @@ class AssetCollectionViewCell: UICollectionViewCell {
override func prepareForReuse() {
super.prepareForReuse()
imageView.image = nil
selectionIndex = nil
}

func updateAccessibilityLabel(_ selected: Bool) {
Expand All @@ -108,10 +112,10 @@ class AssetCollectionViewCell: UICollectionViewCell {

private func updateAlpha(_ selected: Bool) {
if selected {
self.checkmarkView.alpha = 1.0
self.selectionView.alpha = 1.0
self.selectionOverlayView.alpha = 0.3
} else {
self.checkmarkView.alpha = 0.0
self.selectionView.alpha = 0.0
self.selectionOverlayView.alpha = 0.0
}
}
Expand Down
12 changes: 7 additions & 5 deletions Sources/Scene/Assets/AssetsCollectionViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ class AssetsCollectionViewDataSource : NSObject, UICollectionViewDataSource {
private let fetchResult: PHFetchResult<PHAsset>
private let imageManager = PHCachingImageManager.default()
private let durationFormatter = DateComponentsFormatter()
private let store: AssetStore

private let scale: CGFloat
private var targetSize: CGSize = .zero

init(fetchResult: PHFetchResult<PHAsset>, scale: CGFloat = UIScreen.main.scale) {
init(fetchResult: PHFetchResult<PHAsset>, store: AssetStore, scale: CGFloat = UIScreen.main.scale) {
self.fetchResult = fetchResult
self.store = store
self.scale = scale
durationFormatter.unitsStyle = .positional
durationFormatter.zeroFormattingBehavior = [.pad]
Expand Down Expand Up @@ -68,14 +70,14 @@ class AssetsCollectionViewDataSource : NSObject, UICollectionViewDataSource {
}
UIView.setAnimationsEnabled(animationsWasEnabled)

cell.accessibilityIdentifier = "photo_cell_\(indexPath.item)"
cell.accessibilityIdentifier = "Photo \(indexPath.item + 1)"
cell.accessibilityTraits = UIAccessibilityTraits.button
cell.isAccessibilityElement = true
cell.settings = settings

loadImage(for: asset, in: cell)

cell.isAccessibilityElement = true
cell.accessibilityTraits = UIAccessibilityTraits.button

cell.selectionIndex = store.index(of: asset)

return cell
}
Expand Down
Loading

0 comments on commit fca8944

Please sign in to comment.