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-01 17:34:06 +00:00
@ Binding var chatId : String ?
2022-01-29 11:10:04 +00:00
var chatInfo : ChatInfo
2022-02-01 17:34:06 +00:00
var width : CGFloat
@ State private var inProgress : Bool = false
2022-01-29 11:10:04 +00:00
var body : some View {
VStack {
if let chat : Chat = chatModel . chats [ chatInfo . id ] {
2022-02-01 17:34:06 +00:00
ScrollView {
LazyVStack ( spacing : 5 ) {
ForEach ( chat . chatItems ) {
ChatItemView ( chatItem : $0 )
2022-01-29 11:10:04 +00:00
}
}
}
2022-02-01 17:34:06 +00:00
} else {
2022-01-29 11:10:04 +00:00
Text ( " unexpected: chat not found... " )
}
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-01 17:34:06 +00:00
. toolbar {
HStack {
Button { chatId = nil } label : { Image ( systemName : " chevron.backward " ) }
Spacer ( )
Text ( chatInfo . localDisplayName )
. font ( . title3 )
Spacer ( )
EmptyView ( )
}
. padding ( . horizontal )
. frame ( minWidth : width , maxWidth : . infinity , alignment : . center )
}
. navigationBarBackButtonHidden ( true )
2022-01-29 11:10:04 +00:00
}
2022-01-29 23:37:02 +00:00
func sendMessage ( _ msg : String ) {
2022-01-30 18:27:20 +00:00
do {
let chatItem = try apiSendMessage ( type : chatInfo . chatType , id : chatInfo . apiId , msg : . text ( msg ) )
let chat = chatModel . chats [ chatInfo . id ] ? ? Chat ( chatInfo : chatInfo , chatItems : [ ] )
chatModel . chats [ chatInfo . id ] = chat
chat . chatItems . append ( chatItem )
} 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 {
2022-02-01 17:34:06 +00:00
@ State var chatId : String ? = " @1 "
2022-01-29 23:37:02 +00:00
let chatModel = ChatModel ( )
chatModel . chats = [
" @1 " : Chat (
chatInfo : sampleDirectChatInfo ,
2022-01-31 21:28:07 +00:00
chatItems : [
chatItemSample ( 1 , . directSnd , Date . now , " hello " ) ,
chatItemSample ( 2 , . directRcv , Date . now , " hi " ) ,
chatItemSample ( 3 , . directRcv , Date . now , " hi there " ) ,
chatItemSample ( 4 , . directRcv , Date . now , " hello again " ) ,
chatItemSample ( 5 , . directSnd , Date . now , " hi there!!! " ) ,
chatItemSample ( 6 , . directSnd , Date . now , " how are you? " ) ,
chatItemSample ( 7 , . directSnd , Date . 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-01 17:34:06 +00:00
return ChatView ( chatId : $ chatId , chatInfo : sampleDirectChatInfo , width : 300 )
2022-01-29 23:37:02 +00:00
. environmentObject ( chatModel )
}
}