conversations-classic-ios/ConversationsClassic/View/Main/Conversation/ConversationScreen.swift

107 lines
4 KiB
Swift
Raw Normal View History

2024-08-13 08:40:27 +00:00
import Combine
import Foundation
import Martin
import SwiftUI
struct ConversationScreen: View {
@Environment(\.router) var router
2024-08-15 15:15:49 +00:00
@StateObject var conversation: ConversationStore
2024-08-13 08:40:27 +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()
}
),
centerText: .init(text: L10n.Conversation.title)
)
// Msg list
let messages = conversation.messages
if !messages.isEmpty {
2024-08-14 14:37:45 +00:00
ScrollViewReader { proxy in
2024-08-13 08:40:27 +00:00
List {
2024-08-14 14:37:45 +00:00
ForEach(messages) { message in
ConversationMessageRow(message: message)
.id(message.id)
.onAppear {
if message.id == messages.first?.id {
firstIsVisible = true
autoScroll = true
}
}
.onDisappear {
if message.id == messages.first?.id {
firstIsVisible = false
autoScroll = false
}
}
}
.rotationEffect(.degrees(180))
2024-08-13 08:40:27 +00:00
}
.rotationEffect(.degrees(180))
.listStyle(.plain)
.background(Color.Material.Background.light)
.scrollDismissesKeyboard(.immediately)
.scrollIndicators(.hidden)
2024-08-14 14:37:45 +00:00
.onChange(of: autoScroll) { new in
if new, !firstIsVisible {
withAnimation {
proxy.scrollTo(messages.first?.id, anchor: .top)
}
}
}
2024-08-13 08:40:27 +00:00
}
} 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)
}
}
}
}
}
2024-08-14 14:37:45 +00:00
.environmentObject(conversation)
.safeAreaInset(edge: .bottom, spacing: 0) {
ConversationTextInput(autoScroll: $autoScroll)
2024-08-15 15:15:49 +00:00
.environmentObject(conversation)
2024-08-15 11:37:21 +00:00
}
2024-08-13 08:40:27 +00:00
}
}