2 // NewsDetailsViewController.swift
5 // Created by Pawel Dabrowski on 15/09/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
11 class NewsDetailsViewController: WLViewController {
13 @IBOutlet weak var scrollView: UIScrollView!
14 @IBOutlet weak var backButton: UIButton!
15 @IBOutlet weak var headerView: UIView!
16 @IBOutlet weak var titleLabel: UILabel!
18 @IBOutlet weak var whenTitleLabel: UILabel!
19 @IBOutlet weak var whenDescLabel: UILabel!
20 @IBOutlet weak var whereTitleLabel: UILabel!
21 @IBOutlet weak var whereDescLabel: UILabel!
22 @IBOutlet weak var descLabel: UILabel!
24 @IBOutlet weak var shareButton: UIButton!
25 @IBOutlet weak var galleryCollectionView: UICollectionView!
26 @IBOutlet weak var pageControl: UIPageControl!
27 @IBOutlet weak var headerViewHeightLayoutConstraint: NSLayoutConstraint!
29 private var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
31 var imageFullScreen = false
32 var countDidLayoutSubviews = 0
33 var collectionViewInitialized = false
34 var newsModel: NewsModel!
35 var smallHeaderHeight: CGFloat = 200
37 open override var preferredStatusBarStyle : UIStatusBarStyle {
41 static func instance(newsModel: NewsModel) -> NewsDetailsViewController {
42 let controller = NewsDetailsViewController.instance()
43 controller.newsModel = newsModel
47 public var currentPage: Int {
49 return Int(galleryCollectionView.contentOffset.x / galleryCollectionView.frame.size.width)
53 override func viewDidLoad() {
56 title = newsModel.title
57 smallHeaderHeight = 44 + UIApplication.shared.statusBarFrame.size.height
60 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
61 self.setupCollectionViewIfNeeded()
63 shareButton.layer.cornerRadius = 21
64 shareButton.addDropShadow()
66 titleLabel.text = newsModel.title
67 whenTitleLabel.text = "news_when".localized
68 whenDescLabel.text = newsModel.time
69 whereTitleLabel.text = "news_where".localized
70 whereDescLabel.text = newsModel.place
72 if let htmlString = try? NSAttributedString(html: newsModel.body){
73 descLabel.attributedText = htmlString
76 descLabel.text = newsModel.body
80 func updatePageControl() {
81 pageControl.currentPage = currentPage
84 private func setupCollectionViewIfNeeded(){
85 if collectionViewInitialized == false{
90 private func setupCollectionView(){// -> UICollectionView {
92 collectionViewInitialized = true
94 flowLayout.scrollDirection = UICollectionViewScrollDirection.horizontal
95 flowLayout.minimumInteritemSpacing = 0
96 flowLayout.minimumLineSpacing = 0
98 let size = galleryCollectionView.bounds.size
99 flowLayout.itemSize = size
101 galleryCollectionView.collectionViewLayout = flowLayout
102 galleryCollectionView.register(UINib (nibName: "SimpleGalleryCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "SimpleGalleryCollectionViewCell")
103 galleryCollectionView.dataSource = self
104 galleryCollectionView.delegate = self
105 galleryCollectionView.backgroundColor = UIColor.black
106 galleryCollectionView.isPagingEnabled = true
108 galleryCollectionView.contentSize = CGSize(width: 1000.0, height: 1.0)
110 galleryCollectionView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(NewsDetailsViewController.collectionViewTapped)))
113 @objc func collectionViewTapped() {
114 guard newsModel.gallery_urls.count > 0 else{
118 navigationController?.present(GalleryViewController.instance(photos: newsModel.gallery_urls,startIndex: currentPage), animated: true, completion: nil)
121 @IBAction func shareButtonAction(_ sender: UIButton) {
122 if let url = newsModel?.url {
124 self.share(string: url, button: sender)
130 extension NewsDetailsViewController: UICollectionViewDataSource {
132 public func numberOfSections(in collectionView: UICollectionView) -> Int {
136 public func collectionView(_ imageCollectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
137 return newsModel.gallery_urls.count
140 public func collectionView(_ imageCollectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
142 let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "SimpleGalleryCollectionViewCell", for: indexPath) as! SimpleGalleryCollectionViewCell
143 cell.setup(imageUrlString: newsModel.gallery_urls[indexPath.row])
148 extension NewsDetailsViewController: UICollectionViewDelegate {
150 public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
152 // If the scroll animation ended, update the page control to reflect the current page we are on