2022-01-29 11:10:04 +00:00
//
2022-02-02 16:46:05 +00:00
// C h a t N a v L a b e l . s w i f t
2022-01-29 11:10:04 +00:00
// 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 8 / 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
struct ChatPreviewView : View {
2022-08-23 18:18:12 +04:00
@ EnvironmentObject var chatModel : ChatModel
2022-02-02 12:51:39 +00:00
@ ObservedObject var chat : Chat
2022-02-06 07:44:41 +00:00
@ Environment ( \ . colorScheme ) var colorScheme
var darkGreen = Color ( red : 0 , green : 0.5 , blue : 0 )
2022-02-02 12:51:39 +00:00
2022-01-29 11:10:04 +00:00
var body : some View {
2022-02-02 12:51:39 +00:00
let cItem = chat . chatItems . last
2022-02-12 15:59:43 +00:00
let unread = chat . chatStats . unreadCount
2022-02-04 22:13:52 +00:00
return HStack ( spacing : 8 ) {
2022-07-30 16:49:34 +04:00
ZStack ( alignment : . bottomTrailing ) {
ChatInfoImage ( chat : chat )
. frame ( width : 63 , height : 63 )
chatPreviewImageOverlayIcon ( )
. padding ( [ . bottom , . trailing ] , 1 )
}
. padding ( . leading , 4 )
2022-02-05 20:10:47 +00:00
2022-02-04 22:13:52 +00:00
VStack ( spacing : 0 ) {
HStack ( alignment : . top ) {
2022-07-18 21:58:32 +04:00
chatPreviewTitle ( )
2022-02-04 22:13:52 +00:00
Spacer ( )
2022-05-20 12:00:58 +04:00
( cItem ? . timestampText ? ? formatTimestampText ( chat . chatInfo . updatedAt ) )
2022-01-31 21:28:07 +00:00
. font ( . subheadline )
. frame ( minWidth : 60 , alignment : . trailing )
. foregroundColor ( . secondary )
2022-02-12 15:59:43 +00:00
. padding ( . top , 4 )
2022-01-31 21:28:07 +00:00
}
2022-02-04 22:13:52 +00:00
. padding ( . top , 4 )
. padding ( . horizontal , 8 )
2022-05-10 08:04:18 +01:00
ZStack ( alignment : . topTrailing ) {
2022-07-18 21:58:32 +04:00
chatPreviewText ( cItem , unread )
2022-05-10 08:04:18 +01:00
if case . direct = chat . chatInfo {
chatStatusImage ( )
. padding ( . top , 24 )
. padding ( . bottom , 4 )
. frame ( maxWidth : . infinity , alignment : . trailing )
2022-02-12 15:59:43 +00:00
}
2022-02-04 22:13:52 +00:00
}
2022-05-10 08:04:18 +01:00
. padding ( . trailing , 8 )
2022-01-31 21:28:07 +00:00
}
}
2022-01-29 11:10:04 +00:00
}
2022-02-08 09:19:25 +00:00
2022-07-30 16:49:34 +04:00
@ ViewBuilder private func chatPreviewImageOverlayIcon ( ) -> some View {
if case let . group ( groupInfo ) = chat . chatInfo {
switch ( groupInfo . membership . memberStatus ) {
case . memLeft :
groupInactiveIcon ( )
case . memRemoved :
groupInactiveIcon ( )
case . memGroupDeleted :
groupInactiveIcon ( )
default : EmptyView ( )
}
} else {
EmptyView ( )
}
}
@ ViewBuilder private func groupInactiveIcon ( ) -> some View {
2022-07-30 18:46:10 +01:00
Image ( systemName : " multiply.circle.fill " )
. foregroundColor ( . secondary )
2022-07-30 16:49:34 +04:00
. background ( Circle ( ) . foregroundColor ( Color ( uiColor : . systemBackground ) ) )
}
2022-07-18 21:58:32 +04:00
@ ViewBuilder private func chatPreviewTitle ( ) -> some View {
let v = Text ( chat . chatInfo . chatViewName )
. font ( . title3 )
. fontWeight ( . bold )
2022-07-19 18:21:15 +04:00
. lineLimit ( 1 )
2022-07-18 21:58:32 +04:00
. frame ( maxHeight : . infinity , alignment : . topLeading )
switch ( chat . chatInfo ) {
case . direct :
v . foregroundColor ( chat . chatInfo . ready ? . primary : . secondary )
case . group ( groupInfo : let groupInfo ) :
switch ( groupInfo . membership . memberStatus ) {
case . memInvited :
2022-08-23 18:18:12 +04:00
interactiveIncognito ? v . foregroundColor ( . indigo ) : v . foregroundColor ( . accentColor )
2022-07-18 21:58:32 +04:00
case . memAccepted :
v . foregroundColor ( . secondary )
2022-07-19 18:21:15 +04:00
default : v
2022-07-18 21:58:32 +04:00
}
2022-07-19 18:21:15 +04:00
default : v
2022-07-18 21:58:32 +04:00
}
}
2022-08-23 18:18:12 +04:00
private var interactiveIncognito : Bool {
chat . chatInfo . incognito || chatModel . incognito
}
2022-07-18 21:58:32 +04:00
@ ViewBuilder private func chatPreviewText ( _ cItem : ChatItem ? , _ unread : Int ) -> some View {
if let cItem = cItem {
ZStack ( alignment : . topTrailing ) {
( itemStatusMark ( cItem ) + messageText ( cItem . text , cItem . formattedText , cItem . memberDisplayName , preview : true ) )
. frame ( maxWidth : . infinity , minHeight : 44 , maxHeight : 44 , alignment : . topLeading )
. padding ( . leading , 8 )
. padding ( . trailing , 36 )
. padding ( . bottom , 4 )
if unread > 0 {
2022-08-16 13:13:29 +01:00
unreadCountText ( unread )
2022-07-18 21:58:32 +04:00
. font ( . caption )
. foregroundColor ( . white )
. padding ( . horizontal , 4 )
. frame ( minWidth : 18 , minHeight : 18 )
2022-08-20 12:47:48 +01:00
. background ( chat . chatInfo . ntfsEnabled ? Color . accentColor : Color . secondary )
2022-07-18 21:58:32 +04:00
. cornerRadius ( 10 )
2022-08-20 12:47:48 +01:00
} else if ! chat . chatInfo . ntfsEnabled {
Image ( systemName : " speaker.slash.fill " )
. foregroundColor ( . secondary )
2022-07-18 21:58:32 +04:00
}
}
} else {
switch ( chat . chatInfo ) {
case let . direct ( contact ) :
if ! contact . ready {
2022-08-03 11:40:36 +04:00
chatPreviewInfoText ( " connecting… " )
2022-07-18 21:58:32 +04:00
}
case let . group ( groupInfo ) :
2022-07-19 18:21:15 +04:00
switch ( groupInfo . membership . memberStatus ) {
2022-08-23 18:18:12 +04:00
case . memInvited : chatPreviewInfoText ( groupInfo . membership . memberIncognito ? " you are invited to group incognito " : " you are invited to group " )
2022-08-03 11:40:36 +04:00
case . memAccepted : chatPreviewInfoText ( " connecting… " )
2022-07-19 18:21:15 +04:00
default : EmptyView ( )
2022-07-18 21:58:32 +04:00
}
default : EmptyView ( )
}
}
}
2022-07-19 18:21:15 +04:00
@ ViewBuilder private func chatPreviewInfoText ( _ text : LocalizedStringKey ) -> some View {
Text ( text )
2022-07-18 21:58:32 +04:00
. frame ( maxWidth : . infinity , minHeight : 44 , maxHeight : 44 , alignment : . topLeading )
. padding ( [ . leading , . trailing ] , 8 )
. padding ( . bottom , 4 )
}
2022-02-12 15:59:43 +00:00
private func itemStatusMark ( _ cItem : ChatItem ) -> Text {
switch cItem . meta . itemStatus {
case . sndErrorAuth :
return Text ( Image ( systemName : " multiply " ) )
. font ( . caption )
. foregroundColor ( . red ) + Text ( " " )
case . sndError :
return Text ( Image ( systemName : " exclamationmark.triangle.fill " ) )
. font ( . caption )
. foregroundColor ( . yellow ) + Text ( " " )
default : return Text ( " " )
}
}
2022-05-01 14:05:01 +01:00
@ ViewBuilder private func chatStatusImage ( ) -> some View {
switch chat . serverInfo . networkStatus {
case . connected : EmptyView ( )
case . error :
2022-05-10 08:04:18 +01:00
Image ( systemName : " exclamationmark.circle " )
2022-05-01 14:05:01 +01:00
. resizable ( )
. scaledToFit ( )
2022-05-10 08:04:18 +01:00
. frame ( width : 17 , height : 17 )
2022-05-01 14:05:01 +01:00
. foregroundColor ( . secondary )
default :
2022-05-10 08:04:18 +01:00
ProgressView ( )
2022-05-01 14:05:01 +01:00
}
}
2022-01-29 11:10:04 +00:00
}
2022-08-16 13:13:29 +01:00
func unreadCountText ( _ n : Int ) -> Text {
Text ( n > 999 ? " \( n / 1000 ) k " : " \( n ) " )
}
2022-01-29 11:10:04 +00:00
struct ChatPreviewView_Previews : PreviewProvider {
static var previews : some View {
2022-02-11 07:42:00 +00:00
Group {
2022-02-02 12:51:39 +00:00
ChatPreviewView ( chat : Chat (
2022-02-08 09:19:25 +00:00
chatInfo : ChatInfo . sampleData . direct ,
2022-01-31 21:28:07 +00:00
chatItems : [ ]
) )
2022-02-02 12:51:39 +00:00
ChatPreviewView ( chat : Chat (
2022-02-08 09:19:25 +00:00
chatInfo : ChatInfo . sampleData . direct ,
2022-02-12 15:59:43 +00:00
chatItems : [ ChatItem . getSample ( 1 , . directSnd , . now , " hello " , . sndSent ) ]
2022-01-31 21:28:07 +00:00
) )
2022-05-10 08:04:18 +01:00
ChatPreviewView ( chat : Chat (
chatInfo : ChatInfo . sampleData . direct ,
chatItems : [ ChatItem . getSample ( 1 , . directSnd , . now , " hello " , . sndSent ) ] ,
chatStats : ChatStats ( unreadCount : 11 , minUnreadItemId : 0 )
) )
ChatPreviewView ( chat : Chat (
chatInfo : ChatInfo . sampleData . direct ,
chatItems : [ ChatItem . getSample ( 1 , . directSnd , . now , " hello " , . sndSent ) ] ,
chatStats : ChatStats ( unreadCount : 3 , minUnreadItemId : 0 ) ,
serverInfo : Chat . ServerInfo ( networkStatus : . error ( " status " ) )
) )
2022-02-02 12:51:39 +00:00
ChatPreviewView ( chat : Chat (
2022-02-08 09:19:25 +00:00
chatInfo : ChatInfo . sampleData . group ,
2022-02-13 10:09:09 +00:00
chatItems : [ ChatItem . getSample ( 1 , . directSnd , . now , " Lorem ipsum dolor sit amet, d. 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. " ) ] ,
chatStats : ChatStats ( unreadCount : 11 , minUnreadItemId : 0 )
2022-01-31 21:28:07 +00:00
) )
2022-01-30 00:35:20 +04:00
}
2022-02-04 22:13:52 +00:00
. previewLayout ( . fixed ( width : 360 , height : 78 ) )
2022-01-29 11:10:04 +00:00
}
}