Cotton code
147 lines · 6.5 KB · SwiftRaw
// A business app's shell (Sharmila's Yoga Zone, Nancy's Naga Kitchen): the// fan at the foot, a tile for each view the business's object lets this phone// read, and Me.//// **The object decides; the phone draws.** Everything on the fan comes from// one answer, `business.pages`, read from the core's cache after a sync: each// tool the credential's role reads, each of its views with its rows drawn// (`HomeCustomerModel`). A stranger sees the public views; a member their own// beside them; staff and the owner the views and acts their role reads — the// same shell, never one built per role. Each view's tile is named as the view,// on its tool's picture and stock, grouped by tool in the object's order, and// its page is a deck of that view's rows on their tables' faces// (`CustomerDeck`); tap or hold opens the card in place, its acts on the fan. Nobody is// asked to sign in until the object refuses an act for want of a person: the// sign-in sheet opens over the page, and the act is taken once they are in.//// A hand-built Swift host, for `HomeCustomer`'s reason: it holds a credential,// and a definition may say how a page looks but never where a credential goes.// Until the first pages are read the ground stands, never a blank fan.import CottonKitimport CottonValuesimport SwiftUIpublic struct CustomerShell: View {    private let manifest: FlavourManifest    @State private var model: HomeCustomerModel    @State private var pageOpen = false    /// The business this host opens for, or nil when the app opens on    /// Cotton's own frame — a personal app's object is its database, not a    /// business's shell (`CustomerShellTests`).    public nonisolated static func opens(_ manifest: FlavourManifest?) -> BusinessAddress? {        manifest?.personalObject == nil ? manifest?.business : nil    }    public init(manifest: FlavourManifest, client: BusinessClient) {        self.manifest = manifest        _model = State(initialValue: HomeCustomerModel(client: client, business: manifest.name))    }    /// The fan's tiles: one per view the object answered, and Me.    var tiles: [FanTile] { CustomerShellTiles.dealt(model.pages) }    public var body: some View {        ZStack {            PageGroundFill().ignoresSafeArea()            if model.pagesRead {                #if os(iOS)                FanHome(                    tiles: tiles,                    page: { index in page(index) },                    show: nil,                    caps: false,                    read: true,                    opens: CustomerShellTiles.opens(tiles, pages: model.pages, fan: manifest.fan),                    // No record button stands under this fan: a card's acts                    // close by the fan's own X.                    pageOpen: pageOpen,                    closesEdits: true)                .environment(\.pageLayerShown, { pageOpen = $0 })                #else                page(0)                #endif            } else {                // The theme's ground while the first pages are read: never a                // blank fan and never a spinner.                Color.clear                    .accessibilityHidden(true)                    .accessibilityIdentifier("home-customer-waiting")            }        }        .modifier(CustomerSheets(model: model))        .task { await model.load() }        .onAppear { hideSplashOnce() }        .accessibilityIdentifier("customer-shell")    }    private func page(_ index: Int) -> AnyView {        let tiles = tiles        guard tiles.indices.contains(index) else { return AnyView(PageGroundFill()) }        let tile = tiles[index]        if tile.key == CustomerShellTiles.me {            return AnyView(PaperScreen(title: tile.name, showsBack: false) {                CustomerPage(model: model)            })        }        return AnyView(CustomerDeck(model: model, key: tile.key, tile: tile))    }}/// The shell's fan, as data: a tile per view the object answered, keyed/// `tool/view`, named as the view, on its tool's picture and stock, grouped/// by tool in the object's order — then Me. The role's views are the fan:/// a stranger's, a member's, staff's and the owner's differ only in what the/// object answered.enum CustomerShellTiles {    static let me = "me"    static let meName = "Me"    /// A view's tile: its tool and its view.    static func viewKey(tool: String, view: String) -> String { tool + "/" + view }    static func dealt(_ pages: BusinessPages?) -> [FanTile] {        let views = (pages?.tools ?? []).flatMap { tool in            tool.views.map { view in                FanTile(key: viewKey(tool: tool.tool, view: view.id), name: view.label, picture: picture(tool.picture),                        stock: tool.cardStock.flatMap(ToolCardTint.init(rawValue:)) ?? ToolCardTint.of(tool.tool))            }        }        return views + [FanTile(key: me, name: meName, picture: nil, stock: ToolCardTint.of(me))]    }    /// The tool and view a tile's `key` stands for.    static func view(_ key: String, in pages: BusinessPages?) -> (tool: BusinessTool, view: BusinessView)? {        for tool in pages?.tools ?? [] {            if let view = tool.views.first(where: { viewKey(tool: tool.tool, view: $0.id) == key }) { return (tool, view) }        }        return nil    }    /// The tile the fan opens on: the default view of the tool the    /// manifest's dial opens on (Sharmila's Yoga), else that tool's first    /// view, else the first default view, else the first tile.    static func opens(_ tiles: [FanTile], pages: BusinessPages?, fan: FlavourManifest.Fan) -> Int {        let tools = pages?.tools ?? []        var key: String?        if let wanted = fan.opens, let tool = tools.first(where: { $0.tool == wanted }),           let view = tool.views.first(where: { $0.isDefault == true }) ?? tool.views.first {            key = viewKey(tool: tool.tool, view: view.id)        } else {            for tool in tools {                if let view = tool.views.first(where: { $0.isDefault == true }) {                    key = viewKey(tool: tool.tool, view: view.id)                    break                }            }        }        return key.flatMap { key in tiles.firstIndex { $0.key == key } } ?? 0    }    /// The picture a definition names, read as the card draws it.    private static func picture(_ value: JSONValue?) -> ToolPicture? {        guard let value, let data = try? JSONEncoder().encode(value) else { return nil }        return (try? JSONDecoder().decode(DefinitionToolPicture.self, from: data))?.drawn    }}