|
| 1 | +// |
| 2 | +// FetchAllAssets.swift |
| 3 | +// MediaCore |
| 4 | +// |
| 5 | +// Created by Christian Elies on 13.02.21. |
| 6 | +// |
| 7 | + |
| 8 | +import Photos |
| 9 | + |
| 10 | +/// Property wrapper for fetching all assets from the photo library. |
| 11 | +/// Fetches the assets lazily (after accessing the property). |
| 12 | +/// |
| 13 | +@propertyWrapper |
| 14 | +public struct FetchAllAssets { |
| 15 | + static var phAsset: PHAsset.Type = PHAsset.self |
| 16 | + |
| 17 | + private var assetCollection: PHAssetCollection? |
| 18 | + private let options = PHFetchOptions() |
| 19 | + private let defaultSort: Media.Sort<Media.SortKey> = Media.Sort(key: .creationDate, ascending: false) |
| 20 | + |
| 21 | + /// Wrapped array of `AnyMedia` instances. |
| 22 | + public var wrappedValue: [AnyMedia] { |
| 23 | + let result: PHFetchResult<PHAsset> |
| 24 | + if let assetCollection = assetCollection { |
| 25 | + result = Self.phAsset.fetchAssets(in: assetCollection, options: options) |
| 26 | + } else { |
| 27 | + result = Self.phAsset.fetchAssets(with: options) |
| 28 | + } |
| 29 | + var media: [AnyMedia] = [] |
| 30 | + result.enumerateObjects { asset, _, _ in |
| 31 | + guard let anyMedia = asset.anyMedia else { |
| 32 | + return |
| 33 | + } |
| 34 | + media.append(anyMedia) |
| 35 | + } |
| 36 | + return media |
| 37 | + } |
| 38 | + |
| 39 | + /// Initializes the property wrapper using a default sort descriptor |
| 40 | + /// (sort by `creationDate descending`). |
| 41 | + /// |
| 42 | + public init() { |
| 43 | + options.sortDescriptors = [defaultSort.sortDescriptor] |
| 44 | + } |
| 45 | + |
| 46 | + /// Initializes the property wrapper using the given sort descriptors |
| 47 | + /// to define the `PHFetchOptions`. |
| 48 | + /// |
| 49 | + /// - Parameters: |
| 50 | + /// - assetCollection: limits the fetch to a specific asset collection, by default all assets in the library are taken into account. |
| 51 | + /// - sort: a set of `Sort<MediaSortKey>` for sorting the assets |
| 52 | + /// - fetchLimit: a maximum number of results to fetch, defaults to 0 (no limit) |
| 53 | + /// - includeAllBurstAssets: a Boolean value that determines whether the fetch result includes all assets from burst photo sequences, defaults to false |
| 54 | + /// - includeHiddenAssets: a Boolean value that determines whether the fetch result includes assets marked as hidden, defaults to false |
| 55 | + /// |
| 56 | + public init( |
| 57 | + in assetCollection: PHAssetCollection? = nil, |
| 58 | + sort: Set<Media.Sort<Media.SortKey>> = [], |
| 59 | + fetchLimit: Int = 0, |
| 60 | + includeAllBurstAssets: Bool = false, |
| 61 | + includeHiddenAssets: Bool = false |
| 62 | + ) { |
| 63 | + self.assetCollection = assetCollection |
| 64 | + |
| 65 | + var sortKeys = sort |
| 66 | + sortKeys.insert(defaultSort) |
| 67 | + |
| 68 | + if !sortKeys.isEmpty { |
| 69 | + let sortDescriptors = sortKeys.map { $0.sortDescriptor } |
| 70 | + options.sortDescriptors = sortDescriptors |
| 71 | + } |
| 72 | + |
| 73 | + options.fetchLimit = fetchLimit |
| 74 | + options.includeAllBurstAssets = includeAllBurstAssets |
| 75 | + options.includeHiddenAssets = includeHiddenAssets |
| 76 | + } |
| 77 | +} |
0 commit comments