added iOS source code
[wl-app.git] / iOS / WolneLektury / Screens / NewsDetails / Gallery / GalleryViewController.swift
1 //
2 //  GalleryViewController.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 17/09/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10
11 class GalleryViewController: WLViewController {
12     
13     @IBOutlet weak var closeButton: UIButton!
14     
15     @IBOutlet weak var galleryCollectionView: UICollectionView!
16     
17     private var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
18     
19     var dataSource: [String]!
20     
21     var countDidLayoutSubviews = 0
22     var collectionViewInitialized = false
23     var startIndex: Int!
24     
25     class func instance(photos: [String], startIndex: Int) -> GalleryViewController{
26         
27         let controller = GalleryViewController.instance()
28         controller.dataSource = photos
29         controller.startIndex = startIndex
30         return controller
31     }
32     
33     override func viewDidLoad() {
34         super.viewDidLoad()
35         closeButton.tintColor = UIColor.white
36         closeButton.accessibilityLabel = "close".localized
37         closeButton.accessibilityHint = "close".localized
38     }
39     
40     @IBAction func closeButtonAction(_ sender: Any) {
41         dismiss(animated: true, completion: nil)
42     }
43     
44     override func viewDidLayoutSubviews() {
45         super.viewDidLayoutSubviews()
46         
47         countDidLayoutSubviews += 1
48         
49         if countDidLayoutSubviews == 2 && collectionViewInitialized == false{
50             setupCollectionViewIfNeeded()
51         }
52     }
53     
54     private func setupCollectionViewIfNeeded(){
55         if collectionViewInitialized == false{
56             setupCollectionView()
57         }
58     }
59     
60     open override var preferredStatusBarStyle : UIStatusBarStyle {
61         return .lightContent
62     }
63
64     private func setupCollectionView(){// -> UICollectionView {
65         // Set up flow layout
66         collectionViewInitialized = true
67         
68         flowLayout.scrollDirection = UICollectionViewScrollDirection.horizontal
69         flowLayout.minimumInteritemSpacing = 0
70         flowLayout.minimumLineSpacing = 0
71         
72         flowLayout.itemSize = galleryCollectionView.bounds.size
73         
74         galleryCollectionView.collectionViewLayout = flowLayout
75         
76         galleryCollectionView.register(GalleryCell.self, forCellWithReuseIdentifier: "GalleryCell")
77         galleryCollectionView.dataSource = self
78         galleryCollectionView.delegate = self
79         galleryCollectionView.backgroundColor = UIColor.black
80         galleryCollectionView.isPagingEnabled = true
81         
82         galleryCollectionView.contentSize = CGSize(width: 1000.0, height: 1.0)
83         
84         galleryCollectionView.scrollToItem(
85             at: IndexPath(row: startIndex, section: 0),
86             at: [],
87             animated: false)
88     }
89 }
90
91 extension GalleryViewController: UICollectionViewDataSource {
92     
93     public func numberOfSections(in collectionView: UICollectionView) -> Int {
94         return 1
95     }
96     
97     public func collectionView(_ imageCollectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
98         return dataSource.count
99     }
100     
101     public func collectionView(_ imageCollectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
102         
103         let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "GalleryCell", for: indexPath) as! GalleryCell
104         cell.url = dataSource[indexPath.row]
105         cell.setScrollEnabled(scrollEnabled: true)
106         return cell
107     }
108 }
109
110 // MARK: UICollectionViewDelegate Methods
111 extension GalleryViewController: UICollectionViewDelegate {
112     
113     public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
114         if let cell = cell as? GalleryCell {
115             cell.configureForNewImageUrl()
116         }
117     }
118     
119     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
120         
121     }
122     
123     public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
124         
125         // If the scroll animation ended, update the page control to reflect the current page we are on
126         //        updatePageControl()
127     }
128 }