Skip to content

Commit 865d330

Browse files
authored
Resolve photo deletion bug (#94)
* Reset uploaded items list during tidy up * Bump version
1 parent 61c55d1 commit 865d330

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

Tree Tracker/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundlePackageType</key>
1818
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>0.10.8</string>
20+
<string>0.10.9</string>
2121
<key>CFBundleVersion</key>
2222
<string>$(CURRENT_PROJECT_VERSION)</string>
2323
<key>ITSAppUsesNonExemptEncryption</key>

Tree Tracker/Services/Database.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ final class Database {
214214
}
215215
}
216216
}
217+
218+
func clearUploadedItems(completion: @escaping () -> Void) {
219+
dbQueue?.asyncWrite { db in
220+
try? UploadedTree.deleteAll(db)
221+
} completion: { db, result in
222+
switch result {
223+
case .failure(let error):
224+
Rollbar.errorError(error, data: nil, context: "Error deleting uploaded items")
225+
default:
226+
DispatchQueue.main.async {
227+
completion()
228+
}
229+
}
230+
}
231+
}
217232

218233
func update(tree: LocalTree, completion: @escaping () -> Void) {
219234
dbQueue?.asyncWrite { db in

Tree Tracker/Services/ProtectEarth/ProtectEarthTreeService.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,36 @@ class ProtectEarthTreeService: TreeService {
1212

1313
// Complete any local tidy up following an upload session
1414
func tidyUp() {
15-
database.fetchAll(UploadedTree.self) { uploadItems in
15+
database.fetchAll(UploadedTree.self) { [weak self] uploadItems in
1616
if uploadItems.isNotEmpty {
1717
let assetManager = PHAssetManager()
1818
let assetIds = uploadItems.map { $0.phImageId }
19-
Rollbar.infoMessage("Cleaning up \(uploadItems.count) tree photos")
20-
assetManager.deletePhotoAssets(withIds: assetIds)
19+
assetManager.deletePhotoAssets(withIds: assetIds) { success, error in
20+
if success {
21+
print("Deleted \(assetIds.count) photos successfully")
22+
Rollbar.infoMessage("Successfully cleared photos from library",
23+
data: ["assetCount": assetIds.count],
24+
context: "PHAssetManager.deletePhotoAssets")
25+
26+
} else {
27+
print("Photos could not be cleared: \(error!.localizedDescription)")
28+
Rollbar.errorError(error!,
29+
data: nil,
30+
context: "PHAssetManager.deletePhotoAssets")
31+
}
32+
/*
33+
If we did not delete photos this is probably because the user declined this option.
34+
35+
We should clear down the uploaded items list regardless because otherwise we risk
36+
deleting those photos at a later date.
37+
38+
A consequence of this is that if the user declines, they will always have to clean up
39+
those photos manually, since the app will lose track of them.
40+
*/
41+
self?.database.clearUploadedItems {
42+
print("Uploaded items list cleared")
43+
}
44+
}
2145
}
2246
}
2347
}

Tree Tracker/Utilities/PHAssetManager.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import Foundation
22
import Photos
33
import UIKit
4+
import RollbarNotifier
45

56
protocol AssetManaging {
67
func findAssets(for ids: [String]) -> [PHAsset]
78
func save(image: UIImage, location: CLLocation?, completion: @escaping (Result<PHAsset, PHPhotoLibrarySaveImageError>) -> Void)
8-
func deletePhotoAssets(withIds assetIds: [String])
9+
func deletePhotoAssets(withIds assetIds: [String], completion: @escaping (Bool, Error?) -> Void)
910
}
1011

1112
struct PHAssetManager: AssetManaging {
@@ -27,12 +28,15 @@ struct PHAssetManager: AssetManaging {
2728
PHPhotoLibrary.shared().save(image: image, location: location, completion: completion)
2829
}
2930

30-
func deletePhotoAssets(withIds assetIds: [String]) {
31+
func deletePhotoAssets(withIds assetIds: [String], completion: @escaping (Bool, Error?) -> Void) {
3132
let options = PHFetchOptions()
3233
options.wantsIncrementalChangeDetails = false
3334
let assets = PHAsset.fetchAssets(withLocalIdentifiers: assetIds, options: options)
34-
PHPhotoLibrary.shared().performChanges({
35+
36+
PHPhotoLibrary.shared().performChanges {
3537
PHAssetChangeRequest.deleteAssets(assets)
36-
})
38+
} completionHandler: { success, error in
39+
completion(success, error)
40+
}
3741
}
3842
}

Unit Tests/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>0.10.8</string>
18+
<string>0.10.9</string>
1919
<key>CFBundleVersion</key>
2020
<string>1</string>
2121
</dict>

0 commit comments

Comments
 (0)