Kordophone client for iOS and macOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.2 KiB

3 years ago
import SwiftUI
import KordophoneKit
struct ConversationView : View {
var conversation: Conversation
var messages: [Message]
var sendAction: (String) -> Void
3 years ago
@State private var inputText = ""
@ViewBuilder
private var transcript: some View {
ScrollViewReader { proxy in
ScrollView {
LazyVStack {
ForEach(messages) { message in
if message.sentByMe {
Spacer()
}
MessageView(message: message)
.id(message)
if !message.sentByMe {
Spacer()
}
3 years ago
}
}
}
.onChange(of: messages) { newValue in
proxy.scrollTo(newValue.last, anchor: .bottom)
}
3 years ago
}
}
@ViewBuilder
private var inputView: some View {
TextField.init(text: $inputText) {
Text("Message")
}
.padding()
.onSubmit {
if inputText.count > 0 {
sendAction(inputText)
inputText = ""
}
}
}
var body: some View {
VStack(spacing: 0) {
transcript
inputView
}
.navigationTitle(conversation.effectiveDisplayName)
}
3 years ago
}
struct ConversationViewWrapper : View {
var conversation: Conversation
@State private var messages = [Message]()
@EnvironmentObject private var connection: Connection
@StateObject private var updater = IntervalUpdater(5)
3 years ago
private func refresh() {
Task {
self.messages = await connection.messages(in: conversation) ?? .init()
}
}
var body: some View {
ConversationView(conversation: conversation, messages: messages) { sent in
Task {
await connection.send(text: sent, in: conversation)
3 years ago
}
refresh()
}
.onChange(of: conversation, perform: { newValue in
refresh()
})
.onAppear {
refresh()
}
.onChange(of: updater.seed) { newValue in
refresh()
}
3 years ago
}
}