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
@ State private var chatId : String ?
2022-02-01 20:30:33 +00:00
@ State private var connectAlert = false
@ State private var connectError : Error ?
2022-02-01 17:34:06 +00:00
var user : User
var body : some View {
2022-02-01 20:30:33 +00:00
VStack {
2022-02-01 17:34:06 +00:00
// i f c h a t M o d e l . c h a t s . i s E m p t y {
// V S t a c k {
// T e x t ( " H e l l o c h a t " )
// T e x t ( " A c t i v e u s e r : \ ( u s e r . l o c a l D i s p l a y N a m e ) ( \ ( u s e r . p r o f i l e . f u l l N a m e ) ) " )
// }
// }
NavigationView {
GeometryReader { geometry in
List {
NavigationLink {
TerminalView ( )
} label : {
Text ( " Terminal " )
}
ForEach ( chatModel . chatPreviews ) { chatPreview in
ChatListNavLink (
chatId : $ chatId ,
chatPreview : chatPreview ,
width : geometry . size . width
)
}
}
. padding ( 0 )
. offset ( x : - 8 )
. listStyle ( . plain )
. toolbar { ChatListToolbar ( width : geometry . size . width ) }
. navigationBarTitleDisplayMode ( . inline )
2022-02-01 20:30:33 +00:00
. alert ( isPresented : $ connectAlert ) { connectionErrorAlert ( ) }
2022-02-01 17:34:06 +00:00
}
}
2022-02-01 20:30:33 +00:00
. alert ( isPresented : $ chatModel . connectViaUrl ) { connectViaUrlAlert ( ) }
2022-02-01 17:34:06 +00:00
}
}
2022-02-01 20:30:33 +00:00
private func connectViaUrlAlert ( ) -> Alert {
if let url = chatModel . appOpenUrl {
var path = url . path
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
print ( error )
}
chatModel . appOpenUrl = nil
} , secondaryButton : . cancel ( ) {
chatModel . appOpenUrl = nil
}
)
} else {
return Alert ( title : Text ( " Error: URL not available " ) )
}
} 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 ( )
chatModel . chatPreviews = [
Chat (
chatInfo : sampleDirectChatInfo ,
chatItems : [ chatItemSample ( 1 , . directSnd , Date . now , " hello " ) ]
) ,
Chat (
chatInfo : sampleGroupChatInfo ,
chatItems : [ chatItemSample ( 1 , . 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. " ) ]
) ,
Chat (
chatInfo : sampleContactRequestChatInfo ,
chatItems : [ ]
)
]
return ChatListView ( user : sampleUser )
. environmentObject ( chatModel )
}
}