another.im-ios/Monal/another.im/Views/Conversation/ConversationTextInput.swift
2024-11-25 16:26:52 +01:00

105 lines
4.2 KiB
Swift

import SwiftUI
import UIKit
struct ConversationTextInput: View {
@Environment(\.router) var router
@EnvironmentObject var chatWrapper: MonalChatWrapper
@State private var messageStr = ""
@FocusState private var isFocused: Bool
@Binding var autoScroll: Bool
var body: some View {
VStack(spacing: 0) {
Rectangle()
.foregroundColor(.Material.Shape.separator)
.frame(height: 0.5)
.padding(.bottom, 8)
if !chatWrapper.replyText.isEmpty {
VStack(spacing: 0) {
HStack(alignment: .top) {
Text(chatWrapper.replyText)
.font(.body3)
.foregroundColor(Color.Material.Text.main)
.multilineTextAlignment(.leading)
.lineLimit(3)
.padding(8)
Spacer()
Image(systemName: "xmark")
.font(.title2)
.foregroundColor(.Material.Elements.active)
.padding(.leading, 8)
.tappablePadding(.symmetric(8)) {
chatWrapper.replyText = ""
}
.padding(8)
}
.frame(maxWidth: .infinity)
.background(RoundedRectangle(cornerRadius: 4)
.foregroundColor(.Material.Background.light)
.shadow(radius: 0.5)
)
.padding(.bottom, 8)
.padding(.horizontal, 8)
}
.padding(.horizontal, 8)
}
HStack {
Image(systemName: "paperclip")
.font(.title2)
.foregroundColor(.Material.Elements.active)
.padding(.leading, 8)
.tappablePadding(.symmetric(8)) {
router.showScreen(.fullScreenCover) { _ in
Text("not yet implemented")
// AttachmentPickerScreen()
// .environmentObject(messages)
// .environmentObject(attachments)
}
}
TextField("", text: $messageStr, prompt: Text(L10n.Chat.textfieldPrompt).foregroundColor(.Material.Shape.separator), axis: .vertical)
.font(.body1)
.foregroundColor(Color.Material.Text.main)
.accentColor(.Material.Shape.black)
.focused($isFocused)
.padding(.horizontal, 8)
.padding(.vertical, 4)
.background(Color.Material.Shape.white)
.clipShape(RoundedRectangle(cornerRadius: 8))
.padding(.vertical, 4)
let img = messageStr.isEmpty ? "paperplane" : "paperplane.fill"
Image(systemName: img)
.font(.title2)
.foregroundColor(messageStr.isEmpty ? .Material.Elements.inactive : .Material.Elements.active)
.padding(.trailing, 8)
.tappablePadding(.symmetric(8)) {
if !messageStr.isEmpty {
chatWrapper.sendText(composedMessage)
messageStr = ""
autoScroll = true
if !chatWrapper.replyText.isEmpty {
chatWrapper.replyText = ""
}
}
}
}
}
.padding(.bottom, 8)
.background(Color.Material.Background.dark)
.onChange(of: chatWrapper.replyText) { new in
if !new.isEmpty {
isFocused = true
}
}
}
private var composedMessage: String {
var result = ""
if !chatWrapper.replyText.isEmpty {
result += chatWrapper.replyText.makeReply + "\n\n"
}
result += messageStr
return result
}
}