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
2022-05-31 07:55:13 +01:00
import SimpleXChat
2022-01-29 11:10:04 +00:00
2022-03-30 08:57:42 +01:00
private let memberImageSize : CGFloat = 34
2022-01-29 11:10:04 +00:00
struct ChatView : View {
@ EnvironmentObject var chatModel : ChatModel
2022-02-08 09:19:25 +00:00
@ Environment ( \ . colorScheme ) var colorScheme
2022-02-05 20:10:47 +00:00
@ ObservedObject var chat : Chat
2022-06-03 13:19:41 +04:00
@ Binding var showChatInfo : Bool
2022-05-07 06:40:46 +01:00
@ State private var composeState = ComposeState ( )
@ State private var deletingItem : ChatItem ? = nil
2022-02-11 07:42:00 +00:00
@ FocusState private var keyboardVisible : Bool
2022-03-30 20:37:47 +04:00
@ State private var showDeleteMessage = false
2022-02-01 17:34:06 +00:00
2022-01-29 11:10:04 +00:00
var body : some View {
2022-02-08 09:19:25 +00:00
let cInfo = chat . chatInfo
return VStack {
2022-02-04 22:13:52 +00:00
GeometryReader { g in
2022-03-30 08:57:42 +01:00
let maxWidth =
cInfo . chatType = = . group
? ( g . size . width - 28 ) * 0.84 - 42
: ( g . size . width - 32 ) * 0.84
2022-02-04 22:13:52 +00:00
ScrollViewReader { proxy in
ScrollView {
2022-03-17 09:42:59 +00:00
LazyVStack ( spacing : 5 ) {
ForEach ( chatModel . chatItems ) { ci in
2022-03-30 08:57:42 +01:00
if case let . groupRcv ( member ) = ci . chatDir {
let prevItem = chatModel . getPrevChatItem ( ci )
HStack ( alignment : . top , spacing : 0 ) {
2022-03-30 20:04:25 +01:00
let showMember = prevItem = = nil || showMemberImage ( member , prevItem )
2022-03-30 08:57:42 +01:00
if showMember {
ProfileImage ( imageStr : member . memberProfile . image )
. frame ( width : memberImageSize , height : memberImageSize )
} else {
Rectangle ( ) . fill ( . clear )
. frame ( width : memberImageSize , height : memberImageSize )
}
chatItemWithMenu ( ci , maxWidth , showMember : showMember ) . padding ( . leading , 8 )
2022-03-17 09:42:59 +00:00
}
2022-03-30 08:57:42 +01:00
. padding ( . trailing )
. padding ( . leading , 12 )
} else {
chatItemWithMenu ( ci , maxWidth ) . padding ( . horizontal )
}
2022-02-04 22:13:52 +00:00
}
2022-02-12 15:59:43 +00:00
. onAppear {
DispatchQueue . main . async {
scrollToFirstUnread ( proxy )
}
markAllRead ( )
}
2022-07-10 14:28:00 +01:00
. onChange ( of : chatModel . chatItems . last ? . id ) { _ in
2022-02-12 15:59:43 +00:00
scrollToBottom ( proxy )
}
2022-02-11 07:42:00 +00:00
. onChange ( of : keyboardVisible ) { _ in
if keyboardVisible {
DispatchQueue . main . asyncAfter ( deadline : . now ( ) + 0.25 ) {
scrollToBottom ( proxy , animation : . easeInOut ( duration : 1 ) )
}
}
}
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
2022-03-17 09:42:59 +00:00
ComposeView (
2022-04-25 12:44:24 +04:00
chat : chat ,
composeState : $ composeState ,
keyboardVisible : $ keyboardVisible
2022-02-11 07:42:00 +00:00
)
2022-01-29 11:10:04 +00:00
}
2022-02-08 09:19:25 +00:00
. navigationTitle ( cInfo . chatViewName )
2022-02-05 20:10:47 +00:00
. 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 " )
2022-04-16 09:37:01 +01:00
Text ( " Chats " , comment : " back button to return to chats list " )
2022-02-04 16:31:08 +00:00
}
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 {
2022-06-03 13:19:41 +04:00
showChatInfo = true
2022-02-05 20:10:47 +00:00
} label : {
2022-02-12 15:59:43 +00:00
ChatInfoToolbar ( chat : chat )
2022-02-05 20:10:47 +00:00
}
2022-06-03 13:19:41 +04:00
. sheet ( isPresented : $ showChatInfo ) {
ChatInfoView ( chat : chat , showChatInfo : $ showChatInfo )
2022-02-05 20:10:47 +00:00
}
}
2022-05-24 19:34:27 +01:00
ToolbarItem ( placement : . navigationBarTrailing ) {
2022-07-05 15:24:51 +04:00
if case let . direct ( contact ) = cInfo {
2022-05-24 19:34:27 +01:00
HStack {
callButton ( contact , . audio , imageName : " phone " )
callButton ( contact , . video , imageName : " video " )
}
}
}
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-05-07 06:40:46 +01:00
private func callButton ( _ contact : Contact , _ media : CallMediaType , imageName : String ) -> some View {
Button {
2022-05-24 19:34:27 +01:00
CallController . shared . startCall ( contact , media )
2022-05-07 06:40:46 +01:00
} label : {
Image ( systemName : imageName )
}
}
2022-03-30 08:57:42 +01:00
private func chatItemWithMenu ( _ ci : ChatItem , _ maxWidth : CGFloat , showMember : Bool = false ) -> some View {
let alignment : Alignment = ci . chatDir . sent ? . trailing : . leading
2022-05-21 12:13:37 +01:00
return ChatItemView ( chatInfo : chat . chatInfo , chatItem : ci , showMember : showMember , maxWidth : maxWidth )
2022-03-30 08:57:42 +01:00
. contextMenu {
2022-03-30 20:37:47 +04:00
if ci . isMsgContent ( ) {
Button {
withAnimation {
2022-05-06 21:10:32 +04:00
if composeState . editing ( ) {
composeState = ComposeState ( contextItem : . quotedItem ( chatItem : ci ) )
} else {
composeState = composeState . copy ( contextItem : . quotedItem ( chatItem : ci ) )
}
2022-03-30 20:37:47 +04:00
}
} label : { Label ( " Reply " , systemImage : " arrowshape.turn.up.left " ) }
Button {
2022-04-19 12:29:03 +04:00
var shareItems : [ Any ] = [ ci . content . text ]
2022-05-07 16:25:04 +04:00
if case . image = ci . content . msgContent , let image = getLoadedImage ( ci . file ) {
2022-04-19 12:29:03 +04:00
shareItems . append ( image )
}
showShareSheet ( items : shareItems )
2022-03-30 20:37:47 +04:00
} label : { Label ( " Share " , systemImage : " square.and.arrow.up " ) }
Button {
2022-04-25 12:44:24 +04:00
if case let . image ( text , _ ) = ci . content . msgContent ,
text = = " " ,
2022-05-07 16:25:04 +04:00
let image = getLoadedImage ( ci . file ) {
2022-04-19 12:29:03 +04:00
UIPasteboard . general . image = image
} else {
UIPasteboard . general . string = ci . content . text
}
2022-03-30 20:37:47 +04:00
} label : { Label ( " Copy " , systemImage : " doc.on.doc " ) }
2022-05-04 09:10:36 +04:00
if case . image = ci . content . msgContent ,
2022-05-07 16:25:04 +04:00
let image = getLoadedImage ( ci . file ) {
2022-05-04 09:10:36 +04:00
Button {
UIImageWriteToSavedPhotosAlbum ( image , nil , nil , nil )
} label : { Label ( " Save " , systemImage : " square.and.arrow.down " ) }
}
2022-03-30 20:37:47 +04:00
if ci . meta . editable {
Button {
withAnimation {
2022-04-25 12:44:24 +04:00
composeState = ComposeState ( editingItem : ci )
2022-03-30 20:37:47 +04:00
}
} label : { Label ( " Edit " , systemImage : " square.and.pencil " ) }
2022-03-30 08:57:42 +01:00
}
2022-03-30 20:37:47 +04:00
Button ( role : . destructive ) {
showDeleteMessage = true
deletingItem = ci
2022-05-17 22:48:54 +04:00
} label : { Label ( " Delete " , systemImage : " trash " ) }
} else if ci . isDeletedContent ( ) {
Button ( role : . destructive ) {
showDeleteMessage = true
deletingItem = ci
} label : { Label ( " Delete " , systemImage : " trash " ) }
2022-03-30 20:37:47 +04:00
}
}
. confirmationDialog ( " Delete message? " , isPresented : $ showDeleteMessage , titleVisibility : . visible ) {
Button ( " Delete for me " , role : . destructive ) {
deleteMessage ( . cidmInternal )
}
2022-04-19 13:24:26 +04:00
if let di = deletingItem {
if di . meta . editable {
2022-05-07 06:40:46 +01:00
Button ( " Delete for everyone " , role : . destructive ) {
deleteMessage ( . cidmBroadcast )
2022-04-19 13:24:26 +04:00
}
}
}
2022-03-30 08:57:42 +01:00
}
. frame ( maxWidth : maxWidth , maxHeight : . infinity , alignment : alignment )
. frame ( minWidth : 0 , maxWidth : . infinity , alignment : alignment )
}
private func showMemberImage ( _ member : GroupMember , _ prevItem : ChatItem ? ) -> Bool {
switch ( prevItem ? . chatDir ) {
case . groupSnd : return true
case let . groupRcv ( prevMember ) : return prevMember . groupMemberId != member . groupMemberId
default : return false
}
}
2022-02-11 07:42:00 +00:00
func scrollToBottom ( _ proxy : ScrollViewProxy , animation : Animation = . default ) {
2022-02-12 15:59:43 +00:00
withAnimation ( animation ) { scrollToBottom_ ( proxy ) }
}
func scrollToBottom_ ( _ proxy : ScrollViewProxy ) {
2022-02-02 16:46:05 +00:00
if let id = chatModel . chatItems . last ? . id {
2022-02-12 15:59:43 +00:00
proxy . scrollTo ( id , anchor : . bottom )
}
}
// a l i g n f i r s t u n r e a d w i t h t h e t o p o r t h e l a s t u n r e a d w i t h b o t t o m
func scrollToFirstUnread ( _ proxy : ScrollViewProxy ) {
if let cItem = chatModel . chatItems . first ( where : { $0 . isRcvNew ( ) } ) {
proxy . scrollTo ( cItem . id )
} else {
scrollToBottom_ ( proxy )
}
}
func markAllRead ( ) {
2022-07-02 17:18:45 +01:00
DispatchQueue . main . asyncAfter ( deadline : . now ( ) + 1 ) {
2022-02-12 15:59:43 +00:00
if chatModel . chatId = = chat . id {
2022-02-24 17:16:41 +00:00
Task { await markChatRead ( chat ) }
2022-02-02 16:46:05 +00:00
}
}
}
2022-03-30 20:37:47 +04:00
func deleteMessage ( _ mode : CIDeleteMode ) {
logger . debug ( " ChatView deleteMessage " )
Task {
logger . debug ( " ChatView deleteMessage: in Task " )
do {
if let di = deletingItem {
let toItem = try await apiDeleteChatItem (
type : chat . chatInfo . chatType ,
id : chat . chatInfo . apiId ,
itemId : di . id ,
mode : mode
)
DispatchQueue . main . async {
deletingItem = nil
let _ = chatModel . removeChatItem ( chat . chatInfo , toItem )
}
}
} catch {
logger . error ( " ChatView.deleteMessage error: \( error . localizedDescription ) " )
}
}
}
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-08 09:19:25 +00:00
ChatItem . getSample ( 1 , . directSnd , . now , " hello " ) ,
ChatItem . getSample ( 2 , . directRcv , . now , " hi " ) ,
ChatItem . getSample ( 3 , . directRcv , . now , " hi there " ) ,
2022-03-30 20:37:47 +04:00
ChatItem . getDeletedContentSample ( 4 ) ,
ChatItem . getSample ( 5 , . directRcv , . now , " hello again " ) ,
ChatItem . getSample ( 6 , . directSnd , . now , " hi there!!! " ) ,
ChatItem . getSample ( 7 , . directSnd , . now , " how are you? " ) ,
ChatItem . getSample ( 8 , . directSnd , . now , " 👍👍👍👍 " ) ,
ChatItem . getSample ( 9 , . 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-06-03 13:19:41 +04:00
@ State var showChatInfo = false
return ChatView ( chat : Chat ( chatInfo : ChatInfo . sampleData . direct , chatItems : [ ] ) , showChatInfo : $ showChatInfo )
2022-01-29 23:37:02 +00:00
. environmentObject ( chatModel )
}
}