Răsfoiți Sursa

close app selector after app selection and show app in draft area

cyberta 2 ani în urmă
părinte
comite
612eef4139

+ 6 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -23,6 +23,8 @@
 		3022E6BE22E8768800763272 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 3022E6C022E8768800763272 /* InfoPlist.strings */; };
 		30238CFB28A501C300EF14AC /* WebxdcSelector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30238CFA28A501C300EF14AC /* WebxdcSelector.swift */; };
 		30238CFD28A5028300EF14AC /* WebxdcGridCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30238CFC28A5028300EF14AC /* WebxdcGridCell.swift */; };
+		30238CFF28A5554C00EF14AC /* FileHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30238CFE28A5554C00EF14AC /* FileHelper.swift */; };
+		30238D0028A557E800EF14AC /* FileHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30238CFE28A5554C00EF14AC /* FileHelper.swift */; };
 		302589FF2452FA280086C1CD /* ShareAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 302589FE2452FA280086C1CD /* ShareAttachment.swift */; };
 		30260CA7238F02F700D8D52C /* MultilineTextFieldCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30260CA6238F02F700D8D52C /* MultilineTextFieldCell.swift */; };
 		302B84C6239676F0001C261F /* AvatarHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 30AC265E237F1807002A943F /* AvatarHelper.swift */; };
@@ -269,6 +271,7 @@
 		3022E6D322E876A100763272 /* uk */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = uk; path = uk.lproj/InfoPlist.strings; sourceTree = "<group>"; };
 		30238CFA28A501C300EF14AC /* WebxdcSelector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebxdcSelector.swift; sourceTree = "<group>"; };
 		30238CFC28A5028300EF14AC /* WebxdcGridCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebxdcGridCell.swift; sourceTree = "<group>"; };
+		30238CFE28A5554C00EF14AC /* FileHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileHelper.swift; sourceTree = "<group>"; };
 		302589FE2452FA280086C1CD /* ShareAttachment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareAttachment.swift; sourceTree = "<group>"; };
 		30260CA6238F02F700D8D52C /* MultilineTextFieldCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultilineTextFieldCell.swift; sourceTree = "<group>"; };
 		302B84C42396627F001C261F /* RelayHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayHelper.swift; sourceTree = "<group>"; };
@@ -981,6 +984,7 @@
 				21D6C9392606190600D0755A /* NotificationManager.swift */,
 				302D544F268B6B2300A8B271 /* MessageUtils.swift */,
 				3011E8042787365D00214221 /* KeychainManager.swift */,
+				30238CFE28A5554C00EF14AC /* FileHelper.swift */,
 			);
 			path = Helper;
 			sourceTree = "<group>";
@@ -1387,6 +1391,7 @@
 				3057029F24C6445000D84EFC /* EmptyStateLabel.swift in Sources */,
 				305702A224C6455400D84EFC /* TypeAlias.swift in Sources */,
 				308850A0282A914F00204623 /* DcMsg+Extension.swift in Sources */,
+				30238D0028A557E800EF14AC /* FileHelper.swift in Sources */,
 				30152C9D25A5D95400377714 /* MessageLabelDelegate.swift in Sources */,
 				30E8F2442449C64100CE2C90 /* ChatListCell.swift in Sources */,
 				30E8F2132447285600CE2C90 /* ShareViewController.swift in Sources */,
@@ -1541,6 +1546,7 @@
 				3080A028277DE12D00E74565 /* InputBarSendButton.swift in Sources */,
 				AE0AA958247834A400D42A7F /* Date+Extension.swift in Sources */,
 				307D822E241669C7006D2490 /* LocationManager.swift in Sources */,
+				30238CFF28A5554C00EF14AC /* FileHelper.swift in Sources */,
 				AE851AD0227DF50900ED86F0 /* GroupChatDetailViewController.swift in Sources */,
 				30FDB72124D838240066C48D /* BaseMessageCell.swift in Sources */,
 				7A451DB01FB1F84900177250 /* AppCoordinator.swift in Sources */,

+ 21 - 1
deltachat-ios/Chat/ChatViewController.swift

@@ -1486,7 +1486,8 @@ class ChatViewController: UITableViewController {
 
     private func showWebxdcSelector() {
         let msgIds = dcContext.getChatMedia(chatId: 0, messageType: DC_MSG_WEBXDC, messageType2: 0, messageType3: 0)
-        let webxdcSelector = WebxdcSelector(context: dcContext, mediaMessageIds: msgIds)
+        let webxdcSelector = WebxdcSelector(context: dcContext, mediaMessageIds: msgIds.reversed())
+        webxdcSelector.delegate = self
         navigationController?.present(webxdcSelector, animated: true)
     }
 
@@ -2398,3 +2399,22 @@ extension ChatViewController: ChatInputTextViewPasteDelegate {
         sendSticker(image)
     }
 }
+
+
+extension ChatViewController: WebxdcSelectorDelegate {
+    func onWebxdcSelected(msgId: Int) {
+        keepKeyboard = true
+        DispatchQueue.main.async { [weak self] in
+            guard let self = self else { return }
+            let message = self.dcContext.getMessage(id: msgId)
+            if let filename = message.fileURL {
+                let nsdata = NSData(contentsOf: filename)
+                guard let data = nsdata as? Data else { return }
+                let url = FileHelper.saveData(data: data, suffix: "xdc", directory: .cachesDirectory)
+                self.draft.setAttachment(viewType: DC_MSG_WEBXDC, path: url)
+                self.configureDraftArea(draft: self.draft)
+                self.focusInputTextView()
+            }
+        }
+    }
+}

+ 7 - 2
deltachat-ios/Controller/WebxdcSelector.swift

@@ -2,6 +2,10 @@ import UIKit
 import DcCore
 import QuickLook
 
+protocol WebxdcSelectorDelegate: AnyObject {
+    func onWebxdcSelected(msgId: Int)
+}
+
 class WebxdcSelector: UIViewController {
 
     private let dcContext: DcContext
@@ -11,6 +15,7 @@ class WebxdcSelector: UIViewController {
 
     // MARK: - subview specs
     private let gridDefaultSpacing: CGFloat = 5
+    weak var delegate: WebxdcSelectorDelegate?
 
     private lazy var gridLayout: GridCollectionViewFlowLayout = {
         let layout = GridCollectionViewFlowLayout()
@@ -128,9 +133,9 @@ extension WebxdcSelector: UICollectionViewDataSource, UICollectionViewDelegate {
 
     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
         let msgId = mediaMessageIds[indexPath.row]
-        // TODO: implement callback
+        delegate?.onWebxdcSelected(msgId: msgId)
         collectionView.deselectItem(at: indexPath, animated: true)
-        UIMenuController.shared.setMenuVisible(false, animated: true)
+        self.dismiss(animated: true, completion: nil)
     }
 }
 

+ 59 - 0
deltachat-ios/Helper/FileHelper.swift

@@ -0,0 +1,59 @@
+import Foundation
+
+public class FileHelper {
+    
+    // implementation is following Apple's recommendations
+    // https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html
+    public static func saveData(data: Data, name: String? = nil, suffix: String, directory: FileManager.SearchPathDirectory = .applicationSupportDirectory) -> String? {
+        var path: URL?
+
+        // ensure directory exists (application support dir doesn't exist per default)
+        let fileManager = FileManager.default
+        let urls = fileManager.urls(for: directory, in: .userDomainMask) as [URL]
+        guard let identifier = Bundle.main.bundleIdentifier else {
+            print("err: Could not find bundle identifier")
+            return nil
+        }
+        guard let directoryURL = urls.first else {
+            print("err: Could not find directory url for \(String(describing: directory)) in .userDomainMask")
+            return nil
+        }
+        var subdirectoryURL = directoryURL.appendingPathComponent(identifier)
+        do {
+            if !fileManager.fileExists(atPath: subdirectoryURL.path) {
+                try fileManager.createDirectory(at: subdirectoryURL, withIntermediateDirectories: true, attributes: nil)
+            }
+        } catch {
+            print("err: \(error.localizedDescription)")
+            return nil
+        }
+
+        // Opt out from iCloud backup
+        var resourceValues: URLResourceValues = URLResourceValues()
+        resourceValues.isExcludedFromBackup = true
+        do {
+            try subdirectoryURL.setResourceValues(resourceValues)
+        } catch {
+            print("err: \(error.localizedDescription)")
+            return nil
+        }
+
+        // add file name to path
+        if let name = name {
+            path = subdirectoryURL.appendingPathComponent("\(name).\(suffix)")
+        } else {
+            let timestamp = Double(Date().timeIntervalSince1970)
+            path = subdirectoryURL.appendingPathComponent("\(timestamp).\(suffix)")
+        }
+        guard let path = path else { return nil }
+
+        // write data
+        do {
+            try data.write(to: path)
+            return path.relativePath
+        } catch {
+            print("err: \(error.localizedDescription)")
+            return nil
+        }
+    }
+}

+ 1 - 1
deltachat-ios/Helper/ImageFormat.swift

@@ -73,7 +73,7 @@ extension ImageFormat {
            let data = image.sd_imageData() {
             let format = ImageFormat.get(from: data)
             if format != .unknown {
-                return ImageFormat.saveImage(data: data, name: name, suffix: format.rawValue, directory: directory)
+                return FileHelper.saveData(data: data, name: name, suffix: format.rawValue, directory: directory)
             }
         }
         let suffix = image.isTransparent() ? "png" : "jpg"