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

Keep selection #47

Open
wants to merge 3 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions Example/Views/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16096" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="16097" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
Expand All @@ -20,7 +20,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" spacing="10" translatesAutoresizingMaskIntoConstraints="NO" id="50u-FM-ddp">
<rect key="frame" x="60.5" y="238" width="254" height="191"/>
<rect key="frame" x="60.5" y="218" width="254" height="231"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="zZH-ey-tdq">
<rect key="frame" x="0.0" y="0.0" width="254" height="30"/>
Expand Down Expand Up @@ -50,8 +50,15 @@
<action selector="showTatsiPickerWithCustomColors:" destination="BYZ-38-t0r" eventType="touchUpInside" id="69M-C4-9g4"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="6qs-vy-Yvi">
<rect key="frame" x="0.0" y="160" width="254" height="30"/>
<state key="normal" title="Show Tatsi Picker Keeping Selection"/>
<connections>
<action selector="showTatsiPickerKeepingSelection:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Vi5-j8-hZL"/>
</connections>
</button>
<stackView opaque="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="gk2-Pn-L7o">
<rect key="frame" x="0.0" y="160" width="254" height="31"/>
<rect key="frame" x="0.0" y="200" width="254" height="31"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Open with last album" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="eLC-7F-Mrx">
<rect key="frame" x="0.0" y="0.0" width="205" height="31"/>
Expand Down
19 changes: 19 additions & 0 deletions Example/Views/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ final class ViewController: UIViewController {
// It is not recommended to PHAssetCollection in persitant storage. If you do, check if the album is still available before showing the picker.
private var lastSelectedCollection: PHAssetCollection?

private var selectedAssets = [PHAsset]()

// If the rememberCollectioSwitch is turned on we return the last known collection, if available.
private var firstView: TatsiConfig.StartView {
if self.rememberCollectionSwitch.isOn, let lastCollection = self.lastSelectedCollection {
Expand Down Expand Up @@ -84,6 +86,22 @@ final class ViewController: UIViewController {
pickerViewController.modalPresentationStyle = .fullScreen
self.present(pickerViewController, animated: true, completion: nil)
}

@IBAction private func showTatsiPickerKeepingSelection(_ sender: Any) {
var config = TatsiConfig.default

config.keepSelectionBetweenAlbums = true
config.preselectedAssets = self.selectedAssets

config.showCameraOption = true
config.supportedMediaTypes = [.video, .image]
config.firstView = self.firstView
config.maxNumberOfSelections = 50

let pickerViewController = TatsiPickerViewController(config: config)
pickerViewController.pickerDelegate = self
self.present(pickerViewController, animated: true, completion: nil)
}

}

Expand All @@ -107,6 +125,7 @@ extension ViewController: TatsiPickerViewControllerDelegate {

func pickerViewController(_ pickerViewController: TatsiPickerViewController, didPickAssets assets: [PHAsset]) {
pickerViewController.dismiss(animated: true, completion: nil)
self.selectedAssets = assets
print("Picked assets: \(assets)")
}

Expand Down
6 changes: 6 additions & 0 deletions Tatsi/Config/TatsiConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public struct TatsiConfig {
/// If the picker should be a single view. This means the picker will open on the user library or a specific album. Switching can be done by tapping the on the header.
public var singleViewMode = false

/// If the picker should keeps its selection while switching between albums. Defaults to false.
public var keepSelectionBetweenAlbums = false

/// The first view the picker should show to the user. Defaults to the user library.
public var firstView = StartView.userLibrary

Expand All @@ -87,6 +90,9 @@ public struct TatsiConfig {
LocalizableStrings.tableName = localizableStringsTableName
}
}

/// Set this to a number of PHAsset instances that were selected before the picker was opened.
public var preselectedAssets: [PHAsset]? = nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my suggestion is to declare this as an empty array like public var preselectedAssets = [PHAsset]() because that is the actual default behaviour, that none are preselected. nil as default does not have an extra meaning here over an empty array, I think.


// MARK: - Internal features

Expand Down
15 changes: 11 additions & 4 deletions Tatsi/Views/Assets Grid/AssetsGridViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,22 @@ final internal class AssetsGridViewController: UICollectionViewController, Picke
guard self.album != oldValue else {
return
}
self.selectedAssets = []
if !(self.config?.keepSelectionBetweenAlbums ?? false) {
self.selectedAssets = []
}
self.assets = []
self.collectionView?.reloadData()

self.configureForNewAlbum()
}
}

internal fileprivate(set) var selectedAssets = [PHAsset]() {
didSet {
self.reloadDoneButtonState()
internal fileprivate(set) var selectedAssets: [PHAsset] {
get {
self.pickerViewController?.selectedAssets ?? []
}
set {
self.pickerViewController?.selectedAssets = newValue
}
}

Expand Down Expand Up @@ -462,13 +467,15 @@ extension AssetsGridViewController {
self.present(cameraController, animated: true, completion: nil)
collectionView.deselectItem(at: indexPath, animated: true)
}
self.reloadDoneButtonState()
}

override func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
guard let asset = self.asset(for: indexPath), let index = self.selectedAssets.firstIndex(of: asset) else {
return
}
self.selectedAssets.remove(at: index)
self.reloadDoneButtonState()
}

}
Expand Down
5 changes: 5 additions & 0 deletions Tatsi/Views/TatsiPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ final public class TatsiPickerViewController: UINavigationController {
public let config: TatsiConfig

public weak var pickerDelegate: TatsiPickerViewControllerDelegate?

// Contains all the selected assets the user has selected. If "keepSelectionBetweenAlbums" is turned off this array can reset when changing albums.
internal var selectedAssets = [PHAsset]()

override public var preferredStatusBarStyle: UIStatusBarStyle {
return self.config.preferredStatusBarStyle
Expand All @@ -29,6 +32,8 @@ final public class TatsiPickerViewController: UINavigationController {

navigationBar.barTintColor = config.colors.background
navigationBar.tintColor = config.colors.link

self.selectedAssets = config.preselectedAssets ?? []

self.setIntialViewController()
}
Expand Down