Commit c1d5dff6 by Daniel Dahan

added PhotoLibraryDataSource struct type for moving PhotoLibrary collections…

added PhotoLibraryDataSource struct type for moving PhotoLibrary collections arount with their assets
parent b7cefc98
...@@ -30,6 +30,11 @@ ...@@ -30,6 +30,11 @@
import Photos import Photos
public struct PhotoLibraryDataSource {
public private(set) var collection: PHAssetCollection
public private(set) var assets: [PHAsset]
}
@objc(PhotoLibraryDelegate) @objc(PhotoLibraryDelegate)
public protocol PhotoLibraryDelegate { public protocol PhotoLibraryDelegate {
/** /**
...@@ -106,15 +111,15 @@ public class PhotoLibrary: NSObject { ...@@ -106,15 +111,15 @@ public class PhotoLibrary: NSObject {
public private(set) var fetchResult: PHFetchResult<PHAssetCollection>? public private(set) var fetchResult: PHFetchResult<PHAssetCollection>?
/// The assets used in the album. /// The assets used in the album.
public private(set) var collections: [PHAssetCollection: [PHAsset]]! { public private(set) var collections: [PhotoLibraryDataSource]! {
willSet { willSet {
cachingImageManager.stopCachingImagesForAllAssets() cachingImageManager.stopCachingImagesForAllAssets()
} }
didSet { didSet {
cachingImageManager.allowsCachingHighQualityImages = true cachingImageManager.allowsCachingHighQualityImages = true
for (_, assets) in collections { for dataSource in collections {
cachingImageManager.startCachingImages(for: assets, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFit, options: nil) cachingImageManager.startCachingImages(for: dataSource.assets, targetSize: PHImageManagerMaximumSize, contentMode: .aspectFit, options: nil)
} }
} }
} }
...@@ -182,7 +187,7 @@ public class PhotoLibrary: NSObject { ...@@ -182,7 +187,7 @@ public class PhotoLibrary: NSObject {
- Parameter subtype: A PHAssetCollectionSubtype. - Parameter subtype: A PHAssetCollectionSubtype.
- Parameter completion: An optional completion block. - Parameter completion: An optional completion block.
*/ */
public func fetch(type: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, completion: ([PHAssetCollection: [PHAsset]]) -> Void) { public func fetch(type: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, completion: ([PhotoLibraryDataSource]) -> Void) {
DispatchQueue.global(attributes: DispatchQueue.GlobalAttributes.qosDefault).async { [weak self, type = type, subtype = subtype, completion = completion] in DispatchQueue.global(attributes: DispatchQueue.GlobalAttributes.qosDefault).async { [weak self, type = type, subtype = subtype, completion = completion] in
guard let s = self else { guard let s = self else {
return return
...@@ -200,7 +205,7 @@ public class PhotoLibrary: NSObject { ...@@ -200,7 +205,7 @@ public class PhotoLibrary: NSObject {
options.wantsIncrementalChangeDetails = false options.wantsIncrementalChangeDetails = false
s.fetchResult = PHAssetCollection.fetchAssetCollections(with: type, subtype: subtype, options: options) s.fetchResult = PHAssetCollection.fetchAssetCollections(with: type, subtype: subtype, options: options)
s.fetchResult?.enumerateObjects(options: []) { [weak self] (collection, _, _) in s.fetchResult?.enumerateObjects(options: [.concurrent]) { [weak self] (collection, _, _) in
guard let s = self else { guard let s = self else {
return return
} }
...@@ -216,7 +221,7 @@ public class PhotoLibrary: NSObject { ...@@ -216,7 +221,7 @@ public class PhotoLibrary: NSObject {
assets.append(asset) assets.append(asset)
} }
s.collections[collection] = assets s.collections.append(PhotoLibraryDataSource(collection: collection, assets: assets))
} }
} }
} }
...@@ -229,7 +234,7 @@ public class PhotoLibrary: NSObject { ...@@ -229,7 +234,7 @@ public class PhotoLibrary: NSObject {
/// Prepares the collections. /// Prepares the collections.
private func prepareCollections() { private func prepareCollections() {
collections = [PHAssetCollection: [PHAsset]]() collections = [PhotoLibraryDataSource]()
} }
/// A method used to enable change observation. /// A method used to enable change observation.
...@@ -239,7 +244,7 @@ public class PhotoLibrary: NSObject { ...@@ -239,7 +244,7 @@ public class PhotoLibrary: NSObject {
// public func moments(in momentList: PHCollectionList, options: PHFetchOptions?) -> [PHAssetCollection] { // public func moments(in momentList: PHCollectionList, options: PHFetchOptions?) -> [PHAssetCollection] {
// var v = [PHAssetCollection]() // var v = [PHAssetCollection]()
// PHAssetCollection.fetchMoments(inMomentList: momentList, options: options).enumerateObjects(options: EnumerationOptions.concurrent) { (collection, _, _) in // PHAssetCollection.fetchMoments(inMomentList: momentList, options: options).enumerateObjects(options: [.concurrent]) { (collection, _, _) in
// v.append(collection) // v.append(collection)
// } // }
// return v // return v
...@@ -308,7 +313,7 @@ extension PhotoLibrary: PHPhotoLibraryChangeObserver { ...@@ -308,7 +313,7 @@ extension PhotoLibrary: PHPhotoLibraryChangeObserver {
delegate?.photoLibrary?(photoLibrary: self, didChange: changeInfo) delegate?.photoLibrary?(photoLibrary: self, didChange: changeInfo)
// Notifiy about specific changes. // Notifiy about specific changes.
fetchResult?.enumerateObjects(options: EnumerationOptions.concurrent) { [weak self, changeInfo = changeInfo] (collection, _, _) in fetchResult?.enumerateObjects(options: .concurrent) { [weak self, changeInfo = changeInfo] (collection, _, _) in
guard let s = self else { guard let s = self else {
return return
} }
......
...@@ -30,11 +30,6 @@ ...@@ -30,11 +30,6 @@
import Photos import Photos
public struct Item {
var album: PHAssetCollection
var assets: [PHAsset]
}
public class PhotoLibraryController: UIViewController, PhotoLibraryDelegate { public class PhotoLibraryController: UIViewController, PhotoLibraryDelegate {
/// A reference to a PhotoLibrary. /// A reference to a PhotoLibrary.
public private(set) var photoLibrary: PhotoLibrary! public private(set) var photoLibrary: PhotoLibrary!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment