Files
simplex-chat/apps/ios/Shared/Views/TerminalView.swift
Evgeny @ SimpleX Chat 73761e6e0b core, ui: test app views on the core's recorded responses (#8234)
* core, ui: plan name alert tests against core fixtures

* core: write name lookup responses to app fixtures in name tests

* android, desktop: name alerts tested against core fixtures

* core, ios: plan Swift fixtures and iOS unit tests of name alerts

* core: write name lookup responses in the Swift form to iOS fixtures

* ios: name alerts tested against core fixtures

* core: write both app fixtures of a name test in one run

* ui: add name lookup responses written by the core tests as fixtures

* core: keep the fixture writer of name tests to the forms it receives

* desktop: run name alert tests in the US locale

* ios: one switch for the title and effect of name alert actions

* docs: name alert tests in the app specs, CI guide and plan

* docs: known gaps of the name alert tests in the plan

* ui: restore the name alert code and the iOS project of master

* core: record name lookup responses and name parses for app tests

* android, desktop: test name alerts of planAndConnect on recorded responses

* ci: run the app tests for a change of the name fixtures

* core: name lookup responses and name parses recorded by the core tests

* docs: plan of app views tested on core responses

* ios: test name alerts of planAndConnect on recorded responses

* ci: iOS unit tests in the iOS job, the app launched on stub migrations

* android, desktop: core hooks below the externals, in the specs

* ios: name alert tests compile with the converter at file scope

* ui: the moved name test expects the alert to open a chat with the new address

* core, ui: app test fixtures under tests/fixtures/api

* core: record the responses of the name lookup flows that the apps test

* core: record URI parses, and normalize the random values of fixtures

* android, desktop: URI parse hook and event processing for tests

* ios: URI parse hook for tests

* desktop: full failure messages of tests

* core: domain errors view compiles with GHC 8.10.7

* core: normalize parses and profile send times in fixtures

* core: record the parses of the chat list search

* core: recorded responses of the name flows and the contact address

* android, desktop: test name alerts, buttons, search and name view

* ios: test name alerts, buttons, search and the moved event

* docs: plan, specs and CI guide of the app tests
2026-11-16 23:36:37 +00:00

207 lines
7.8 KiB
Swift

//
// TerminalView.swift
// SimpleX
//
// Created by Evgeny Poberezkin on 27/01/2022.
// Copyright © 2022 SimpleX Chat. All rights reserved.
//
import SwiftUI
import SimpleXChat
private let terminalFont = Font.custom("Menlo", size: 16)
private let maxItemSize: Int = 50000
struct TerminalView: View {
@EnvironmentObject var chatModel: ChatModel
@AppStorage(DEFAULT_PERFORM_LA) private var prefPerformLA = false
@AppStorage(DEFAULT_DEVELOPER_TOOLS) private var developerTools = false
@State var composeState: ComposeState = ComposeState()
@State var selectedRange = NSRange()
@State private var keyboardVisible = false
@State private var keyboardHiddenDate = Date.now
@State var authorized = !UserDefaults.standard.bool(forKey: DEFAULT_PERFORM_LA)
@State private var terminalItem: TerminalItem?
@State private var scrolled = false
@State private var showing = false
var body: some View {
if authorized {
terminalView()
.onAppear {
if showing { return }
showing = true
Task {
let items = await TerminalItems.shared.items()
await MainActor.run {
chatModel.terminalItems = items
chatModel.showingTerminal = true
}
}
}
.onDisappear {
if terminalItem == nil {
chatModel.showingTerminal = false
chatModel.terminalItems = []
}
}
} else {
Button(action: runAuth) { Label("Unlock", systemImage: "lock") }
.onAppear(perform: runAuth)
}
}
private func runAuth() { authorize(NSLocalizedString("Open chat console", comment: "authentication reason"), $authorized) }
private func terminalView() -> some View {
VStack {
ScrollViewReader { proxy in
ScrollView {
LazyVStack {
ForEach(chatModel.terminalItems) { item in
Button {
terminalItem = item
} label: {
HStack {
Text(item.id.formatted(date: .omitted, time: .standard))
Text(item.label)
.frame(maxWidth: .infinity, maxHeight: 30, alignment: .leading)
}
.font(terminalFont)
.padding(.horizontal)
}
}
.onAppear {
if !scrolled {
scrollToBottom(proxy)
scrolled = true
}
}
.onChange(of: chatModel.terminalItems.count) { _ in scrollToBottom(proxy) }
.onChange(of: keyboardVisible) { _ in
if keyboardVisible {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
scrollToBottom(proxy, animation: .easeInOut(duration: 1))
}
}
}
.background(NavigationLink(
isActive: Binding(get: { terminalItem != nil }, set: { _ in }),
destination: terminalItemView,
label: { EmptyView() }
))
}
}
Spacer()
SendMessageView(
composeState: $composeState,
selectedRange: $selectedRange,
sendMessage: { _ in consoleSendMessage() },
showVoiceMessageButton: false,
onMediaAdded: { _ in },
keyboardVisible: $keyboardVisible,
keyboardHiddenDate: $keyboardHiddenDate
)
.padding(.horizontal, 12)
}
}
.navigationViewStyle(.stack)
.toolbar {
// Redaction broken for `.navigationTitle` - using a toolbar item instead.
ToolbarItem(placement: .principal) {
Text("Chat console").font(.headline)
}
}
.modifier(ThemedBackground())
}
func scrollToBottom(_ proxy: ScrollViewProxy, animation: Animation = .default) {
if let id = chatModel.terminalItems.last?.id {
withAnimation(animation) {
proxy.scrollTo(id, anchor: .bottom)
}
}
}
func terminalItemView() -> some View {
let s = terminalItem?.details ?? ""
return ScrollView {
Text(s.prefix(maxItemSize))
.padding()
.frame(maxWidth: .infinity)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button { showShareSheet(items: [s]) } label: {
Image(systemName: "square.and.arrow.up")
}
}
}
.onDisappear { terminalItem = nil }
.modifier(ThemedBackground())
}
func consoleSendMessage() {
if composeState.message.starts(with: "/sql") && (!prefPerformLA || !developerTools) {
let resp: APIResult<ChatResponse2> = APIResult.error(ChatError.error(errorType: ChatErrorType.commandError(message: "Failed reading: empty")))
Task {
await TerminalItems.shared.addCommand(.now, .string(composeState.message), resp)
}
} else {
let cmd = composeState.message
DispatchQueue.global().async {
Task {
await MainActor.run { composeState.inProgress = true }
await sendTerminalCmd(cmd)
await MainActor.run { composeState.inProgress = false }
}
}
}
composeState = ComposeState()
}
}
func sendTerminalCmd(_ cmd: String) async {
let start: Date = .now
await withCheckedContinuation { (cont: CheckedContinuation<Void, Never>) in
let d = sendSimpleXCmdStr(cmd, nil, 0)
Task {
guard let d else {
await TerminalItems.shared.addCommand(start, ChatCommand.string(cmd), APIResult<ChatResponse2>.error(.invalidJSON(json: nil)))
return
}
let r0: APIResult<ChatResponse0> = decodeAPIResult(d)
guard case .invalid = r0 else {
await TerminalItems.shared.addCommand(start, .string(cmd), r0)
return
}
let r1: APIResult<ChatResponse1> = decodeAPIResult(d)
guard case .invalid = r1 else {
await TerminalItems.shared.addCommand(start, .string(cmd), r1)
return
}
let r2: APIResult<ChatResponse2> = decodeAPIResult(d)
await TerminalItems.shared.addCommand(start, .string(cmd), r2)
}
cont.resume(returning: ())
}
}
struct TerminalView_Previews: PreviewProvider {
static var previews: some View {
let chatModel = ChatModel()
chatModel.terminalItems = [
.err(previewDate, APIResult<ChatResponse2>.invalid(type: "contactSubscribed", json: "{}".data(using: .utf8)!).unexpected),
.err(previewDate + 1, APIResult<ChatResponse2>.invalid(type: "newChatItems", json: "{}".data(using: .utf8)!).unexpected)
]
return NavigationView {
TerminalView()
.environmentObject(chatModel)
}
}
}