Browse Source

speedup opening of attach menu (#1900)

* speedup opening of attach menu

the check, if there were any webxdc apps ever is comparable expensive
on larger databases and if there are webxdc apps.
in this case, check only once (otherwise the checke is not that expensive).

* move loading to thread and do not mix virtual list with addItem()
bjoern 1 year ago
parent
commit
5f5ba210c6

+ 5 - 1
DcCore/DcCore/DC/Wrapper.swift

@@ -105,6 +105,7 @@ public class DcContext {
     var contextPointer: OpaquePointer?
     var contextPointer: OpaquePointer?
     public var lastWarningString: String = "" // temporary thing to get a grip on some weird errors
     public var lastWarningString: String = "" // temporary thing to get a grip on some weird errors
     public var maxConfigureProgress: Int = 0 // temporary thing to get a grip on some weird errors
     public var maxConfigureProgress: Int = 0 // temporary thing to get a grip on some weird errors
+    private var anyWebxdcSeen: Bool = false
 
 
     public init(contextPointer: OpaquePointer?, logger: Logger?) {
     public init(contextPointer: OpaquePointer?, logger: Logger?) {
         self.contextPointer = contextPointer
         self.contextPointer = contextPointer
@@ -273,7 +274,10 @@ public class DcContext {
     }
     }
 
 
     public func hasWebxdc(chatId: Int) -> Bool {
     public func hasWebxdc(chatId: Int) -> Bool {
-        return !getChatMedia(chatId: chatId, messageType: DC_MSG_WEBXDC, messageType2: 0, messageType3: 0).isEmpty
+        if !anyWebxdcSeen {
+            anyWebxdcSeen = !getChatMedia(chatId: chatId, messageType: DC_MSG_WEBXDC, messageType2: 0, messageType3: 0).isEmpty
+        }
+        return anyWebxdcSeen
     }
     }
 
 
     public func getAllMediaCount(chatId: Int) -> String {
     public func getAllMediaCount(chatId: Int) -> String {

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

@@ -1524,8 +1524,7 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
     }
     }
 
 
     private func showWebxdcSelector() {
     private func showWebxdcSelector() {
-        let msgIds = dcContext.getChatMedia(chatId: 0, messageType: DC_MSG_WEBXDC, messageType2: 0, messageType3: 0)
-        let webxdcSelector = WebxdcSelector(context: dcContext, mediaMessageIds: msgIds.reversed())
+        let webxdcSelector = WebxdcSelector(context: dcContext)
         webxdcSelector.delegate = self
         webxdcSelector.delegate = self
         let webxdcSelectorNavigationController = UINavigationController(rootViewController: webxdcSelector)
         let webxdcSelectorNavigationController = UINavigationController(rootViewController: webxdcSelector)
         if #available(iOS 15.0, *) {
         if #available(iOS 15.0, *) {

+ 9 - 20
deltachat-ios/Controller/WebxdcSelector.swift

@@ -11,9 +11,7 @@ class WebxdcSelector: UIViewController {
 
 
     private let dcContext: DcContext
     private let dcContext: DcContext
     // MARK: - data
     // MARK: - data
-    private var mediaMessageIds: [Int]
-    private var deduplicatedMessageHashes: [String: Int]
-    private var deduplicatedMessageIds: [Int]
+    private var deduplicatedMessageIds: [Int] = []
     private var items: [Int: GalleryItem] = [:]
     private var items: [Int: GalleryItem] = [:]
 
 
     // MARK: - subview specs
     // MARK: - subview specs
@@ -70,13 +68,9 @@ class WebxdcSelector: UIViewController {
         return mediaPicker
         return mediaPicker
     }()
     }()
 
 
-    init(context: DcContext, mediaMessageIds: [Int]) {
+    init(context: DcContext) {
         self.dcContext = context
         self.dcContext = context
-        self.mediaMessageIds = mediaMessageIds
-        self.deduplicatedMessageHashes = [:]
-        self.deduplicatedMessageIds = []
         super.init(nibName: nil, bundle: nil)
         super.init(nibName: nil, bundle: nil)
-        deduplicateWebxdcs()
     }
     }
 
 
     required init?(coder: NSCoder) {
     required init?(coder: NSCoder) {
@@ -90,13 +84,7 @@ class WebxdcSelector: UIViewController {
         title = String.localized("webxdc_apps")
         title = String.localized("webxdc_apps")
         navigationItem.setLeftBarButton(filesButton, animated: false)
         navigationItem.setLeftBarButton(filesButton, animated: false)
         navigationItem.setRightBarButton(cancelButton, animated: false)
         navigationItem.setRightBarButton(cancelButton, animated: false)
-        if mediaMessageIds.isEmpty {
-            emptyStateView.isHidden = false
-        }
-    }
-
-    override func viewWillAppear(_ animated: Bool) {
-        grid.reloadData()
+        deduplicateWebxdcs()
     }
     }
 
 
     override func viewWillLayoutSubviews() {
     override func viewWillLayoutSubviews() {
@@ -112,21 +100,21 @@ class WebxdcSelector: UIViewController {
         grid.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
         grid.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
         grid.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
         grid.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
         grid.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
         grid.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
-
         emptyStateView.addCenteredTo(parentView: view)
         emptyStateView.addCenteredTo(parentView: view)
     }
     }
 
 
     func deduplicateWebxdcs() {
     func deduplicateWebxdcs() {
+        var deduplicatedMessageHashes: [String: Int] = [:]
         DispatchQueue.global(qos: .userInteractive).async { [weak self] in
         DispatchQueue.global(qos: .userInteractive).async { [weak self] in
             guard let self = self else { return }
             guard let self = self else { return }
-            for id in self.mediaMessageIds {
+            let mediaMessageIds = dcContext.getChatMedia(chatId: 0, messageType: DC_MSG_WEBXDC, messageType2: 0, messageType3: 0).reversed()
+            for id in mediaMessageIds {
                 guard let filename = self.dcContext.getMessage(id: id).fileURL else { continue }
                 guard let filename = self.dcContext.getMessage(id: id).fileURL else { continue }
                 if let hash = try? NSData(contentsOf: filename).sha1() {
                 if let hash = try? NSData(contentsOf: filename).sha1() {
                     DispatchQueue.main.async {
                     DispatchQueue.main.async {
-                        if self.deduplicatedMessageHashes[hash] == nil {
-                            self.deduplicatedMessageHashes[hash] = id
+                        if deduplicatedMessageHashes[hash] == nil {
+                            deduplicatedMessageHashes[hash] = id
                             self.deduplicatedMessageIds.append(id)
                             self.deduplicatedMessageIds.append(id)
-                            self.grid.insertItems(at: [IndexPath(row: self.deduplicatedMessageIds.count - 1, section: 0)])
                         }
                         }
                     }
                     }
                 }
                 }
@@ -136,6 +124,7 @@ class WebxdcSelector: UIViewController {
                 if self.deduplicatedMessageIds.isEmpty {
                 if self.deduplicatedMessageIds.isEmpty {
                     self.emptyStateView.isHidden = false
                     self.emptyStateView.isHidden = false
                 }
                 }
+                self.grid.reloadData()
             }
             }
         }
         }
     }
     }