2022-05-04 09:10:36 +04:00
//
// C I F i l e 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 J R o b e r t s o n 2 8 / 0 4 / 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-05-04 09:10:36 +04:00
struct CIFileView : View {
2023-10-31 09:44:57 +00:00
@ EnvironmentObject var m : ChatModel
2024-07-03 22:42:13 +01:00
@ EnvironmentObject var theme : AppTheme
2022-05-04 09:10:36 +04:00
let file : CIFile ?
let edited : Bool
2024-07-28 21:53:21 +01:00
var smallViewSize : CGFloat ?
2022-05-04 09:10:36 +04:00
var body : some View {
2024-07-28 21:53:21 +01:00
if smallViewSize != nil {
2024-07-24 00:11:42 +07:00
fileIndicator ( )
ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap (#5880)
* ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap
* fix voice recording
* fix video, accepting calls from chat, preference toggles in chat
* WIP message and meta
* handle links in attributed strings
* custom attribute for links to prevent race conditions with default tap handler
2025-05-10 14:37:45 +01:00
. simultaneousGesture ( TapGesture ( ) . onEnded ( fileAction ) )
2024-07-24 00:11:42 +07:00
} else {
let metaReserve = edited
? " "
: " "
ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap (#5880)
* ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap
* fix voice recording
* fix video, accepting calls from chat, preference toggles in chat
* WIP message and meta
* handle links in attributed strings
* custom attribute for links to prevent race conditions with default tap handler
2025-05-10 14:37:45 +01:00
HStack ( alignment : . bottom , spacing : 6 ) {
fileIndicator ( )
. padding ( . top , 5 )
. padding ( . bottom , 3 )
if let file = file {
let prettyFileSize = ByteCountFormatter . string ( fromByteCount : file . fileSize , countStyle : . binary )
VStack ( alignment : . leading , spacing : 2 ) {
Text ( file . fileName )
. lineLimit ( 1 )
. multilineTextAlignment ( . leading )
. foregroundColor ( theme . colors . onBackground )
Text ( prettyFileSize + metaReserve )
. font ( . caption )
. lineLimit ( 1 )
. multilineTextAlignment ( . leading )
. foregroundColor ( theme . colors . secondary )
2022-05-04 09:10:36 +04:00
}
ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap (#5880)
* ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap
* fix voice recording
* fix video, accepting calls from chat, preference toggles in chat
* WIP message and meta
* handle links in attributed strings
* custom attribute for links to prevent race conditions with default tap handler
2025-05-10 14:37:45 +01:00
} else {
Text ( metaReserve )
2022-05-04 09:10:36 +04:00
}
}
ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap (#5880)
* ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap
* fix voice recording
* fix video, accepting calls from chat, preference toggles in chat
* WIP message and meta
* handle links in attributed strings
* custom attribute for links to prevent race conditions with default tap handler
2025-05-10 14:37:45 +01:00
. padding ( . top , 4 )
. padding ( . bottom , 6 )
. padding ( . leading , 10 )
. padding ( . trailing , 12 )
. simultaneousGesture ( TapGesture ( ) . onEnded ( fileAction ) )
2024-07-24 00:11:42 +07:00
. disabled ( ! itemInteractive )
2022-05-04 09:10:36 +04:00
}
2023-03-24 15:20:15 +04:00
}
ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap (#5880)
* ios: fix XCode 16 regressions (tap not working on files, quotes, images, voice messages, etc.), open link previews on tap
* fix voice recording
* fix video, accepting calls from chat, preference toggles in chat
* WIP message and meta
* handle links in attributed strings
* custom attribute for links to prevent race conditions with default tap handler
2025-05-10 14:37:45 +01:00
@ inline ( __always )
2023-03-27 21:02:54 +04:00
private var itemInteractive : Bool {
2023-03-24 15:20:15 +04:00
if let file = file {
switch ( file . fileStatus ) {
2024-01-18 22:57:14 +07:00
case . sndStored : return file . fileProtocol = = . local
2023-03-24 15:20:15 +04:00
case . sndTransfer : return false
2024-05-31 20:20:49 +04:00
case . sndComplete : return true
2023-03-24 15:20:15 +04:00
case . sndCancelled : return false
2024-06-05 21:03:05 +04:00
case . sndError : return true
case . sndWarning : return true
2023-03-24 15:20:15 +04:00
case . rcvInvitation : return true
2023-03-29 15:48:00 +04:00
case . rcvAccepted : return true
2023-03-24 15:20:15 +04:00
case . rcvTransfer : return false
2024-05-20 17:49:19 +04:00
case . rcvAborted : return true
2023-03-24 15:20:15 +04:00
case . rcvComplete : return true
case . rcvCancelled : return false
2024-06-05 21:03:05 +04:00
case . rcvError : return true
case . rcvWarning : return true
2023-07-31 11:54:39 +04:00
case . invalid : return false
2023-03-24 15:20:15 +04:00
}
}
return false
2022-05-04 09:10:36 +04:00
}
2023-03-27 21:02:54 +04:00
private func fileAction ( ) {
2022-07-18 21:58:32 +04:00
logger . debug ( " CIFileView fileAction " )
2022-05-04 09:10:36 +04:00
if let file = file {
switch ( file . fileStatus ) {
2024-05-20 17:49:19 +04:00
case . rcvInvitation , . rcvAborted :
2024-04-19 20:21:35 +04:00
if fileSizeValid ( file ) {
2022-05-04 09:10:36 +04:00
Task {
2024-05-20 17:49:19 +04:00
logger . debug ( " CIFileView fileAction - in .rcvInvitation, .rcvAborted, in Task " )
2023-10-31 09:44:57 +00:00
if let user = m . currentUser {
2024-01-16 18:49:44 +07:00
await receiveFile ( user : user , fileId : file . fileId )
2023-02-02 16:09:36 +00:00
}
2022-05-04 09:10:36 +04:00
}
} else {
2023-03-28 22:20:06 +04:00
let prettyMaxFileSize = ByteCountFormatter . string ( fromByteCount : getMaxFileSize ( file . fileProtocol ) , countStyle : . binary )
2022-05-04 09:10:36 +04:00
AlertManager . shared . showAlertMsg (
title : " Large file! " ,
2022-05-06 21:10:32 +04:00
message : " Your contact sent a file that is larger than currently supported maximum size ( \( prettyMaxFileSize ) ). "
2022-05-04 09:10:36 +04:00
)
}
case . rcvAccepted :
2023-03-29 15:48:00 +04:00
switch file . fileProtocol {
case . xftp :
AlertManager . shared . showAlertMsg (
title : " Waiting for file " ,
message : " File will be received when your contact completes uploading it. "
)
case . smp :
2023-03-28 22:20:06 +04:00
AlertManager . shared . showAlertMsg (
title : " Waiting for file " ,
message : " File will be received when your contact is online, please wait or check later! "
)
2024-01-18 22:57:14 +07:00
case . local : ( )
2023-03-28 22:20:06 +04:00
}
2022-05-04 09:10:36 +04:00
case . rcvComplete :
2022-07-18 21:58:32 +04:00
logger . debug ( " CIFileView fileAction - in .rcvComplete " )
2023-09-07 11:28:37 +01:00
if let fileSource = getLoadedFileSource ( file ) {
saveCryptoFile ( fileSource )
2022-05-04 09:10:36 +04:00
}
2024-06-05 21:03:05 +04:00
case let . rcvError ( rcvFileError ) :
logger . debug ( " CIFileView fileAction - in .rcvError " )
2025-01-12 21:25:25 +00:00
showFileErrorAlert ( rcvFileError )
2024-06-05 21:03:05 +04:00
case let . rcvWarning ( rcvFileError ) :
logger . debug ( " CIFileView fileAction - in .rcvWarning " )
2025-01-12 21:25:25 +00:00
showFileErrorAlert ( rcvFileError , temporary : true )
2024-01-18 22:57:14 +07:00
case . sndStored :
logger . debug ( " CIFileView fileAction - in .sndStored " )
if file . fileProtocol = = . local , let fileSource = getLoadedFileSource ( file ) {
saveCryptoFile ( fileSource )
}
2024-05-31 20:20:49 +04:00
case . sndComplete :
logger . debug ( " CIFileView fileAction - in .sndComplete " )
if let fileSource = getLoadedFileSource ( file ) {
saveCryptoFile ( fileSource )
}
2024-06-05 21:03:05 +04:00
case let . sndError ( sndFileError ) :
logger . debug ( " CIFileView fileAction - in .sndError " )
2025-01-12 21:25:25 +00:00
showFileErrorAlert ( sndFileError )
2024-06-05 21:03:05 +04:00
case let . sndWarning ( sndFileError ) :
logger . debug ( " CIFileView fileAction - in .sndWarning " )
2025-01-12 21:25:25 +00:00
showFileErrorAlert ( sndFileError , temporary : true )
2022-05-04 09:10:36 +04:00
default : break
}
}
}
2023-03-27 21:02:54 +04:00
@ ViewBuilder private func fileIndicator ( ) -> some View {
2022-05-04 09:10:36 +04:00
if let file = file {
switch file . fileStatus {
2023-03-29 15:48:00 +04:00
case . sndStored :
switch file . fileProtocol {
case . xftp : progressView ( )
case . smp : fileIcon ( " doc.fill " )
2024-01-18 22:57:14 +07:00
case . local : fileIcon ( " doc.fill " )
2023-03-29 15:48:00 +04:00
}
2023-03-28 22:20:06 +04:00
case let . sndTransfer ( sndProgress , sndTotal ) :
switch file . fileProtocol {
case . xftp : progressCircle ( sndProgress , sndTotal )
case . smp : progressView ( )
2024-01-18 22:57:14 +07:00
case . local : EmptyView ( )
2023-03-28 22:20:06 +04:00
}
2022-05-06 21:10:32 +04:00
case . sndComplete : fileIcon ( " doc.fill " , innerIcon : " checkmark " , innerIconSize : 10 )
2022-05-04 09:10:36 +04:00
case . sndCancelled : fileIcon ( " doc.fill " , innerIcon : " xmark " , innerIconSize : 10 )
2023-04-18 12:48:36 +04:00
case . sndError : fileIcon ( " doc.fill " , innerIcon : " xmark " , innerIconSize : 10 )
2024-06-05 21:03:05 +04:00
case . sndWarning : fileIcon ( " doc.fill " , innerIcon : " exclamationmark.triangle.fill " , innerIconSize : 10 )
2022-05-04 09:10:36 +04:00
case . rcvInvitation :
2024-04-19 20:21:35 +04:00
if fileSizeValid ( file ) {
2024-07-03 22:42:13 +01:00
fileIcon ( " arrow.down.doc.fill " , color : theme . colors . primary )
2022-05-04 09:10:36 +04:00
} else {
fileIcon ( " doc.fill " , color : . orange , innerIcon : " exclamationmark " , innerIconSize : 12 )
}
case . rcvAccepted : fileIcon ( " doc.fill " , innerIcon : " ellipsis " , innerIconSize : 12 )
2023-03-28 22:20:06 +04:00
case let . rcvTransfer ( rcvProgress , rcvTotal ) :
if file . fileProtocol = = . xftp && rcvProgress < rcvTotal {
progressCircle ( rcvProgress , rcvTotal )
} else {
progressView ( )
}
2024-05-20 17:49:19 +04:00
case . rcvAborted :
2024-07-03 22:42:13 +01:00
fileIcon ( " doc.fill " , color : theme . colors . primary , innerIcon : " exclamationmark.arrow.circlepath " , innerIconSize : 12 )
2022-05-10 12:15:46 +04:00
case . rcvComplete : fileIcon ( " doc.fill " )
2022-05-04 09:10:36 +04:00
case . rcvCancelled : fileIcon ( " doc.fill " , innerIcon : " xmark " , innerIconSize : 10 )
2023-04-18 12:48:36 +04:00
case . rcvError : fileIcon ( " doc.fill " , innerIcon : " xmark " , innerIconSize : 10 )
2024-06-05 21:03:05 +04:00
case . rcvWarning : fileIcon ( " doc.fill " , innerIcon : " exclamationmark.triangle.fill " , innerIconSize : 10 )
2023-07-31 11:54:39 +04:00
case . invalid : fileIcon ( " doc.fill " , innerIcon : " questionmark " , innerIconSize : 10 )
2022-05-04 09:10:36 +04:00
}
} else {
fileIcon ( " doc.fill " )
}
}
2023-03-27 21:02:54 +04:00
private func fileIcon ( _ icon : String , color : Color = Color ( uiColor : . tertiaryLabel ) , innerIcon : String ? = nil , innerIconSize : CGFloat ? = nil ) -> some View {
2024-07-28 21:53:21 +01:00
let size = smallViewSize ? ? 30
return ZStack ( alignment : . center ) {
2022-05-04 09:10:36 +04:00
Image ( systemName : icon )
. resizable ( )
. aspectRatio ( contentMode : . fit )
2024-07-28 21:53:21 +01:00
. frame ( width : size , height : size )
2022-05-04 09:10:36 +04:00
. foregroundColor ( color )
if let innerIcon = innerIcon ,
2024-07-28 21:53:21 +01:00
let innerIconSize = innerIconSize , ( smallViewSize = = nil || file ? . showStatusIconInSmallView = = true ) {
2022-05-04 09:10:36 +04:00
Image ( systemName : innerIcon )
. resizable ( )
. aspectRatio ( contentMode : . fit )
. frame ( maxHeight : 16 )
. frame ( width : innerIconSize , height : innerIconSize )
. foregroundColor ( . white )
2024-07-28 21:53:21 +01:00
. padding ( . top , size / 2.5 )
2022-05-04 09:10:36 +04:00
}
}
}
2023-03-27 21:02:54 +04:00
2023-03-28 22:20:06 +04:00
private func progressView ( ) -> some View {
ProgressView ( ) . frame ( width : 30 , height : 30 )
}
2023-03-27 21:02:54 +04:00
private func progressCircle ( _ progress : Int64 , _ total : Int64 ) -> some View {
Circle ( )
. trim ( from : 0 , to : Double ( progress ) / Double ( total ) )
. stroke (
2023-03-29 15:48:00 +04:00
Color ( uiColor : . tertiaryLabel ) ,
2023-03-27 21:02:54 +04:00
style : StrokeStyle ( lineWidth : 3 )
)
. rotationEffect ( . degrees ( - 90 ) )
. frame ( width : 30 , height : 30 )
}
2022-05-04 09:10:36 +04:00
}
2024-04-19 20:21:35 +04:00
func fileSizeValid ( _ file : CIFile ? ) -> Bool {
if let file = file {
return file . fileSize <= getMaxFileSize ( file . fileProtocol )
}
return false
}
2023-09-07 11:28:37 +01:00
func saveCryptoFile ( _ fileSource : CryptoFile ) {
if let cfArgs = fileSource . cryptoArgs {
let url = getAppFilePath ( fileSource . filePath )
let tempUrl = getTempFilesDirectory ( ) . appendingPathComponent ( fileSource . filePath )
Task {
do {
try decryptCryptoFile ( fromPath : url . path , cryptoArgs : cfArgs , toPath : tempUrl . path )
await MainActor . run {
showShareSheet ( items : [ tempUrl ] ) {
removeFile ( tempUrl )
}
}
} catch {
await MainActor . run {
AlertManager . shared . showAlertMsg ( title : " Error decrypting file " , message : " Error: \( error . localizedDescription ) " )
}
}
}
} else {
let url = getAppFilePath ( fileSource . filePath )
showShareSheet ( items : [ url ] )
}
}
2025-01-12 21:25:25 +00:00
func showFileErrorAlert ( _ err : FileError , temporary : Bool = false ) {
let title : String = if temporary {
NSLocalizedString ( " Temporary file error " , comment : " file error alert title " )
} else {
NSLocalizedString ( " File error " , comment : " file error alert title " )
}
if let btn = err . moreInfoButton {
showAlert ( title , message : err . errorInfo ) {
[
okAlertAction ,
UIAlertAction ( title : NSLocalizedString ( " How it works " , comment : " alert button " ) , style : . default , handler : { _ in
UIApplication . shared . open ( contentModerationPostLink )
} )
]
}
} else {
showAlert ( title , message : err . errorInfo )
}
}
2022-05-04 09:10:36 +04:00
struct CIFileView_Previews : PreviewProvider {
static var previews : some View {
2025-05-04 21:27:20 +00:00
let im = ItemsModel . shared
2022-05-31 07:55:13 +01:00
let sentFile : ChatItem = ChatItem (
2022-05-04 09:10:36 +04:00
chatDir : . directSnd ,
2023-07-28 13:16:52 +04:00
meta : CIMeta . getSample ( 1 , . now , " " , . sndSent ( sndProgress : . complete ) , itemEdited : true ) ,
2022-05-04 09:10:36 +04:00
content : . sndMsgContent ( msgContent : . file ( " " ) ) ,
quotedItem : nil ,
2022-05-06 21:10:32 +04:00
file : CIFile . getSample ( fileStatus : . sndComplete )
2022-05-04 09:10:36 +04:00
)
let fileChatItemWtFile = ChatItem (
chatDir : . directRcv ,
2023-02-09 15:10:35 +04:00
meta : CIMeta . getSample ( 1 , . now , " " , . rcvRead ) ,
2022-05-04 09:10:36 +04:00
content : . rcvMsgContent ( msgContent : . file ( " " ) ) ,
quotedItem : nil ,
file : nil
)
2022-11-24 21:18:28 +04:00
Group {
2025-05-15 15:04:03 +00:00
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : sentFile , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : ChatItem . getFileMsgContentSample ( ) , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : ChatItem . getFileMsgContentSample ( fileName : " some_long_file_name_here " , fileStatus : . rcvInvitation ) , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : ChatItem . getFileMsgContentSample ( fileStatus : . rcvAccepted ) , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : ChatItem . getFileMsgContentSample ( fileStatus : . rcvTransfer ( rcvProgress : 7 , rcvTotal : 10 ) ) , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : ChatItem . getFileMsgContentSample ( fileStatus : . rcvCancelled ) , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : ChatItem . getFileMsgContentSample ( fileSize : 1_000_000_000 , fileStatus : . rcvInvitation ) , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : ChatItem . getFileMsgContentSample ( text : " Hello there " , fileStatus : . rcvInvitation ) , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : ChatItem . getFileMsgContentSample ( text : " 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. " , fileStatus : . rcvInvitation ) , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
ChatItemView ( chat : Chat . sampleData , im : im , chatItem : fileChatItemWtFile , scrollToItem : { _ in } , scrollToItemId : Binding . constant ( nil ) )
2022-05-04 09:10:36 +04:00
}
2024-10-15 10:58:54 +03:00
. environment ( \ . revealed , false )
2022-05-04 09:10:36 +04:00
. previewLayout ( . fixed ( width : 360 , height : 360 ) )
}
}