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-01 20:30:33 +00:00
@ State private var connectAlert = false
@ State private var connectError : Error ?
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-01 17:34:06 +00:00
var user : User
var body : some View {
2022-02-11 07:42:00 +00:00
NavigationView {
List {
if chatModel . chats . isEmpty {
VStack ( alignment : . leading ) {
ChatHelp ( showSettings : $ showSettings )
HStack {
Text ( " This text is available in settings " )
SettingsButton ( )
}
. padding ( . leading )
2022-02-02 16:46:05 +00:00
}
}
2022-02-11 07:42:00 +00:00
ForEach ( chatModel . chats ) { chat in
ChatListNavLink ( chat : chat )
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 " )
. toolbar {
ToolbarItem ( placement : . navigationBarLeading ) {
SettingsButton ( )
}
ToolbarItem ( placement : . navigationBarTrailing ) {
NewChatButton ( )
}
}
. alert ( isPresented : $ chatModel . connectViaUrl ) { connectViaUrlAlert ( ) }
2022-02-01 17:34:06 +00:00
}
2022-02-11 07:42:00 +00:00
. navigationViewStyle ( . stack )
. alert ( isPresented : $ connectAlert ) { connectionErrorAlert ( ) }
2022-02-01 17:34:06 +00:00
}
2022-02-01 20:30:33 +00:00
private func connectViaUrlAlert ( ) -> Alert {
2022-02-09 22:53:06 +00:00
logger . debug ( " ChatListView.connectViaUrlAlert " )
2022-02-01 20:30:33 +00:00
if let url = chatModel . appOpenUrl {
var path = url . path
2022-02-09 22:53:06 +00:00
logger . debug ( " ChatListView.connectViaUrlAlert path: \( path ) " )
2022-02-01 20:30:33 +00:00
if ( path = = " /contact " || path = = " /invitation " ) {
path . removeFirst ( )
let link = url . absoluteString . replacingOccurrences ( of : " /// \( path ) " , with : " / \( path ) " )
return Alert (
title : Text ( " Connect via \( path ) link? " ) ,
message : Text ( " Your profile will be sent to the contact that you received this link from: \( link ) " ) ,
primaryButton : . default ( Text ( " Connect " ) ) {
do {
try apiConnect ( connReq : link )
} catch {
connectAlert = true
connectError = error
2022-02-09 22:53:06 +00:00
logger . debug ( " ChatListView.connectViaUrlAlert: apiConnect error: \( error . localizedDescription ) " )
2022-02-01 20:30:33 +00:00
}
chatModel . appOpenUrl = nil
} , secondaryButton : . cancel ( ) {
chatModel . appOpenUrl = nil
}
)
} else {
2022-02-09 22:53:06 +00:00
return Alert ( title : Text ( " Error: URL is invalid " ) )
2022-02-01 20:30:33 +00:00
}
} else {
return Alert ( title : Text ( " Error: URL not available " ) )
}
}
private func connectionErrorAlert ( ) -> Alert {
Alert (
title : Text ( " Connection error " ) ,
message : Text ( connectError ? . localizedDescription ? ? " " )
)
}
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
}
}