another.im-ios/ConversationsClassic/View/Main/Conversation/ConversationTextInput.swift

102 lines
4 KiB
Swift
Raw Normal View History

2024-08-14 14:37:45 +00:00
import SwiftUI
import UIKit
struct ConversationTextInput: View {
2024-08-17 08:50:44 +00:00
@Environment(\.router) var router
2024-08-18 16:42:59 +00:00
@EnvironmentObject var messages: MessagesStore
2024-08-18 09:05:43 +00:00
@EnvironmentObject var attachments: AttachmentsStore
2024-08-15 15:15:49 +00:00
2024-08-14 14:37:45 +00:00
@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)
2024-08-18 16:42:59 +00:00
if !messages.replyText.isEmpty {
2024-08-14 14:37:45 +00:00
VStack(spacing: 0) {
HStack(alignment: .top) {
2024-08-18 16:42:59 +00:00
Text(messages.replyText)
2024-08-14 14:37:45 +00:00
.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)) {
2024-08-18 16:42:59 +00:00
messages.replyText = ""
2024-08-14 14:37:45 +00:00
}
.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)) {
2024-08-17 08:50:44 +00:00
router.showScreen(.fullScreenCover) { _ in
2024-08-18 09:05:43 +00:00
AttachmentPickerScreen()
2024-08-18 16:42:59 +00:00
.environmentObject(messages)
2024-08-18 09:05:43 +00:00
.environmentObject(attachments)
2024-08-17 08:50:44 +00:00
}
2024-08-14 14:37:45 +00:00
}
TextField("", text: $messageStr, prompt: Text(L10n.Chat.textfieldPrompt).foregroundColor(.Material.Shape.separator))
.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 {
2024-08-18 16:42:59 +00:00
messages.sendMessage(composedMessage)
2024-08-18 10:26:54 +00:00
messageStr = ""
autoScroll = true
2024-08-14 14:37:45 +00:00
}
}
}
}
.padding(.bottom, 8)
.background(Color.Material.Background.dark)
2024-08-18 16:42:59 +00:00
.onChange(of: messages.replyText) { new in
2024-08-15 15:15:49 +00:00
if !new.isEmpty {
isFocused = true
}
}
2024-08-14 14:37:45 +00:00
}
private var composedMessage: String {
var result = ""
2024-08-18 16:42:59 +00:00
if !messages.replyText.isEmpty {
result += messages.replyText + "\n\n"
2024-08-14 14:37:45 +00:00
}
result += messageStr
return result
}
}