2022-01-29 23:37:02 +00:00
|
|
|
//
|
|
|
|
// SendMessageView.swift
|
|
|
|
// SimpleX
|
|
|
|
//
|
|
|
|
// Created by Evgeny Poberezkin on 29/01/2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct SendMessageView: View {
|
|
|
|
var sendMessage: (String) -> Void
|
|
|
|
var inProgress: Bool = false
|
2022-04-16 09:37:01 +01:00
|
|
|
@Binding var message: String
|
2022-02-05 14:24:23 +00:00
|
|
|
@Namespace var namespace
|
2022-02-11 07:42:00 +00:00
|
|
|
@FocusState.Binding var keyboardVisible: Bool
|
2022-03-25 22:26:05 +04:00
|
|
|
@Binding var editing: Bool
|
2022-04-19 12:29:03 +04:00
|
|
|
@Binding var sendEnabled: Bool
|
2022-02-05 14:24:23 +00:00
|
|
|
@State private var teHeight: CGFloat = 42
|
|
|
|
@State private var teFont: Font = .body
|
|
|
|
var maxHeight: CGFloat = 360
|
|
|
|
var minHeight: CGFloat = 37
|
2022-01-29 23:37:02 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2022-02-05 14:24:23 +00:00
|
|
|
ZStack {
|
|
|
|
HStack(alignment: .bottom) {
|
|
|
|
ZStack(alignment: .leading) {
|
|
|
|
Text(message)
|
|
|
|
.font(teFont)
|
|
|
|
.foregroundColor(.clear)
|
|
|
|
.padding(.horizontal, 10)
|
|
|
|
.padding(.vertical, 8)
|
|
|
|
.matchedGeometryEffect(id: "te", in: namespace)
|
|
|
|
.background(GeometryReader(content: updateHeight))
|
|
|
|
TextEditor(text: $message)
|
|
|
|
.onSubmit(submit)
|
2022-02-11 07:42:00 +00:00
|
|
|
.focused($keyboardVisible)
|
2022-02-05 14:24:23 +00:00
|
|
|
.font(teFont)
|
2022-02-11 07:42:00 +00:00
|
|
|
.textInputAutocapitalization(.sentences)
|
2022-02-05 14:24:23 +00:00
|
|
|
.padding(.horizontal, 5)
|
|
|
|
.allowsTightening(false)
|
|
|
|
.frame(height: teHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inProgress) {
|
|
|
|
ProgressView()
|
|
|
|
.scaleEffect(1.4)
|
|
|
|
.frame(width: 31, height: 31, alignment: .center)
|
|
|
|
.padding([.bottom, .trailing], 3)
|
|
|
|
} else {
|
|
|
|
Button(action: submit) {
|
2022-03-25 22:26:05 +04:00
|
|
|
Image(systemName: editing ? "checkmark.circle.fill" : "arrow.up.circle.fill")
|
2022-02-05 14:24:23 +00:00
|
|
|
.resizable()
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
}
|
2022-04-19 12:29:03 +04:00
|
|
|
.disabled(!sendEnabled)
|
2022-02-05 14:24:23 +00:00
|
|
|
.frame(width: 29, height: 29)
|
|
|
|
.padding([.bottom, .trailing], 4)
|
|
|
|
}
|
2022-01-29 23:37:02 +00:00
|
|
|
}
|
2022-02-05 14:24:23 +00:00
|
|
|
|
|
|
|
RoundedRectangle(cornerSize: CGSize(width: 20, height: 20))
|
|
|
|
.strokeBorder(.secondary, lineWidth: 0.3, antialiased: true)
|
|
|
|
.frame(height: teHeight)
|
2022-01-29 23:37:02 +00:00
|
|
|
}
|
2022-02-05 14:24:23 +00:00
|
|
|
.padding(.vertical, 8)
|
2022-01-29 23:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func submit() {
|
2022-02-05 14:24:23 +00:00
|
|
|
sendMessage(message)
|
|
|
|
message = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateHeight(_ g: GeometryProxy) -> Color {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
teHeight = min(max(g.frame(in: .local).size.height, minHeight), maxHeight)
|
2022-02-13 08:45:08 +00:00
|
|
|
teFont = isShortEmoji(message)
|
|
|
|
? message.count < 4
|
|
|
|
? largeEmojiFont
|
|
|
|
: mediumEmojiFont
|
|
|
|
: .body
|
2022-02-05 14:24:23 +00:00
|
|
|
}
|
|
|
|
return Color.clear
|
2022-01-29 23:37:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SendMessageView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-03-25 22:26:05 +04:00
|
|
|
@State var message: String = ""
|
2022-02-11 07:42:00 +00:00
|
|
|
@FocusState var keyboardVisible: Bool
|
2022-03-25 22:26:05 +04:00
|
|
|
@State var editingOff: Bool = false
|
|
|
|
@State var editingOn: Bool = true
|
2022-04-19 12:29:03 +04:00
|
|
|
@State var sendEnabled: Bool = true
|
2022-03-25 22:26:05 +04:00
|
|
|
@State var item: ChatItem? = ChatItem.getSample(1, .directSnd, .now, "hello")
|
2022-02-11 07:42:00 +00:00
|
|
|
|
2022-03-25 22:26:05 +04:00
|
|
|
return Group {
|
|
|
|
VStack {
|
|
|
|
Text("")
|
|
|
|
Spacer(minLength: 0)
|
|
|
|
SendMessageView(
|
|
|
|
sendMessage: { print ($0) },
|
|
|
|
message: $message,
|
|
|
|
keyboardVisible: $keyboardVisible,
|
2022-04-19 12:29:03 +04:00
|
|
|
editing: $editingOff,
|
|
|
|
sendEnabled: $sendEnabled
|
2022-03-25 22:26:05 +04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
VStack {
|
|
|
|
Text("")
|
|
|
|
Spacer(minLength: 0)
|
|
|
|
SendMessageView(
|
|
|
|
sendMessage: { print ($0) },
|
|
|
|
message: $message,
|
|
|
|
keyboardVisible: $keyboardVisible,
|
2022-04-19 12:29:03 +04:00
|
|
|
editing: $editingOn,
|
|
|
|
sendEnabled: $sendEnabled
|
2022-03-25 22:26:05 +04:00
|
|
|
)
|
|
|
|
}
|
2022-02-05 14:24:23 +00:00
|
|
|
}
|
2022-01-29 23:37:02 +00:00
|
|
|
}
|
|
|
|
}
|