2024-06-27 11:39:41 +00:00
|
|
|
import SwiftUI
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
struct ConversationTextInput: View {
|
|
|
|
@EnvironmentObject var store: AppStore
|
|
|
|
|
|
|
|
@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 !replyText.isEmpty {
|
|
|
|
VStack(spacing: 0) {
|
|
|
|
HStack(alignment: .top) {
|
|
|
|
Text(replyText)
|
|
|
|
.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)) {
|
|
|
|
store.dispatch(.conversationAction(.setReplyText("")))
|
|
|
|
}
|
|
|
|
.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)) {
|
2024-07-02 08:32:14 +00:00
|
|
|
store.dispatch(.conversationAction(.showAttachmentPicker(true)))
|
2024-06-27 11:39:41 +00:00
|
|
|
}
|
|
|
|
TextField(L10n.Chat.textfieldPrompt, text: $messageStr)
|
|
|
|
.font(.body1)
|
|
|
|
.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 {
|
|
|
|
guard let acc = store.state.conversationsState.currentChat?.account else { return }
|
|
|
|
guard let contact = store.state.conversationsState.currentChat?.participant else { return }
|
|
|
|
store.dispatch(.conversationAction(.sendMessage(
|
|
|
|
from: acc,
|
|
|
|
to: contact,
|
|
|
|
body: composedMessage
|
|
|
|
)))
|
|
|
|
messageStr = ""
|
2024-07-01 10:05:39 +00:00
|
|
|
// UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
2024-06-27 11:39:41 +00:00
|
|
|
store.dispatch(.conversationAction(.setReplyText("")))
|
2024-07-02 08:32:14 +00:00
|
|
|
autoScroll = true
|
2024-06-27 11:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.padding(.bottom, 8)
|
2024-07-04 08:21:12 +00:00
|
|
|
.background(Color.Material.Background.dark)
|
2024-06-27 11:39:41 +00:00
|
|
|
.onChange(of: store.state.conversationsState.replyText) { new in
|
|
|
|
if !new.isEmpty {
|
|
|
|
isFocused = true
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 08:32:14 +00:00
|
|
|
.fullScreenCover(isPresented: Binding<Bool>(
|
|
|
|
get: { store.state.conversationsState.attachmentPickerVisible },
|
|
|
|
set: { _ in }
|
|
|
|
)) {
|
|
|
|
AttachmentPickerScreen()
|
|
|
|
}
|
2024-06-27 11:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private var replyText: String {
|
|
|
|
store.state.conversationsState.replyText
|
|
|
|
}
|
|
|
|
|
|
|
|
private var composedMessage: String {
|
|
|
|
var result = ""
|
|
|
|
if !replyText.isEmpty {
|
2024-07-01 11:54:43 +00:00
|
|
|
result += replyText + "\n\n"
|
2024-06-27 11:39:41 +00:00
|
|
|
}
|
|
|
|
result += messageStr
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|