Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. //
  2. // AllCoursesViewController.swift
  3. // AeroBuddy
  4. //
  5. // Created by Luke Collister on 01/09/2019.
  6. // Copyright © 2019 Luke Collister. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class AllCoursesViewController: UITableViewController {
  12.  
  13. var tempData = [Course]()
  14.  
  15. var allCourses = [Course]()
  16. var ownedCourses = [Course]()
  17. var displayCourses = [Course]()
  18. var mycourse: Course!
  19.  
  20. let retrieveAllCoursesDG = DispatchGroup()
  21. let retrieveOwnedCoursesDG = DispatchGroup()
  22.  
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. // Get all courses once we've checked authentication.
  26. print("Get All Courses")
  27. self.getAllCourses(endpoint: "courses", stateDG: self.retrieveAllCoursesDG)
  28. // Get owned courses once we've got all courses.
  29. retrieveAllCoursesDG.notify(queue: .main) {
  30. print("Get Owned Courses")
  31. self.allCourses = self.tempData.copy()
  32. self.getAllCourses(endpoint: "courses/my", stateDG: self.retrieveOwnedCoursesDG)
  33. }
  34.  
  35. // Do the thing where we check courses against owned or whatever.
  36. retrieveOwnedCoursesDG.notify(queue: .main) {
  37. print("Filter Courses")
  38. self.ownedCourses = self.tempData.copy()
  39. for i in 0 ..< self.ownedCourses.count {
  40. self.ownedCourses[i].owned = true
  41. }
  42.  
  43. var idArray = [Int]()
  44. for course in self.ownedCourses {
  45. idArray.append(course.id)
  46. }
  47. for var course in self.allCourses {
  48. course.owned = idArray.contains(course.id)
  49. self.displayCourses.append(course)
  50. }
  51. self.tableView.reloadData()
  52. }
  53.  
  54. }
  55.  
  56. func getAllCourses(endpoint: String, stateDG: DispatchGroup) {
  57. stateDG.enter()
  58. let url = URL(string: "\(ServHandler.connectProtocol)://\(ServHandler.baseURL)/\(endpoint)")
  59. let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
  60. guard error == nil else {
  61. print("Error!")
  62. return
  63. }
  64. guard let content = data else {
  65. print("There is no data!")
  66. return
  67. }
  68.  
  69. let decoder = JSONDecoder()
  70. do {
  71. for course in try decoder.decode([Course].self, from: content) {
  72. self.tempData.append(course)
  73. }
  74. stateDG.leave()
  75. return
  76. } catch {
  77. print(error.localizedDescription)
  78. print(error)
  79. return
  80. }
  81. }
  82. task.resume()
  83. }
  84.  
  85.  
  86. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  87. return self.displayCourses.count
  88. }
  89.  
  90.  
  91. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  92. let cell = tableView.dequeueReusableCell(withIdentifier: "CourseCell", for: indexPath) as! CourseCell
  93. cell.course = displayCourses[indexPath.row]
  94. return cell
  95. }
  96.  
  97.  
  98. // When the cell is pressed
  99. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  100. // Determine which Course was selected
  101. let selectedCourse = displayCourses[indexPath.row]
  102. mycourse = selectedCourse
  103.  
  104. if (selectedCourse.owned == true) {
  105. // Load the subjects for this course
  106. performSegue(withIdentifier: "courseToSubjectSegue", sender: self)
  107. }
  108. else {
  109. // Trigger an alert to buy the course
  110. let alert = UIAlertController(title: "Purhcase Course?", message: "You do not own this course. Do you wish to purchase it?", preferredStyle: .alert)
  111.  
  112. alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { action in
  113. print("RUN IAP CODE BOI")
  114. }))
  115. alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
  116.  
  117. self.present(alert, animated: true)
  118. }
  119. }
  120.  
  121. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  122. if segue.identifier == "courseToSubjectSegue" {
  123. if let destinationVC = segue.destination as? SubjectsViewController {
  124. destinationVC.courseVar = mycourse
  125. }
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement