import HUDKit import SwiftUI /// Your atmosphere, a sheet over the workspace: the apps the account uses /// (`describe_repo` of `me`'s `apps`), each opening its app page; the /// workspace's canvases, each shown on a tap; then the quick ways in, /// Bluesky's: the account's timeline and saved feeds, a feed, an /// account's posts and profile, a thread, any account's repository. What /// opens is a tile of the canvas on screen, beside the column on screen. /// The account itself, its theme and the others signed in are in Settings. struct Home: View { let browser: Browser @State private var actor = "bsky.app" @State private var thread = "" @State private var naming = false @State private var name = "" @ScaledMetric(relativeTo: .footnote) private var cell: CGFloat = 76 private static let discover = "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/whats-hot" private static let feeds = [ discover, "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/with-friends", "at://did:plc:z72i7hdynmk6r22z27h6tvur/app.bsky.feed.generator/bsky-team", ] var body: some View { Form { apps if let workspace = browser.workspace { canvases(workspace) } quick } .navigationTitle("Atmosphere") .navigationBarTitleDisplayMode(.large) .refreshable { await browser.identify(again: true) await browser.workspace?.load() } .task(id: browser.session?.did) { await browser.identify() } .alert("New canvas", isPresented: $naming) { TextField("Name", text: $name) Button("Create") { let given = name.trimmingCharacters(in: .whitespaces) guard let workspace = browser.workspace else { return } browser.showingHome = false Task { await workspace.create(given.isEmpty ? "Untitled canvas" : given) } } Button("Cancel", role: .cancel) {} } } // Your apps. private var apps: some View { Section { if let apps = browser.apps { if apps.isEmpty { Text("No apps yet: your repository holds no records.") .foregroundStyle(.secondary) } else { LazyVGrid(columns: [GridItem(.adaptive(minimum: cell), spacing: Space.s3, alignment: .top)], spacing: Space.s4) { ForEach(apps) { app in appButton(app) } } .padding(.vertical, Space.s2) } } else if let error = browser.appsError { VStack(alignment: .leading, spacing: Space.s1) { Text("Couldn't read your apps") Text(error) .font(.footnote) .foregroundStyle(.secondary) } Button("Try again") { Task { await browser.identify(again: true) } } } else { ProgressView() .frame(maxWidth: .infinity) } } header: { Text("Your apps") } .headerProminence(.increased) } private func appButton(_ app: AppLink) -> some View { Button { browser.launch(app.page(for: "me")) } label: { VStack(spacing: Space.s1) { AppIcon(url: app.icon, symbol: "app", size: 48, relativeTo: .footnote) Text(app.name) .font(.footnote) .foregroundStyle(.primary) .lineLimit(1) } .frame(maxWidth: .infinity) .contentShape(.rect) } .buttonStyle(.plain) .accessibilityHint("Opens its app page") } // Canvases. private func canvases(_ workspace: Workspace) -> some View { Section { let listed = ordered(workspace) if listed.isEmpty { Text(workspace.loaded ? "No canvases yet" : "Loading your canvases…") .foregroundStyle(.secondary) } ForEach(listed) { canvas in canvasRow(canvas, workspace) } } header: { HStack { Text("Canvases") Spacer() Button("New") { name = "" naming = true } .font(.body) } } .headerProminence(.increased) } /// The canvases open as tabs, in their order, then the rest. private func ordered(_ workspace: Workspace) -> [CanvasInfo] { let open = workspace.open.compactMap { workspace.info($0) } return open + workspace.canvases.filter { !workspace.open.contains($0.id) } } private func canvasRow(_ canvas: CanvasInfo, _ workspace: Workspace) -> some View { let on = canvas.id == workspace.current return Button { browser.showingHome = false if !on { Task { await workspace.activate(canvas.id) } } } label: { HStack { Label { VStack(alignment: .leading, spacing: 2) { Text(canvas.name) .foregroundStyle(.primary) Text(detail(canvas, workspace)) .font(.subheadline) .foregroundStyle(.secondary) .lineLimit(2) } } icon: { Image(systemName: symbol(canvas, workspace)) } Spacer(minLength: 0) if on { Image(systemName: "checkmark") .foregroundStyle(.tint) .accessibilityHidden(true) } } } .accessibilityAddTraits(on ? .isSelected : []) } private func symbol(_ canvas: CanvasInfo, _ workspace: Workspace) -> String { if canvas.invitedBy != nil { return "envelope" } if canvas.members.count > 1 || workspace.me.map({ canvas.owner != $0.did }) == true { return "person.2" } return "rectangle.split.3x1" } /// What a canvas row says under its name: whom it is shared with or by, /// what an agent added, and its tiles' titles where read, else how many. private func detail(_ canvas: CanvasInfo, _ workspace: Workspace) -> String { var parts: [String] = [] if let by = canvas.invitedBy { parts.append("Invitation from \(workspace.handles[by].map { "@\($0)" } ?? "a member")") } else if let me = workspace.me?.did, canvas.owner != me { parts.append("Shared by \(workspace.handles[canvas.owner].map { "@\($0)" } ?? "its owner")") } else if canvas.members.count > 1 { parts.append("Shared with \(canvas.members.count - 1)") } if let tiles = workspace.tiles[canvas.id] { let byAgent = tiles.filter(\.byAgent).count if byAgent > 0 { parts.append(byAgent == 1 ? "An agent added a tile" : "An agent added \(byAgent) tiles") } let headings = workspace.columns(canvas.id).flatMap(\.tiles).prefix(3).map(\.heading) parts.append(headings.isEmpty ? "No tiles" : headings.joined(separator: " · ")) } else { parts.append(canvas.tileCount == 1 ? "1 tile" : "\(canvas.tileCount) tiles") } return parts.joined(separator: " · ") } // Feeds: yours first, then Bluesky's. @ViewBuilder private var quick: some View { Section { open("Following", "person.2", query("app.bsky.feed.getTimeline", [:])) open("Saved feeds", "bookmark", query("app.bsky.feed.getFeedGenerators", ["feeds": "saved"])) open("Discover", "sparkles", query("app.bsky.feed.getFeed", ["feed": .string(Self.discover)])) open("Bluesky's feeds", "list.bullet.rectangle", query("app.bsky.feed.getFeedGenerators", ["feeds": .array(Self.feeds.map(JSON.string))])) } header: { Text("Feeds") } .headerProminence(.increased) Section("Account") { TextField("Handle or DID", text: $actor) .textInputAutocapitalization(.never) .autocorrectionDisabled() open("Posts", "text.bubble", query("app.bsky.feed.getAuthorFeed", ["actor": .string(actor)])) open("Profile", "person.crop.circle", query("app.bsky.actor.getProfile", ["actor": .string(actor)])) open("Repository", "externaldrive", Call("describe_repo", ["actor": .string(actor)])) } Section { TextField("bsky.app post link or at-uri", text: $thread) .textInputAutocapitalization(.never) .autocorrectionDisabled() if let uri = Self.postURI(thread) { open("Thread", "bubble.left.and.bubble.right", query("app.bsky.feed.getPostThread", ["uri": .string(uri)])) } } header: { Text("Thread") } } private func query(_ method: String, _ params: [String: JSON]) -> Call { Call("query", ["method": .string(method), "params": .object(params)]) } private func open(_ title: String, _ symbol: String, _ call: Call) -> some View { Button { browser.launch(call) } label: { Label(title, systemImage: symbol) } .disabled(actor.isEmpty && call.args["actor"] != nil) } /// The at-uri a post link names: an at-uri as it is, or a bsky.app post URL's. static func postURI(_ text: String) -> String? { let given = text.trimmingCharacters(in: .whitespaces) if given.hasPrefix("at://") { return given } guard let match = given.wholeMatch(of: /https:\/\/bsky\.app\/profile\/([A-Za-z0-9.:%_-]+)\/post\/([A-Za-z0-9._:~-]+)\/?/) else { return nil } return "at://\(match.output.1.removingPercentEncoding ?? String(match.output.1))/app.bsky.feed.post/\(match.output.2)" } }