nayooti 5 жил өмнө
parent
commit
5b387bc96d

+ 49 - 11
deltachat-ios/Controller/GalleryViewController.swift

@@ -1,11 +1,11 @@
-
 import UIKit
+import DcCore
 
 class GalleryViewController: UIViewController {
 
     private struct GallerySection {
-        let header: String
-        let msgId: [Int]
+        let header: String?
+        let msgIds: [Int]
     }
 
     private let mediaMessageIds: [Int]
@@ -14,6 +14,7 @@ class GalleryViewController: UIViewController {
 
     private lazy var grid: UICollectionView = {
         let layout = UICollectionViewFlowLayout()
+        layout.itemSize = CGSize(width: 50, height: 50)
         let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
         collection.dataSource = self
         collection.delegate = self
@@ -24,7 +25,7 @@ class GalleryViewController: UIViewController {
     init(mediaMessageIds: [Int]) {
         self.mediaMessageIds = mediaMessageIds
         super.init(nibName: nil, bundle: nil)
-        self.gridSections = processData(messageIds: mediaMessageIds)
+        self.gridSections = processData(msgIds: mediaMessageIds)
     }
 
     required init?(coder: NSCoder) {
@@ -34,9 +35,14 @@ class GalleryViewController: UIViewController {
     // MARK: - lifecycle
     override func viewDidLoad() {
         super.viewDidLoad()
+        setupSubviews()
+    }
+
+    override func viewWillAppear(_ animated: Bool) {
+        grid.reloadData()
     }
 
-    // MARK:  - setup
+    // MARK: - setup
     private func setupSubviews() {
         view.addSubview(grid)
         grid.translatesAutoresizingMaskIntoConstraints = false
@@ -48,8 +54,9 @@ class GalleryViewController: UIViewController {
     }
 
     // MARK: - data processing + update
-    private func processData(messageIds: [Int]) -> [GallerySection] {
-        return []
+    private func processData(msgIds: [Int]) -> [GallerySection] {
+        let section = GallerySection(header: nil, msgIds: msgIds)
+        return [section]
     }
 }
 
@@ -61,19 +68,50 @@ extension GalleryViewController: UICollectionViewDataSource, UICollectionViewDel
     }
 
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
-        return gridSections[section].msgId.count
+        return gridSections[section].msgIds.count
     }
 
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         let mediaCell = collectionView.dequeueReusableCell(withReuseIdentifier: MediaCell.reuseIdentifier, for: indexPath) as! MediaCell
-
+        let msg = DcMsg(id: mediaMessageIds[indexPath.row])
+        mediaCell.update(msg: msg)
         // cell update
         return mediaCell
     }
-
-
 }
 
 class MediaCell: UICollectionViewCell {
     static let reuseIdentifier = "media_cell"
+
+    var imageView: UIImageView = {
+        let view = UIImageView()
+        view.contentMode = .scaleAspectFill
+        view.clipsToBounds = true
+        return view
+    }()
+
+    override init(frame: CGRect) {
+        super.init(frame: frame)
+        setupSubviews()
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    private func setupSubviews() {
+        contentView.addSubview(imageView)
+        imageView.translatesAutoresizingMaskIntoConstraints = false
+        imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 0).isActive = true
+        imageView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
+        imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
+        imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true
+    }
+
+    func update(msg: DcMsg) {
+        guard let image = msg.image else {
+            return
+        }
+        imageView.image = image
+    }
 }