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

187 lines
8.2 KiB
Swift
Raw Normal View History

2024-06-27 11:39:41 +00:00
import Combine
import Foundation
import Martin
import SwiftUI
struct ConversationScreen: View {
@EnvironmentObject var store: AppStore
2024-07-01 09:29:31 +00:00
@State private var autoScroll = true
2024-07-01 11:54:43 +00:00
@State private var firstIsVisible = true
2024-07-01 09:29:31 +00:00
2024-06-27 11:39:41 +00:00
var body: some View {
ZStack {
// Background color
2024-07-04 08:21:12 +00:00
Color.Material.Background.light
2024-06-27 11:39:41 +00:00
.ignoresSafeArea()
// Content
VStack(spacing: 0) {
// Header
ConversationHeader()
// Msg list
let messages = store.state.conversationsState.currentMessages
if !messages.isEmpty {
2024-07-01 09:29:31 +00:00
ScrollViewReader { proxy in
List {
ForEach(messages) { message in
ConversationMessageRow(message: message)
.id(message.id)
.onAppear {
2024-07-01 10:05:39 +00:00
if message.id == messages.first?.id {
2024-07-01 11:54:43 +00:00
firstIsVisible = true
2024-07-01 09:29:31 +00:00
autoScroll = true
}
}
.onDisappear {
2024-07-01 10:05:39 +00:00
if message.id == messages.first?.id {
2024-07-01 11:54:43 +00:00
firstIsVisible = false
2024-07-01 09:29:31 +00:00
autoScroll = false
}
}
}
2024-07-01 10:05:39 +00:00
.rotationEffect(.degrees(180))
2024-07-01 09:29:31 +00:00
}
2024-07-01 10:05:39 +00:00
.rotationEffect(.degrees(180))
2024-07-01 09:29:31 +00:00
.listStyle(.plain)
2024-07-04 08:21:12 +00:00
.background(Color.Material.Background.light)
2024-07-01 09:29:31 +00:00
.scrollDismissesKeyboard(.immediately)
.scrollIndicators(.hidden)
.onChange(of: autoScroll) { new in
2024-07-01 11:54:43 +00:00
if new, !firstIsVisible {
2024-07-01 09:29:31 +00:00
withAnimation {
2024-07-01 10:05:39 +00:00
proxy.scrollTo(messages.first?.id, anchor: .top)
2024-07-01 09:29:31 +00:00
}
}
}
2024-06-27 11:39:41 +00:00
}
} else {
Spacer()
}
}
.onTapGesture {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
2024-07-01 09:29:31 +00:00
// Jump to last button
if !autoScroll {
2024-07-01 10:04:19 +00:00
VStack {
Spacer()
HStack {
Spacer()
Button {
autoScroll = true
} label: {
ZStack {
Circle()
2024-07-04 08:21:12 +00:00
.fill(Color.Material.Shape.white)
2024-07-01 10:04:19 +00:00
Image(systemName: "arrow.down")
2024-07-04 08:21:12 +00:00
.foregroundColor(.Material.Elements.active)
2024-07-01 10:04:19 +00:00
}
.frame(width: 40, height: 40)
2024-07-04 08:21:12 +00:00
.shadow(color: .black.opacity(0.2), radius: 4)
2024-07-01 10:04:19 +00:00
.padding(.trailing, 8)
.padding(.bottom, 8)
}
}
2024-07-01 09:29:31 +00:00
}
}
2024-06-27 11:39:41 +00:00
}
.safeAreaInset(edge: .bottom, spacing: 0) {
2024-07-02 08:32:14 +00:00
ConversationTextInput(autoScroll: $autoScroll)
2024-06-27 11:39:41 +00:00
}
}
}
// Preview
#if DEBUG
struct ConversationScreen_Previews: PreviewProvider {
static var previews: some View {
ConversationScreen()
.environmentObject(pStore)
}
static var pStore: AppStore {
let state = pState
return AppStore(initialState: state, reducer: AppState.reducer, middlewares: [])
}
static var pState: AppState {
var state = AppState()
let acc = "user@test.com"
let contact = "some@test.com"
state.conversationsState.currentChat = Chat(id: "1", account: acc, participant: contact, type: .chat)
state.conversationsState.currentMessages = [
Message(
id: "1",
type: .chat,
contentType: .text,
from: contact,
to: acc,
body: "this is for test sdgdsfg dsfg dsfgdg dsfgdfgsdgsdfgdfg sdfgdsfgdfsg dsfgdsfgsdfg dsfgdfgsdg fgf fgfg sdfsdf sdfsdf sdf sdfsdf sdf sdfsdf sdfsdfsdf sdfsdf ",
subject: nil,
thread: nil,
oobUrl: nil,
date: Date(),
pending: true, sentError: false
),
Message(
id: "2",
type: .chat,
contentType: .text,
from: contact,
to: acc,
body: "this is for testsdfsdf sdfsdf sdfs sdf sdffsdf sdf sdf sdf sdf sdf sdff sdfffwwe ",
subject: nil,
thread: nil,
oobUrl: nil,
date: Date(),
pending: false,
sentError: false
),
Message(id: "3", type: .chat, contentType: .text, from: contact, to: acc, body: "this is for test", subject: nil, thread: nil, oobUrl: nil, date: Date(), pending: false, sentError: true),
Message(
id: "4",
type: .chat,
contentType: .text,
from: acc,
to: contact,
body: "this is for test sdfkjwek jwkjfh jwerf jdfhskjdhf jsdhfjhwefh sjdhfh fsdjhfh sd ",
subject: nil,
thread: nil,
oobUrl: nil,
date: Date(),
pending: false,
sentError: false
),
Message(id: "5", type: .chat, contentType: .text, from: contact, to: acc, body: "this is for test sdfjkkeke kekkddjw;; w;edkdjfj l kjwekrjfk wef", subject: nil, thread: nil, oobUrl: nil, date: Date(), pending: false, sentError: false),
Message(id: "6", type: .chat, contentType: .text, from: acc, to: contact, body: "this is for testsdf dsdkkekkddn wejkjfj ", subject: nil, thread: nil, oobUrl: nil, date: Date(), pending: false, sentError: false),
Message(
id: "7",
type: .chat,
contentType: .text,
from: acc,
to: contact,
body: "this is for test sdgdsfg dsfg dsfgdg dsfgdfgsdgsdfgdfg sdfgdsfgdfsg dsfgdsfgsdfg dsfgdfgsdg fgf fgfg sdfsdf sdfsdf sdf sdfsdf sdf sdfsdf sdfsdfsdf sdfsdf ",
subject: nil,
thread: nil,
oobUrl: nil,
date: Date(), pending: false, sentError: false
),
Message(id: "8", type: .chat, contentType: .text, from: acc, to: contact, body: "so test", subject: nil, thread: nil, oobUrl: nil, date: Date(), pending: false, sentError: false),
Message(id: "9", type: .chat, contentType: .text, from: contact, to: acc, body: "so test", subject: nil, thread: nil, oobUrl: nil, date: Date(), pending: false, sentError: false),
Message(id: "10", type: .chat, contentType: .text, from: acc, to: contact, body: "so test so test so test", subject: nil, thread: nil, oobUrl: nil, date: Date(), pending: false, sentError: false),
Message(id: "11", type: .chat, contentType: .text, from: contact, to: acc, body: "xD", subject: nil, thread: nil, oobUrl: nil, date: Date(), pending: false, sentError: false)
]
2024-07-01 08:47:39 +00:00
2024-06-27 11:39:41 +00:00
state.conversationsState.replyText = "> Some Text here! And if it a long and very long text sdfsadfsadfsafsadfsadfsadfsadfassadfsadfsafsafdsadfsafdsadfsadfas sdf sdf asdf sdfasdfsd sdfasdf sdfsdfdsasdfsdfa dsafsaf"
2024-07-01 08:47:39 +00:00
2024-06-27 11:39:41 +00:00
return state
}
}
#endif