App/Sources/StepListView.swift

main
fold/App/Sources/StepListView.swift history · blame · raw

53 lines · 1854 bytes

2 symbols in this file
 1import Folding
 2import SwiftUI
 3
 4/// Every step of the session's model by number and instruction, the current one checked and
 5/// scrolled into view. Choosing a step opens it at its start; choosing the current step leaves it
 6/// as it is. Either closes the list.
 7struct StepListView: View {
 8    let session: FoldSession
 9    private let steps: [FoldStep]
10    @Environment(\.dismiss) private var dismiss
11
12    init(session: FoldSession) {
13        self.session = session
14        steps = (try? readSteps(session.model)) ?? []
15    }
16
17    var body: some View {
18        VStack(spacing: 0) {
19            Text("Steps")
20                .font(.headline)
21                .accessibilityAddTraits(.isHeader)
22                .padding()
23            ScrollViewReader { proxy in
24                List(steps, id: \.index, rowContent: row)
25                    .listStyle(.plain)
26                    .onAppear { proxy.scrollTo(session.step.index) }
27            }
28        }
29    }
30
31    private func row(_ step: FoldStep) -> some View {
32        let isCurrent = step.index == session.step.index
33        return Button {
34            session.open(step: step.index)
35            dismiss()
36        } label: {
37            HStack(alignment: .firstTextBaseline, spacing: 12) {
38                Text("\(step.index)")
39                    .font(.headline)
40                    .monospacedDigit()
41                Text(step.instruction)
42                    .frame(maxWidth: .infinity, alignment: .leading)
43                Image(systemName: "checkmark")
44                    .fontWeight(.semibold)
45                    .opacity(isCurrent ? 1 : 0)
46            }
47        }
48        .foregroundStyle(.primary)
49        .accessibilityLabel("Step \(step.index), \(step.instruction)")
50        .accessibilityAddTraits(isCurrent ? .isSelected : [])
51        .accessibilityIdentifier("step.\(step.index)")
52    }
53}