Advertisement
lcolli98

ContentViewController

Feb 11th, 2020
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.05 KB | None | 0 0
  1. //
  2. //  ContentViewController.swift
  3. //  AeroBuddy
  4. //
  5. //  Created by Kieran Samuel Cross on 25/12/2019.
  6. //  Copyright © 2019 Luke Collister. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ContentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  12.     @IBOutlet weak var topicTitle: UINavigationItem!
  13.     @IBOutlet weak var leadingConstraint: NSLayoutConstraint!
  14.     @IBOutlet var tableView: UITableView!
  15.     @IBOutlet weak var civ: ContentItemView!
  16.    
  17.     var menuShowing = false
  18.     var topic: Topic?
  19.     let dispatchGroup = DispatchGroup()
  20.    
  21.     func setup(){
  22.         topicTitle.title = topic!.title
  23.     }
  24.  
  25.     override func viewDidLoad() {
  26.         super.viewDidLoad()
  27.         print( "get it all" )
  28.         parseJSON()
  29.        
  30.         dispatchGroup.notify(queue: .main ){
  31.             self.civ.contentList = self.topic!.content!
  32.             self.civ.reload()
  33.             self.tableView.reloadData()
  34.             print( "Done getting all" )
  35.         }
  36.  
  37.     }
  38.    
  39.     @IBAction func openMenu( _ sender: Any ) {
  40.         if( menuShowing ){
  41.             leadingConstraint.constant = -240
  42.             UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseOut, animations:
  43.             {
  44.                 self.view.layoutIfNeeded()
  45.             })
  46.         }else{
  47.             leadingConstraint.constant = 0
  48.             UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
  49.             {
  50.                 self.view.layoutIfNeeded()
  51.             })
  52.         }
  53.         menuShowing = !menuShowing
  54.         print( topic!.content!.count )
  55.         print( tableView.numberOfRows(inSection: 0))
  56.         tableView.reloadData()
  57.     }
  58.    
  59.     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  60.         return topic!.content!.count
  61.     }
  62.    
  63.     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  64.         let cell = tableView.dequeueReusableCell(withIdentifier: "MenuItem", for: indexPath) as! MenuItemCell
  65.         let selCont = topic!.content![indexPath.row]
  66.         cell.label.text = selCont.title
  67.         return cell
  68.     }
  69.    
  70.     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  71.         let selCont = topic!.content![indexPath.row]
  72.         civ.currentContent = indexPath.row
  73.         civ.reload()
  74.         openMenu( self )
  75.         print( selCont )
  76.     }
  77.    
  78.     func parseJSON() {
  79.         for index in topic!.content!.indices {
  80.             dispatchGroup.enter()
  81.             let url = URL(string: "\(ServHandler.connectProtocol)://\(ServHandler.baseURL)/content/get/\(topic!.content![index].id)")
  82.             let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
  83.                
  84.                 // Check for errors
  85.                 guard error == nil else {
  86.                     print("There was an error")
  87.                     return
  88.                 }
  89.                
  90.                 // Check for no data
  91.                 guard let content = data else {
  92.                     print("There is no data to show")
  93.                     return
  94.                 }
  95.                
  96.                 // Decode the data, which should return the video url and encryption key
  97.                 let decoder = JSONDecoder()
  98.                
  99.                 do {
  100.                     let jsonOutput = try decoder.decode(Video.self, from: content)
  101.                     self.topic!.content![index].encKey = jsonOutput.encryption_key
  102.                     self.topic!.content![index].content = jsonOutput.content
  103.                     print( self.topic!.content![index].title )
  104.                     //if( index == self.topic!.content!.indices.last ){
  105.                         self.dispatchGroup.leave()
  106.                     //}
  107.                     return
  108.                 }
  109.                 catch {
  110.                     print(error.localizedDescription)
  111.                     print(error)
  112.                     return
  113.                 }
  114.             }
  115.             task.resume()
  116.         }
  117.  
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement