// A list's Set picture from Photos (5 Nov 2026): the system's photo picker,// raised from code by the list's act on the fan (`open_picker`, picker// `photos`), one picture, written as a JPEG the core keeps under a picture// key and names on the list (`groups.update {id, picturePath}`), in one write// with the list's receipt. The original goes up as it is; the object draws// the tile's thumbnail from it, so a huge picture costs the tile nothing.importCottonKitimportCottonValuesimportFoundation#if os(iOS)importPhotosUIimport UIKitimportUniformTypeIdentifiers#endif@MainActorfinalclassListPicture {staticlet shared = ListPicture()privatevar core: (any CottonCalling)?privatevar say: ((String) -> Void)?privatevar tell: ((String) -> Void)?/// The core and the toasts, from the root once a core is open.func attach(core: any CottonCalling, say: @escaping (String) -> Void, tell: @escaping (String) -> Void) {self.core = coreself.say = sayself.tell = tell }/// The photo picker for `group`; the picture picked becomes its own.func pick(for group: String) async {guardlet core else { return } #if os(iOS)guardlet picked = awaitSelf.picked() else { return }guardlet path = Self.written(picked) else { say?(ShellLines.line("lists", "group.picture_unread"))return }defer { try? FileManager.default.removeItem(at: path) }do {let answer = tryawait core.call(CottonMethod.groupsUpdate, ["id": RecordValue.text(group), "picturePath": .text(path.path)],as: CoreRecords.WireGroupWrite.self)iflet said = answer.change.receipt?.said { tell?(said) } } catch { say?(ActionRefused.words(for: error)) } #else say?(ShellLines.line("lists", "group.picture_unread")) #endif } #if os(iOS)/// One picture from the person's photos, as an image; nil when they/// cancelled, or no picker could be shown, or it could not be read.privatestaticfunc picked() async -> UIImage? {guardlet top = SystemShareSheet.topViewController() else { returnnil }var configuration = PHPickerConfiguration() configuration.filter = .images configuration.selectionLimit = 1let picker = PHPickerViewController(configuration: configuration)let delegate = PickedPhoto() picker.delegate = delegatelet image = await withCheckedContinuation { continuation in delegate.continuation = continuation top.present(picker, animated: true) } withExtendedLifetime(delegate) {}return image }/// The picture as a JPEG in this app's temporary folder, at its own size.staticfunc written(_ image: UIImage) -> URL? {guardlet data = image.jpegData(compressionQuality: 0.9) else { returnnil }let url = FileManager.default.temporaryDirectory.appendingPathComponent("list-picture-\(UUID().uuidString).jpg")do {try data.write(to: url)return url } catch {returnnil } } #endif}#if os(iOS)@MainActorprivatefinalclassPickedPhoto: NSObject, PHPickerViewControllerDelegate {var continuation: CheckedContinuation<UIImage?, Never>?func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { picker.dismiss(animated: true)guardlet provider = results.first?.itemProvider, provider.canLoadObject(ofClass: UIImage.self) else {return finish(nil) } provider.loadObject(ofClass: UIImage.self) { object, _ inlet image = object as? UIImageTask { @MainActor inself.finish(image) } } }privatefunc finish(_ image: UIImage?) { continuation?.resume(returning: image) continuation = nil }}#endif