112 lines
4.4 KiB
Swift
112 lines
4.4 KiB
Swift
import SwiftUI
|
|
import UIKit
|
|
|
|
struct ConversationTextInput: View {
|
|
@EnvironmentObject var store: AppStore
|
|
|
|
@State private var messageStr = ""
|
|
@FocusState private var isFocused: Bool
|
|
@Binding var autoScroll: Bool
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
Rectangle()
|
|
.foregroundColor(.Main.separator)
|
|
.frame(height: 0.5)
|
|
.padding(.bottom, 8)
|
|
if !replyText.isEmpty {
|
|
VStack(spacing: 0) {
|
|
HStack(alignment: .top) {
|
|
Text(replyText)
|
|
.font(.body3)
|
|
.foregroundColor(Color.Main.black)
|
|
.multilineTextAlignment(.leading)
|
|
.lineLimit(3)
|
|
.padding(8)
|
|
Spacer()
|
|
Image(systemName: "xmark")
|
|
.font(.title2)
|
|
.foregroundColor(.Tango.blueLight)
|
|
.padding(.leading, 8)
|
|
.tappablePadding(.symmetric(8)) {
|
|
store.dispatch(.conversationAction(.setReplyText("")))
|
|
}
|
|
.padding(8)
|
|
}
|
|
.frame(maxWidth: .infinity)
|
|
.background(RoundedRectangle(cornerRadius: 4)
|
|
.foregroundColor(.Main.backgroundLight)
|
|
.shadow(radius: 0.5)
|
|
)
|
|
.padding(.bottom, 8)
|
|
.padding(.horizontal, 8)
|
|
}
|
|
.padding(.horizontal, 8)
|
|
}
|
|
HStack {
|
|
Image(systemName: "paperclip")
|
|
.font(.title2)
|
|
.foregroundColor(.Tango.blueLight)
|
|
.padding(.leading, 8)
|
|
.tappablePadding(.symmetric(8)) {
|
|
store.dispatch(.conversationAction(.showAttachmentPicker(true)))
|
|
}
|
|
TextField(L10n.Chat.textfieldPrompt, text: $messageStr)
|
|
.font(.body1)
|
|
.focused($isFocused)
|
|
.padding(.horizontal, 8)
|
|
.padding(.vertical, 4)
|
|
.background(Color.Main.white)
|
|
.clipShape(RoundedRectangle(cornerRadius: 8))
|
|
.padding(.vertical, 4)
|
|
let img = messageStr.isEmpty ? "paperplane" : "paperplane.fill"
|
|
Image(systemName: img)
|
|
.font(.title2)
|
|
.foregroundColor(messageStr.isEmpty ? .Main.separator : .Tango.blueLight)
|
|
.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 = ""
|
|
// UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
|
store.dispatch(.conversationAction(.setReplyText("")))
|
|
autoScroll = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.padding(.bottom, 8)
|
|
.background(Color.Main.backgroundDark)
|
|
.onChange(of: store.state.conversationsState.replyText) { new in
|
|
if !new.isEmpty {
|
|
isFocused = true
|
|
}
|
|
}
|
|
.fullScreenCover(isPresented: Binding<Bool>(
|
|
get: { store.state.conversationsState.attachmentPickerVisible },
|
|
set: { _ in }
|
|
)) {
|
|
AttachmentPickerScreen()
|
|
}
|
|
}
|
|
|
|
private var replyText: String {
|
|
store.state.conversationsState.replyText
|
|
}
|
|
|
|
private var composedMessage: String {
|
|
var result = ""
|
|
if !replyText.isEmpty {
|
|
result += replyText + "\n\n"
|
|
}
|
|
result += messageStr
|
|
return result
|
|
}
|
|
}
|