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
2022-05-31 07:55:13 +01:00
import SimpleXChat
2022-02-01 17:34:06 +00:00
struct ChatListView : View {
@ EnvironmentObject var chatModel : ChatModel
2023-04-12 12:22:55 +02:00
@ Binding var showSettings : Bool
2022-02-13 08:45:08 +00:00
@ State private var searchText = " "
2022-09-21 15:11:52 +01:00
@ State private var showAddChat = false
2023-01-17 17:47:37 +00:00
@ State var userPickerVisible = false
2022-02-01 17:34:06 +00:00
var body : some View {
2023-01-17 17:47:37 +00:00
ZStack ( alignment : . topLeading ) {
2023-01-23 18:45:24 +00:00
NavStackCompat (
isActive : Binding (
get : { ChatModel . shared . chatId != nil } ,
set : { _ in }
) ,
destination : chatView
) {
2023-01-17 17:47:37 +00:00
VStack {
if chatModel . chats . isEmpty {
onboardingButtons ( )
}
if chatModel . chats . count > 8 {
chatList . searchable ( text : $ searchText )
} else {
chatList
}
2022-02-02 16:46:05 +00:00
}
2023-01-17 17:47:37 +00:00
}
if userPickerVisible {
Rectangle ( ) . fill ( . white . opacity ( 0.001 ) ) . onTapGesture {
withAnimation {
userPickerVisible . toggle ( )
}
2022-09-24 21:02:01 +01:00
}
2022-02-12 15:59:43 +00:00
}
2023-01-20 12:38:38 +00:00
UserPicker ( showSettings : $ showSettings , userPickerVisible : $ userPickerVisible )
2022-09-21 15:11:52 +01:00
}
}
var chatList : some View {
List {
ForEach ( filteredChats ( ) , id : \ . viewId ) { chat in
ChatListNavLink ( chat : chat )
. padding ( . trailing , - 16 )
. disabled ( chatModel . chatRunning != true )
2022-02-12 15:59:43 +00:00
}
2022-09-21 15:11:52 +01:00
}
. onChange ( of : chatModel . chatId ) { _ in
if chatModel . chatId = = nil , let chatId = chatModel . chatToTop {
chatModel . chatToTop = nil
chatModel . popChat ( chatId )
2022-02-01 17:34:06 +00:00
}
2022-09-21 15:11:52 +01:00
}
. onChange ( of : chatModel . appOpenUrl ) { _ in connectViaUrl ( ) }
. onAppear ( ) { connectViaUrl ( ) }
2023-01-17 17:47:37 +00:00
. onDisappear ( ) { withAnimation { userPickerVisible = false } }
2022-09-21 15:11:52 +01:00
. offset ( x : - 8 )
. listStyle ( . plain )
. navigationTitle ( " Your chats " )
. navigationBarTitleDisplayMode ( . inline )
. toolbar {
ToolbarItem ( placement : . navigationBarLeading ) {
2023-01-17 17:47:37 +00:00
Button {
2023-03-27 19:58:14 +03:00
if chatModel . users . filter { u in u . user . activeUser || ! u . user . hidden } . count > 1 {
2023-01-20 13:17:41 +00:00
withAnimation {
userPickerVisible . toggle ( )
}
} else {
showSettings = true
2023-01-17 17:47:37 +00:00
}
} label : {
let user = chatModel . currentUser ? ? User . sampleData
2023-01-20 13:17:41 +00:00
ZStack ( alignment : . topTrailing ) {
2023-01-24 19:00:30 +00:00
ProfileImage ( imageStr : user . image , color : Color ( uiColor : . quaternaryLabel ) )
2023-01-17 17:47:37 +00:00
. frame ( width : 32 , height : 32 )
2023-01-20 13:17:41 +00:00
. padding ( . trailing , 4 )
let allRead = chatModel . users
2023-03-23 23:09:37 +00:00
. filter { u in ! u . user . activeUser && ! u . user . hidden }
2023-01-20 13:17:41 +00:00
. allSatisfy { u in u . unreadCount = = 0 }
if ! allRead {
unreadBadge ( size : 12 )
2023-01-17 17:47:37 +00:00
}
}
}
2022-09-21 15:11:52 +01:00
}
ToolbarItem ( placement : . principal ) {
if ( chatModel . incognito ) {
HStack {
if ( chatModel . chats . count > 8 ) {
Text ( " Your chats " ) . font ( . headline )
Spacer ( ) . frame ( width : 16 )
2022-08-23 18:18:12 +04:00
}
2022-09-21 15:11:52 +01:00
Image ( systemName : " theatermasks " ) . frame ( maxWidth : 24 , maxHeight : 24 , alignment : . center ) . foregroundColor ( . indigo )
2022-08-23 18:18:12 +04:00
}
2023-01-24 19:24:46 +00:00
} else {
Text ( " Your chats " ) . font ( . headline )
2022-08-23 18:18:12 +04:00
}
2022-09-21 15:11:52 +01:00
}
ToolbarItem ( placement : . navigationBarTrailing ) {
switch chatModel . chatRunning {
case . some ( true ) : NewChatButton ( showAddChat : $ showAddChat )
case . some ( false ) : chatStoppedIcon ( )
case . none : EmptyView ( )
2022-02-11 07:42:00 +00:00
}
}
2022-02-01 17:34:06 +00:00
}
2022-09-21 15:11:52 +01:00
}
2022-02-13 08:45:08 +00:00
2023-01-20 13:17:41 +00:00
private func unreadBadge ( _ text : Text ? = Text ( " " ) , size : CGFloat = 18 ) -> some View {
Circle ( )
. frame ( width : size , height : size )
. foregroundColor ( . accentColor )
}
2022-09-21 15:11:52 +01:00
private func onboardingButtons ( ) -> some View {
VStack ( alignment : . trailing , spacing : 0 ) {
Path { p in
p . move ( to : CGPoint ( x : 8 , y : 0 ) )
p . addLine ( to : CGPoint ( x : 16 , y : 10 ) )
p . addLine ( to : CGPoint ( x : 0 , y : 10 ) )
p . addLine ( to : CGPoint ( x : 8 , y : 0 ) )
}
. fill ( Color . accentColor )
. frame ( width : 20 , height : 10 )
. padding ( . trailing , 12 )
connectButton ( " Tap to start a new chat " ) {
showAddChat = true
}
connectButton ( " or chat with the developers " ) {
DispatchQueue . main . async {
UIApplication . shared . open ( simplexTeamURL )
}
}
. padding ( . top , 10 )
Spacer ( )
Text ( " You have no chats " )
. foregroundColor ( . secondary )
. frame ( maxWidth : . infinity )
}
. padding ( . trailing , 6 )
. frame ( maxHeight : . infinity )
}
private func connectButton ( _ label : LocalizedStringKey , action : @ escaping ( ) -> Void ) -> some View {
Button ( action : action ) {
Text ( label )
. padding ( . vertical , 10 )
. padding ( . horizontal , 20 )
2022-02-13 08:45:08 +00:00
}
2022-09-21 15:11:52 +01:00
. background ( Color . accentColor )
. foregroundColor ( . white )
. clipShape ( RoundedRectangle ( cornerRadius : 16 ) )
2022-02-13 08:45:08 +00:00
}
2022-12-30 21:47:11 +04:00
@ ViewBuilder private func chatView ( ) -> some View {
if let chatId = chatModel . chatId , let chat = chatModel . getChat ( chatId ) {
2022-08-29 14:08:46 +01:00
ChatView ( chat : chat ) . onAppear {
loadChat ( chat : chat )
}
}
}
2022-02-13 08:45:08 +00:00
private func filteredChats ( ) -> [ Chat ] {
let s = searchText . trimmingCharacters ( in : . whitespaces ) . localizedLowercase
2022-08-03 18:02:59 +01:00
return s = = " "
2022-02-13 08:45:08 +00:00
? chatModel . chats
2022-08-25 17:36:26 +04:00
: chatModel . chats . filter { chat in
let contains = chat . chatInfo . chatViewName . localizedLowercase . contains ( s )
switch chat . chatInfo {
case let . direct ( contact ) :
return contains
|| contact . profile . displayName . localizedLowercase . contains ( s )
|| contact . fullName . localizedLowercase . contains ( s )
case . contactConnection : return false
default : return contains
}
2022-04-25 10:39:28 +01:00
}
2022-02-01 17:34:06 +00:00
}
}
2022-06-24 13:52:20 +01:00
func chatStoppedIcon ( ) -> some View {
Button {
AlertManager . shared . showAlertMsg (
title : " Chat is stopped " ,
message : " You can start chat via app Settings / Database or by restarting the app "
)
} label : {
Image ( systemName : " exclamationmark.octagon.fill " ) . foregroundColor ( . red )
}
}
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 {
2023-04-12 12:22:55 +02:00
ChatListView ( showSettings : Binding . constant ( false ) )
2022-02-11 07:42:00 +00:00
. environmentObject ( chatModel )
2023-04-12 12:22:55 +02:00
ChatListView ( showSettings : Binding . constant ( false ) )
2022-02-11 07:42:00 +00:00
. environmentObject ( ChatModel ( ) )
}
2022-02-01 17:34:06 +00:00
}
}