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 { struct CIFileView: View {
@EnvironmentObject var m: ChatModel @EnvironmentObject var m: ChatModel
@EnvironmentObject var theme: AppTheme @EnvironmentObject var theme: AppTheme
@StateObject var documentController = DocumentController()
let file: CIFile? let file: CIFile?
let edited: Bool let edited: Bool
var smallViewSize: CGFloat? var smallViewSize: CGFloat?
@ -114,7 +116,7 @@ struct CIFileView: View {
case .rcvComplete: case .rcvComplete:
logger.debug("CIFileView fileAction - in .rcvComplete") logger.debug("CIFileView fileAction - in .rcvComplete")
if let fileSource = getLoadedFileSource(file) { if let fileSource = getLoadedFileSource(file) {
saveCryptoFile(fileSource) saveOrOpenCryptoFile(fileSource, documentController)
} }
case let .rcvError(rcvFileError): case let .rcvError(rcvFileError):
logger.debug("CIFileView fileAction - in .rcvError") logger.debug("CIFileView fileAction - in .rcvError")
@ -125,12 +127,12 @@ struct CIFileView: View {
case .sndStored: case .sndStored:
logger.debug("CIFileView fileAction - in .sndStored") logger.debug("CIFileView fileAction - in .sndStored")
if file.fileProtocol == .local, let fileSource = getLoadedFileSource(file) { if file.fileProtocol == .local, let fileSource = getLoadedFileSource(file) {
saveCryptoFile(fileSource) saveOrOpenCryptoFile(fileSource, documentController)
} }
case .sndComplete: case .sndComplete:
logger.debug("CIFileView fileAction - in .sndComplete") logger.debug("CIFileView fileAction - in .sndComplete")
if let fileSource = getLoadedFileSource(file) { if let fileSource = getLoadedFileSource(file) {
saveCryptoFile(fileSource) saveOrOpenCryptoFile(fileSource, documentController)
} }
case let .sndError(sndFileError): case let .sndError(sndFileError):
logger.debug("CIFileView fileAction - in .sndError") 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 { func fileSizeValid(_ file: CIFile?) -> Bool {
if let file = file { if let file = file {
return file.fileSize <= getMaxFileSize(file.fileProtocol) return file.fileSize <= getMaxFileSize(file.fileProtocol)
@ -232,7 +252,7 @@ func fileSizeValid(_ file: CIFile?) -> Bool {
return false return false
} }
func saveCryptoFile(_ fileSource: CryptoFile) { func saveOrOpenCryptoFile(_ fileSource: CryptoFile, _ documentController: DocumentController? = nil) {
if let cfArgs = fileSource.cryptoArgs { if let cfArgs = fileSource.cryptoArgs {
let url = getAppFilePath(fileSource.filePath) let url = getAppFilePath(fileSource.filePath)
let tempUrl = getTempFilesDirectory().appendingPathComponent(fileSource.filePath) let tempUrl = getTempFilesDirectory().appendingPathComponent(fileSource.filePath)
@ -240,8 +260,12 @@ func saveCryptoFile(_ fileSource: CryptoFile) {
do { do {
try decryptCryptoFile(fromPath: url.path, cryptoArgs: cfArgs, toPath: tempUrl.path) try decryptCryptoFile(fromPath: url.path, cryptoArgs: cfArgs, toPath: tempUrl.path)
await MainActor.run { await MainActor.run {
showShareSheet(items: [tempUrl]) { if let documentController {
removeFile(tempUrl) documentController.presentDocument(url: tempUrl)
} else {
showShareSheet(items: [tempUrl]) {
removeFile(tempUrl)
}
} }
} }
} catch { } catch {
@ -252,7 +276,11 @@ func saveCryptoFile(_ fileSource: CryptoFile) {
} }
} else { } else {
let url = getAppFilePath(fileSource.filePath) let url = getAppFilePath(fileSource.filePath)
showShareSheet(items: [url]) if let documentController {
documentController.presentDocument(url: url)
} else {
showShareSheet(items: [url])
}
} }
} }

View file

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