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:
agra
2026-05-11 09:52:17 +03:00
parent 3eba30358c
commit 77cda5a17e
5 changed files with 226 additions and 9 deletions

View File

@@ -6,9 +6,17 @@ import UIKit
/// `Photos.framework` bridge for `UxGallery` paginated asset queries,
/// cell-sized thumbnails via `PHCachingImageManager`, and on-demand
/// file resolution into the app cache.
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
/// Active Dart subscriber for the `ux/gallery/changes` event channel.
/// Push a `nil` event on every `photoLibraryDidChange` so callers
/// reload reactively against the committed library state the
/// observer fires after iOS commits, sidestepping the
/// `presentLimitedLibraryPicker` completion-vs-commit race.
private var libraryEventSink: FlutterEventSink?
public func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(
@@ -18,15 +26,69 @@ 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
}
/// `PHFetchResult` snapshots go stale after the limited-library
/// subset changes or any external Photos.app edit; observing
/// change events lets us drop the cache so the next `assets`
/// call re-fetches against the live library. Deferred to first
/// granted/limited authorization registering at plugin init
/// triggers iOS's permission evaluation on app launch even before
/// the user touches the picker.
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) {
// Callback runs on an arbitrary background thread; hop to main
// before touching `fetchCache` or pushing the Flutter event so
// we don't race the method-channel handler.
DispatchQueue.main.async { [weak self] in
self?.fetchCache.removeAll()
self?.libraryEventSink?(nil)
}
}
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))
}
}
@@ -36,10 +98,19 @@ public class GalleryPlugin: NSObject, NativePlugin {
}
result(nil)
case "presentLimitedLibraryPicker":
if #available(iOS 14, *), let vc = UxWindow.topViewController {
// Just dismissal signal the actual reload trigger is the
// `ux/gallery/changes` event channel driven by the library
// observer (which fires after iOS commits the new subset).
if #available(iOS 15, *), let vc = UxWindow.topViewController {
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: vc) { _ in
result(nil)
}
} else if #available(iOS 14, *), let vc = UxWindow.topViewController {
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: vc)
result(nil)
} else {
result(nil)
}
result(nil)
case "albums":
handleAlbums(call: call, result: result)
case "assets":
@@ -70,6 +141,12 @@ public class GalleryPlugin: NSObject, NativePlugin {
}
}
private static func isAuthorizedForLibrary(_ status: PHAuthorizationStatus) -> Bool {
if status == .authorized { return true }
if #available(iOS 14, *), status == .limited { return true }
return false
}
private static func permissionString(_ status: PHAuthorizationStatus) -> String {
switch status {
case .notDetermined: return "notDetermined"