Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 选图后立即预览并后台读取 iCloud 照片 (#6242)

Dash91634b5552f1
diff --git a/apps/mobile/modules/cindy-photo-picker/expo-module.config.json b/apps/mobile/modules/cindy-photo-picker/expo-module.config.json
new file mode 100644
index 0000000000..03e97b4566
--- /dev/null
+++ b/apps/mobile/modules/cindy-photo-picker/expo-module.config.json
@@ -0,0 +1,6 @@
+{
+ "platforms": ["apple"],
+ "apple": {
+ "modules": ["CindyPhotoPickerModule"]
+ }
+}
diff --git a/apps/mobile/modules/cindy-photo-picker/ios/CindyPhotoPicker.podspec b/apps/mobile/modules/cindy-photo-picker/ios/CindyPhotoPicker.podspec
new file mode 100644
index 0000000000..624c903e96
--- /dev/null
+++ b/apps/mobile/modules/cindy-photo-picker/ios/CindyPhotoPicker.podspec
@@ -0,0 +1,15 @@
+Pod::Spec.new do |s|
+ s.name = 'CindyPhotoPicker'
+ s.version = '0.1.0'
+ s.summary = 'Immediate photo selection with deferred, cancellable file loading.'
+ s.description = s.summary
+ s.license = { :type => 'Apache-2.0' }
+ s.author = 'Cindy'
+ s.homepage = 'https://github.com/makecindy/cindy'
+ s.source = { git: s.homepage }
+ s.platforms = { :ios => '16.4' }
+ s.swift_version = '5.9'
+ s.static_framework = true
+ s.dependency 'ExpoModulesCore'
+ s.source_files = '**/*.swift'
+end
diff --git a/apps/mobile/modules/cindy-photo-picker/ios/CindyPhotoPickerModule.swift b/apps/mobile/modules/cindy-photo-picker/ios/CindyPhotoPickerModule.swift
new file mode 100644
index 0000000000..d1dd13e2ed
--- /dev/null
+++ b/apps/mobile/modules/cindy-photo-picker/ios/CindyPhotoPickerModule.swift
@@ -0,0 +1,203 @@
+import ExpoModulesCore
+import PhotosUI
+import UniformTypeIdentifiers
+import ImageIO
+
+/// PHPicker's selection callback precedes iCloud loading. Keep the item providers
+/// until the existing attachment queue consumes or abandons each selection.
+public final class CindyPhotoPickerModule: Module {
+ private var picker: PHPickerViewController?
+ private var pickerDelegate: PhotoPickerDelegate?
+ private var pickerPromise: Promise?
+ private var selections: [String: NSItemProvider] = [:]
+ private var loads: [String: PhotoLoad] = [:]
+
+ public func definition() -> ModuleDefinition {
+ Name("CindyPhotoPicker")
+ AsyncFunction("pick") { (limit: Int, promise: Promise) in
+ self.present(limit: limit, promise: promise)
+ }.runOnQueue(.main)
+ AsyncFunction("load") { (id: String, promise: Promise) in
+ self.load(id: id, promise: promise)
+ }.runOnQueue(.main)
+ AsyncFunction("cancel") { (id: String) in
+ self.cancel(id: id)
+ }.runOnQueue(.main)
+ AsyncFunction("release") { (id: String) in
+ self.selections.removeValue(forKey: id)
+ self.cancel(id: id)
+ }.runOnQueue(.main)
+ OnDestroy {
+ DispatchQueue.main.async {
+ self.pickerPromise?.resolve([])
+ self.pickerPromise = nil
+ self.picker?.dismiss(animated: false)
+ self.picker = nil
+ self.pickerDelegate = nil
+ self.selections.removeAll()
+ for id in Array(self.loads.keys) { self.cancel(id: id) }
+ }
+ }
+ }
+
+ private func present(limit: Int, promise: Promise) {
+ guard picker == nil else {
+ promise.reject("ERR_PHOTO_PICKER_BUSY", "Photo picker is already open.")
+ return
+ }
+ guard let presenter = appContext?.utilities?.currentViewController() else {
+ promise.reject("ERR_PHOTO_PICKER_PRESENTER", "No view controller for photo picker.")
+ return
+ }
+ var configuration = PHPickerConfiguration(photoLibrary: .shared())
+ configuration.filter = .images
+ configuration.selection = .ordered
+ configuration.selectionLimit = max(1, limit)
+ configuration.preferredAssetRepresentationMode = .current
+ let picker = PHPickerViewController(configuration: configuration)
+ let delegate = PhotoPickerDelegate { [weak self, weak picker] results in
+ guard let self, let picker, self.picker === picker else { return }
+ let selected = results.enumerated().map { index, result -> [String: Any] in
+ let id = UUID().uuidString
+ self.selections[id] = result.itemProvider
+ var value: [String: Any] = [
+ "id": id,
+ "name": result.itemProvider.suggestedName ?? "photo-\(index + 1)",
+ "previewUri": ""
+ ]
+ // Read only a small LOCAL thumbnail. Never wait for an original or ask
+ // for broader library permission just to preview a selected photo.
+ let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)
+ if (status == .authorized || status == .limited), let assetID = result.assetIdentifier,
+ let asset = PHAsset.fetchAssets(withLocalIdentifiers: [assetID], options: nil).firstObject {
+ value["width"] = asset.pixelWidth
+ value["height"] = asset.pixelHeight
+ let options = PHImageRequestOptions()
+ options.isNetworkAccessAllowed = false
+ options.isSynchronous = true
+ options.deliveryMode = .fastFormat
+ options.resizeMode = .fast
+ PHImageManager.default().requestImage(
+ for: asset, targetSize: CGSize(width: 216, height: 216),
+ contentMode: .aspectFit, options: options
+ ) { image, _ in
+ if let data = image?.jpegData(compressionQuality: 0.7) {
+ value["previewUri"] = "data:image/jpeg;base64,\(data.base64EncodedString())"
+ }
+ }
+ }
+ return value
+ }
+ // Resolve before dismissal so the composer already has the selection on
+ // its first visible frame. No file representation is requested here.
+ self.pickerPromise?.resolve(selected)
+ self.pickerPromise = nil
+ self.picker = nil
+ self.pickerDelegate = nil
+ picker.dismiss(animated: true)
+ }
+ picker.delegate = delegate
+ picker.presentationController?.delegate = delegate
+ self.picker = picker
+ pickerDelegate = delegate
+ pickerPromise = promise
+ presenter.present(picker, animated: true)
+ picker.presentationController?.delegate = delegate
+ }
+
+ private func load(id: String, promise: Promise) {
+ guard let provider = selections[id] else {
+ promise.reject("ERR_PHOTO_SELECTION_RELEASED", "Photo selection is no longer available.")
+ return
+ }
+ guard loads[id] == nil else {
+ promise.reject("ERR_PHOTO_LOAD_BUSY", "Photo is already loading.")
+ return
+ }
+ let load = PhotoLoad(promise: promise)
+ loads[id] = load
+ // NSItemProvider preserves the picker grant even for a photo outside the
+ // limited library. This also keeps edits, GIFs and the current representation.
+ load.progress = provider.loadFileRepresentation(forTypeIdentifier: UTType.image.identifier) { [weak self] url, error in
+ var copy: URL?
+ var failure = error
+ if let url, failure == nil {
+ do {
+ let directory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
+ .appendingPathComponent("ImagePicker", isDirectory: true)
+ try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
+ let target = directory.appendingPathComponent("cindy-photo-\(UUID().uuidString)")
+ .appendingPathExtension(url.pathExtension)
+ // The provider's URL is valid only inside this callback.
+ do {
+ try FileManager.default.copyItem(at: url, to: target)
+ } catch {
+ try? FileManager.default.removeItem(at: target)
+ throw error
+ }
+ copy = target
+ } catch { failure = error }
+ }
+ let copied = copy
+ let loadError = failure
+ DispatchQueue.main.async {
+ guard let self, self.loads[id] === load else {
+ if let copied { try? FileManager.default.removeItem(at: copied) }
+ return
+ }
+ self.loads.removeValue(forKey: id)
+ guard let copied, loadError == nil else {
+ load.promise.reject("ERR_PHOTO_LOAD", loadError?.localizedDescription ?? "Unable to read photo.")
+ return
+ }
+ let type = UTType(filenameExtension: copied.pathExtension)
+ let name = provider.suggestedName.map { URL(fileURLWithPath: $0).deletingPathExtension().lastPathComponent }
+ ?? "photo"
+ let size = (try? copied.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0
+ var result: [String: Any] = [
+ "uri": copied.absoluteString,
+ "fileName": copied.pathExtension.isEmpty ? name : "\(name).\(copied.pathExtension)",
+ "mimeType": type?.preferredMIMEType ?? "application/octet-stream",
+ "fileSize": size
+ ]
+ // Read dimensions without decoding full pixels. Limited-library photos
+ // have no PHAsset metadata, and edits can change the original dimensions.
+ if let source = CGImageSourceCreateWithURL(copied as CFURL, nil),
+ let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [CFString: Any],
+ let width = properties[kCGImagePropertyPixelWidth] as? Int,
+ let height = properties[kCGImagePropertyPixelHeight] as? Int {
+ let orientation = properties[kCGImagePropertyOrientation] as? Int ?? 1
+ let rotated = (5...8).contains(orientation)
+ result["width"] = rotated ? height : width
+ result["height"] = rotated ? width : height
+ }
+ load.promise.resolve(result)
+ }
+ }
+ }
+
+ private func cancel(id: String) {
+ guard let load = loads.removeValue(forKey: id) else { return }
+ load.progress?.cancel()
+ load.promise.reject("ERR_PHOTO_CANCELLED", "Photo loading was cancelled.")
+ // Keep the provider for the existing failed-card retry. Late callbacks are
+ // identified by PhotoLoad identity and dispose only their own file copy.
+ }
+}
+
+private final class PhotoLoad {
+ let promise: Promise
+ var progress: Progress?
+ init(promise: Promise) { self.promise = promise }
+}
+
+private final class PhotoPickerDelegate: NSObject, PHPickerViewControllerDelegate, UIAdaptivePresentationControllerDelegate {
+ let onResults: ([PHPickerResult]) -> Void
+ init(onResults: @escaping ([PHPickerResult]) -> Void) { self.onResults = onResults }
+ func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
+ onResults(results)
+ }
+ func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
+ onResults([])
+ }
+}
diff --git a/apps/mobile/src/__tests__/deferredPhotoLibrary.test.ts b/apps/mobile/src/__tests__/deferredPhotoLibrary.test.ts
new file mode 100644
index 0000000000..2b4bf3d790
--- /dev/null
+++ b/apps/mobile/src/__tests__/deferredPhotoLibrary.test.ts
@@ -0,0 +1,192 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { i18n } from '@/i18n';
+import { pickDeferredLibraryPhotos } from '@/session/deferredPhotoLibrary';
+import { createMobileLocalAttachmentUploadController } from '@/session/mobileLocalAttachmentUpload';
+import type { PendingLocalAttachmentUpload } from '@/session/mobileLocalAttachmentUpload';
+import type { RemoteSerializedAttachment } from '@/session/types';
+
+const mock = vi.hoisted(() => ({
+ available: true,
+ native: { pick: vi.fn(), load: vi.fn(), cancel: vi.fn(), release: vi.fn() },
+ deleteAsync: vi.fn(),
+}));
+vi.mock('expo-modules-core', () => ({ requireOptionalNativeModule: () => mock.available ? mock.native : null }));
+vi.mock('expo-file-system/legacy', () => ({ deleteAsync: mock.deleteAsync }));
+
+const selection = (id: string) => ({ id, name: `${id}.jpg`, previewUri: `data:image/jpeg;base64,${id}` });
+const file = (id: string) => ({ uri: `file:///cache/${id}.jpg`, fileName: `${id}.jpg`, mimeType: 'image/jpeg', fileSize: 100 });
+const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
+const initialLanguage = i18n.language;
+
+afterEach(async () => { await i18n.changeLanguage(initialLanguage); });
+
+beforeEach(() => {
+ vi.resetAllMocks();
+ mock.available = true;
+ mock.native.pick.mockResolvedValue([selection('cloud'), selection('local')]);
+ mock.native.load.mockImplementation(async (id: string) => file(id));
+ mock.native.cancel.mockResolvedValue(undefined);
+ mock.native.release.mockResolvedValue(undefined);
+ mock.deleteAsync.mockResolvedValue(undefined);
+});
+
+function makeController() {
+ let pending: readonly PendingLocalAttachmentUpload[] = [];
+ const uploaded = vi.fn();
+ const upload = vi.fn(async (candidate: { name: string }) => ({ id: candidate.name, name: candidate.name } as RemoteSerializedAttachment));
+ const controller = createMobileLocalAttachmentUploadController({
+ preprocess: async (source) => source,
+ statSize: async () => 100,
+ assertSize: () => {},
+ upload,
+ discard: vi.fn(),
+ onPendingChange: (value) => { pending = value; },
+ onUploaded: uploaded,
+ onFailed: vi.fn(),
+ });
+ return { controller, upload, uploaded, pending: () => pending };
+}
+
+describe('deferred photo library', () => {
+ it.each([
+ ['zh-CN', '没有读取到可上传的图片。', '附件上传已取消。'],
+ ['en', 'No uploadable image was read.', 'Attachment upload cancelled.'],
+ ])('localizes native failures using the current %s app language', async (locale, failed, cancelled) => {
+ const candidates = (await pickDeferredLibraryPhotos(1))!;
+ // A language change after selection must also apply to a later load/retry.
+ await i18n.changeLanguage(locale);
+ for (const code of ['ERR_PHOTO_PICKER_BUSY', 'ERR_PHOTO_PICKER_PRESENTER']) {
+ mock.native.pick.mockRejectedValueOnce(Object.assign(new Error('Native English message'), { code }));
+ await expect(pickDeferredLibraryPhotos(1)).rejects.toThrow(failed);
+ }
+ for (const code of ['ERR_PHOTO_LOAD', 'ERR_PHOTO_LOAD_BUSY', 'ERR_PHOTO_SELECTION_RELEASED', 'ERR_PHOTO_CANCELLED', undefined]) {
+ mock.native.load.mockRejectedValueOnce(Object.assign(new Error('Provider failure at /private/photo.jpg'), { code }));
+ await expect(candidates[0].resolve!()).rejects.toThrow(code === 'ERR_PHOTO_CANCELLED' ? cancelled : failed);
+ }
+ expect(mock.native.release).not.toHaveBeenCalled();
+ await expect(candidates[0].resolve!()).resolves.toMatchObject({ uri: file('cloud').uri });
+ });
+
+ it('returns every preview before downloading, and a local image completes while iCloud is pending', async () => {
+ let finishCloud!: (value: ReturnType<typeof file>) => void;
+ mock.native.load.mockImplementation((id: string) => id === 'cloud'
+ ? new Promise((resolve) => { finishCloud = resolve; }) : Promise.resolve(file(id)));
+ const candidates = (await pickDeferredLibraryPhotos(8))!;
+ expect(mock.native.pick).toHaveBeenCalledWith(8);
+ expect(mock.native.load).not.toHaveBeenCalled();
+ const { controller, pending, upload } = makeController();
+ controller.enqueue(candidates, { token: 'token' });
+ expect(pending().map((item) => item.previewUri)).toEqual(candidates.map((item) => item.uri));
+ await flush();
+ expect(upload.mock.calls.map(([item]) => item.name)).toEqual(['local.jpg']);
+ expect(mock.native.release).toHaveBeenCalledWith('local');
+ expect(mock.native.release).not.toHaveBeenCalledWith('cloud');
+ finishCloud(file('cloud'));
+ expect(await controller.waitForIdle()).toEqual({ failedCount: 0 });
+ expect(mock.native.release).toHaveBeenCalledWith('cloud');
+ controller.dispose();
+ });
+
+ it('keeps a failed selection for retry and releases it only after successful delivery', async () => {
+ mock.native.pick.mockResolvedValue([selection('cloud')]);
+ mock.native.load.mockRejectedValueOnce(new Error('network down')).mockResolvedValue(file('cloud'));
+ const { controller, pending } = makeController();
+ controller.enqueue((await pickDeferredLibraryPhotos(1))!, { token: 'token' });
+ expect(await controller.waitForIdle()).toEqual({ failedCount: 1 });
+ expect(pending()[0]).toMatchObject({ failed: true, previewUri: selection('cloud').previewUri });
+ expect(mock.native.release).not.toHaveBeenCalled();
+ controller.retry(pending()[0].localId, { token: 'token' });
+ expect(await controller.waitForIdle()).toEqual({ failedCount: 0 });
+ expect(mock.native.release).toHaveBeenCalledExactlyOnceWith('cloud');
+ controller.dispose();
+ });
+
+ it('cancels the native download on removal and cleans a late file without uploading it', async () => {
+ mock.native.pick.mockResolvedValue([selection('cloud')]);
+ let finish!: (value: ReturnType<typeof file>) => void;
+ mock.native.load.mockReturnValue(new Promise((resolve) => { finish = resolve; }));
+ const { controller, pending, upload } = makeController();
+ controller.enqueue((await pickDeferredLibraryPhotos(1))!, { token: 'token' });
+ await flush();
+ controller.remove(pending()[0].localId);
+ await flush();
+ expect(mock.native.cancel).toHaveBeenCalledWith('cloud');
+ expect(mock.native.release).toHaveBeenCalledExactlyOnceWith('cloud');
+ finish(file('cloud'));
+ await flush();
+ expect(upload).not.toHaveBeenCalled();
+ expect(mock.deleteAsync).toHaveBeenCalledWith(file('cloud').uri, { idempotent: true });
+ expect(mock.deleteAsync).not.toHaveBeenCalledWith(selection('cloud').previewUri, expect.anything());
+ controller.dispose();
+ });
+
+ it('keeps the native selection when staging fails after upload, so retry can read the original again', async () => {
+ mock.native.pick.mockResolvedValue([selection('cloud')]);
+ const { controller, uploaded, pending } = makeController();
+ uploaded.mockRejectedValueOnce(new Error('disk full'));
+ controller.enqueue((await pickDeferredLibraryPhotos(1))!, { token: 'token' });
+ expect(await controller.waitForIdle()).toEqual({ failedCount: 1 });
+ expect(mock.native.release).not.toHaveBeenCalled();
+ controller.retry(pending()[0].localId, { token: 'token' });
+ expect(await controller.waitForIdle()).toEqual({ failedCount: 0 });
+ expect(mock.native.load).toHaveBeenCalledTimes(2);
+ expect(mock.native.release).toHaveBeenCalledExactlyOnceWith('cloud');
+ controller.dispose();
+ });
+
+ it('cancels a timed-out native read without losing the selection needed by retry', async () => {
+ vi.useFakeTimers();
+ const { controller, pending } = makeController();
+ try {
+ mock.native.pick.mockResolvedValue([selection('cloud')]);
+ let rejectLoad!: (error: Error) => void;
+ mock.native.load.mockImplementationOnce(() => new Promise((_resolve, reject) => { rejectLoad = reject; }));
+ mock.native.cancel.mockImplementation(async () => { rejectLoad(new Error('cancelled')); });
+ controller.enqueue((await pickDeferredLibraryPhotos(1))!, { token: 'token' });
+ await vi.advanceTimersByTimeAsync(180_000);
+ expect(await controller.waitForIdle()).toEqual({ failedCount: 1 });
+ expect(mock.native.cancel).toHaveBeenCalledWith('cloud');
+ expect(mock.native.release).not.toHaveBeenCalled();
+ controller.retry(pending()[0].localId, { token: 'token' });
+ await vi.advanceTimersByTimeAsync(0);
+ expect(await controller.waitForIdle()).toEqual({ failedCount: 0 });
+ expect(mock.native.release).toHaveBeenCalledExactlyOnceWith('cloud');
+ } finally {
+ controller.dispose();
+ vi.useRealTimers();
+ }
+ });
+
+ it('hands downloaded bytes to the durable outbox before releasing the native selection', async () => {
+ mock.native.pick.mockResolvedValue([selection('cloud')]);
+ const { controller } = makeController();
+ controller.enqueue((await pickDeferredLibraryPhotos(1))!, { token: new Promise(() => {}) });
+ const handoff = controller.beginHandoff();
+ const prepared = await handoff.prepare();
+ expect(prepared[0].source.uri).toBe(file('cloud').uri);
+ expect(mock.native.release).not.toHaveBeenCalled();
+ handoff.release(true);
+ await flush();
+ expect(mock.native.release).toHaveBeenCalledExactlyOnceWith('cloud');
+ controller.dispose();
+ });
+
+ it('preserves selected slots without a local thumbnail and never uploads the placeholder', async () => {
+ mock.native.pick.mockResolvedValue([{ ...selection('limited'), previewUri: '' }]);
+ const { controller, pending, upload } = makeController();
+ controller.enqueue((await pickDeferredLibraryPhotos(1))!, { token: 'token' });
+ expect(pending()).toHaveLength(1);
+ await controller.waitForIdle();
+ expect(upload).toHaveBeenCalledWith(expect.anything(), file('limited').uri, expect.anything());
+ controller.dispose();
+ });
+
+ it('returns null only when the native module is absent; cancellation stays an empty selection', async () => {
+ mock.available = false;
+ expect(await pickDeferredLibraryPhotos(1)).toBeNull();
+ mock.available = true;
+ mock.native.pick.mockResolvedValue([]);
+ expect(await pickDeferredLibraryPhotos(1)).toEqual([]);
+ expect(mock.native.load).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/mobile/src/__tests__/useMobileLocalAttachmentsAbandon.test.tsx b/apps/mobile/src/__tests__/useMobileLocalAttachmentsAbandon.test.tsx
index fd158c1215..56faea3a6c 100644
--- a/apps/mobile/src/__tests__/useMobileLocalAttachmentsAbandon.test.tsx
+++ b/apps/mobile/src/__tests__/useMobileLocalAttachmentsAbandon.test.tsx
@@ -12,6 +12,7 @@ import {
const runtime = vi.hoisted(() => ({
uploads: [] as Array<{ name: string; resolve: () => void }>,
discarded: [] as unknown[],
+ pickPhotos: vi.fn(),
}));
vi.mock('react-native', () => ({ Platform: { OS: 'ios' } }));
@@ -20,7 +21,8 @@ vi.mock('react-i18next', async (importOriginal) => {
return { ...await importOriginal<typeof import('react-i18next')>(), useTranslation: () => ({ t }) };
});
vi.mock('expo-document-picker', () => ({}));
-vi.mock('expo-image-picker', () => ({}));
+vi.mock('expo-image-picker', () => ({ requestMediaLibraryPermissionsAsync: async () => ({ granted: true }) }));
+vi.mock('@/session/deferredPhotoLibrary', () => ({ pickDeferredLibraryPhotos: runtime.pickPhotos }));
vi.mock('expo-file-system/legacy', () => ({
documentDirectory: 'file:///docs/',
deleteAsync: async () => undefined,
@@ -56,6 +58,7 @@ beforeEach(() => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
runtime.uploads = [];
runtime.discarded = [];
+ runtime.pickPhotos.mockReset();
root = createRoot(document.createElement('div'));
});
afterEach(() => act(() => root.unmount()));
@@ -67,6 +70,7 @@ function mount(scopeKey: string) {
getAttachmentCount: () => 0,
onUploaded: vi.fn(),
onError: vi.fn(),
+ onPicked: vi.fn(),
};
const results: UseMobileLocalAttachmentsResult[] = [];
function Harness({ scope }: { scope: string }) {
@@ -77,6 +81,7 @@ function mount(scopeKey: string) {
return {
options,
first: results[0],
+ latest: () => results.at(-1)!,
rerender: (scope: string) => act(() => root.render(<Harness scope={scope} />)),
};
}
@@ -92,6 +97,34 @@ const imageCandidate = (name: string, onAbandoned: () => void): MobileLocalAttac
const flush = () => new Promise((resolve) => { setTimeout(resolve, 0); });
describe('useMobileLocalAttachments abandonment on scope change', () => {
+ it('puts selected photos in the tray and closes the context panel before their originals load', async () => {
+ const harness = mount('session-a');
+ runtime.pickPhotos.mockResolvedValue([{
+ ...imageCandidate('cloud.jpg', vi.fn()),
+ uri: 'data:image/jpeg;base64,preview',
+ resolve: () => new Promise(() => {}),
+ }]);
+ await act(async () => { await harness.first.addImages('library'); });
+ expect(harness.latest().pendingUploads).toHaveLength(1);
+ expect(harness.latest().pendingUploads[0].previewUri).toBe('data:image/jpeg;base64,preview');
+ expect(harness.options.onPicked).toHaveBeenCalledOnce();
+ expect(runtime.uploads).toHaveLength(0);
+ });
+
+ it('releases native selections returned after a scope change instead of attaching them to another task', async () => {
+ const harness = mount('session-a');
+ let finish!: (value: MobileLocalAttachmentUploadCandidate[]) => void;
+ runtime.pickPhotos.mockReturnValue(new Promise((resolve) => { finish = resolve; }));
+ let picking!: Promise<void>;
+ await act(async () => { picking = harness.first.addImages('library'); });
+ act(() => harness.first.discardAllPendingUploadsForScopeChange());
+ const abandoned = vi.fn();
+ await act(async () => { finish([imageCandidate('late.jpg', abandoned)]); await picking; });
+ expect(abandoned).toHaveBeenCalledOnce();
+ expect(harness.latest().pendingUploads).toEqual([]);
+ expect(harness.options.onPicked).not.toHaveBeenCalled();
+ });
+
it('abandons a candidate enqueued from a stale scope without starting an upload', async () => {
const harness = mount('session-a');
act(() => harness.first.discardAllPendingUploadsForScopeChange());
diff --git a/apps/mobile/src/session/ComposerAttachmentTray.tsx b/apps/mobile/src/session/ComposerAttachmentTray.tsx
index 998309c1f4..38aa5e9301 100644
--- a/apps/mobile/src/session/ComposerAttachmentTray.tsx
+++ b/apps/mobile/src/session/ComposerAttachmentTray.tsx
@@ -128,7 +128,9 @@ export function ComposerAttachmentTray({
{pendingUploads.map((pending) => (pending.kind === 'image' ? (
<View key={pending.localId} style={styles.thumbCard} testID={`${testIDPrefix}.attachmentPendingThumb`}>
<View style={styles.thumbPressable}>
- <Image contentFit="cover" source={{ uri: pending.previewUri }} style={styles.thumbImage} transition={0} />
+ {pending.previewUri ? (
+ <Image contentFit="cover" source={{ uri: pending.previewUri }} style={styles.thumbImage} transition={0} />
+ ) : null}
{pending.failed ? (
// 失败态:遮罩变为可点的「重试」——弱网下让用户原地重试,不用重新找图。
<Pressable
diff --git a/apps/mobile/src/session/deferredPhotoLibrary.ts b/apps/mobile/src/session/deferredPhotoLibrary.ts
new file mode 100644
index 0000000000..3da6608c15
--- /dev/null
+++ b/apps/mobile/src/session/deferredPhotoLibrary.ts
@@ -0,0 +1,90 @@
+import { i18n } from '@/i18n';
+import { buildMobileImageAttachmentCandidate, type MobileImagePickerAssetLike } from './mobileImageAttachment';
+import type { MobileLocalAttachmentUploadCandidate } from './mobileLocalAttachmentUpload';
+
+interface PhotoSelection {
+ id: string;
+ name: string;
+ previewUri: string;
+ width?: number;
+ height?: number;
+}
+
+interface DeferredPhotoPicker {
+ pick(limit: number): Promise<PhotoSelection[]>;
+ load(id: string): Promise<MobileImagePickerAssetLike>;
+ cancel(id: string): Promise<void>;
+ release(id: string): Promise<void>;
+}
+
+function throwLocalizedPhotoError(error: unknown): never {
+ const code = error && typeof error === 'object' && 'code' in error ? error.code : undefined;
+ // Native/provider messages can use the OS language and contain internal file
+ // paths. Only translate this boundary, preserving downstream conversion errors.
+ throw new Error(i18n.t(code === 'ERR_PHOTO_CANCELLED'
+ ? 'composer.upload.cancelled'
+ : 'composer.upload.noImageRead'), { cause: error });
+}
+
+async function cleanupLocalUris(uris: readonly string[]): Promise<void> {
+ const files = uris.filter((uri) => uri.startsWith('file://'));
+ if (files.length === 0) return;
+ const { deleteAsync } = await import('expo-file-system/legacy');
+ await Promise.all(files.map((uri) => deleteAsync(uri, { idempotent: true })));
+}
+
+/** null means an older native binary; preserve its Expo picker as a fallback.
+ * Selection returns before any original is downloaded. The existing upload
+ * queue owns resolution, cancellation, retry, outbox handoff and file cleanup.
+ */
+export async function pickDeferredLibraryPhotos(
+ limit: number,
+): Promise<MobileLocalAttachmentUploadCandidate[] | null> {
+ const { requireOptionalNativeModule } = await import('expo-modules-core');
+ const native = requireOptionalNativeModule<DeferredPhotoPicker>('CindyPhotoPicker');
+ if (!native) return null;
+ const selected = await native.pick(limit).catch(throwLocalizedPhotoError);
+ return selected.map((photo, index) => {
+ let released = false;
+ const release = () => {
+ if (released) return;
+ released = true;
+ void native.release(photo.id).catch(() => undefined);
+ };
+ return {
+ kind: 'image',
+ uri: photo.previewUri,
+ name: photo.name,
+ size: 0,
+ width: photo.width,
+ height: photo.height,
+ onAbandoned: release,
+ onConsumed: release,
+ cleanupLocalUris,
+ resolve: async (signal?: AbortSignal) => {
+ if (signal?.aborted || released) throw new Error(i18n.t('composer.upload.cancelled'));
+ const cancel = () => { void native.cancel(photo.id).catch(() => undefined); };
+ const loaded = native.load(photo.id).catch(throwLocalizedPhotoError);
+ signal?.addEventListener('abort', cancel, { once: true });
+ let uri: string | undefined;
+ try {
+ const asset = await loaded;
+ uri = asset.uri ?? undefined;
+ if (signal?.aborted || released) throw new Error(i18n.t('composer.upload.cancelled'));
+ const candidate = buildMobileImageAttachmentCandidate({ ...photo, ...asset }, index);
+ if (!candidate.resolve) return candidate;
+ const converted = await candidate.resolve();
+ // Conversion creates a second owned file; only it needs to enter the
+ // controller's temporary-file set. Preserve the provider for retries.
+ if (uri && converted.uri !== uri) void cleanupLocalUris([uri]).catch(() => undefined);
+ return { ...candidate, ...converted };
+ } catch (error) {
+ if (uri) void cleanupLocalUris([uri]).catch(() => undefined);
+ throw error;
+ } finally {
+ signal?.removeEventListener('abort', cancel);
+ }
+ },
+ };
+ });
+}
diff --git a/apps/mobile/src/session/mobileLocalAttachmentUpload.ts b/apps/mobile/src/session/mobileLocalAttachmentUpload.ts
index 6b433d1ed9..59a00cf44d 100644
--- a/apps/mobile/src/session/mobileLocalAttachmentUpload.ts
+++ b/apps/mobile/src/session/mobileLocalAttachmentUpload.ts
@@ -46,7 +46,7 @@ export interface MobileLocalAttachmentUploadCandidate {
* 返回的字段覆盖到 candidate 上再进后续管线;托盘预览始终用原始 uri,所以入队
* 当帧就有缩略图,重活全在后台。
*/
- resolve?: () => Promise<Partial<Pick<
+ resolve?: (signal?: AbortSignal) => Promise<Partial<Pick<
MobileLocalAttachmentUploadCandidate,
'uri' | 'name' | 'mimeType' | 'size' | 'width' | 'height' | 'skipPreprocess'
>>>;
@@ -85,6 +85,8 @@ export interface MobileLocalAttachmentUploadCandidate {
* 供自行生成了输入文件的调用方(标注烧录图等)按生命周期回收;管线不消费。
*/
onAbandoned?: () => void;
+ /** 上传已交付后释放原生选图句柄;失败仍保留,供原地重试。 */
+ onConsumed?: () => void;
}
/** 托盘 pending 卡的渲染数据(image → 缩略卡;file → 文件名 chip)。 */
@@ -355,7 +357,7 @@ export function createMobileLocalAttachmentUploadController(
// 先跑 candidate 自带的就位钩子(相册换址 / HEIC 转码),结果覆盖进管线输入。
let source = task.candidate;
if (source.resolve) {
- source = { ...source, ...(await step(source.resolve())) };
+ source = { ...source, ...(await step(source.resolve(task.abort.signal))) };
}
task.resolvedSource = source;
task.localUris.add(source.uri);
@@ -473,6 +475,7 @@ export function createMobileLocalAttachmentUploadController(
outcome = 'discarded';
} else {
outcome = 'uploaded';
+ try { task.candidate.onConsumed?.(); } catch { /* 回收不改变交付结果 */ }
}
}
} catch (err) {
diff --git a/apps/mobile/src/session/useMobileLocalAttachments.ts b/apps/mobile/src/session/useMobileLocalAttachments.ts
index a9addf46af..df586b40f0 100644
--- a/apps/mobile/src/session/useMobileLocalAttachments.ts
+++ b/apps/mobile/src/session/useMobileLocalAttachments.ts
@@ -23,6 +23,7 @@ import { retainComposerAttachmentFile } from './durableOutboxFiles';
import * as ImagePicker from 'expo-image-picker';
import { formatRemoteError } from '@/device-link/remoteStatus';
import { canBrowsePhotoLibraryDirectly } from '@/session/photoLibraryPolicy';
+import { pickDeferredLibraryPhotos } from '@/session/deferredPhotoLibrary';
import {
MOBILE_MAX_ATTACHMENTS,
assertMobileDocumentSize,
@@ -315,7 +316,9 @@ export function useMobileLocalAttachments(
}
}
if (candidate.cleanupLocalUris) {
- for (const uri of localUris) pastedImageLocalUrisRef.current.add(uri);
+ for (const uri of localUris) {
+ if (uri.startsWith('file://')) pastedImageLocalUrisRef.current.add(uri);
+ }
}
if (!isActive()) return;
// Keep the actual PUT bytes for all file kinds until the composer hands them off.
@@ -330,7 +333,7 @@ export function useMobileLocalAttachments(
uploadedSourcesRef.current.set(attachment.id, {
...candidate, uri: stageUri, name: attachment.name, mimeType: attachment.mimeType,
size: attachment.size, resolve: undefined, skipPreprocess: true, cleanupLocalUris: undefined,
- onAbandoned: undefined,
+ onAbandoned: undefined, onConsumed: undefined,
});
optionsRef.current.onUploaded(attachment, deliveredCandidate, localId);
if (candidate.cleanupLocalUris) {
@@ -461,6 +464,24 @@ export function useMobileLocalAttachments(
}
}
+ if (source === 'library' && Platform.OS === 'ios') {
+ const candidates = await pickDeferredLibraryPhotos(begun.remainingSlots);
+ if (candidates !== null) {
+ if (!isAttachmentScopeActive()) {
+ for (const candidate of candidates) candidate.onAbandoned?.();
+ return;
+ }
+ // 原图尚未从 iCloud 取回也能立即入队、claim 到乐观消息。
+ // 读取与上传共用既有取消/重试/作用域生命周期,不另建下载队列。
+ for (const candidate of candidates.slice(begun.remainingSlots)) candidate.onAbandoned?.();
+ const accepted = candidates.slice(0, begun.remainingSlots);
+ if (accepted.length === 0) return;
+ enqueueUploads(accepted, { token: optionsRef.current.getAccessToken() });
+ optionsRef.current.onPicked?.();
+ return;
+ }
+ }
+
const picked = source === 'camera'
? await ImagePicker.launchCameraAsync({
mediaTypes: ['images'],
diff --git a/docs/dev-rules/mobile-development.md b/docs/dev-rules/mobile-development.md
index b35f438974..93acfb078d 100644
--- a/docs/dev-rules/mobile-development.md
+++ b/docs/dev-rules/mobile-development.md
@@ -81,6 +81,21 @@ pnpm --filter mobile test:smoke
## 专项入口
+### 相册选图与 iCloud 原图
+
+iOS 的系统选图器在确认后立即把选中项交给现有附件队列。预览只读取本机小缩略图,
+不等待 iCloud 原图;受限照片权限下拿不到缩略图时保留附件占位,不扩大照片库权限。
+原图由选图器授予的 `NSItemProvider` 在队列的 `resolve` 阶段读取,随后沿用格式转换、
+压缩、上传和持久发件箱交接。发送自动等待原图就绪,不要求用户另行下载或再次发送。
+失败保留选中项供重试,移除/换任务/成功交付后释放原生句柄;取消后的迟到文件只清理不回填。
+临时副本使用 App 私有 `Caches/ImagePicker`,由现有附件生命周期回收。
+
+实现:`modules/cindy-photo-picker/`、`src/session/deferredPhotoLibrary.ts` 与
+`useMobileLocalAttachments.ts`;回归:`deferredPhotoLibrary.test.ts`、
+`useMobileLocalAttachmentsAbandon.test.tsx`。新增原生模块需要冷更,不能作为旧安装包的 OTA
+独立交付;旧原生包缺模块时继续使用 Expo 选图入口。首次发布应随下一次 iOS 原生包交付,
+指纹与存量装机影响仍须按下文冷更门确认。Android 保持系统选图器和既有附件队列。
+
### 任务入口与创建失败恢复
- 来源未确认时不挂载远端任务内容,但必须显示读取状态和返回入口;读取失败、空记录或