Advertisement
lcolli98

Untitled

Mar 29th, 2020
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.16 KB | None | 0 0
  1. //
  2. //  HomeViewController.swift
  3. //  AeroBuddy
  4. //
  5. //  Created by Luke Collister on 29/11/2019.
  6. //  Copyright © 2019 Luke Collister. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SafariServices
  11.  
  12. class HomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SFSafariViewControllerDelegate {
  13.    
  14.     var newsArray = [News]()
  15.  
  16.     // MARK: - IB
  17.     @IBAction func openURL(_ sender: Any) {
  18.         // Check if website exists
  19.         guard let url = URL(string: "http://www.3lcare.co.uk/aerobuddy") else {
  20.             return
  21.         }
  22.  
  23.         let safariVC = SFSafariViewController(url: url)
  24.         safariVC.delegate = self
  25.         present(safariVC, animated: true, completion: nil)
  26.     }
  27.    
  28.    
  29.     // MARK: - SFSafariViewController
  30.     func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
  31.         controller.dismiss(animated: true, completion: nil)
  32.     }
  33.    
  34.    
  35.     // MARK: - UICollectionViewDataSource protocol
  36.     // Pull the data
  37.     func loadNewsData() {
  38.         DispatchQueue.global(qos: .background).async {
  39.             APIHandler.load(endpoint: .News, completion: {(data) in
  40.                 // Add news to an array of News
  41.                 for news in data {
  42.                     self.newsArray.append(news as! News)
  43.                 }
  44.                 print(self.newsArray[0].title)
  45.                 print(self.newsArray[0].content)
  46.             })
  47.         }
  48.     }
  49.    
  50.     // MARK: - Default Methods
  51.     override func viewDidLoad() {
  52.         super.viewDidLoad()
  53.        
  54.         //self.collectionView.delegate = self
  55.         loadNewsData()
  56.     }
  57.    
  58.    
  59.     // tell the collection view how many cells to make
  60.     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  61.         return self.newsArray.count
  62.     }
  63.  
  64.     // make a cell for each cell index path
  65.     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  66.         // get a reference to our storyboard cell
  67.         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! HomeCollectionViewCell
  68.  
  69.         // Use the outlet in our custom class to get a reference to the UILabel in the cell
  70.         cell.cellTitle.text = newsArray[indexPath.item].title
  71.         cell.cellDescription.text = newsArray[indexPath.item].content
  72.         let base64image = newsArray[indexPath.item].cover_image
  73.         let decodedData = NSData(base64Encoded: base64image, options: [])
  74.         if let data = decodedData {
  75.             let decodedimage = UIImage(data: data as Data)
  76.             cell.cellImage.image = decodedimage
  77.         } else {
  78.             print("error with decodedData")
  79.         }
  80.         print("INDEX PATH: ", indexPath.item)
  81.  
  82.         cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
  83.  
  84.         return cell
  85.     }
  86.  
  87.     // MARK: - UICollectionViewDelegate protocol
  88.     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  89.         // handle tap events
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement