Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AllCoursesViewController.swift
- // AeroBuddy
- //
- // Created by Luke Collister on 01/09/2019.
- // Copyright © 2019 Luke Collister. All rights reserved.
- //
- import UIKit
- class AllCoursesViewController: UITableViewController {
- var tempData = [Course]()
- var allCourses = [Course]()
- var ownedCourses = [Course]()
- var displayCourses = [Course]()
- var mycourse: Course!
- let retrieveAllCoursesDG = DispatchGroup()
- let retrieveOwnedCoursesDG = DispatchGroup()
- override func viewDidLoad() {
- super.viewDidLoad()
- // Get all courses once we've checked authentication.
- print("Get All Courses")
- self.getAllCourses(endpoint: "courses", stateDG: self.retrieveAllCoursesDG)
- // Get owned courses once we've got all courses.
- retrieveAllCoursesDG.notify(queue: .main) {
- print("Get Owned Courses")
- self.allCourses = self.tempData.copy()
- self.getAllCourses(endpoint: "courses/my", stateDG: self.retrieveOwnedCoursesDG)
- }
- // Do the thing where we check courses against owned or whatever.
- retrieveOwnedCoursesDG.notify(queue: .main) {
- print("Filter Courses")
- self.ownedCourses = self.tempData.copy()
- for i in 0 ..< self.ownedCourses.count {
- self.ownedCourses[i].owned = true
- }
- var idArray = [Int]()
- for course in self.ownedCourses {
- idArray.append(course.id)
- }
- for var course in self.allCourses {
- course.owned = idArray.contains(course.id)
- self.displayCourses.append(course)
- }
- self.tableView.reloadData()
- }
- }
- func getAllCourses(endpoint: String, stateDG: DispatchGroup) {
- stateDG.enter()
- let url = URL(string: "\(ServHandler.connectProtocol)://\(ServHandler.baseURL)/\(endpoint)")
- let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
- guard error == nil else {
- print("Error!")
- return
- }
- guard let content = data else {
- print("There is no data!")
- return
- }
- let decoder = JSONDecoder()
- do {
- for course in try decoder.decode([Course].self, from: content) {
- self.tempData.append(course)
- }
- stateDG.leave()
- return
- } catch {
- print(error.localizedDescription)
- print(error)
- return
- }
- }
- task.resume()
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return self.displayCourses.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "CourseCell", for: indexPath) as! CourseCell
- cell.course = displayCourses[indexPath.row]
- return cell
- }
- // When the cell is pressed
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- // Determine which Course was selected
- let selectedCourse = displayCourses[indexPath.row]
- mycourse = selectedCourse
- if (selectedCourse.owned == true) {
- // Load the subjects for this course
- performSegue(withIdentifier: "courseToSubjectSegue", sender: self)
- }
- else {
- // Trigger an alert to buy the course
- let alert = UIAlertController(title: "Purhcase Course?", message: "You do not own this course. Do you wish to purchase it?", preferredStyle: .alert)
- alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { action in
- print("RUN IAP CODE BOI")
- }))
- alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
- self.present(alert, animated: true)
- }
- }
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == "courseToSubjectSegue" {
- if let destinationVC = segue.destination as? SubjectsViewController {
- destinationVC.courseVar = mycourse
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement