SimpleX-Chat/apps/ios/Shared/Views/TerminalView.swift

143 lines
5.2 KiB
Swift
Raw Normal View History

//
// 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()
@FocusState private var keyboardVisible: Bool
@State var authorized = !UserDefaults.standard.bool(forKey: DEFAULT_PERFORM_LA)
var body: some View {
if authorized {
terminalView()
} else {
Button(action: runAuth) { Label("Unlock", systemImage: "lock") }
.onAppear(perform: runAuth)
}
}
private func runAuth() {
authenticate(reason: NSLocalizedString("Open chat console", comment: "authentication reason")) { laResult in
switch laResult {
case .success: authorized = true
case .unavailable: authorized = true
case .failed: authorized = false
}
}
}
private func terminalView() -> some View {
VStack {
2022-02-04 16:31:08 +00:00
ScrollViewReader { proxy in
ScrollView {
LazyVStack {
ForEach(chatModel.terminalItems) { item in
NavigationLink {
let s = item.details
2022-02-04 16:31:08 +00:00
ScrollView {
Text(s.prefix(maxItemSize))
.padding()
2022-02-04 16:31:08 +00:00
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button { showShareSheet(items: [s]) } label: {
Image(systemName: "square.and.arrow.up")
}
}
}
2022-02-04 16:31:08 +00:00
} label: {
HStack {
Text(item.id.formatted(date: .omitted, time: .standard))
Text(item.label)
.frame(maxWidth: .infinity, maxHeight: 30, alignment: .leading)
}
.font(terminalFont)
.padding(.horizontal)
}
}
2022-02-04 16:31:08 +00:00
.onAppear { scrollToBottom(proxy) }
.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))
}
}
}
}
}
2022-02-04 16:31:08 +00:00
Spacer()
SendMessageView(
composeState: $composeState,
sendMessage: sendMessage,
showVoiceMessageButton: false,
onImagesAdded: { _ in },
keyboardVisible: $keyboardVisible
)
mobile: support images (#536) * ios api * ios wip * android wip * ios files folder * ios get address on start * android app files folder * ios more backend * android more backend * translation * ios image without text, remove preview * android image without text, remove preview * fix translation * file name in previews and w/t text * Revert "file name in previews and w/t text" This reverts commit 0110570e55d23ecc361613f41aeadcaff07ac903. * ios filename in preview * android filename in preview * android wider images * ios determine width on image for correct quote width * ios images in previews wip * ios square image in quote * ios: update image layout * android images in quotes * android remove redundant modifier * android clip to bounds * android - image in right side of quote * android refactor image view * android - refactor, align quote text top * android fix emoji view * fix image layout * full screen image view, fix quote layout * android various size * android fixed image width * android meta on image * ios: add drag gesture to hide full-screen image * android: make image-only meta white * refactor file.stored * android: meta icon color * android: open chat scrolled to last unread item * copy/share image messages * android: full screen image * check file is loaded * terminal: refactor view for messages with files * android: change to onClick, only show stored file * android: remove close sheet bar * android: close image view on click * translation * android: pass showMenu to CIImageView to show menu on long click * increase DropDown width Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
2022-04-19 12:29:03 +04:00
.padding(.horizontal, 12)
2022-02-04 16:31:08 +00:00
}
}
.navigationViewStyle(.stack)
.navigationTitle("Chat console")
}
func scrollToBottom(_ proxy: ScrollViewProxy, animation: Animation = .default) {
2022-02-04 16:31:08 +00:00
if let id = chatModel.terminalItems.last?.id {
withAnimation(animation) {
2022-02-04 16:31:08 +00:00
proxy.scrollTo(id, anchor: .bottom)
}
}
}
2022-04-27 20:54:21 +04:00
func sendMessage() {
let cmd = ChatCommand.string(composeState.message)
if composeState.message.starts(with: "/sql") && (!prefPerformLA || !developerTools) {
let resp = ChatResponse.chatCmdError(user: nil, chatError: ChatError.error(errorType: ChatErrorType.commandError(message: "Failed reading: empty")))
DispatchQueue.main.async {
ChatModel.shared.addTerminalItem(.cmd(.now, cmd))
ChatModel.shared.addTerminalItem(.resp(.now, resp))
}
} else {
DispatchQueue.global().async {
Task {
composeState.inProgress = true
_ = await chatSendCmd(cmd)
composeState.inProgress = false
}
}
}
composeState = ComposeState()
}
}
struct TerminalView_Previews: PreviewProvider {
static var previews: some View {
let chatModel = ChatModel()
chatModel.terminalItems = [
.resp(.now, ChatResponse.response(type: "contactSubscribed", json: "{}")),
.resp(.now, ChatResponse.response(type: "newChatItem", json: "{}"))
]
return NavigationView {
TerminalView()
.environmentObject(chatModel)
}
}
}