Browse Source

did select shows preview

nayooti 5 years ago
parent
commit
51dd9903f9

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

@@ -132,6 +132,7 @@
 		8B6D425BC604F7C43B65D436 /* Pods_deltachat_ios.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6241BE1534A653E79AD5D01D /* Pods_deltachat_ios.framework */; };
 		AE0AA952247800E700D42A7F /* GalleryCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0AA951247800E700D42A7F /* GalleryCell.swift */; };
 		AE0AA9542478010D00D42A7F /* GallerySectionHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0AA9532478010D00D42A7F /* GallerySectionHeader.swift */; };
+		AE0AA9562478191900D42A7F /* GridCollectionViewFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0AA9552478191900D42A7F /* GridCollectionViewFlowLayout.swift */; };
 		AE0D26FD1FB1FE88002FAFCE /* ChatListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */; };
 		AE18F294228C602A0007B1BE /* SecuritySettingsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE18F293228C602A0007B1BE /* SecuritySettingsController.swift */; };
 		AE19887523EB264000B4CD5F /* HelpViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE19887423EB264000B4CD5F /* HelpViewController.swift */; };
@@ -414,6 +415,7 @@
 		A8615D4600859851E53CAA9C /* Pods-deltachat-ios.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-deltachat-ios.release.xcconfig"; path = "Pods/Target Support Files/Pods-deltachat-ios/Pods-deltachat-ios.release.xcconfig"; sourceTree = "<group>"; };
 		AE0AA951247800E700D42A7F /* GalleryCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryCell.swift; sourceTree = "<group>"; };
 		AE0AA9532478010D00D42A7F /* GallerySectionHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GallerySectionHeader.swift; sourceTree = "<group>"; };
+		AE0AA9552478191900D42A7F /* GridCollectionViewFlowLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GridCollectionViewFlowLayout.swift; sourceTree = "<group>"; };
 		AE0D26FC1FB1FE88002FAFCE /* ChatListController.swift */ = {isa = PBXFileReference; indentWidth = 4; lastKnownFileType = sourcecode.swift; path = ChatListController.swift; sourceTree = "<group>"; tabWidth = 4; };
 		AE18F293228C602A0007B1BE /* SecuritySettingsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecuritySettingsController.swift; sourceTree = "<group>"; };
 		AE19887423EB264000B4CD5F /* HelpViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HelpViewController.swift; sourceTree = "<group>"; };
@@ -934,6 +936,7 @@
 				AE1988A423EB2FBA00B4CD5F /* Errors.swift */,
 				AEFBE23023FF09B20045327A /* TypeAlias.swift */,
 				307D822D241669C7006D2490 /* LocationManager.swift */,
+				AE0AA9552478191900D42A7F /* GridCollectionViewFlowLayout.swift */,
 			);
 			path = Helper;
 			sourceTree = "<group>";
@@ -1416,6 +1419,7 @@
 				308FEA52246ABA2700FCEAD6 /* FileMessageSizeCalculator.swift in Sources */,
 				305961F52346125100C80F33 /* TypingIndicatorCell.swift in Sources */,
 				305961FF2346125100C80F33 /* AvatarView.swift in Sources */,
+				AE0AA9562478191900D42A7F /* GridCollectionViewFlowLayout.swift in Sources */,
 				3059620C2346125100C80F33 /* MessageSizeCalculator.swift in Sources */,
 				305962042346125100C80F33 /* MessagesCollectionViewLayoutAttributes.swift in Sources */,
 				AEA0F6A124474146009F887B /* ProfileInfoViewController.swift in Sources */,

+ 26 - 67
deltachat-ios/Controller/GalleryViewController.swift

@@ -12,6 +12,7 @@ class GalleryViewController: UIViewController {
     private let mediaMessageIds: [Int]
     private var gridSections: [GallerySection] = []
 
+    // MARK: - subview specs
     private lazy var gridLayout: GridCollectionViewFlowLayout = {
         let layout = GridCollectionViewFlowLayout()
         layout.minimumLineSpacing = 10
@@ -20,7 +21,6 @@ class GalleryViewController: UIViewController {
         return layout
     }()
 
-    // MARK: - subview specs
     private lazy var grid: UICollectionView = {
         let collection = UICollectionView(frame: .zero, collectionViewLayout: gridLayout)
         collection.dataSource = self
@@ -36,7 +36,6 @@ class GalleryViewController: UIViewController {
         return collection
     }()
 
-    // MARK: - specs
     private let gridInsets: CGFloat = 10
 
     init(mediaMessageIds: [Int]) {
@@ -114,24 +113,29 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
         return UICollectionReusableView()
     }
 
+    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
+        let msgId = gridSections[indexPath.section].msgIds[indexPath.row]
+        showPreview(msgId: msgId)
+    }
+
     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
         return CGSize(width: collectionView.frame.width - 2 * gridInsets, height: 32)
     }
 }
 
+// MARK: - update layout
 extension GalleryViewController {
     private func reloadCollectionViewLayout() {
 
         // columns specification
         let phonePortrait = 2
         let phoneLandscape = 3
-        let padPortrait = 3
-        let padLandscape = 5
+        let padPortrait = 4
+        let padLandscape = 6
 
         let orientation = UIDevice.current.orientation
         let deviceType = UIDevice.current.userInterfaceIdiom
 
-
         var gridDisplay: GridDisplay?
         if deviceType == .phone {
             if orientation.isPortrait {
@@ -155,7 +159,23 @@ extension GalleryViewController {
         let containerWidth = view.bounds.width - view.safeAreaInsets.left - view.safeAreaInsets.right - 2 * gridInsets
         gridLayout.containerWidth = containerWidth
     }
- }
+}
+
+// MARK: - coordinator
+extension GalleryViewController {
+    func showPreview(msgId: Int) {
+        let msg = DcMsg(id: msgId)
+        guard let url = msg.fileURL, let index = mediaMessageIds.index(of: msgId) else {
+            return
+        }
+        let previousUrls: [URL] = msg.previousMediaURLs()
+        let nextUrls: [URL] = msg.nextMediaURLs()
+        // these are the files user will be able to swipe trough
+        let mediaUrls: [URL] = previousUrls + [url] + nextUrls
+        let previewController = PreviewController(currentIndex: index, urls: mediaUrls)
+        present(previewController, animated: true, completion: nil)
+    }
+}
 
 
 enum GridDisplay {
@@ -184,64 +204,3 @@ extension GridDisplay: Equatable {
     }
 }
 
-class GridCollectionViewFlowLayout: UICollectionViewFlowLayout {
-
-    var display: GridDisplay = .list {
-        didSet {
-            if display != oldValue {
-                self.invalidateLayout()
-            }
-        }
-    }
-
-    var containerWidth: CGFloat = 0.0 {
-        didSet {
-            if containerWidth != oldValue {
-                self.invalidateLayout()
-            }
-        }
-    }
-
-    var format: GridItemFormat = .square {
-        didSet {
-            self.invalidateLayout()
-        }
-    }
-
-    convenience init(display: GridDisplay, containerWidth: CGFloat, format: GridItemFormat) {
-        self.init()
-        self.display = display
-        self.containerWidth = containerWidth
-        self.format = format
-        self.configLayout()
-    }
-
-    private func configLayout() {
-        switch display {
-        case .grid(let column):
-            self.scrollDirection = .vertical
-            let spacing = CGFloat(column - 1) * minimumLineSpacing
-            let optimisedWidth = (containerWidth - spacing) / CGFloat(column)
-            let height = calculateHeight(width: optimisedWidth)
-            self.itemSize = CGSize(width: optimisedWidth, height: height) // keep as square
-        case .list:
-            self.scrollDirection = .vertical
-            let height = calculateHeight(width: containerWidth)
-            self.itemSize = CGSize(width: containerWidth, height: height)
-        }
-    }
-
-    private func calculateHeight(width: CGFloat) -> CGFloat {
-        switch format {
-        case .square:
-            return width
-        case .rect(let ratio):
-            return width * ratio
-        }
-    }
-
-    override func invalidateLayout() {
-        super.invalidateLayout()
-        self.configLayout()
-    }
-}

+ 63 - 0
deltachat-ios/Helper/GridCollectionViewFlowLayout.swift

@@ -0,0 +1,63 @@
+import UIKit
+
+class GridCollectionViewFlowLayout: UICollectionViewFlowLayout {
+
+    var display: GridDisplay = .list {
+        didSet {
+            if display != oldValue {
+                self.invalidateLayout()
+            }
+        }
+    }
+
+    var containerWidth: CGFloat = 0.0 {
+        didSet {
+            if containerWidth != oldValue {
+                self.invalidateLayout()
+            }
+        }
+    }
+
+    var format: GridItemFormat = .square {
+        didSet {
+            self.invalidateLayout()
+        }
+    }
+
+    convenience init(display: GridDisplay, containerWidth: CGFloat, format: GridItemFormat) {
+        self.init()
+        self.display = display
+        self.containerWidth = containerWidth
+        self.format = format
+        self.configLayout()
+    }
+
+    private func configLayout() {
+        switch display {
+        case .grid(let column):
+            self.scrollDirection = .vertical
+            let spacing = CGFloat(column - 1) * minimumLineSpacing
+            let optimisedWidth = (containerWidth - spacing) / CGFloat(column)
+            let height = calculateHeight(width: optimisedWidth)
+            self.itemSize = CGSize(width: optimisedWidth, height: height) // keep as square
+        case .list:
+            self.scrollDirection = .vertical
+            let height = calculateHeight(width: containerWidth)
+            self.itemSize = CGSize(width: containerWidth, height: height)
+        }
+    }
+
+    private func calculateHeight(width: CGFloat) -> CGFloat {
+        switch format {
+        case .square:
+            return width
+        case .rect(let ratio):
+            return width * ratio
+        }
+    }
+
+    override func invalidateLayout() {
+        super.invalidateLayout()
+        self.configLayout()
+    }
+}