gallery: library-change Stream + native observer parity
Add UxGallery.libraryChanges — a Stream<void> that emits whenever the underlying photo library reports a change, so picker UIs can drop their cached asset lists and reload reactively. - iOS: GalleryPlugin conforms to PHPhotoLibraryChangeObserver + FlutterStreamHandler; on photoLibraryDidChange the fetchCache is cleared and a void event is pushed over ux/gallery/changes. The observer is registered lazily on the first granted/limited authorization so plugin init doesn't trigger iOS's permission evaluation at app launch. - macOS: near-verbatim port of the iOS shape (same Photos.framework, same fetchCache staleness, same fix). - Android: registers a MediaStore ContentObserver on Files.getContentUri(VOLUME_EXTERNAL) with notifyForDescendants = true; observer lifecycle tracks the Dart subscription (registered on onListen, unregistered on onCancel / plugin detach). No native cache to invalidate today, but the pipeline is wired for the upcoming READ_MEDIA_VISUAL_USER_SELECTED limited-access work. - iOS presentLimitedLibraryPicker switched to the iOS 15+ completion- handler variant so the Dart await resolves on dismissal, not on presentation. The actual reload is now driven by the change observer (which fires after iOS commits the new subset), side-stepping the completion-vs-commit race that produced an off-by-one in the picker on consecutive MANAGE taps. - FakeUxGalleryBackend exposes emitLibraryChange() so tests can drive the reactive-reload wiring without going through the real method channel.
This commit is contained in:
@@ -5,9 +5,14 @@ import Photos
|
||||
/// macOS counterpart of the iOS gallery bridge — same `Photos.framework`
|
||||
/// data layer, with `NSImage` swapped in for `UIImage` and the
|
||||
/// limited-library picker dropped (macOS has no equivalent).
|
||||
public class GalleryPlugin: NSObject, NativePlugin {
|
||||
public class GalleryPlugin: NSObject, NativePlugin, PHPhotoLibraryChangeObserver, FlutterStreamHandler {
|
||||
private let imageManager = PHCachingImageManager()
|
||||
private var fetchCache: [String: PHFetchResult<PHAsset>] = [:]
|
||||
private var libraryObserverRegistered = false
|
||||
|
||||
/// Sink for the `ux/gallery/changes` event channel — emits void on
|
||||
/// every `photoLibraryDidChange` so picker UIs can reload reactively.
|
||||
private var libraryEventSink: FlutterEventSink?
|
||||
|
||||
public func register(with registrar: FlutterPluginRegistrar) {
|
||||
let channel = FlutterMethodChannel(
|
||||
@@ -17,15 +22,68 @@ public class GalleryPlugin: NSObject, NativePlugin {
|
||||
channel.setMethodCallHandler { [weak self] call, result in
|
||||
self?.handle(call: call, result: result)
|
||||
}
|
||||
let events = FlutterEventChannel(
|
||||
name: "ux/gallery/changes",
|
||||
binaryMessenger: registrar.messenger,
|
||||
)
|
||||
events.setStreamHandler(self)
|
||||
}
|
||||
|
||||
public func onListen(
|
||||
withArguments arguments: Any?,
|
||||
eventSink events: @escaping FlutterEventSink,
|
||||
) -> FlutterError? {
|
||||
libraryEventSink = events
|
||||
return nil
|
||||
}
|
||||
|
||||
public func onCancel(withArguments arguments: Any?) -> FlutterError? {
|
||||
libraryEventSink = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
/// Photos delivers change events only after the user grants access;
|
||||
/// defer registration until then so plugin init doesn't poke the
|
||||
/// permission machinery at app launch.
|
||||
private func ensureLibraryObserver() {
|
||||
guard !libraryObserverRegistered else { return }
|
||||
PHPhotoLibrary.shared().register(self)
|
||||
libraryObserverRegistered = true
|
||||
}
|
||||
|
||||
deinit {
|
||||
if libraryObserverRegistered {
|
||||
PHPhotoLibrary.shared().unregisterChangeObserver(self)
|
||||
}
|
||||
}
|
||||
|
||||
public func photoLibraryDidChange(_ changeInstance: PHChange) {
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.fetchCache.removeAll()
|
||||
self?.libraryEventSink?(nil)
|
||||
}
|
||||
}
|
||||
|
||||
private static func isAuthorizedForLibrary(_ status: PHAuthorizationStatus) -> Bool {
|
||||
if status == .authorized { return true }
|
||||
if #available(macOS 11.0, *), status == .limited { return true }
|
||||
return false
|
||||
}
|
||||
|
||||
private func handle(call: FlutterMethodCall, result: @escaping FlutterResult) {
|
||||
switch call.method {
|
||||
case "permission":
|
||||
result(Self.permissionString(Self.currentAuthorization()))
|
||||
let status = Self.currentAuthorization()
|
||||
if Self.isAuthorizedForLibrary(status) {
|
||||
ensureLibraryObserver()
|
||||
}
|
||||
result(Self.permissionString(status))
|
||||
case "requestPermission":
|
||||
Self.requestAuthorization { status in
|
||||
Self.requestAuthorization { [weak self] status in
|
||||
DispatchQueue.main.async {
|
||||
if Self.isAuthorizedForLibrary(status) {
|
||||
self?.ensureLibraryObserver()
|
||||
}
|
||||
result(Self.permissionString(status))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user