-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMediaLibrary.swift
262 lines (214 loc) · 7.67 KB
/
MediaLibrary.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
See the LICENSE.txt file for this sample’s licensing information.
Abstract:
The object that manages access to a person's Photos and the app's reading and writing tasks in the Photos library.
*/
import CoreSpotlight
import Photos
import SwiftUI
@Observable
final class MediaLibrary: Sendable {
enum Error: Swift.Error {
case missingLocalIdentifier
case failedToFetchAsset
}
// MARK: Properties
@MainActor var assets: [Asset]
@MainActor var albums: [Album]
// MARK: Lifecycle
@MainActor init() {
self.assets = []
self.albums = []
}
// MARK: Internal Methods
func load() {
Task.detached {
await self.fetchAssets()
await self.fetchAlbums()
}
}
@discardableResult
func createAsset(from image: Image) async throws -> Asset {
var assetPlaceholder: PHObjectPlaceholder?
// Convert the image.
let kitImage = await MainActor.run {
image.kit
}
// Create a new placeholder asset.
try await PHPhotoLibrary.shared().performChanges {
let request = PHAssetChangeRequest.creationRequestForAsset(from: kitImage)
assetPlaceholder = request.placeholderForCreatedAsset
}
// Process the input.
guard let identifier = assetPlaceholder?.localIdentifier else {
throw Error.missingLocalIdentifier
}
// Fetch assets.
let assets = PHAsset.fetchAssets(withLocalIdentifiers: [identifier], options: nil)
guard let phAsset = assets.firstObject else {
throw Error.failedToFetchAsset
}
let asset = Asset(phAsset: phAsset)
// Update the assets on the main thread.
await MainActor.run {
self.assets.append(asset)
}
// Index new entity with Spotlight.
Task {
try await CSSearchableIndex.default().indexAppEntities([asset.entity])
}
return asset
}
func deleteAssets(_ assets: [Asset]) async throws {
let toDelete = assets.map(\.phAsset)
try await PHPhotoLibrary.shared().performChanges {
PHAssetChangeRequest.deleteAssets(toDelete as NSFastEnumeration)
}
// Update the assets on the main thread.
await MainActor.run {
assets.forEach { asset in
self.assets.removeAll(where: { $0 == asset })
}
}
// Remove indexed entities from Spotlight.
Task {
try await CSSearchableIndex.default().deleteAppEntities(identifiedBy: assets.map(\.id), ofType: AssetEntity.self)
}
}
@discardableResult
func createAlbum(with name: String) async throws -> Album {
var collectionPlaceholder: PHObjectPlaceholder?
// Create a new placeholder album.
try await PHPhotoLibrary.shared().performChanges {
let createAlbumRequest = PHAssetCollectionChangeRequest.creationRequestForAssetCollection(withTitle: name)
collectionPlaceholder = createAlbumRequest.placeholderForCreatedAssetCollection
}
// Process any input.
guard let identifier = collectionPlaceholder?.localIdentifier else {
throw Error.missingLocalIdentifier
}
// Fetch an album.
let collections = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [identifier], options: nil)
guard let collection = collections.firstObject else {
throw Error.failedToFetchAsset
}
let album = Album(collection: collection)
// Update the albums on the main thread.
await MainActor.run {
self.albums.append(album)
}
// Index new entity with Spotlight.
Task {
try await CSSearchableIndex.default().indexAppEntities([album.entity])
}
return album
}
func deleteAlbums(_ albums: [Album]) async throws {
let toDelete = albums.map(\.collection)
try await PHPhotoLibrary.shared().performChanges {
PHAssetCollectionChangeRequest.deleteAssetCollections(toDelete as NSFastEnumeration)
}
// Update the albums on the main thread.
await MainActor.run {
albums.forEach { album in
self.albums.removeAll(where: { $0 == album })
}
}
// Remove indexed entities from Spotlight.
Task {
try await CSSearchableIndex.default().deleteAppEntities(identifiedBy: albums.map(\.id), ofType: AlbumEntity.self)
}
}
// MARK: Private Methods
private func fetchAssets() async {
// Request access to the person's library.
guard await getLibraryAuthorization() else {
return
}
// Fetch all photos from the photo library.
let fetchResult = PHAsset.fetchAssets(with: nil)
// Enumerate and insert assets.
var assets = [Asset]()
fetchResult.enumerateObjects { (object, count, stop) in
let asset = Asset(phAsset: object)
assets.append(asset)
}
// Update the assets on the main thread.
await MainActor.run { [assets] in
self.assets = assets
}
// Index updated entities with Spotlight.
let entities = assets.map(\.entity)
Task {
try await CSSearchableIndex.default().indexAppEntities(entities)
}
}
private func fetchAlbums() async {
// Request access to the person's library.
guard await getLibraryAuthorization() else {
return
}
// Fetch all albums from the photo library.
let fetchResult = PHAssetCollection.fetchAssetCollections(with: .album, subtype: .any, options: nil)
// Enumerate and insert albums.
var albums = [Album]()
fetchResult.enumerateObjects { (object, count, stop) in
let album = Album(collection: object)
albums.append(album)
}
// Update the albums on the main thread.
await MainActor.run { [albums] in
self.albums = albums
}
// Index updated entities with Spotlight.
let entities = albums.map(\.entity)
Task {
try await CSSearchableIndex.default().indexAppEntities(entities)
}
}
private func getLibraryAuthorization() async -> Bool {
switch PHPhotoLibrary.authorizationStatus(for: .readWrite) {
case .authorized:
print("Photo library access authorized.")
return true
case .notDetermined:
print("Photo library access not determined.")
return await PHPhotoLibrary.requestAuthorization(for: .readWrite) == .authorized
case .denied:
print("Photo library access denied.")
return false
case .limited:
print("Photo library access limited.")
return true
case .restricted:
print("Photo library access restricted.")
return false
@unknown default:
return false
}
}
}
extension MediaLibrary {
@MainActor
func assets(for identifiers: [Asset.ID]) -> [Asset] {
var toReturn = [Asset]()
identifiers.forEach { id in
toReturn += assets.filter { $0.id == id }
}
return toReturn
}
@MainActor
func albums(for identifiers: [Album.ID]) -> [Album] {
var toReturn = [Album]()
identifiers.forEach { id in
toReturn += albums.filter { $0.id == id }
}
return toReturn
}
@MainActor
func albums(matching string: String) -> [Album] {
albums.filter {
$0.title.lowercased().contains(string.lowercased())
}
}
}