another.im-ios/Monal/another.im/Views/Conversation/ConversationScreen.swift

129 lines
5 KiB
Swift
Raw Normal View History

2024-11-23 23:22:07 +00:00
import Combine
import Foundation
import SwiftUI
struct ConversationScreen: View {
@Environment(\.router) var router
2024-12-09 18:53:56 +00:00
@StateObject var chatWrapper: WrapperChat
2024-11-23 23:22:07 +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-11-28 15:46:16 +00:00
centerText: .init(text: chatWrapper.chatTitle),
rightButton: .init(
image: Image(systemName: "gear"),
action: {
router.showScreen(.push) { _ in
ConversationSettingsScreen()
.environmentObject(chatWrapper)
.navigationBarHidden(true)
}
}
)
2024-11-23 23:22:07 +00:00
)
2024-11-29 22:18:54 +00:00
if chatWrapper.mamRequestInProgress {
ProgressView()
.progressViewStyle(.circular)
.tint(.Material.Shape.separator)
.padding(.top, 8)
}
2024-11-23 23:22:07 +00:00
// Msg list
2024-11-24 18:42:46 +00:00
if !chatWrapper.messages.isEmpty {
ScrollViewReader { proxy in
ScrollView {
LazyVStack(spacing: 0) {
ForEach(chatWrapper.messages) { message in
ConversationMessageRow(message: message)
.id(message.id)
.flip()
.onAppear {
if message.id == chatWrapper.messages.first?.id {
firstIsVisible = true
autoScroll = true
}
2024-11-27 15:49:36 +00:00
if message.id == chatWrapper.messages.last?.id {
chatWrapper.requestMAM()
}
2024-11-24 18:42:46 +00:00
}
.onDisappear {
if message.id == chatWrapper.messages.first?.id {
firstIsVisible = false
autoScroll = false
}
}
}
}
}
.flip()
.scrollDismissesKeyboard(.immediately)
.onChange(of: autoScroll) { new in
print(proxy)
if new, !firstIsVisible {
withAnimation {
proxy.scrollTo(chatWrapper.messages.first?.id, anchor: .top)
}
}
}
}
} else {
Spacer()
}
2024-11-23 23:22:07 +00:00
}
.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-11-25 13:14:23 +00:00
.safeAreaInset(edge: .bottom, spacing: 0) {
ConversationTextInput(autoScroll: $autoScroll)
.environmentObject(chatWrapper)
}
2024-11-28 17:04:59 +00:00
.onLoad {
if chatWrapper.messages.isEmpty {
chatWrapper.requestMAM()
}
}
2024-11-23 23:22:07 +00:00
}
}