2022-01-29 11:10:04 +00:00
//
// C h a t V i e w . s w i f t
// S i m p l e X
//
// C r e a t e d b y E v g e n y P o b e r e z k i n o n 2 7 / 0 1 / 2 0 2 2 .
// C o p y r i g h t © 2 0 2 2 S i m p l e X C h a t . A l l r i g h t s r e s e r v e d .
//
import SwiftUI
struct ChatView : View {
@ EnvironmentObject var chatModel : ChatModel
2022-02-05 20:10:47 +00:00
@ ObservedObject var chat : Chat
2022-02-01 17:34:06 +00:00
@ State private var inProgress : Bool = false
2022-02-05 20:10:47 +00:00
@ State private var showChatInfo = false
2022-02-01 17:34:06 +00:00
2022-01-29 11:10:04 +00:00
var body : some View {
VStack {
2022-02-04 22:13:52 +00:00
GeometryReader { g in
ScrollViewReader { proxy in
ScrollView {
VStack ( spacing : 5 ) {
ForEach ( chatModel . chatItems , id : \ . id ) {
ChatItemView ( chatItem : $0 , width : g . size . width )
}
. onAppear { scrollToBottom ( proxy ) }
. onChange ( of : chatModel . chatItems . count ) { _ in scrollToBottom ( proxy ) }
2022-02-02 16:46:05 +00:00
}
2022-01-29 11:10:04 +00:00
}
2022-02-06 18:26:22 +00:00
. onTapGesture {
UIApplication . shared . sendAction ( #selector ( UIResponder . resignFirstResponder ) , to : nil , from : nil , for : nil )
}
2022-01-29 11:10:04 +00:00
}
}
2022-01-31 21:28:07 +00:00
Spacer ( minLength : 0 )
2022-01-29 23:37:02 +00:00
SendMessageView ( sendMessage : sendMessage , inProgress : inProgress )
2022-01-29 11:10:04 +00:00
}
2022-02-05 20:10:47 +00:00
. navigationTitle ( chat . chatInfo . chatViewName )
. navigationBarTitleDisplayMode ( . inline )
2022-02-01 17:34:06 +00:00
. toolbar {
2022-02-02 16:46:05 +00:00
ToolbarItem ( placement : . navigationBarLeading ) {
2022-02-02 12:51:39 +00:00
Button { chatModel . chatId = nil } label : {
2022-02-04 16:31:08 +00:00
HStack ( spacing : 4 ) {
Image ( systemName : " chevron.backward " )
Text ( " Chats " )
}
2022-02-02 12:51:39 +00:00
}
2022-02-01 17:34:06 +00:00
}
2022-02-05 20:10:47 +00:00
ToolbarItem ( placement : . principal ) {
Button {
showChatInfo = true
} label : {
HStack {
ChatInfoImage ( chat : chat )
. frame ( width : 32 , height : 32 )
. padding ( . trailing , 4 )
VStack {
Text ( chat . chatInfo . localDisplayName ) . font ( . headline )
Text ( chat . chatInfo . fullName ) . font ( . subheadline )
}
}
. foregroundColor ( . primary )
}
. sheet ( isPresented : $ showChatInfo ) {
ChatInfoView ( chat : chat )
}
}
2022-02-01 17:34:06 +00:00
}
. navigationBarBackButtonHidden ( true )
2022-01-29 11:10:04 +00:00
}
2022-01-29 23:37:02 +00:00
2022-02-02 16:46:05 +00:00
func scrollToBottom ( _ proxy : ScrollViewProxy ) {
if let id = chatModel . chatItems . last ? . id {
withAnimation {
proxy . scrollTo ( id , anchor : . bottom )
}
}
}
2022-01-29 23:37:02 +00:00
func sendMessage ( _ msg : String ) {
2022-01-30 18:27:20 +00:00
do {
2022-02-05 20:10:47 +00:00
let chatItem = try apiSendMessage ( type : chat . chatInfo . chatType , id : chat . chatInfo . apiId , msg : . text ( msg ) )
chatModel . addChatItem ( chat . chatInfo , chatItem )
2022-01-30 18:27:20 +00:00
} catch {
print ( error )
}
2022-01-29 23:37:02 +00:00
}
2022-01-29 11:10:04 +00:00
}
2022-01-29 23:37:02 +00:00
struct ChatView_Previews : PreviewProvider {
static var previews : some View {
let chatModel = ChatModel ( )
2022-02-02 12:51:39 +00:00
chatModel . chatId = " @1 "
chatModel . chatItems = [
2022-02-04 22:13:52 +00:00
chatItemSample ( 1 , . directSnd , . now , " hello " ) ,
chatItemSample ( 2 , . directRcv , . now , " hi " ) ,
chatItemSample ( 3 , . directRcv , . now , " hi there " ) ,
chatItemSample ( 4 , . directRcv , . now , " hello again " ) ,
chatItemSample ( 5 , . directSnd , . now , " hi there!!! " ) ,
chatItemSample ( 6 , . directSnd , . now , " how are you? " ) ,
2022-02-05 14:24:23 +00:00
chatItemSample ( 7 , . directSnd , . now , " 👍👍👍👍 " ) ,
chatItemSample ( 8 , . directSnd , . now , " Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. " )
2022-01-29 23:37:02 +00:00
]
2022-02-05 20:10:47 +00:00
return ChatView ( chat : Chat ( chatInfo : sampleDirectChatInfo , chatItems : [ ] ) )
2022-01-29 23:37:02 +00:00
. environmentObject ( chatModel )
}
}