2024-09-03 15:13:58 +00:00
|
|
|
import Combine
|
|
|
|
import Foundation
|
|
|
|
import Martin
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ConversationScreen: View {
|
|
|
|
@Environment(\.router) var router
|
|
|
|
@StateObject var messagesStore: MessagesStore
|
|
|
|
@StateObject var attachments: AttachmentsStore
|
2024-10-15 11:39:23 +00:00
|
|
|
@StateObject var settings: ChatSettingsStore
|
2024-09-03 15:13:58 +00:00
|
|
|
|
|
|
|
@State private var autoScroll = true
|
|
|
|
@State private var firstIsVisible = true
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ZStack {
|
|
|
|
// Background color
|
|
|
|
Color.Material.Background.light
|
|
|
|
.ignoresSafeArea()
|
|
|
|
|
|
|
|
// Content
|
|
|
|
VStack(spacing: 0) {
|
|
|
|
// Header
|
|
|
|
SharedNavigationBar(
|
|
|
|
leftButton: .init(
|
|
|
|
image: Image(systemName: "chevron.left"),
|
|
|
|
action: {
|
|
|
|
router.dismissScreen()
|
|
|
|
}
|
|
|
|
),
|
2024-10-23 15:13:18 +00:00
|
|
|
centerText: .init(text: centerText()),
|
|
|
|
rightButton: .init(
|
|
|
|
image: Image(systemName: "gear"),
|
|
|
|
action: {
|
|
|
|
router.showScreen(.push) { _ in
|
|
|
|
ConversationSettingsScreen()
|
|
|
|
.environmentObject(settings)
|
|
|
|
.navigationBarHidden(true)
|
|
|
|
}
|
2024-09-19 16:50:48 +00:00
|
|
|
}
|
2024-10-23 15:13:18 +00:00
|
|
|
)
|
2024-09-03 15:13:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Msg list
|
|
|
|
let messages = messagesStore.messages
|
|
|
|
if !messages.isEmpty {
|
|
|
|
ScrollViewReader { proxy in
|
|
|
|
ScrollView {
|
|
|
|
LazyVStack(spacing: 0) {
|
|
|
|
ForEach(messages) { message in
|
|
|
|
ConversationMessageRow(message: message)
|
|
|
|
.id(message.id)
|
|
|
|
.flip()
|
|
|
|
.onAppear {
|
|
|
|
if message.id == messages.first?.id {
|
|
|
|
firstIsVisible = true
|
|
|
|
autoScroll = true
|
|
|
|
}
|
|
|
|
messagesStore.scrolledMessage(message.id)
|
|
|
|
}
|
|
|
|
.onDisappear {
|
|
|
|
if message.id == messages.first?.id {
|
|
|
|
firstIsVisible = false
|
|
|
|
autoScroll = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.flip()
|
|
|
|
.scrollDismissesKeyboard(.immediately)
|
|
|
|
.onChange(of: autoScroll) { new in
|
|
|
|
if new, !firstIsVisible {
|
|
|
|
withAnimation {
|
|
|
|
proxy.scrollTo(messages.first?.id, anchor: .top)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.onTapGesture {
|
|
|
|
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jump to last button
|
|
|
|
if !autoScroll {
|
|
|
|
VStack {
|
|
|
|
Spacer()
|
|
|
|
HStack {
|
|
|
|
Spacer()
|
|
|
|
Button {
|
|
|
|
autoScroll = true
|
|
|
|
} label: {
|
|
|
|
ZStack {
|
|
|
|
Circle()
|
|
|
|
.fill(Color.Material.Shape.white)
|
|
|
|
Image(systemName: "arrow.down")
|
|
|
|
.foregroundColor(.Material.Elements.active)
|
|
|
|
}
|
|
|
|
.frame(width: 40, height: 40)
|
|
|
|
.shadow(color: .black.opacity(0.2), radius: 4)
|
|
|
|
.padding(.trailing, 8)
|
|
|
|
.padding(.bottom, 8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.environmentObject(messagesStore)
|
|
|
|
.environmentObject(attachments)
|
|
|
|
.safeAreaInset(edge: .bottom, spacing: 0) {
|
|
|
|
ConversationTextInput(autoScroll: $autoScroll)
|
|
|
|
.environmentObject(messagesStore)
|
|
|
|
.environmentObject(attachments)
|
2024-09-20 15:32:10 +00:00
|
|
|
.environmentObject(settings)
|
2024-09-03 15:13:58 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-19 15:24:56 +00:00
|
|
|
|
|
|
|
private func centerText() -> String {
|
|
|
|
let name = messagesStore.roster.name ?? JID(messagesStore.roster.contactBareJid).localPart
|
|
|
|
if let name = name {
|
|
|
|
return name
|
|
|
|
} else {
|
|
|
|
return L10n.Conversation.title
|
|
|
|
}
|
|
|
|
}
|
2024-09-03 15:13:58 +00:00
|
|
|
}
|