Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
Material
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dmitriy Stepanets
Material
Commits
10d035e7
Commit
10d035e7
authored
Aug 03, 2016
by
Daniel Dahan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
issue-479: Updating change observation for PhotoLibrary
parent
ee817cba
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
134 additions
and
146 deletions
+134
-146
Sources/iOS/PhotoLibrary.swift
+134
-146
No files found.
Sources/iOS/PhotoLibrary.swift
View file @
10d035e7
...
@@ -60,6 +60,11 @@ public class PhotoLibraryMove: NSObject {
...
@@ -60,6 +60,11 @@ public class PhotoLibraryMove: NSObject {
}
}
}
}
public
struct
PhotoLibraryFetchResultDataSource
{
public
private(set)
var
fetchResult
:
PHFetchResult
<
PHObject
>
public
private(set)
var
objects
:
[
PHObject
]
}
@objc(PhotoLibraryDelegate)
@objc(PhotoLibraryDelegate)
public
protocol
PhotoLibraryDelegate
{
public
protocol
PhotoLibraryDelegate
{
/**
/**
...
@@ -172,20 +177,14 @@ public protocol PhotoLibraryDelegate {
...
@@ -172,20 +177,14 @@ public protocol PhotoLibraryDelegate {
@objc(PhotoLibrary)
@objc(PhotoLibrary)
public
class
PhotoLibrary
:
NSObject
{
public
class
PhotoLibrary
:
NSObject
{
/// A reference to the type currently being managed.
public
private(set)
var
type
:
PHAssetCollectionType
?
/// A reference to the subtype currently being managed.
public
private(set)
var
subtype
:
PHAssetCollectionSubtype
?
/// A reference to the PHCachingImageManager.
/// A reference to the PHCachingImageManager.
public
private(set)
lazy
var
cachingImageManager
=
PHCachingImageManager
()
public
private(set)
lazy
var
cachingImageManager
=
PHCachingImageManager
()
/// A reference to
the collection PHFetchResult
.
/// A reference to
all current PHFetchResults
.
public
private(set)
var
collectionFetchResult
:
PHFetchResult
<
PHAssetCollection
>
?
public
private(set)
lazy
var
fetchResults
=
[
PhotoLibraryFetchResultDataSource
]()
/// The assets used in the album.
/// The assets used in the album.
public
private(set)
var
collections
:
[
PhotoLibraryDataSource
]
!
{
public
private(set)
var
collections
=
[
PhotoLibraryDataSource
]()
{
willSet
{
willSet
{
guard
.
authorized
==
authorizationStatus
else
{
guard
.
authorized
==
authorizationStatus
else
{
return
return
...
@@ -224,58 +223,58 @@ public class PhotoLibrary: NSObject {
...
@@ -224,58 +223,58 @@ public class PhotoLibrary: NSObject {
prepare
()
prepare
()
}
}
/**
//
/**
Fetch all the PHAssetCollections asynchronously based on a type and subtype.
//
Fetch all the PHAssetCollections asynchronously based on a type and subtype.
- Parameter type: A PHAssetCollectionType.
//
- Parameter type: A PHAssetCollectionType.
- Parameter subtype: A PHAssetCollectionSubtype.
//
- Parameter subtype: A PHAssetCollectionSubtype.
- Parameter completion: A completion block.
//
- Parameter completion: A completion block.
*/
//
*/
public
func
fetchAssetCollections
(
with
type
:
PHAssetCollectionType
,
subtype
:
PHAssetCollectionSubtype
,
completion
:
([
PhotoLibraryDataSource
])
->
Void
)
{
//
public func fetchAssetCollections(with type: PHAssetCollectionType, subtype: PHAssetCollectionSubtype, completion: ([PhotoLibraryDataSource]) -> Void) {
self
.
type
=
type
//
self.type = type
self
.
subtype
=
subtype
//
self.subtype = subtype
//
DispatchQueue
.
global
(
qos
:
.
default
)
.
async
{
[
weak
self
,
type
=
type
,
subtype
=
subtype
,
completion
=
completion
]
in
//
DispatchQueue.global(qos: .default).async { [weak self, type = type, subtype = subtype, completion = completion] in
guard
let
s
=
self
else
{
//
guard let s = self else {
return
//
return
}
//
}
//
defer
{
//
defer {
DispatchQueue
.
main
.
async
{
[
weak
self
]
in
//
DispatchQueue.main.async { [weak self] in
guard
let
s
=
self
else
{
//
guard let s = self else {
return
//
return
}
//
}
completion
(
s
.
collections
)
//
completion(s.collections)
}
//
}
}
//
}
//
let
options
=
PHFetchOptions
()
//
let options = PHFetchOptions()
options
.
includeHiddenAssets
=
true
//
options.includeHiddenAssets = true
options
.
includeAllBurstAssets
=
true
//
options.includeAllBurstAssets = true
options
.
wantsIncrementalChangeDetails
=
true
//
options.wantsIncrementalChangeDetails = true
//
s
.
collectionFetchResult
=
PHAssetCollection
.
fetchAssetCollections
(
with
:
type
,
subtype
:
subtype
,
options
:
options
)
//
s.collectionFetchResult = PHAssetCollection.fetchAssetCollections(with: type, subtype: subtype, options: options)
//
s
.
collectionFetchResult
?
.
enumerateObjects
(
options
:
[
.
concurrent
])
{
[
weak
self
]
(
collection
,
_
,
_
)
in
//
s.collectionFetchResult?.enumerateObjects(options: [.concurrent]) { [weak self] (collection, _, _) in
guard
let
s
=
self
else
{
//
guard let s = self else {
return
//
return
}
//
}
//
let
options
=
PHFetchOptions
()
//
let options = PHFetchOptions()
let
descriptor
=
NSSortDescriptor
(
key
:
"creationDate"
,
ascending
:
false
)
//
let descriptor = NSSortDescriptor(key: "creationDate", ascending: false)
options
.
sortDescriptors
=
[
descriptor
]
//
options.sortDescriptors = [descriptor]
options
.
includeHiddenAssets
=
true
//
options.includeHiddenAssets = true
options
.
includeAllBurstAssets
=
true
//
options.includeAllBurstAssets = true
options
.
wantsIncrementalChangeDetails
=
true
//
options.wantsIncrementalChangeDetails = true
//
s
.
fetchAssets
(
in
:
collection
,
options
:
options
)
{
[
weak
self
]
(
assets
,
fetchResult
)
in
//
s.fetchAssets(in: collection, options: options) { [weak self] (assets, fetchResult) in
guard
let
s
=
self
else
{
//
guard let s = self else {
return
//
return
}
//
}
s
.
collections
.
append
(
PhotoLibraryDataSource
(
fetchResult
:
fetchResult
,
collection
:
collection
,
assets
:
assets
))
//
s.collections.append(PhotoLibraryDataSource(fetchResult: fetchResult, collection: collection, assets: assets))
}
//
}
}
//
}
}
//
}
}
//
}
/**
/**
Performes an asynchronous change to the PHPhotoLibrary database.
Performes an asynchronous change to the PHPhotoLibrary database.
...
@@ -290,15 +289,9 @@ public class PhotoLibrary: NSObject {
...
@@ -290,15 +289,9 @@ public class PhotoLibrary: NSObject {
/// A method used to prepare the instance object.
/// A method used to prepare the instance object.
private
func
prepare
()
{
private
func
prepare
()
{
prepareCollections
()
prepareChangeObservers
()
prepareChangeObservers
()
}
}
/// Prepares the collections.
private
func
prepareCollections
()
{
collections
=
[
PhotoLibraryDataSource
]()
}
/// A method used to enable change observation.
/// A method used to enable change observation.
private
func
prepareChangeObservers
()
{
private
func
prepareChangeObservers
()
{
PHPhotoLibrary
.
shared
()
.
register
(
self
)
PHPhotoLibrary
.
shared
()
.
register
(
self
)
...
@@ -444,6 +437,11 @@ extension PhotoLibrary {
...
@@ -444,6 +437,11 @@ extension PhotoLibrary {
private
func
fetch
<
T
:
PHFetchResult
<
U
>
,
U
:
PHObject
>
(
result
:
T
,
completion
:
([
U
],
T
)
->
Void
)
{
private
func
fetch
<
T
:
PHFetchResult
<
U
>
,
U
:
PHObject
>
(
result
:
T
,
completion
:
([
U
],
T
)
->
Void
)
{
var
objects
=
[
U
]()
var
objects
=
[
U
]()
defer
{
// Stores for change observation.
fetchResults
.
append
(
PhotoLibraryFetchResultDataSource
(
fetchResult
:
result
as!
PHFetchResult
<
PHObject
>
,
objects
:
objects
))
}
result
.
enumerateObjects
({
(
collection
,
_
,
_
)
in
result
.
enumerateObjects
({
(
collection
,
_
,
_
)
in
objects
.
append
(
collection
)
objects
.
append
(
collection
)
})
})
...
@@ -709,84 +707,74 @@ extension PhotoLibrary: PHPhotoLibraryChangeObserver {
...
@@ -709,84 +707,74 @@ extension PhotoLibrary: PHPhotoLibraryChangeObserver {
photo library.
photo library.
*/
*/
public
func
photoLibraryDidChange
(
_
changeInfo
:
PHChange
)
{
public
func
photoLibraryDidChange
(
_
changeInfo
:
PHChange
)
{
guard
let
t
=
type
else
{
for
dataSource
in
fetchResults
{
return
print
(
dataSource
)
}
}
guard
let
st
=
subtype
else
{
// fetchAssetCollections(with: t, subtype: st) { [weak self, oldCollections = oldCollections] _ in
return
// DispatchQueue.main.async { [weak self, oldCollections = oldCollections] in
}
// guard let s = self else {
// return
guard
let
oldCollections
=
collections
else
{
// }
return
//
}
// // Notify about the general change.
// s.delegate?.photoLibrary?(photoLibrary: s, didChange: changeInfo)
collections
.
removeAll
()
//
// // Notifiy about specific changes.
fetchAssetCollections
(
with
:
t
,
subtype
:
st
)
{
[
weak
self
,
oldCollections
=
oldCollections
]
_
in
// s.collectionFetchResult?.enumerateObjects(options: .concurrent) { [weak self, changeInfo = changeInfo] (collection, _, _) in
DispatchQueue
.
main
.
async
{
[
weak
self
,
oldCollections
=
oldCollections
]
in
// guard let s = self else {
guard
let
s
=
self
else
{
// return
return
// }
}
//
// guard let details = changeInfo.changeDetails(for: collection) else {
// Notify about the general change.
// return
s
.
delegate
?
.
photoLibrary
?(
photoLibrary
:
s
,
didChange
:
changeInfo
)
// }
//
// Notifiy about specific changes.
// guard let afterChanges = details.objectAfterChanges else {
s
.
collectionFetchResult
?
.
enumerateObjects
(
options
:
.
concurrent
)
{
[
weak
self
,
changeInfo
=
changeInfo
]
(
collection
,
_
,
_
)
in
// return
guard
let
s
=
self
else
{
// }
return
//
}
// s.delegate?.photoLibrary?(photoLibrary: s, beforeChanges: details.objectBeforeChanges, afterChanges: afterChanges, assetContentChanged: details.assetContentChanged, objectWasDeleted: details.objectWasDeleted)
// }
guard
let
details
=
changeInfo
.
changeDetails
(
for
:
collection
)
else
{
//
return
// for i in 0..<oldCollections.count {
}
// let dataSource = oldCollections[i]
//
guard
let
afterChanges
=
details
.
objectAfterChanges
else
{
// if let details = changeInfo.changeDetails(for: dataSource.fetchResult as! PHFetchResult<AnyObject>) {
return
// s.delegate?.photoLibrary?(photoLibrary: s, fetchBeforeChanges: details.fetchResultBeforeChanges, fetchAfterChanges: details.fetchResultAfterChanges)
}
//
// guard details.hasIncrementalChanges else {
s
.
delegate
?
.
photoLibrary
?(
photoLibrary
:
s
,
beforeChanges
:
details
.
objectBeforeChanges
,
afterChanges
:
afterChanges
,
assetContentChanged
:
details
.
assetContentChanged
,
objectWasDeleted
:
details
.
objectWasDeleted
)
// return
}
// }
//
for
i
in
0
..<
oldCollections
.
count
{
// let removedIndexes = details.removedIndexes
let
dataSource
=
oldCollections
[
i
]
// let insertedIndexes = details.insertedIndexes
// let changedIndexes = details.changedIndexes
if
let
details
=
changeInfo
.
changeDetails
(
for
:
dataSource
.
fetchResult
as!
PHFetchResult
<
AnyObject
>
)
{
//
s
.
delegate
?
.
photoLibrary
?(
photoLibrary
:
s
,
fetchBeforeChanges
:
details
.
fetchResultBeforeChanges
,
fetchAfterChanges
:
details
.
fetchResultAfterChanges
)
// if nil != removedIndexes {
// s.delegate?.photoLibrary?(photoLibrary: s, removed: removedIndexes!, for: details.removedObjects)
guard
details
.
hasIncrementalChanges
else
{
// }
return
//
}
// if nil != insertedIndexes {
// s.delegate?.photoLibrary?(photoLibrary: s, inserted: insertedIndexes!, for: details.insertedObjects)
let
removedIndexes
=
details
.
removedIndexes
// }
let
insertedIndexes
=
details
.
insertedIndexes
//
let
changedIndexes
=
details
.
changedIndexes
// if nil != changedIndexes {
// s.delegate?.photoLibrary?(photoLibrary: s, changed: changedIndexes!, for: details.changedObjects)
if
nil
!=
removedIndexes
{
// }
s
.
delegate
?
.
photoLibrary
?(
photoLibrary
:
s
,
removed
:
removedIndexes
!
,
for
:
details
.
removedObjects
)
//
}
// var moves = [PhotoLibraryMove]()
//
if
nil
!=
insertedIndexes
{
// if details.hasMoves {
s
.
delegate
?
.
photoLibrary
?(
photoLibrary
:
s
,
inserted
:
insertedIndexes
!
,
for
:
details
.
insertedObjects
)
// details.enumerateMoves { (from, to) in
}
// moves.append(PhotoLibraryMove(from: from, to: to))
// }
if
nil
!=
changedIndexes
{
// }
s
.
delegate
?
.
photoLibrary
?(
photoLibrary
:
s
,
changed
:
changedIndexes
!
,
for
:
details
.
changedObjects
)
//
}
// s.delegate?.photoLibrary?(photoLibrary: s, removedIndexes: removedIndexes, insertedIndexes: insertedIndexes, changedIndexes: changedIndexes, has: moves)
// }
var
moves
=
[
PhotoLibraryMove
]()
// }
// }
if
details
.
hasMoves
{
// }
details
.
enumerateMoves
{
(
from
,
to
)
in
moves
.
append
(
PhotoLibraryMove
(
from
:
from
,
to
:
to
))
}
}
s
.
delegate
?
.
photoLibrary
?(
photoLibrary
:
s
,
removedIndexes
:
removedIndexes
,
insertedIndexes
:
insertedIndexes
,
changedIndexes
:
changedIndexes
,
has
:
moves
)
}
}
}
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment