Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ContentViewController.swift
- // AeroBuddy
- //
- // Created by Kieran Samuel Cross on 25/12/2019.
- // Copyright © 2019 Luke Collister. All rights reserved.
- //
- import UIKit
- class ContentViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
- @IBOutlet weak var topicTitle: UINavigationItem!
- @IBOutlet weak var leadingConstraint: NSLayoutConstraint!
- @IBOutlet var tableView: UITableView!
- @IBOutlet weak var civ: ContentItemView!
- var menuShowing = false
- var topic: Topic?
- let dispatchGroup = DispatchGroup()
- func setup(){
- topicTitle.title = topic!.title
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- print( "get it all" )
- parseJSON()
- dispatchGroup.notify(queue: .main ){
- self.civ.contentList = self.topic!.content!
- self.civ.reload()
- self.tableView.reloadData()
- print( "Done getting all" )
- }
- }
- @IBAction func openMenu( _ sender: Any ) {
- if( menuShowing ){
- leadingConstraint.constant = -240
- UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseOut, animations:
- {
- self.view.layoutIfNeeded()
- })
- }else{
- leadingConstraint.constant = 0
- UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
- {
- self.view.layoutIfNeeded()
- })
- }
- menuShowing = !menuShowing
- print( topic!.content!.count )
- print( tableView.numberOfRows(inSection: 0))
- tableView.reloadData()
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return topic!.content!.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "MenuItem", for: indexPath) as! MenuItemCell
- let selCont = topic!.content![indexPath.row]
- cell.label.text = selCont.title
- return cell
- }
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- let selCont = topic!.content![indexPath.row]
- civ.currentContent = indexPath.row
- civ.reload()
- openMenu( self )
- print( selCont )
- }
- func parseJSON() {
- for index in topic!.content!.indices {
- dispatchGroup.enter()
- let url = URL(string: "\(ServHandler.connectProtocol)://\(ServHandler.baseURL)/content/get/\(topic!.content![index].id)")
- let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
- // Check for errors
- guard error == nil else {
- print("There was an error")
- return
- }
- // Check for no data
- guard let content = data else {
- print("There is no data to show")
- return
- }
- // Decode the data, which should return the video url and encryption key
- let decoder = JSONDecoder()
- do {
- let jsonOutput = try decoder.decode(Video.self, from: content)
- self.topic!.content![index].encKey = jsonOutput.encryption_key
- self.topic!.content![index].content = jsonOutput.content
- print( self.topic!.content![index].title )
- //if( index == self.topic!.content!.indices.last ){
- self.dispatchGroup.leave()
- //}
- return
- }
- catch {
- print(error.localizedDescription)
- print(error)
- return
- }
- }
- task.resume()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement