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
2024-07-03 22:42:13 +01:00
@ EnvironmentObject var theme : AppTheme
2024-07-28 21:53:21 +01:00
@ Environment ( \ . dynamicTypeSize ) private var userFont : DynamicTypeSize
2022-02-02 12:51:39 +00:00
@ ObservedObject var chat : Chat
2023-10-26 10:32:11 +04:00
@ Binding var progressByTimeout : Bool
2024-01-10 23:57:34 +07:00
@ State var deleting : Bool = false
2022-02-06 07:44:41 +00:00
var darkGreen = Color ( red : 0 , green : 0.5 , blue : 0 )
2024-07-24 00:11:42 +07:00
@ State private var activeContentPreview : ActiveContentPreview ? = nil
@ State private var showFullscreenGallery : Bool = false
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
2024-07-29 22:11:02 +01:00
var dynamicMediaSize : CGFloat { dynamicSize ( userFont ) . mediaSize }
var dynamicChatInfoSize : CGFloat { dynamicSize ( userFont ) . chatInfoSize }
2024-07-28 21:53:21 +01: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-04 22:13:52 +00:00
return HStack ( spacing : 8 ) {
2022-07-30 16:49:34 +04:00
ZStack ( alignment : . bottomTrailing ) {
2024-07-29 22:11:02 +01:00
ChatInfoImage ( chat : chat , size : dynamicSize ( userFont ) . profileImageSize )
2022-07-30 16:49:34 +04:00
chatPreviewImageOverlayIcon ( )
. padding ( [ . bottom , . trailing ] , 1 )
}
. padding ( . leading , 4 )
2022-02-05 20:10:47 +00:00
2024-09-21 23:33:18 +03:00
let chatTs = if let cItem {
cItem . meta . itemTs
} else {
chat . chatInfo . chatTs
}
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 ( )
2024-09-21 23:33:18 +03:00
( formatTimestampText ( chatTs ) )
2022-01-31 21:28:07 +00:00
. font ( . subheadline )
. frame ( minWidth : 60 , alignment : . trailing )
2024-07-03 22:42:13 +01:00
. foregroundColor ( theme . colors . 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 ) {
2024-07-24 00:11:42 +07:00
let chat = activeContentPreview ? . chat ? ? chat
let ci = activeContentPreview ? . ci ? ? chat . chatItems . last
let mc = ci ? . content . msgContent
HStack ( alignment : . top ) {
let deleted = ci ? . isDeletedContent = = true || ci ? . meta . itemDeleted != nil
let showContentPreview = ( showChatPreviews && chatModel . draftChatId != chat . id && ! deleted ) || activeContentPreview != nil
if let ci , showContentPreview {
chatItemContentPreview ( chat , ci )
}
let mcIsVoice = switch mc { case . voice : true ; default : false }
if ! mcIsVoice || ! showContentPreview || mc ? . text != " " || chatModel . draftChatId = = chat . id {
let hasFilePreview = if case . file = mc { true } else { false }
chatMessagePreview ( cItem , hasFilePreview )
} else {
Spacer ( )
chatInfoIcon ( chat ) . frame ( minWidth : 37 , alignment : . trailing )
}
}
. onChange ( of : chatModel . stopPreviousRecPlay ? . path ) { _ in
checkActiveContentPreview ( chat , ci , mc )
}
. onChange ( of : activeContentPreview ) { _ in
checkActiveContentPreview ( chat , ci , mc )
}
. onChange ( of : showFullscreenGallery ) { _ in
checkActiveContentPreview ( chat , ci , mc )
}
2023-08-08 17:26:56 +04:00
chatStatusImage ( )
2024-07-29 22:11:02 +01:00
. padding ( . top , dynamicChatInfoSize * 1.44 )
2023-08-08 17:26:56 +04:00
. frame ( maxWidth : . infinity , alignment : . trailing )
2022-02-04 22:13:52 +00:00
}
2024-07-24 00:11:42 +07:00
. frame ( maxWidth : . infinity , alignment : . leading )
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 )
2024-01-10 23:57:34 +07:00
. onChange ( of : chatModel . deletedChats . contains ( chat . chatInfo . id ) ) { contains in
deleting = contains
2024-07-24 00:11:42 +07:00
// S t o p v o i c e w h e n d e l e t i n g t h e c h a t
if contains , let ci = activeContentPreview ? . ci {
VoiceItemState . stopVoiceInSmallView ( chat . chatInfo , ci )
}
}
func checkActiveContentPreview ( _ chat : Chat , _ ci : ChatItem ? , _ mc : MsgContent ? ) {
let playing = chatModel . stopPreviousRecPlay
if case . voice = activeContentPreview ? . mc , playing = = nil {
activeContentPreview = nil
} else if activeContentPreview = = nil {
if case . image = mc , let ci , let mc , showFullscreenGallery {
activeContentPreview = ActiveContentPreview ( chat : chat , ci : ci , mc : mc )
}
if case . video = mc , let ci , let mc , showFullscreenGallery {
activeContentPreview = ActiveContentPreview ( chat : chat , ci : ci , mc : mc )
}
if case . voice = mc , let ci , let mc , let fileSource = ci . file ? . fileSource , playing ? . path . hasSuffix ( fileSource . filePath ) = = true {
activeContentPreview = ActiveContentPreview ( chat : chat , ci : ci , mc : mc )
}
} else if case . voice = activeContentPreview ? . mc {
if let playing , let fileSource = ci ? . file ? . fileSource , ! playing . path . hasSuffix ( fileSource . filePath ) {
activeContentPreview = nil
}
} else if ! showFullscreenGallery {
activeContentPreview = nil
}
2024-01-10 23:57:34 +07: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 {
2023-09-27 20:07:32 +04:00
switch chat . chatInfo {
case let . direct ( contact ) :
if ! contact . active {
inactiveIcon ( )
} else {
EmptyView ( )
}
case let . group ( groupInfo ) :
2022-07-30 16:49:34 +04:00
switch ( groupInfo . membership . memberStatus ) {
2023-09-27 20:07:32 +04:00
case . memLeft : inactiveIcon ( )
case . memRemoved : inactiveIcon ( )
case . memGroupDeleted : inactiveIcon ( )
2022-07-30 16:49:34 +04:00
default : EmptyView ( )
}
2023-09-27 20:07:32 +04:00
default :
2022-07-30 16:49:34 +04:00
EmptyView ( )
}
}
2023-09-27 20:07:32 +04:00
@ ViewBuilder private func inactiveIcon ( ) -> 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 ) :
2024-01-10 23:57:34 +07:00
previewTitle ( contact . verified = = true ? verifiedIcon + t : t ) . foregroundColor ( deleting ? Color . secondary : nil )
2022-12-12 08:59:35 +00:00
case let . group ( groupInfo ) :
2024-01-19 02:14:18 +07:00
let v = previewTitle ( t )
2022-07-18 21:58:32 +04:00
switch ( groupInfo . membership . memberStatus ) {
2024-07-03 22:42:13 +01:00
case . memInvited : v . foregroundColor ( deleting ? theme . colors . secondary : chat . chatInfo . incognito ? . indigo : theme . colors . primary )
case . memAccepted : v . foregroundColor ( theme . colors . secondary )
default : if deleting { v . foregroundColor ( theme . colors . secondary ) } else { 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 {
2024-11-27 19:01:16 +00:00
( Text ( Image ( systemName : " checkmark.shield " ) ) + textSpace )
2024-07-03 22:42:13 +01:00
. foregroundColor ( theme . colors . secondary )
2022-12-12 08:59:35 +00:00
. baselineOffset ( 1 )
. kerning ( - 2 )
}
2024-07-24 00:11:42 +07:00
private func chatPreviewLayout ( _ text : Text ? , draft : Bool = false , _ hasFilePreview : Bool = false ) -> some View {
2023-01-25 08:35:25 +00:00
ZStack ( alignment : . topTrailing ) {
2023-10-15 18:12:13 +01:00
let t = text
2024-07-28 21:53:21 +01:00
. lineLimit ( userFont <= . xxxLarge ? 2 : 1 )
2023-01-25 08:35:25 +00:00
. multilineTextAlignment ( . leading )
. frame ( maxWidth : . infinity , alignment : . topLeading )
2024-07-24 00:11:42 +07:00
. padding ( . leading , hasFilePreview ? 0 : 8 )
. padding ( . trailing , hasFilePreview ? 38 : 36 )
. offset ( x : hasFilePreview ? - 2 : 0 )
2024-07-25 20:53:10 +07:00
. fixedSize ( horizontal : false , vertical : true )
2023-10-15 18:12:13 +01:00
if ! showChatPreviews && ! draft {
t . privacySensitive ( true ) . redacted ( reason : . privacy )
} else {
t
}
2024-07-24 00:11:42 +07:00
chatInfoIcon ( chat ) . frame ( minWidth : 37 , alignment : . trailing )
}
}
@ ViewBuilder private func chatInfoIcon ( _ chat : Chat ) -> some View {
let s = chat . chatStats
if s . unreadCount > 0 || s . unreadChat {
unreadCountText ( s . unreadCount )
2024-07-28 21:53:21 +01:00
. font ( userFont <= . xxxLarge ? . caption : . caption2 )
2024-07-24 00:11:42 +07:00
. foregroundColor ( . white )
2024-07-29 22:11:02 +01:00
. padding ( . horizontal , dynamicSize ( userFont ) . unreadPadding )
2024-07-28 21:53:21 +01:00
. frame ( minWidth : dynamicChatInfoSize , minHeight : dynamicChatInfoSize )
2024-07-24 00:11:42 +07:00
. background ( chat . chatInfo . ntfsEnabled || chat . chatInfo . chatType = = . local ? theme . colors . primary : theme . colors . secondary )
2024-07-29 22:11:02 +01:00
. cornerRadius ( dynamicSize ( userFont ) . unreadCorner )
2024-07-24 00:11:42 +07:00
} else if ! chat . chatInfo . ntfsEnabled && chat . chatInfo . chatType != . local {
Image ( systemName : " speaker.slash.fill " )
2024-07-28 21:53:21 +01:00
. resizable ( )
. scaledToFill ( )
. frame ( width : dynamicChatInfoSize , height : dynamicChatInfoSize )
2024-07-24 00:11:42 +07:00
. foregroundColor ( theme . colors . secondary )
} else if chat . chatInfo . chatSettings ? . favorite ? ? false {
Image ( systemName : " star.fill " )
. resizable ( )
. scaledToFill ( )
2024-07-28 21:53:21 +01:00
. frame ( width : dynamicChatInfoSize , height : dynamicChatInfoSize )
2024-07-24 00:11:42 +07:00
. padding ( . trailing , 1 )
2024-07-28 21:53:21 +01:00
. foregroundColor ( theme . colors . secondary . opacity ( 0.65 ) )
2024-07-24 00:11:42 +07:00
} else {
Color . clear . frame ( width : 0 )
2023-01-25 08:35:25 +00:00
}
}
private func messageDraft ( _ draft : ComposeState ) -> Text {
let msg = draft . message
2024-07-03 22:42:13 +01:00
return image ( " rectangle.and.pencil.and.ellipsis " , color : theme . colors . primary )
2023-01-25 08:35:25 +00:00
+ attachment ( )
2024-07-03 22:42:13 +01:00
+ messageText ( msg , parseSimpleXMarkdown ( msg ) , nil , preview : true , showSecrets : false , secondaryColor : theme . colors . secondary )
2023-01-25 08:35:25 +00:00
func image ( _ s : String , color : Color = Color ( uiColor : . tertiaryLabel ) ) -> Text {
2024-11-27 19:01:16 +00:00
Text ( Image ( systemName : s ) ) . foregroundColor ( color ) + textSpace
2023-01-25 08:35:25 +00:00
}
func attachment ( ) -> Text {
switch draft . preview {
2024-11-27 19:01:16 +00:00
case let . filePreview ( fileName , _ ) : return image ( " doc.fill " ) + Text ( fileName ) + textSpace
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 {
2024-01-24 13:44:29 +04:00
let itemText = cItem . meta . itemDeleted = = nil ? cItem . text : markedDeletedText ( )
2023-02-09 15:10:35 +04:00
let itemFormattedText = cItem . meta . itemDeleted = = nil ? cItem . formattedText : nil
2024-07-24 00:11:42 +07:00
return messageText ( itemText , itemFormattedText , cItem . memberDisplayName , icon : nil , preview : true , showSecrets : false , secondaryColor : theme . colors . secondary )
2023-01-27 22:09:39 +00:00
2024-01-24 13:44:29 +04:00
// s a m e t e x t s a r e i n m a r k e d D e l e t e d T e x t i n M a r k e d D e l e t e d I t e m V i e w , b u t i t r e t u r n s L o c a l i z e d S t r i n g K e y ;
// c a n b e r e f a c t o r e d i n t o a s i n g l e f u n c t i o n i f f u n c t i o n s c a l l i n g t h e s e a r e c h a n g e d t o r e t u r n s a m e t y p e
func markedDeletedText ( ) -> String {
switch cItem . meta . itemDeleted {
case let . moderated ( _ , byGroupMember ) : String . localizedStringWithFormat ( NSLocalizedString ( " moderated by %@ " , comment : " marked deleted chat item preview text " ) , byGroupMember . displayName )
case . blocked : NSLocalizedString ( " blocked " , comment : " marked deleted chat item preview text " )
case . blockedByAdmin : NSLocalizedString ( " blocked by admin " , comment : " marked deleted chat item preview text " )
case . deleted , nil : NSLocalizedString ( " marked deleted " , comment : " marked deleted chat item preview text " )
}
}
2023-01-27 22:09:39 +00:00
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
}
}
}
2024-07-24 00:11:42 +07:00
@ ViewBuilder private func chatMessagePreview ( _ cItem : ChatItem ? , _ hasFilePreview : Bool = false ) -> some View {
2023-01-25 08:35:25 +00:00
if chatModel . draftChatId = = chat . id , let draft = chatModel . draft {
2024-07-24 00:11:42 +07:00
chatPreviewLayout ( messageDraft ( draft ) , draft : true , hasFilePreview )
2023-01-25 08:35:25 +00:00
} else if let cItem = cItem {
2024-07-24 00:11:42 +07:00
chatPreviewLayout ( itemStatusMark ( cItem ) + chatItemPreview ( cItem ) , hasFilePreview )
2022-07-18 21:58:32 +04:00
} else {
switch ( chat . chatInfo ) {
case let . direct ( contact ) :
2024-08-05 12:58:24 +01:00
if contact . activeConn = = nil && contact . profile . contactLink != nil && contact . active {
2023-11-10 10:16:06 +04:00
chatPreviewInfoText ( " Tap to Connect " )
2024-07-03 22:42:13 +01:00
. foregroundColor ( theme . colors . primary )
2024-07-19 11:31:43 +04:00
} else if ! contact . sndReady && contact . activeConn != nil {
2023-09-20 12:26:16 +04:00
if contact . nextSendGrpInv {
chatPreviewInfoText ( " send direct message " )
2023-09-27 20:07:32 +04:00
} else if contact . active {
2023-09-20 12:26:16 +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-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 ( )
}
}
}
2024-07-24 00:11:42 +07:00
@ ViewBuilder func chatItemContentPreview ( _ chat : Chat , _ ci : ChatItem ) -> some View {
let mc = ci . content . msgContent
switch mc {
case let . link ( _ , preview ) :
2024-07-28 21:53:21 +01:00
smallContentPreview ( size : dynamicMediaSize ) {
2024-07-24 00:11:42 +07:00
ZStack ( alignment : . topTrailing ) {
2024-09-04 14:49:01 +01:00
Image ( uiImage : imageFromBase64 ( preview . image ) ? ? UIImage ( systemName : " arrow.up.right " ) ! )
2024-07-24 00:11:42 +07:00
. resizable ( )
. aspectRatio ( contentMode : . fill )
2024-07-28 21:53:21 +01:00
. frame ( width : dynamicMediaSize , height : dynamicMediaSize )
2024-07-24 00:11:42 +07:00
ZStack {
Image ( systemName : " arrow.up.right " )
. resizable ( )
. foregroundColor ( Color . white )
. font ( . system ( size : 15 , weight : . black ) )
. frame ( width : 8 , height : 8 )
}
. frame ( width : 16 , height : 16 )
. background ( Color . black . opacity ( 0.25 ) )
. cornerRadius ( 8 )
}
. onTapGesture {
UIApplication . shared . open ( preview . uri )
}
2024-07-28 21:53:21 +01:00
}
2024-07-24 00:11:42 +07:00
case let . image ( _ , image ) :
2024-07-28 21:53:21 +01:00
smallContentPreview ( size : dynamicMediaSize ) {
2024-09-04 14:49:01 +01:00
CIImageView ( chatItem : ci , preview : imageFromBase64 ( image ) , maxWidth : dynamicMediaSize , smallView : true , showFullScreenImage : $ showFullscreenGallery )
2024-08-29 19:25:08 +03:00
. environmentObject ( ReverseListScrollModel ( ) )
2024-07-28 21:53:21 +01:00
}
2024-07-24 00:11:42 +07:00
case let . video ( _ , image , duration ) :
2024-07-28 21:53:21 +01:00
smallContentPreview ( size : dynamicMediaSize ) {
2024-09-04 14:49:01 +01:00
CIVideoView ( chatItem : ci , preview : imageFromBase64 ( image ) , duration : duration , maxWidth : dynamicMediaSize , videoWidth : nil , smallView : true , showFullscreenPlayer : $ showFullscreenGallery )
2024-08-29 19:25:08 +03:00
. environmentObject ( ReverseListScrollModel ( ) )
2024-07-28 21:53:21 +01:00
}
2024-07-24 00:11:42 +07:00
case let . voice ( _ , duration ) :
2024-07-28 21:53:21 +01:00
smallContentPreviewVoice ( size : dynamicMediaSize ) {
CIVoiceView ( chat : chat , chatItem : ci , recordingFile : ci . file , duration : duration , allowMenu : Binding . constant ( true ) , smallViewSize : dynamicMediaSize )
}
2024-07-24 00:11:42 +07:00
case . file :
2024-07-28 21:53:21 +01:00
smallContentPreviewFile ( size : dynamicMediaSize ) {
CIFileView ( file : ci . file , edited : ci . meta . itemEdited , smallViewSize : dynamicMediaSize )
}
2024-07-24 00:11:42 +07: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 {
2024-05-15 16:09:42 +04:00
case . sndErrorAuth , . sndError :
2022-02-12 15:59:43 +00:00
return Text ( Image ( systemName : " multiply " ) )
. font ( . caption )
2024-11-27 19:01:16 +00:00
. foregroundColor ( . red ) + textSpace
2024-05-15 16:09:42 +04:00
case . sndWarning :
2022-02-12 15:59:43 +00:00
return Text ( Image ( systemName : " exclamationmark.triangle.fill " ) )
. font ( . caption )
2024-11-27 19:01:16 +00:00
. foregroundColor ( . orange ) + textSpace
2022-02-12 15:59:43 +00:00
default : return Text ( " " )
}
}
2022-05-01 14:05:01 +01:00
@ ViewBuilder private func chatStatusImage ( ) -> some View {
2024-07-29 22:11:02 +01:00
let size = dynamicSize ( userFont ) . incognitoSize
2023-01-20 14:56:05 +04:00
switch chat . chatInfo {
case let . direct ( contact ) :
2023-11-10 10:16:06 +04:00
if contact . active && contact . activeConn != nil {
2024-08-13 23:08:04 +03:00
NetworkStatusView ( contact : contact , size : size )
2023-09-27 20:07:32 +04:00
} else {
2024-07-29 22:11:02 +01:00
incognitoIcon ( chat . chatInfo . incognito , theme . colors . secondary , size : size )
2023-01-20 14:56:05 +04:00
}
2023-10-26 10:32:11 +04:00
case . group :
if progressByTimeout {
ProgressView ( )
} else {
2024-07-29 22:11:02 +01:00
incognitoIcon ( chat . chatInfo . incognito , theme . colors . secondary , size : size )
2023-10-26 10:32:11 +04:00
}
2022-05-01 14:05:01 +01:00
default :
2024-07-29 22:11:02 +01:00
incognitoIcon ( chat . chatInfo . incognito , theme . colors . secondary , size : size )
2022-05-01 14:05:01 +01:00
}
}
2024-08-13 23:08:04 +03:00
struct NetworkStatusView : View {
@ Environment ( \ . dynamicTypeSize ) private var userFont : DynamicTypeSize
@ EnvironmentObject var theme : AppTheme
@ ObservedObject var networkModel = NetworkModel . shared
let contact : Contact
let size : CGFloat
var body : some View {
let dynamicChatInfoSize = dynamicSize ( userFont ) . chatInfoSize
switch ( networkModel . contactNetworkStatus ( contact ) ) {
case . connected : incognitoIcon ( contact . contactConnIncognito , theme . colors . secondary , size : size )
case . error :
Image ( systemName : " exclamationmark.circle " )
. resizable ( )
. scaledToFit ( )
. frame ( width : dynamicChatInfoSize , height : dynamicChatInfoSize )
. foregroundColor ( theme . colors . secondary )
default :
ProgressView ( )
}
}
}
2022-01-29 11:10:04 +00:00
}
2024-07-29 22:11:02 +01:00
@ ViewBuilder func incognitoIcon ( _ incognito : Bool , _ secondaryColor : Color , size : CGFloat ) -> some View {
2023-08-08 17:26:56 +04:00
if incognito {
Image ( systemName : " theatermasks " )
. resizable ( )
. scaledToFit ( )
2024-07-29 22:11:02 +01:00
. frame ( width : size , height : size )
2024-07-03 22:42:13 +01:00
. foregroundColor ( secondaryColor )
2023-08-08 17:26:56 +04:00
} else {
EmptyView ( )
}
}
2024-07-28 21:53:21 +01:00
func smallContentPreview ( size : CGFloat , _ view : @ escaping ( ) -> some View ) -> some View {
view ( )
. frame ( width : size , height : size )
2024-07-24 00:11:42 +07:00
. cornerRadius ( 8 )
. overlay ( RoundedRectangle ( cornerSize : CGSize ( width : 8 , height : 8 ) )
. strokeBorder ( . secondary , lineWidth : 0.3 , antialiased : true ) )
2024-07-28 21:53:21 +01:00
. padding ( . vertical , size / 6 )
. padding ( . leading , 3 )
2024-07-24 00:11:42 +07:00
. offset ( x : 6 )
}
2024-07-28 21:53:21 +01:00
func smallContentPreviewVoice ( size : CGFloat , _ view : @ escaping ( ) -> some View ) -> some View {
view ( )
. frame ( height : voiceMessageSizeBasedOnSquareSize ( size ) )
. padding ( . vertical , size / 6 )
2024-07-24 00:11:42 +07:00
. padding ( . leading , 8 )
}
2024-07-28 21:53:21 +01:00
func smallContentPreviewFile ( size : CGFloat , _ view : @ escaping ( ) -> some View ) -> some View {
view ( )
. frame ( width : size , height : size )
. padding ( . vertical , size / 7 )
2024-07-24 00:11:42 +07:00
. padding ( . leading , 5 )
}
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
}
2024-07-24 00:11:42 +07:00
private struct ActiveContentPreview : Equatable {
var chat : Chat
var ci : ChatItem
var mc : MsgContent
static func = = ( lhs : ActiveContentPreview , rhs : ActiveContentPreview ) -> Bool {
lhs . chat . id = = rhs . chat . id && lhs . ci . id = = rhs . ci . id && lhs . mc = = rhs . mc
}
}
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 : [ ]
2023-10-26 10:32:11 +04:00
) , progressByTimeout : Binding . constant ( false ) )
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 ) ) ]
2023-10-26 10:32:11 +04:00
) , progressByTimeout : Binding . constant ( false ) )
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 )
2023-10-26 10:32:11 +04:00
) , progressByTimeout : Binding . constant ( false ) )
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 ) ) ]
2023-10-26 10:32:11 +04:00
) , progressByTimeout : Binding . constant ( false ) )
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 )
2023-10-26 10:32:11 +04:00
) , progressByTimeout : Binding . constant ( false ) )
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 )
2023-10-26 10:32:11 +04:00
) , progressByTimeout : Binding . constant ( false ) )
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
}
}