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

105 lines
4.2 KiB
Swift
Raw Permalink Normal View History

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