Cotton code
81 lines · 3.2 KB · SwiftRaw
// A picture as a page draws it: from the LARGEST copy on this phone// (original → display → thumbnail, `tier: .largest`), because a hero drawn at// the width of the screen asked with a tile's order is handed the 256 px// thumbnail enlarged while the original sits on disk (`ThumbFetchSweepTests`).// It asks `read.mediaPath` through `PhotosPicture` and nothing else; a tap// opens the viewer, which is what fetches. Without the core (a preview, a// test) it draws the thumbnail-tier picture every face draws. Where the// environment hands pictures by key (`facePictures`: a gallery, a face// capture's fixtures) it draws that one and asks nothing. A key that is a web// address is a kept page's own picture (Cotton Pages), drawn from it. On a business app's// shell a key is the business's photo, fetched from its object's photos// (`facePhotoBase`, `BusinessPages.photo`).import CottonKitimport SwiftUIstruct PagePicture: View {    let key: String    let edge: CGFloat    var blankWhileLoading = false    @Environment(\.cottonCore) private var core    @Environment(\.facePictures) private var pictures    @Environment(\.facePhotoBase) private var photoBase    var body: some View {        if let shown = pictures?(key) {            shown.resizable().interpolation(.high).scaledToFill()        } else if let photoBase, let url = BusinessPages.photo(key, at: photoBase) {            AsyncImage(url: url) { phase in                if let image = phase.image {                    image.resizable().interpolation(.high).scaledToFill()                } else {                    Color.clear                }            }        } else if let url = Self.web(key) {            // A kept page's lead picture is the page's own, on the web            // (`own_pages__picture`, Cotton Pages): drawn from its address.            AsyncImage(url: url) { phase in                if let image = phase.image {                    image.resizable().interpolation(.high).scaledToFill()                } else {                    Color.clear                }            }        } else if let core {            PhotosPicture(                media: PhotosMediaSource(core: core), key: key,                maxPixel: Int(edge * PagePicture.scale), tier: .largest, blankWhileLoading: blankWhileLoading)        } else {            RecordPicture(avatarKey: key, edge: edge)        }    }    /// A key that is a web address (a page's own picture), as its URL; nil    /// for a key of this phone's media.    static func web(_ key: String) -> URL? {        guard key.hasPrefix("https://") || key.hasPrefix("http://") else { return nil }        return URL(string: key)    }    @MainActor static var scale: CGFloat {        #if canImport(UIKit)        UIScreen.main.scale        #else        2        #endif    }}extension EnvironmentValues {    /// Pictures a face draws by key without the core — a gallery's invented    /// people; nil asks the phone.    @Entry var facePictures: ((String) -> Image?)? = nil    /// Where a business's photos are fetched (`BusinessPages.photos`): a    /// photo column's key is drawn from this base with the key after it; nil    /// asks the phone.    @Entry var facePhotoBase: String? = nil}