DKGroupDataManager.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // This file is based on third party code, see below for the original author
  2. // and original license.
  3. // Modifications are (c) by Threema GmbH and licensed under the AGPLv3.
  4. //
  5. // DKGroupDataManager.swift
  6. // DKImagePickerControllerDemo
  7. //
  8. // Created by ZhangAo on 15/12/16.
  9. // Copyright © 2015年 ZhangAo. All rights reserved.
  10. //
  11. import Photos
  12. @objc
  13. protocol DKGroupDataManagerObserver {
  14. @objc optional func groupDidUpdate(_ groupId: String)
  15. @objc optional func groupDidRemove(_ groupId: String)
  16. @objc optional func group(_ groupId: String, didRemoveAssets assets: [DKAsset])
  17. @objc optional func group(_ groupId: String, didInsertAssets assets: [DKAsset])
  18. @objc optional func groupDidUpdateComplete(_ groupId: String)
  19. @objc optional func groupsDidInsert(_ groupIds: [String])
  20. }
  21. public class DKGroupDataManager: DKBaseManager, PHPhotoLibraryChangeObserver {
  22. public var groupIds: [String]?
  23. private var groups: [String : DKAssetGroup]?
  24. private var assets = [String: DKAsset]()
  25. public var assetGroupTypes: [PHAssetCollectionSubtype]?
  26. public var assetFetchOptions: PHFetchOptions?
  27. public var showsEmptyAlbums: Bool = true
  28. public var assetFilter: ((_ asset: PHAsset) -> Bool)?
  29. deinit {
  30. PHPhotoLibrary.shared().unregisterChangeObserver(self)
  31. }
  32. public func invalidate() {
  33. self.groupIds?.removeAll()
  34. self.groups?.removeAll()
  35. self.assets.removeAll()
  36. PHPhotoLibrary.shared().unregisterChangeObserver(self)
  37. }
  38. public func fetchGroups(_ completeBlock: @escaping (_ groups: [String]?, _ error: NSError?) -> Void) {
  39. if let assetGroupTypes = self.assetGroupTypes {
  40. DispatchQueue.global(qos: .userInteractive).async {
  41. [weak self] in
  42. guard let strongSelf = self else {
  43. return
  44. }
  45. guard strongSelf.groups == nil else {
  46. DispatchQueue.main.async {
  47. completeBlock(strongSelf.groupIds, nil)
  48. }
  49. return
  50. }
  51. var groups: [String : DKAssetGroup] = [:]
  52. var groupIds: [String] = []
  53. strongSelf.fetchGroups(assetGroupTypes: assetGroupTypes, block: { (collection) in
  54. let assetGroup = strongSelf.makeDKAssetGroup(with: collection)
  55. if strongSelf.showsEmptyAlbums || assetGroup.totalCount > 0 {
  56. groups[assetGroup.groupId] = assetGroup
  57. groupIds.append(assetGroup.groupId)
  58. }
  59. if !groupIds.isEmpty {
  60. strongSelf.updatePartial(groups: groups, groupIds: groupIds, completeBlock: completeBlock)
  61. }
  62. })
  63. PHPhotoLibrary.shared().register(strongSelf)
  64. if !groupIds.isEmpty {
  65. strongSelf.updatePartial(groups: groups, groupIds: groupIds, completeBlock: completeBlock)
  66. }
  67. /***** BEGIN THREEMA MODIFICATION: add reload data *********/
  68. let fetchMoments = PHAssetCollection.fetchMoments(with: nil)
  69. if (fetchMoments.count > 0) {
  70. var assetsCount = 0
  71. fetchMoments.enumerateObjects({ (collection, idx, stop) in
  72. assetsCount = assetsCount + collection.estimatedAssetCount
  73. })
  74. let momentsAssets = [PHAsset](repeating: PHAsset(), count: assetsCount)
  75. let collection = PHAssetCollection.transientAssetCollection(with: momentsAssets, title: NSLocalizedString("gallery_moments", comment: ""))
  76. let assetGroup = strongSelf.makeDKAssetGroup(with: collection)
  77. assetGroup.momentsResult = fetchMoments
  78. if strongSelf.showsEmptyAlbums || assetGroup.totalCount > 0 {
  79. groups[assetGroup.groupId] = assetGroup
  80. groupIds.insert(assetGroup.groupId, at: 1)
  81. }
  82. if !groupIds.isEmpty {
  83. strongSelf.updatePartial(groups: groups, groupIds: groupIds, completeBlock: completeBlock)
  84. }
  85. }
  86. /***** END THREEMA MODIFICATION: add reload data *********/
  87. }
  88. }
  89. }
  90. public func fetchGroupWithGroupId(_ groupId: String) -> DKAssetGroup {
  91. return self.groups![groupId]!
  92. }
  93. public func fetchGroupThumbnailForGroup(_ groupId: String, size: CGSize, options: PHImageRequestOptions, completeBlock: @escaping (_ image: UIImage?, _ info: [AnyHashable: Any]?) -> Void) {
  94. let group = self.fetchGroupWithGroupId(groupId)
  95. if group.totalCount == 0 {
  96. completeBlock(nil, nil)
  97. return
  98. }
  99. /***** BEGIN THREEMA MODIFICATION: add reload data *********/
  100. let latestAsset: DKAsset!
  101. if (group.momentsResult != nil && !group.momentsLoaded) {
  102. let result = PHAsset.fetchAssets(in: (group.momentsResult?.lastObject)!, options: nil)
  103. latestAsset = DKAsset(originalAsset: result.lastObject!)
  104. latestAsset.fetchImageWithSize(size, options: options, completeBlock: completeBlock)
  105. } else {
  106. latestAsset = DKAsset(originalAsset:group.fetchResult.lastObject!)
  107. latestAsset.fetchImageWithSize(size, options: options, completeBlock: completeBlock)
  108. }
  109. /***** END THREEMA MODIFICATION: add reload data *********/
  110. }
  111. public func fetchAsset(_ group: DKAssetGroup, index: Int) -> DKAsset {
  112. let originalAsset = self.fetchOriginalAsset(group, index: index)
  113. var asset = self.assets[originalAsset.localIdentifier]
  114. if asset == nil {
  115. asset = DKAsset(originalAsset:originalAsset)
  116. self.assets[originalAsset.localIdentifier] = asset
  117. }
  118. return asset!
  119. }
  120. public func fetchOriginalAsset(_ group: DKAssetGroup, index: Int) -> PHAsset {
  121. return group.fetchResult[group.totalCount - index - 1]
  122. }
  123. /***** BEGIN THREEMA MODIFICATION: add reload data *********/
  124. public func loadMomentsGroup(_ group: DKAssetGroup, completion: () -> ()) {
  125. var momentsAssets = [PHAsset]()
  126. group.momentsResult?.enumerateObjects({ (collection, idx, stop) in
  127. let fetchResult = PHAsset.fetchAssets(in: collection, options: nil)
  128. fetchResult.enumerateObjects({ (assets, jdx, stop2) in
  129. momentsAssets.append(assets)
  130. })
  131. })
  132. let collection = PHAssetCollection.transientAssetCollection(with: momentsAssets, title: NSLocalizedString("gallery_moments", comment: ""))
  133. self.updateGroup(group, collection: collection)
  134. self.updateGroup(group, fetchResult: PHAsset.fetchAssets(in: collection, options: self.assetFetchOptions))
  135. completion()
  136. }
  137. /***** END THREEMA MODIFICATION: add reload data *********/
  138. // MARK: - Private methods
  139. private func makeDKAssetGroup(with collection: PHAssetCollection) -> DKAssetGroup {
  140. let assetGroup = DKAssetGroup()
  141. assetGroup.groupId = collection.localIdentifier
  142. self.updateGroup(assetGroup, collection: collection)
  143. self.updateGroup(assetGroup, fetchResult: PHAsset.fetchAssets(in: collection, options: self.assetFetchOptions))
  144. return assetGroup
  145. }
  146. private func collectionTypeForSubtype(_ subtype: PHAssetCollectionSubtype) -> PHAssetCollectionType {
  147. return subtype.rawValue < PHAssetCollectionSubtype.smartAlbumGeneric.rawValue ? .album : .smartAlbum
  148. }
  149. private func fetchGroups(assetGroupTypes: [PHAssetCollectionSubtype], block: @escaping (PHAssetCollection) -> Void) {
  150. for (_, groupType) in assetGroupTypes.enumerated() {
  151. let fetchResult = PHAssetCollection.fetchAssetCollections(with: self.collectionTypeForSubtype(groupType),
  152. subtype: groupType,
  153. options: nil)
  154. fetchResult.enumerateObjects({ (collection, index, stop) in
  155. block(collection)
  156. })
  157. }
  158. }
  159. private func updatePartial(groups: [String : DKAssetGroup], groupIds: [String], completeBlock: @escaping (_ groups: [String]?, _ error: NSError?) -> Void) {
  160. self.groups = groups
  161. self.groupIds = groupIds
  162. DispatchQueue.main.async {
  163. completeBlock(groupIds, nil)
  164. }
  165. }
  166. private func updateGroup(_ group: DKAssetGroup, collection: PHAssetCollection) {
  167. group.groupName = collection.localizedTitle
  168. group.originalCollection = collection
  169. }
  170. private func updateGroup(_ group: DKAssetGroup, fetchResult: PHFetchResult<PHAsset>) {
  171. group.fetchResult = filterResults(fetchResult)
  172. group.totalCount = group.fetchResult.count
  173. }
  174. private func filterResults(_ fetchResult: PHFetchResult<PHAsset>) -> PHFetchResult<PHAsset> {
  175. guard let filter = assetFilter else { return fetchResult }
  176. var filtered = [PHAsset]()
  177. for i in 0..<fetchResult.count {
  178. if filter(fetchResult[i]) {
  179. filtered.append(fetchResult[i])
  180. }
  181. }
  182. let collection = PHAssetCollection.transientAssetCollection(with: filtered, title: nil)
  183. return PHAsset.fetchAssets(in: collection, options: nil)
  184. }
  185. // MARK: - PHPhotoLibraryChangeObserver methods
  186. public func photoLibraryDidChange(_ changeInstance: PHChange) {
  187. /***** BEGIN THREEMA MODIFICATION: check self.groups not nil *********/
  188. guard self.groups != nil else {
  189. return
  190. }
  191. /***** END THREEMA MODIFICATION: check self.groups not nil *********/
  192. for group in self.groups!.values {
  193. if let changeDetails = changeInstance.changeDetails(for: group.originalCollection) {
  194. if changeDetails.objectWasDeleted {
  195. self.groups![group.groupId] = nil
  196. self.notifyObserversWithSelector(#selector(DKGroupDataManagerObserver.groupDidRemove(_:)), object: group.groupId as AnyObject?)
  197. continue
  198. }
  199. if let objectAfterChanges = changeDetails.objectAfterChanges {
  200. self.updateGroup(self.groups![group.groupId]!, collection: objectAfterChanges)
  201. self.notifyObserversWithSelector(#selector(DKGroupDataManagerObserver.groupDidUpdate(_:)), object: group.groupId as AnyObject?)
  202. }
  203. }
  204. if let changeDetails = changeInstance.changeDetails(for: group.fetchResult) {
  205. let removedAssets = changeDetails.removedObjects.map{ DKAsset(originalAsset: $0) }
  206. if removedAssets.count > 0 {
  207. self.notifyObserversWithSelector(#selector(DKGroupDataManagerObserver.group(_:didRemoveAssets:)), object: group.groupId as AnyObject?, objectTwo: removedAssets as AnyObject?)
  208. }
  209. self.updateGroup(group, fetchResult: changeDetails.fetchResultAfterChanges)
  210. let insertedAssets = changeDetails.insertedObjects.map{ DKAsset(originalAsset: $0) }
  211. if insertedAssets.count > 0 {
  212. self.notifyObserversWithSelector(#selector(DKGroupDataManagerObserver.group(_:didInsertAssets:)), object: group.groupId as AnyObject?, objectTwo: insertedAssets as AnyObject?)
  213. }
  214. self.notifyObserversWithSelector(#selector(DKGroupDataManagerObserver.groupDidUpdateComplete(_:)), object: group.groupId as AnyObject?)
  215. }
  216. }
  217. if let assetGroupTypes = self.assetGroupTypes {
  218. var insertedGroupIds: [String] = []
  219. self.fetchGroups(assetGroupTypes: assetGroupTypes, block: { (collection) in
  220. if (self.groups![collection.localIdentifier] == nil) {
  221. let assetGroup = self.makeDKAssetGroup(with: collection)
  222. self.groups![assetGroup.groupId] = assetGroup
  223. self.groupIds!.append(assetGroup.groupId)
  224. insertedGroupIds.append(assetGroup.groupId)
  225. }
  226. })
  227. if (insertedGroupIds.count > 0) {
  228. self.notifyObserversWithSelector(#selector(DKGroupDataManagerObserver.groupsDidInsert(_:)), object: insertedGroupIds as AnyObject)
  229. }
  230. }
  231. }
  232. }