2022-02-01 17:34:06 +00:00
//
// C h a t L i s 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 ChatListView : View {
@ EnvironmentObject var chatModel : ChatModel
2022-02-11 07:42:00 +00:00
// n o t r e a l l y u s e d i n t h i s v i e w
@ State private var showSettings = false
2022-02-13 08:45:08 +00:00
@ State private var searchText = " "
2022-05-07 06:40:46 +01:00
@ State private var showCallView = false
2022-05-07 16:10:57 +01:00
@ AppStorage ( DEFAULT_PENDING_CONNECTIONS ) private var pendingConnections = true
2022-02-01 17:34:06 +00:00
var user : User
var body : some View {
2022-02-13 08:45:08 +00:00
let v = NavigationView {
2022-02-11 07:42:00 +00:00
List {
if chatModel . chats . isEmpty {
2022-04-16 09:37:01 +01:00
ChatHelp ( showSettings : $ showSettings )
} else {
ForEach ( filteredChats ( ) ) { chat in
2022-05-07 06:40:46 +01:00
ChatListNavLink ( chat : chat , showCallView : $ showCallView )
2022-04-16 09:37:01 +01:00
. padding ( . trailing , - 16 )
2022-02-02 16:46:05 +00:00
}
}
2022-02-12 15:59:43 +00:00
}
. onChange ( of : chatModel . chatId ) { _ in
if chatModel . chatId = = nil , let chatId = chatModel . chatToTop {
chatModel . chatToTop = nil
chatModel . popChat ( chatId )
}
}
. onChange ( of : chatModel . appOpenUrl ) { _ in
if let url = chatModel . appOpenUrl {
chatModel . appOpenUrl = nil
AlertManager . shared . showAlert ( connectViaUrlAlert ( url ) )
2022-02-01 17:34:06 +00:00
}
}
2022-02-11 07:42:00 +00:00
. offset ( x : - 8 )
. listStyle ( . plain )
. navigationTitle ( chatModel . chats . isEmpty ? " Welcome \( user . displayName ) ! " : " Your chats " )
2022-02-13 08:45:08 +00:00
. navigationBarTitleDisplayMode ( chatModel . chats . count > 8 ? . inline : . large )
2022-02-11 07:42:00 +00:00
. toolbar {
ToolbarItem ( placement : . navigationBarLeading ) {
SettingsButton ( )
}
ToolbarItem ( placement : . navigationBarTrailing ) {
NewChatButton ( )
}
}
2022-05-07 06:40:46 +01:00
. fullScreenCover ( isPresented : $ showCallView ) {
ActiveCallView ( showCallView : $ showCallView )
}
. onChange ( of : showCallView ) { _ in
if ( showCallView ) { return }
if let call = chatModel . activeCall {
Task {
do {
try await apiEndCall ( call . contact )
} catch {
logger . error ( " ChatListView apiEndCall error: \( error . localizedDescription ) " )
}
}
}
chatModel . callCommand = . end
}
. onChange ( of : chatModel . activeCallInvitation ) { _ in
if let contactRef = chatModel . activeCallInvitation ,
case let . direct ( contact ) = chatModel . getChat ( contactRef . id ) ? . chatInfo ,
let invitation = chatModel . callInvitations . removeValue ( forKey : contactRef . id ) {
answerCallAlert ( contact , invitation )
}
}
2022-02-01 17:34:06 +00:00
}
2022-02-11 07:42:00 +00:00
. navigationViewStyle ( . stack )
2022-02-13 08:45:08 +00:00
if chatModel . chats . count > 8 {
v . searchable ( text : $ searchText )
} else {
v
}
}
private func filteredChats ( ) -> [ Chat ] {
let s = searchText . trimmingCharacters ( in : . whitespaces ) . localizedLowercase
2022-04-25 10:39:28 +01:00
return s = = " " && pendingConnections
2022-02-13 08:45:08 +00:00
? chatModel . chats
2022-04-25 10:39:28 +01:00
: s = = " "
? chatModel . chats . filter {
pendingConnections || $0 . chatInfo . chatType != . contactConnection
}
: chatModel . chats . filter {
( pendingConnections || $0 . chatInfo . chatType != . contactConnection ) &&
$0 . chatInfo . chatViewName . localizedLowercase . contains ( s )
}
2022-02-01 17:34:06 +00:00
}
2022-02-01 20:30:33 +00:00
2022-02-12 15:59:43 +00:00
private func connectViaUrlAlert ( _ url : URL ) -> Alert {
var path = url . path
logger . debug ( " ChatListView.connectViaUrlAlert path: \( path ) " )
if ( path = = " /contact " || path = = " /invitation " ) {
path . removeFirst ( )
2022-04-16 09:37:01 +01:00
let action : ConnReqType = path = = " contact " ? . contact : . invitation
2022-02-12 15:59:43 +00:00
let link = url . absoluteString . replacingOccurrences ( of : " /// \( path ) " , with : " / \( path ) " )
2022-04-16 09:37:01 +01:00
let title : LocalizedStringKey
if case . contact = action { title = " Connect via contact link? " }
2022-04-25 07:54:07 +01:00
else { title = " Connect via one-time link? " }
2022-02-12 15:59:43 +00:00
return Alert (
2022-04-16 09:37:01 +01:00
title : Text ( title ) ,
2022-02-26 20:21:32 +00:00
message : Text ( " Your profile will be sent to the contact that you received this link from " ) ,
2022-02-12 15:59:43 +00:00
primaryButton : . default ( Text ( " Connect " ) ) {
2022-04-25 07:54:07 +01:00
connectViaLink ( link )
2022-02-12 15:59:43 +00:00
} ,
secondaryButton : . cancel ( )
)
2022-02-01 20:30:33 +00:00
} else {
2022-02-12 15:59:43 +00:00
return Alert ( title : Text ( " Error: URL is invalid " ) )
2022-02-01 20:30:33 +00:00
}
}
2022-05-07 06:40:46 +01:00
private func answerCallAlert ( _ contact : Contact , _ invitation : CallInvitation ) {
AlertManager . shared . showAlert ( Alert (
title : Text ( " Incoming call " ) ,
primaryButton : . default ( Text ( " Answer " ) ) {
if chatModel . activeCallInvitation = = nil {
DispatchQueue . main . async {
AlertManager . shared . showAlertMsg ( title : " Call already ended! " )
}
} else {
chatModel . activeCallInvitation = nil
chatModel . activeCall = Call (
contact : contact ,
callState : . invitationReceived ,
localMedia : invitation . peerMedia
)
showCallView = true
chatModel . callCommand = . start ( media : invitation . peerMedia , aesKey : invitation . sharedKey )
}
} ,
secondaryButton : . cancel ( )
) )
}
2022-02-01 17:34:06 +00:00
}
struct ChatListView_Previews : PreviewProvider {
static var previews : some View {
let chatModel = ChatModel ( )
2022-02-02 12:51:39 +00:00
chatModel . chats = [
2022-02-01 17:34:06 +00:00
Chat (
2022-02-08 09:19:25 +00:00
chatInfo : ChatInfo . sampleData . direct ,
chatItems : [ ChatItem . getSample ( 1 , . directSnd , . now , " hello " ) ]
2022-02-01 17:34:06 +00:00
) ,
Chat (
2022-02-08 09:19:25 +00:00
chatInfo : ChatInfo . sampleData . group ,
chatItems : [ ChatItem . getSample ( 1 , . 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-02-01 17:34:06 +00:00
) ,
Chat (
2022-02-08 09:19:25 +00:00
chatInfo : ChatInfo . sampleData . contactRequest ,
2022-02-01 17:34:06 +00:00
chatItems : [ ]
)
]
2022-02-11 07:42:00 +00:00
return Group {
ChatListView ( user : User . sampleData )
. environmentObject ( chatModel )
ChatListView ( user : User . sampleData )
. environmentObject ( ChatModel ( ) )
}
2022-02-01 17:34:06 +00:00
}
}