Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // HomeViewController.swift
- // AeroBuddy
- //
- // Created by Luke Collister on 29/11/2019.
- // Copyright © 2019 Luke Collister. All rights reserved.
- //
- import UIKit
- import SafariServices
- class HomeViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, SFSafariViewControllerDelegate {
- var newsArray = [News]()
- // MARK: - IB
- @IBAction func openURL(_ sender: Any) {
- // Check if website exists
- guard let url = URL(string: "http://www.3lcare.co.uk/aerobuddy") else {
- return
- }
- let safariVC = SFSafariViewController(url: url)
- safariVC.delegate = self
- present(safariVC, animated: true, completion: nil)
- }
- // MARK: - SFSafariViewController
- func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
- controller.dismiss(animated: true, completion: nil)
- }
- // MARK: - UICollectionViewDataSource protocol
- // Pull the data
- func loadNewsData() {
- DispatchQueue.global(qos: .background).async {
- APIHandler.load(endpoint: .News, completion: {(data) in
- // Add news to an array of News
- for news in data {
- self.newsArray.append(news as! News)
- }
- print(self.newsArray[0].title)
- print(self.newsArray[0].content)
- })
- }
- }
- // MARK: - Default Methods
- override func viewDidLoad() {
- super.viewDidLoad()
- //self.collectionView.delegate = self
- loadNewsData()
- }
- // tell the collection view how many cells to make
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return self.newsArray.count
- }
- // make a cell for each cell index path
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- // get a reference to our storyboard cell
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath as IndexPath) as! HomeCollectionViewCell
- // Use the outlet in our custom class to get a reference to the UILabel in the cell
- cell.cellTitle.text = newsArray[indexPath.item].title
- cell.cellDescription.text = newsArray[indexPath.item].content
- let base64image = newsArray[indexPath.item].cover_image
- let decodedData = NSData(base64Encoded: base64image, options: [])
- if let data = decodedData {
- let decodedimage = UIImage(data: data as Data)
- cell.cellImage.image = decodedimage
- } else {
- print("error with decodedData")
- }
- print("INDEX PATH: ", indexPath.item)
- cell.backgroundColor = UIColor.cyan // make cell more visible in our example project
- return cell
- }
- // MARK: - UICollectionViewDelegate protocol
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- // handle tap events
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement