// 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.importCottonKitimportCottonValuesimportSwiftUIpublicstructCustomerShell: View {privatelet manifest: FlavourManifest @State privatevar model: HomeCustomerModel @State privatevar 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 staticfunc opens(_ manifest: FlavourManifest?) -> BusinessAddress? { manifest?.personalObject == nil ? manifest?.business : nil }publicinit(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) }publicvar body: someView { 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") }privatefunc page(_ index: Int) -> AnyView {let tiles = tilesguard tiles.indices.contains(index) else { returnAnyView(PageGroundFill()) }let tile = tiles[index]if tile.key == CustomerShellTiles.me {returnAnyView(PaperScreen(title: tile.name, showsBack: false) {CustomerPage(model: model) }) }returnAnyView(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.enumCustomerShellTiles {staticlet me = "me"staticlet meName = "Me"/// A view's tile: its tool and its view.staticfunc viewKey(tool: String, view: String) -> String { tool + "/" + view }staticfunc dealt(_ pages: BusinessPages?) -> [FanTile] {let views = (pages?.tools ?? []).flatMap { tool in tool.views.map { view inFanTile(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.staticfunc view(_ key: String, in pages: BusinessPages?) -> (tool: BusinessTool, view: BusinessView)? {for tool in pages?.tools ?? [] {iflet view = tool.views.first(where: { viewKey(tool: tool.tool, view: $0.id) == key }) { return (tool, view) } }returnnil }/// 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.staticfunc opens(_ tiles: [FanTile], pages: BusinessPages?, fan: FlavourManifest.Fan) -> Int {let tools = pages?.tools ?? []var key: String?iflet 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 {iflet 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.privatestaticfunc picture(_ value: JSONValue?) -> ToolPicture? {guardlet value, let data = try? JSONEncoder().encode(value) else { returnnil }return (try? JSONDecoder().decode(DefinitionToolPicture.self, from: data))?.drawn }}