Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mobile/ios/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.build/
.swiftpm/
xcuserdata/
*.xcworkspace/xcuserdata/
216 changes: 216 additions & 0 deletions mobile/ios/App/ApprovalCard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import CompanionCore
import SwiftUI

/// Human-in-the-loop question card: one question at a time, the stack slides between them, single-choice
/// answers advance on their own and multi-select waits for Continue.
struct ApprovalCard: View {
let request: PendingQuestion
let submit: (JSONValue?) async -> Void

private let questions: [FormQuestion]
@State private var index = 0
@State private var answers: [String: QuestionAnswer] = [:]
@State private var sent = false
@State private var pendingAdvance: Task<Void, Never>?

init(request: PendingQuestion, submit: @escaping (JSONValue?) async -> Void) {
self.request = request
self.submit = submit
self.questions = QuestionForm.questions(for: request)
}

private var question: FormQuestion { questions[index] }
private var isLast: Bool { index == questions.count - 1 }
private var answer: QuestionAnswer { answers[question.id] ?? QuestionAnswer() }

var body: some View {
if sent {
Label("答案已发送", systemImage: "checkmark.circle.fill")
.font(.system(size: 12.5, weight: .medium))
.foregroundStyle(.green)
.padding(.vertical, 6)
.padding(.horizontal, 10)
.background(Color.green.opacity(0.12), in: Capsule())
.transition(.scale(scale: 0.95).combined(with: .opacity))
} else {
card
.transition(.move(edge: .bottom).combined(with: .opacity))
}
}

private var card: some View {
VStack(spacing: 0) {
ZStack(alignment: .topLeading) {
questionView
.id(index)
.transition(.asymmetric(
insertion: .move(edge: .bottom).combined(with: .opacity),
removal: .move(edge: .top).combined(with: .opacity)
))
}
.padding(16)
.frame(maxWidth: .infinity, alignment: .leading)
.clipped()
footer
.padding(.horizontal, 12)
.padding(.vertical, 10)
}
.background(Color(.secondarySystemGroupedBackground), in: RoundedRectangle(cornerRadius: 16))
.overlay(RoundedRectangle(cornerRadius: 16).stroke(Color.primary.opacity(0.06)))
.shadow(color: .black.opacity(0.06), radius: 8, y: 2)
.animation(.timingCurve(0.22, 1, 0.36, 1, duration: 0.36), value: index)
.onDisappear { pendingAdvance?.cancel() }
}

private var questionView: some View {
VStack(alignment: .leading, spacing: 10) {
Text(question.text).font(.system(size: 14, weight: .medium))
VStack(alignment: .leading, spacing: 2) {
ForEach(question.choices) { choice in optionRow(choice) }
TextField("其他…", text: customText)
.font(.system(size: 13))
.padding(.vertical, 6)
.padding(.leading, 22)
.submitLabel(.send)
.onSubmit { if !answer.isEmpty { advance() } }
}
}
}

private func optionRow(_ choice: QuestionChoice) -> some View {
let on = answer.selected.contains(choice.value)
return Button {
toggle(choice)
} label: {
HStack(alignment: .top, spacing: 8) {
indicator(on: on)
VStack(alignment: .leading, spacing: 1) {
Text(choice.label)
.font(.system(size: 13))
.foregroundStyle(on ? Color.primary : Color.primary.opacity(0.7))
if let description = choice.description, !description.isEmpty {
Text(description).font(.system(size: 11)).foregroundStyle(.secondary)
}
}
Spacer(minLength: 0)
}
.padding(.vertical, 6)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.accessibilityAddTraits(on ? .isSelected : [])
}

private func indicator(on: Bool) -> some View {
ZStack {
if question.multiSelect {
RoundedRectangle(cornerRadius: 5)
.fill(on ? Color.primary : .clear)
.overlay(RoundedRectangle(cornerRadius: 5).stroke(Color.primary.opacity(on ? 0 : 0.3), lineWidth: 1.5))
if on { Image(systemName: "checkmark").font(.system(size: 9, weight: .heavy)).foregroundStyle(Color(.systemBackground)) }
} else {
Circle()
.fill(on ? Color.primary : .clear)
.overlay(Circle().stroke(Color.primary.opacity(on ? 0 : 0.3), lineWidth: 1.5))
Circle().fill(Color(.systemBackground)).frame(width: 6, height: 6).scaleEffect(on ? 1 : 0)
}
}
.frame(width: 16, height: 16)
.animation(.easeOut(duration: 0.2), value: on)
}

private var footer: some View {
HStack(spacing: 10) {
HStack(spacing: 4) {
navButton("chevron.up", enabled: index > 0) { go(index - 1) }
Text("\(index + 1) / \(questions.count)")
.font(.system(size: 12, weight: .medium))
.monospacedDigit()
.foregroundStyle(.tertiary)
.contentTransition(.numericText())
navButton("chevron.down", enabled: !isLast) { go(index + 1) }
}
Spacer()
Button("跳过") { advance() }
.font(.system(size: 13, weight: .medium))
.foregroundStyle(.secondary)
.buttonStyle(.plain)
.padding(.horizontal, 8)
Button(action: advance) {
HStack(spacing: 6) {
Text(isLast ? "发送" : "继续")
Image(systemName: "return").font(.system(size: 10, weight: .semibold))
}
.font(.system(size: 13, weight: .medium))
.foregroundStyle(Color(.systemBackground))
.padding(.horizontal, 14)
.padding(.vertical, 6)
.background(Color.primary.opacity(answer.isEmpty ? 0.25 : 1), in: Capsule())
}
.buttonStyle(.plain)
.disabled(answer.isEmpty)
}
}

private func navButton(_ symbol: String, enabled: Bool, action: @escaping () -> Void) -> some View {
Button(action: action) {
Image(systemName: symbol)
.font(.system(size: 11, weight: .semibold))
.foregroundStyle(.secondary)
.frame(width: 22, height: 22)
}
.buttonStyle(.plain)
.disabled(!enabled)
.opacity(enabled ? 1 : 0.3)
}

private var customText: Binding<String> {
Binding(
get: { answer.customText },
set: { text in
var next = answer
next.customText = text
if !question.multiSelect, !text.isEmpty { next.selected = [] }
answers[question.id] = next
}
)
}

private func toggle(_ choice: QuestionChoice) {
var next = answer
if question.multiSelect {
if let at = next.selected.firstIndex(of: choice.value) {
next.selected.remove(at: at)
} else {
next.selected.append(choice.value)
}
} else {
next.selected = [choice.value]
next.customText = ""
}
answers[question.id] = next
pendingAdvance?.cancel()
guard !question.multiSelect else { return }
pendingAdvance = Task {
try? await Task.sleep(for: .milliseconds(480))
guard !Task.isCancelled else { return }
await MainActor.run { advance() }
}
}

private func go(_ target: Int) {
pendingAdvance?.cancel()
index = min(max(target, 0), questions.count - 1)
}

private func advance() {
pendingAdvance?.cancel()
if isLast {
withAnimation { sent = true }
let content = QuestionForm.content(for: request, questions: questions, answers: answers)
Task { await submit(content) }
} else {
index += 1
}
}
}
176 changes: 176 additions & 0 deletions mobile/ios/App/BranchedMenu.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import SwiftUI

struct BranchedItem: Identifiable, Equatable {
let id: String
let label: String
var icon: String?
}

struct BranchedSection: Identifiable, Equatable {
let id: String
let label: String
let children: [BranchedItem]
}

/// SwiftUI port of the web BranchedMenu: sections fold open, each child hangs off a drawn branch.
struct BranchedMenu: View {
let sections: [BranchedSection]
@Binding var active: String
var onSelect: ((String) -> Void)?
@State private var open: Set<String>
@Namespace private var marker

private let pad: CGFloat = 6
private let rowHeight: CGFloat = 36
private let indent: CGFloat = 40
private let trunk: CGFloat = 14
private let radius: CGFloat = 10
private let lineWidth: CGFloat = 1.5

init(
sections: [BranchedSection],
active: Binding<String>,
initiallyOpen: Set<String>? = nil,
onSelect: ((String) -> Void)? = nil
) {
self.sections = sections
_active = active
self.onSelect = onSelect
_open = State(initialValue: initiallyOpen ?? Set(sections.map(\.id)))
}

private var line: Color { Color.primary.opacity(0.2) }
private var muted: Color { Color.primary.opacity(0.55) }

var body: some View {
VStack(alignment: .leading, spacing: 0) {
ForEach(sections) { section in
sectionView(section)
}
}
.padding(.leading, 14)
.background(alignment: .topLeading) {
Rectangle()
.fill(LinearGradient(
stops: [.init(color: line, location: 0), .init(color: line, location: 0.55), .init(color: .clear, location: 1)],
startPoint: .top,
endPoint: .bottom
))
.frame(width: 2)
.padding(.top, 8)
}
}

private func sectionView(_ section: BranchedSection) -> some View {
let isOpen = open.contains(section.id)
let holdsActive = section.children.contains { $0.id == active }
return VStack(alignment: .leading, spacing: 0) {
Button {
withAnimation(.timingCurve(0.23, 1, 0.32, 1, duration: 0.3)) {
if isOpen { open.remove(section.id) } else { open.insert(section.id) }
}
} label: {
Text(section.label)
.font(.system(size: 15, weight: .medium))
.foregroundStyle(isOpen ? Color.primary : muted)
.padding(.vertical, 9)
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.background(alignment: .leading) {
if holdsActive && isOpen {
Rectangle()
.fill(Color.primary)
.frame(width: 2, height: 16)
.offset(x: -14)
.matchedGeometryEffect(id: "marker", in: marker)
}
}
if isOpen {
tree(section)
.clipped()
.transition(.opacity.combined(with: .move(edge: .top)))
}
}
}

private func rowY(_ k: Int) -> CGFloat { pad + CGFloat(k) * rowHeight + rowHeight / 2 }

private func tree(_ section: BranchedSection) -> some View {
let kids = section.children
let height = pad * 2 + CGFloat(kids.count) * rowHeight
let endX = indent - 8
let r = min(radius, rowHeight / 2 - 2)
return ZStack(alignment: .topLeading) {
Path { p in
p.move(to: CGPoint(x: trunk, y: 0))
p.addLine(to: CGPoint(x: trunk, y: rowY(kids.count - 1) - r))
}
.stroke(line, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round, lineJoin: .round))
ForEach(Array(kids.enumerated()), id: \.element.id) { k, kid in
branch(k: k, endX: endX, r: r, from: rowY(k) - r)
.stroke(line, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round, lineJoin: .round))
branch(k: k, endX: endX, r: r, from: 0)
.trim(from: 0, to: kid.id == active ? 1 : 0)
.stroke(Color.primary, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round, lineJoin: .round))
.animation(.timingCurve(0.23, 1, 0.32, 1, duration: 0.4), value: active)
}
VStack(spacing: 0) {
ForEach(kids) { kid in
Button {
active = kid.id
onSelect?(kid.id)
} label: {
HStack(spacing: 8) {
if let icon = kid.icon {
Image(systemName: icon).font(.system(size: 14))
}
Text(kid.label).lineLimit(1)
}
.font(.system(size: 14, weight: kid.id == active ? .medium : .regular))
.foregroundStyle(kid.id == active ? Color.primary : muted)
.padding(.leading, indent)
.frame(maxWidth: .infinity, minHeight: rowHeight, maxHeight: rowHeight, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
}
}
.padding(.vertical, pad)
}
.frame(height: height)
}

private func branch(k: Int, endX: CGFloat, r: CGFloat, from startY: CGFloat) -> Path {
let y = rowY(k)
return Path { p in
p.move(to: CGPoint(x: trunk, y: startY))
p.addLine(to: CGPoint(x: trunk, y: y - r))
p.addQuadCurve(to: CGPoint(x: trunk + r, y: y), control: CGPoint(x: trunk, y: y))
p.addLine(to: CGPoint(x: endX, y: y))
}
}
}

#Preview {
struct Demo: View {
@State private var active = "src"
var body: some View {
BranchedMenu(
sections: [
BranchedSection(id: "dirs", label: "目录", children: [
BranchedItem(id: "src", label: "src", icon: "folder"),
BranchedItem(id: "docs", label: "docs", icon: "folder"),
]),
BranchedSection(id: "files", label: "文件", children: [
BranchedItem(id: "readme", label: "README.md", icon: "doc.text"),
]),
],
active: $active
)
.padding()
}
}
return Demo()
}
Loading
Loading