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
2023-08-17 15:04:17 +04:00
@ AppStorage ( DEFAULT_PRIVACY_SHOW_CHAT_PREVIEWS ) private var showChatPreviews = true
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-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-09-22 22:27:20 +01:00
. padding ( . bottom , 4 )
2022-02-04 22:13:52 +00:00
. padding ( . horizontal , 8 )
2022-05-10 08:04:18 +01:00
ZStack ( alignment : . topTrailing ) {
2023-01-27 22:09:39 +00:00
chatMessagePreview ( cItem )
2023-08-08 17:26:56 +04:00
chatStatusImage ( )
. padding ( . top , 26 )
. frame ( maxWidth : . infinity , alignment : . trailing )
2022-02-04 22:13:52 +00:00
}
2022-05-10 08:04:18 +01:00
. padding ( . trailing , 8 )
2022-09-22 22:27:20 +01:00
Spacer ( )
2022-01-31 21:28:07 +00:00
}
2022-09-22 22:27:20 +01:00
. frame ( maxHeight : . infinity )
2022-01-31 21:28:07 +00:00
}
2022-09-22 22:27:20 +01:00
. padding ( . bottom , - 8 )
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 ) {
2023-08-08 17:26:56 +04:00
case . memLeft : groupInactiveIcon ( )
case . memRemoved : groupInactiveIcon ( )
case . memGroupDeleted : groupInactiveIcon ( )
2022-07-30 16:49:34 +04:00
default : EmptyView ( )
}
} else {
EmptyView ( )
}
}
@ ViewBuilder private func groupInactiveIcon ( ) -> some View {
2022-07-30 18:46:10 +01:00
Image ( systemName : " multiply.circle.fill " )
2023-08-08 17:26:56 +04:00
. foregroundColor ( . secondary . opacity ( 0.65 ) )
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 {
2022-12-12 08:59:35 +00:00
let t = Text ( chat . chatInfo . chatViewName ) . font ( . title3 ) . fontWeight ( . bold )
switch chat . chatInfo {
case let . direct ( contact ) :
previewTitle ( contact . verified = = true ? verifiedIcon + t : t )
. foregroundColor ( chat . chatInfo . ready ? . primary : . secondary )
case let . group ( groupInfo ) :
let v = previewTitle ( t )
2022-07-18 21:58:32 +04:00
switch ( groupInfo . membership . memberStatus ) {
2022-12-12 08:59:35 +00:00
case . memInvited : v . foregroundColor ( chat . chatInfo . incognito ? . indigo : . accentColor )
case . memAccepted : v . foregroundColor ( . secondary )
2022-07-19 18:21:15 +04:00
default : v
2022-07-18 21:58:32 +04:00
}
2022-12-12 08:59:35 +00:00
default : previewTitle ( t )
2022-07-18 21:58:32 +04:00
}
}
2022-12-12 08:59:35 +00:00
private func previewTitle ( _ t : Text ) -> some View {
t . lineLimit ( 1 ) . frame ( alignment : . topLeading )
}
private var verifiedIcon : Text {
( Text ( Image ( systemName : " checkmark.shield " ) ) + Text ( " " ) )
. foregroundColor ( . secondary )
. baselineOffset ( 1 )
. kerning ( - 2 )
}
2023-08-17 12:52:03 +01:00
private func chatPreviewLayout ( _ text : Text , draft : Bool = false ) -> some View {
2023-01-25 08:35:25 +00:00
ZStack ( alignment : . topTrailing ) {
2023-08-17 12:52:03 +01:00
text
2023-01-25 08:35:25 +00:00
. lineLimit ( 2 )
. multilineTextAlignment ( . leading )
. frame ( maxWidth : . infinity , alignment : . topLeading )
. padding ( . leading , 8 )
. padding ( . trailing , 36 )
2023-08-17 12:52:03 +01:00
. privacySensitive ( ! showChatPreviews && ! draft )
. redacted ( reason : . privacy )
2023-01-25 08:35:25 +00:00
let s = chat . chatStats
if s . unreadCount > 0 || s . unreadChat {
unreadCountText ( s . unreadCount )
. font ( . caption )
. foregroundColor ( . white )
. padding ( . horizontal , 4 )
. frame ( minWidth : 18 , minHeight : 18 )
. background ( chat . chatInfo . ntfsEnabled ? Color . accentColor : Color . secondary )
. cornerRadius ( 10 )
} else if ! chat . chatInfo . ntfsEnabled {
Image ( systemName : " speaker.slash.fill " )
. foregroundColor ( . secondary )
2023-06-19 11:13:30 +01:00
} else if chat . chatInfo . chatSettings ? . favorite ? ? false {
Image ( systemName : " star.fill " )
. resizable ( )
. scaledToFill ( )
. frame ( width : 18 , height : 18 )
. padding ( . trailing , 1 )
. foregroundColor ( . secondary . opacity ( 0.65 ) )
2023-01-25 08:35:25 +00:00
}
}
}
private func messageDraft ( _ draft : ComposeState ) -> Text {
let msg = draft . message
return image ( " rectangle.and.pencil.and.ellipsis " , color : . accentColor )
+ attachment ( )
+ messageText ( msg , parseSimpleXMarkdown ( msg ) , nil , preview : true )
func image ( _ s : String , color : Color = Color ( uiColor : . tertiaryLabel ) ) -> Text {
Text ( Image ( systemName : s ) ) . foregroundColor ( color ) + Text ( " " )
}
func attachment ( ) -> Text {
switch draft . preview {
2023-01-27 22:09:39 +00:00
case let . filePreview ( fileName , _ ) : return image ( " doc.fill " ) + Text ( fileName ) + Text ( " " )
2023-04-06 20:26:48 +03:00
case . mediaPreviews : return image ( " photo " )
2023-01-27 22:09:39 +00:00
case let . voicePreview ( _ , duration ) : return image ( " play.fill " ) + Text ( durationText ( duration ) )
2023-01-25 08:35:25 +00:00
default : return Text ( " " )
}
}
}
2023-01-27 22:09:39 +00:00
func chatItemPreview ( _ cItem : ChatItem ) -> Text {
2023-02-09 15:10:35 +04:00
let itemText = cItem . meta . itemDeleted = = nil ? cItem . text : NSLocalizedString ( " marked deleted " , comment : " marked deleted chat item preview text " )
let itemFormattedText = cItem . meta . itemDeleted = = nil ? cItem . formattedText : nil
2023-01-27 22:09:39 +00:00
return messageText ( itemText , itemFormattedText , cItem . memberDisplayName , icon : attachment ( ) , preview : true )
func attachment ( ) -> String ? {
switch cItem . content . msgContent {
case . file : return " doc.fill "
case . image : return " photo "
2023-04-06 20:26:48 +03:00
case . video : return " video "
2023-01-27 22:09:39 +00:00
case . voice : return " play.fill "
default : return nil
}
}
}
@ ViewBuilder private func chatMessagePreview ( _ cItem : ChatItem ? ) -> some View {
2023-01-25 08:35:25 +00:00
if chatModel . draftChatId = = chat . id , let draft = chatModel . draft {
2023-08-17 12:52:03 +01:00
chatPreviewLayout ( messageDraft ( draft ) , draft : true )
2023-01-25 08:35:25 +00:00
} else if let cItem = cItem {
2023-01-27 22:09:39 +00:00
chatPreviewLayout ( itemStatusMark ( cItem ) + chatItemPreview ( cItem ) )
2022-07-18 21:58:32 +04:00
} else {
switch ( chat . chatInfo ) {
case let . direct ( contact ) :
if ! contact . ready {
2023-09-20 12:26:16 +04:00
if contact . nextSendGrpInv {
chatPreviewInfoText ( " send direct message " )
} else {
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-29 14:47:29 +04:00
case . memInvited : groupInvitationPreviewText ( groupInfo )
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-08-29 14:47:29 +04:00
@ ViewBuilder private func groupInvitationPreviewText ( _ groupInfo : GroupInfo ) -> some View {
groupInfo . membership . memberIncognito
? chatPreviewInfoText ( " join as \( groupInfo . membership . memberProfile . displayName ) " )
2023-08-08 17:26:56 +04:00
: chatPreviewInfoText ( " you are invited to group " )
2022-08-29 14:47:29 +04:00
}
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 {
2023-01-20 14:56:05 +04:00
switch chat . chatInfo {
case let . direct ( contact ) :
switch ( chatModel . contactNetworkStatus ( contact ) ) {
2023-08-08 17:26:56 +04:00
case . connected : incognitoIcon ( chat . chatInfo . incognito )
2023-01-20 14:56:05 +04:00
case . error :
Image ( systemName : " exclamationmark.circle " )
. resizable ( )
. scaledToFit ( )
. frame ( width : 17 , height : 17 )
. foregroundColor ( . secondary )
default :
ProgressView ( )
}
2022-05-01 14:05:01 +01:00
default :
2023-08-08 17:26:56 +04:00
incognitoIcon ( chat . chatInfo . incognito )
2022-05-01 14:05:01 +01:00
}
}
2022-01-29 11:10:04 +00:00
}
2023-08-08 17:26:56 +04:00
@ ViewBuilder func incognitoIcon ( _ incognito : Bool ) -> some View {
if incognito {
Image ( systemName : " theatermasks " )
. resizable ( )
. scaledToFit ( )
. frame ( width : 22 , height : 22 )
. foregroundColor ( . secondary )
} else {
EmptyView ( )
}
}
2022-08-16 13:13:29 +01:00
func unreadCountText ( _ n : Int ) -> Text {
2022-10-21 12:32:11 +01:00
Text ( n > 999 ? " \( n / 1000 ) k " : n > 0 ? " \( n ) " : " " )
2022-08-16 13:13:29 +01:00
}
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 ,
2023-07-28 13:16:52 +04:00
chatItems : [ ChatItem . getSample ( 1 , . directSnd , . now , " hello " , . sndSent ( sndProgress : . complete ) ) ]
2022-01-31 21:28:07 +00:00
) )
2022-05-10 08:04:18 +01:00
ChatPreviewView ( chat : Chat (
chatInfo : ChatInfo . sampleData . direct ,
2023-07-28 13:16:52 +04:00
chatItems : [ ChatItem . getSample ( 1 , . directSnd , . now , " hello " , . sndSent ( sndProgress : . complete ) ) ] ,
2022-05-10 08:04:18 +01:00
chatStats : ChatStats ( unreadCount : 11 , minUnreadItemId : 0 )
) )
2022-12-03 15:40:31 +04:00
ChatPreviewView ( chat : Chat (
chatInfo : ChatInfo . sampleData . direct ,
2023-07-28 13:16:52 +04:00
chatItems : [ ChatItem . getSample ( 1 , . directSnd , . now , " hello " , . sndSent ( sndProgress : . complete ) , itemDeleted : . deleted ( deletedTs : . now ) ) ]
2022-12-03 15:40:31 +04:00
) )
2022-05-10 08:04:18 +01:00
ChatPreviewView ( chat : Chat (
chatInfo : ChatInfo . sampleData . direct ,
2023-07-28 13:16:52 +04:00
chatItems : [ ChatItem . getSample ( 1 , . directSnd , . now , " hello " , . sndSent ( sndProgress : . complete ) ) ] ,
2023-01-20 14:56:05 +04:00
chatStats : ChatStats ( unreadCount : 3 , minUnreadItemId : 0 )
2022-05-10 08:04:18 +01:00
) )
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
}
}