// 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. import CottonKit import CottonValues import Foundation #if os(iOS) import PhotosUI import UIKit import UniformTypeIdentifiers #endif @MainActor final class ListPicture { static let shared = ListPicture() private var core: (any CottonCalling)? private var say: ((String) -> Void)? private var 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 = core self.say = say self.tell = tell } /// The photo picker for `group`; the picture picked becomes its own. func pick(for group: String) async { guard let core else { return } #if os(iOS) guard let picked = await Self.picked() else { return } guard let path = Self.written(picked) else { say?(ShellLines.line("lists", "group.picture_unread")) return } defer { try? FileManager.default.removeItem(at: path) } do { let answer = try await core.call( CottonMethod.groupsUpdate, ["id": RecordValue.text(group), "picturePath": .text(path.path)], as: CoreRecords.WireGroupWrite.self) if let 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. private static func picked() async -> UIImage? { guard let top = SystemShareSheet.topViewController() else { return nil } var configuration = PHPickerConfiguration() configuration.filter = .images configuration.selectionLimit = 1 let picker = PHPickerViewController(configuration: configuration) let delegate = PickedPhoto() picker.delegate = delegate let 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. static func written(_ image: UIImage) -> URL? { guard let data = image.jpegData(compressionQuality: 0.9) else { return nil } let url = FileManager.default.temporaryDirectory.appendingPathComponent("list-picture-\(UUID().uuidString).jpg") do { try data.write(to: url) return url } catch { return nil } } #endif } #if os(iOS) @MainActor private final class PickedPhoto: NSObject, PHPickerViewControllerDelegate { var continuation: CheckedContinuation? func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { picker.dismiss(animated: true) guard let provider = results.first?.itemProvider, provider.canLoadObject(ofClass: UIImage.self) else { return finish(nil) } provider.loadObject(ofClass: UIImage.self) { object, _ in let image = object as? UIImage Task { @MainActor in self.finish(image) } } } private func finish(_ image: UIImage?) { continuation?.resume(returning: image) continuation = nil } } #endif