Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save-restore last used album #284

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,28 @@
<action selector="showImagePickerWithSelectedAssets:" destination="BYZ-38-t0r" eventType="touchUpInside" id="m00-0a-bkf"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Pyy-Wk-n2s">
<rect key="frame" x="90" y="432.5" width="195" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="e8n-Nr-IrY"/>
</constraints>
<state key="normal" title="Image picker with last album">
<color key="titleShadowColor" red="0.5" green="0.5" blue="0.5" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="showImagePickerWithAlbums:" destination="BYZ-38-t0r" eventType="touchUpInside" id="R9s-Hs-nqn"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<constraints>
<constraint firstItem="zgs-tU-5p9" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="0A6-aX-1jb"/>
<constraint firstAttribute="centerX" secondItem="DzO-ex-PdY" secondAttribute="centerX" id="EZn-Ed-pHh"/>
<constraint firstItem="zgs-tU-5p9" firstAttribute="top" secondItem="DzO-ex-PdY" secondAttribute="bottom" constant="8" id="GUE-MX-lLP"/>
<constraint firstItem="Pyy-Wk-n2s" firstAttribute="top" secondItem="zgs-tU-5p9" secondAttribute="bottom" constant="8" id="IKo-Ze-hE5"/>
<constraint firstAttribute="centerY" secondItem="GFw-cP-rmQ" secondAttribute="centerY" id="XmI-RH-XSN"/>
<constraint firstItem="DzO-ex-PdY" firstAttribute="top" secondItem="GFw-cP-rmQ" secondAttribute="bottom" constant="8" id="j2M-qn-wIK"/>
<constraint firstItem="Pyy-Wk-n2s" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="spv-Vo-MiH"/>
<constraint firstAttribute="centerX" secondItem="GFw-cP-rmQ" secondAttribute="centerX" id="vJT-bW-R2W"/>
</constraints>
</view>
Expand Down
33 changes: 33 additions & 0 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ class ViewController: UIViewController {
print(finish.timeIntervalSince(start))
})
}

private var lastSelectedAlbum: String?
@IBAction func showImagePickerWithAlbums(_ sender: UIButton) {
let imagePicker = ImagePickerController()
imagePicker.settings.selection.max = 3
imagePicker.settings.theme.selectionStyle = .checked
imagePicker.settings.fetch.assets.supportedMediaTypes = [.image, .video]
imagePicker.settings.selection.unselectOnReachingMax = false

imagePicker.selectedAlbumIdentifier = lastSelectedAlbum
//Show the "Recent" album and all other ones available
let options = imagePicker.settings.fetch.album.options
imagePicker.settings.fetch.album.fetchResults = [
PHAssetCollection.fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary, options: options),
PHAssetCollection.fetchAssetCollections(with: .album, subtype: .albumRegular, options: options),
]

let start = Date()
self.presentImagePicker(imagePicker, select: { (asset) in
print("Selected: \(asset)")
}, deselect: { (asset) in
print("Deselected: \(asset)")
}, cancel: { [weak self] (assets) in
self?.lastSelectedAlbum = imagePicker.selectedAlbumIdentifier
print("Canceled with selections: \(assets)")
}, finish: { [weak self] (assets) in
self?.lastSelectedAlbum = imagePicker.selectedAlbumIdentifier
print("Finished with selections: \(assets)")
}, completion: {
let finish = Date()
print(finish.timeIntervalSince(start))
})
}

@IBAction func showCustomImagePicker(_ sender: UIButton) {
let imagePicker = ImagePickerController()
Expand Down
6 changes: 5 additions & 1 deletion Sources/Controller/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import Photos
var onDeselection: ((_ asset: PHAsset) -> Void)?
var onCancel: ((_ assets: [PHAsset]) -> Void)?
var onFinish: ((_ assets: [PHAsset]) -> Void)?
public var selectedAlbumIdentifier: String?

let assetsViewController: AssetsViewController
let albumsViewController = AlbumsViewController()
Expand Down Expand Up @@ -137,7 +138,10 @@ import Photos
navigationBar.barTintColor = .systemBackgroundColor
}

if let firstAlbum = albums.first {
if let selectedAlbumIdentifier = selectedAlbumIdentifier, let selectedAlbum = albums.first(where: { $0.localIdentifier == selectedAlbumIdentifier }) {
select(album: selectedAlbum)
} else if let firstAlbum = albums.first {
selectedAlbumIdentifier = firstAlbum.localIdentifier //so the check mark appears in the albums list
select(album: firstAlbum)
}
}
Expand Down
36 changes: 36 additions & 0 deletions Sources/Scene/Albums/AlbumsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import UIKit
import Photos

protocol AlbumsViewControllerDelegate: class {
var selectedAlbumIdentifier: String? { get set }
func albumsViewController(_ albumsViewController: AlbumsViewController, didSelectAlbum album: PHAssetCollection)
func didDismissAlbumsViewController(_ albumsViewController: AlbumsViewController)
}
Expand Down Expand Up @@ -71,6 +72,33 @@ class AlbumsViewController: UIViewController {
modalPresentationStyle = .popover
preferredContentSize = CGSize(width: 320, height: 300)
}

private var firstScreenShow = true
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

if firstScreenShow { //on the first show the function does not affect the tableView content offset/scroll
DispatchQueue.main.async { [weak self] in
self?.selectAlbumCellAndScroll()
}
} else {
selectAlbumCellAndScroll()
}
firstScreenShow = false
}

private func selectAlbumCellAndScroll() {
if let selectedAlbum = delegate?.selectedAlbumIdentifier, let albumIndex = albums.firstIndex(where: { $0.localIdentifier == selectedAlbum }), albumIndex != NSNotFound {
var indexPathsForReload = [IndexPath]()
if let indexPath = tableView.indexPathForSelectedRow, indexPath.row != albumIndex {
indexPathsForReload.append(indexPath)
}
let selectedIndexPath = IndexPath(row: albumIndex, section: 0)
indexPathsForReload.append(selectedIndexPath)
tableView.reloadRows(at: indexPathsForReload, with: .none)
tableView.scrollToRow(at: selectedIndexPath, at: .top, animated: false) //.middle works badly as only 2 elements fit the table
}
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Expand All @@ -86,8 +114,16 @@ class AlbumsViewController: UIViewController {
}

extension AlbumsViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let selectedAlbum = delegate?.selectedAlbumIdentifier, let albumIndex = albums.firstIndex(where: { $0.localIdentifier == selectedAlbum }) {
cell.isSelected = indexPath.row == albumIndex
}
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let album = albums[indexPath.row]
delegate?.selectedAlbumIdentifier = album.localIdentifier
delegate?.albumsViewController(self, didSelectAlbum: album)
}

Expand Down