This commit is contained in:
Stanislav Dmitrenko 2025-06-28 00:31:20 +00:00 committed by GitHub
commit 0fdeee3190
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 8 deletions

View file

@ -12,6 +12,8 @@ import SimpleXChat
struct CIFileView: View {
@EnvironmentObject var m: ChatModel
@EnvironmentObject var theme: AppTheme
@StateObject var documentController = DocumentController()
let file: CIFile?
let edited: Bool
var smallViewSize: CGFloat?
@ -114,7 +116,7 @@ struct CIFileView: View {
case .rcvComplete:
logger.debug("CIFileView fileAction - in .rcvComplete")
if let fileSource = getLoadedFileSource(file) {
saveCryptoFile(fileSource)
saveOrOpenCryptoFile(fileSource, documentController)
}
case let .rcvError(rcvFileError):
logger.debug("CIFileView fileAction - in .rcvError")
@ -125,12 +127,12 @@ struct CIFileView: View {
case .sndStored:
logger.debug("CIFileView fileAction - in .sndStored")
if file.fileProtocol == .local, let fileSource = getLoadedFileSource(file) {
saveCryptoFile(fileSource)
saveOrOpenCryptoFile(fileSource, documentController)
}
case .sndComplete:
logger.debug("CIFileView fileAction - in .sndComplete")
if let fileSource = getLoadedFileSource(file) {
saveCryptoFile(fileSource)
saveOrOpenCryptoFile(fileSource, documentController)
}
case let .sndError(sndFileError):
logger.debug("CIFileView fileAction - in .sndError")
@ -225,6 +227,24 @@ struct CIFileView: View {
}
}
class DocumentController: NSObject, ObservableObject, UIDocumentInteractionControllerDelegate {
let controller = UIDocumentInteractionController()
func presentDocument(url: URL) {
controller.delegate = self
controller.url = url
controller.presentOptionsMenu(from: .zero, in: rootController().view, animated: true)
// controller.presentPreview(animated: true)
}
private func rootController() -> UIViewController {
(UIApplication.shared.connectedScenes.compactMap { ($0 as! UIWindowScene).keyWindow }.last?.rootViewController)!
}
func documentInteractionControllerViewControllerForPreview(_: UIDocumentInteractionController) -> UIViewController {
return rootController()
}
}
func fileSizeValid(_ file: CIFile?) -> Bool {
if let file = file {
return file.fileSize <= getMaxFileSize(file.fileProtocol)
@ -232,7 +252,7 @@ func fileSizeValid(_ file: CIFile?) -> Bool {
return false
}
func saveCryptoFile(_ fileSource: CryptoFile) {
func saveOrOpenCryptoFile(_ fileSource: CryptoFile, _ documentController: DocumentController? = nil) {
if let cfArgs = fileSource.cryptoArgs {
let url = getAppFilePath(fileSource.filePath)
let tempUrl = getTempFilesDirectory().appendingPathComponent(fileSource.filePath)
@ -240,10 +260,14 @@ func saveCryptoFile(_ fileSource: CryptoFile) {
do {
try decryptCryptoFile(fromPath: url.path, cryptoArgs: cfArgs, toPath: tempUrl.path)
await MainActor.run {
if let documentController {
documentController.presentDocument(url: tempUrl)
} else {
showShareSheet(items: [tempUrl]) {
removeFile(tempUrl)
}
}
}
} catch {
await MainActor.run {
AlertManager.shared.showAlertMsg(title: "Error decrypting file", message: "Error: \(error.localizedDescription)")
@ -252,9 +276,13 @@ func saveCryptoFile(_ fileSource: CryptoFile) {
}
} else {
let url = getAppFilePath(fileSource.filePath)
if let documentController {
documentController.presentDocument(url: url)
} else {
showShareSheet(items: [url])
}
}
}
func showFileErrorAlert(_ err: FileError, temporary: Bool = false) {
let title: String = if temporary {

View file

@ -1961,7 +1961,7 @@ struct ChatView: View {
func saveButton(file: CryptoFile) -> Button<some View> {
Button {
saveCryptoFile(file)
saveOrOpenCryptoFile(file)
} label: {
Label(
NSLocalizedString("Save", comment: "chat item action"),