Advertisement
lcolli98

Untitled

Nov 15th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 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. import StoreKit
  11.  
  12. class AllCoursesViewController: UITableViewController {
  13.  
  14. // MARK: - Properties
  15. var allCoursesArray = [Course]()
  16. var ownedCoursesArray = [Course]()
  17. var coursesToShow = [Course]()
  18. var IDArray = [Int]()
  19. var mycourse: Course!
  20. var IAPArray = [SKProduct]()
  21.  
  22. let dispatchGroup = DispatchGroup()
  23.  
  24. override func viewWillAppear(_ animated: Bool) {
  25. let url = URL(string: "http://fe01.kilosierracharlie.me/user")
  26. let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
  27. guard error == nil else {
  28. print(error?.localizedDescription ?? "")
  29. return
  30. }
  31.  
  32. // Check if the user is already logged in
  33. if let httpStatus = response as? HTTPURLResponse {
  34. print("status code = \(httpStatus.statusCode)")
  35. // If logged in, then go straight to "All Courses"
  36. if httpStatus.statusCode == 200 {
  37. DispatchQueue.main.async {
  38. //self.performSegue(withIdentifier: "loginToAllCoursesSegue", sender: self)
  39. }
  40. }
  41. // If not logged in, then ask to login
  42. else {
  43. // Segue to Login screen
  44. print("You need to log in")
  45. }
  46. }
  47. }
  48. task.resume()
  49. }
  50.  
  51. override func viewDidLoad() {
  52. super.viewDidLoad()
  53. // Pull all owned courses, setting flags
  54. parseOwnedCoursesJSON()
  55. // Pull all courses
  56. parseAllCoursesJSON()
  57.  
  58. dispatchGroup.notify(queue: .main) {
  59. // Merge both arrays, removing any duplicates where ID is present twice and flag is 0
  60. for cou in self.ownedCoursesArray {
  61. self.IDArray.append(cou.id)
  62. }
  63. for var cou in self.allCoursesArray {
  64. cou.owned = self.IDArray.contains(cou.id)
  65. self.coursesToShow.append(cou)
  66. }
  67. self.tableView.reloadData()
  68. }
  69.  
  70. // IAP CODE
  71. //IAPHandler.shared.setProductIDs(IDs: self.productIDs)
  72. IAPHandler.shared.fetchAvailableProducts { [weak self](products)
  73. in
  74. guard let sSelf = self else {return}
  75. sSelf.IAPArray = products
  76. }
  77. }
  78.  
  79.  
  80. // PULL ALL COURSES AND FILL ARRAY
  81. func parseAllCoursesJSON() {
  82. dispatchGroup.enter()
  83.  
  84. let url = URL(string: "http://fe01.kilosierracharlie.me/courses")
  85. let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
  86.  
  87. // Check for errors
  88. guard error == nil else {
  89. print("There was an error")
  90. return
  91. }
  92.  
  93. // Check for no data
  94. guard let content = data else {
  95. print("There is no data to show")
  96. return
  97. }
  98.  
  99. // Using Codable, fill allCoursesArray
  100. let decoder = JSONDecoder()
  101.  
  102. do {
  103. for cou in try decoder.decode([Course].self, from: content) {
  104. self.allCoursesArray.append(cou)
  105. }
  106. self.dispatchGroup.leave()
  107. return
  108. }
  109. catch {
  110. print(error.localizedDescription)
  111. print(error)
  112. return
  113. }
  114. }
  115. task.resume()
  116. }
  117.  
  118.  
  119.  
  120. // PULL ALL OWNED COURSES AND FILL THE ARRAY
  121. func parseOwnedCoursesJSON() {
  122. dispatchGroup.enter()
  123.  
  124. let url = URL(string: "http://fe01.kilosierracharlie.me/courses/my")
  125. let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
  126.  
  127. // Check for errors
  128. guard error == nil else {
  129. print("There was an error")
  130. return
  131. }
  132.  
  133. // Check for no data
  134. guard let content = data else {
  135. print("There is no data to show")
  136. return
  137. }
  138.  
  139. // Using Codable, fill ownedCoursesArray and set the owned flag to true
  140. let decoder = JSONDecoder()
  141.  
  142. do {
  143. for cou in try decoder.decode([Course].self, from: content) {
  144. self.ownedCoursesArray.append(cou)
  145. print(cou)
  146. }
  147. for index in 0 ..< self.ownedCoursesArray.count {
  148. self.ownedCoursesArray[index].owned = true
  149. }
  150. self.dispatchGroup.leave()
  151. return
  152. }
  153. catch {
  154. print(error.localizedDescription)
  155. print(error)
  156. return
  157. }
  158. }
  159. task.resume()
  160. }
  161.  
  162.  
  163. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  164. return self.coursesToShow.count
  165. }
  166.  
  167. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  168. let cell = tableView.dequeueReusableCell(withIdentifier: "CourseCell", for: indexPath) as! CourseCell
  169. cell.course = coursesToShow[indexPath.row]
  170. return cell
  171. }
  172.  
  173. @objc func buttonClicked(sender : UIButton!) {
  174. print("Clicked!")
  175. }
  176.  
  177.  
  178. // When the cell is pressed
  179. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  180. // Determine which Course was selected
  181. let selectedCourse = coursesToShow[indexPath.row]
  182. mycourse = selectedCourse
  183.  
  184. if (selectedCourse.owned == true) {
  185. // Load the subjects for this course
  186. performSegue(withIdentifier: "courseToSubjectSegue", sender: self)
  187. }
  188. else {
  189. // Trigger an alert to buy the course
  190. let alert = UIAlertController(title: "Purhcase Course?", message: "You do not own this course. Do you wish to purchase it?", preferredStyle: .alert)
  191.  
  192. alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { action in
  193. print("RUN IAP CODE BOI")
  194. }))
  195. alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
  196.  
  197. self.present(alert, animated: true)
  198. }
  199. }
  200.  
  201. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  202. if segue.identifier == "courseToSubjectSegue" {
  203. if let destinationVC = segue.destination as? SubjectsViewController {
  204. destinationVC.courseVar = mycourse
  205. }
  206. }
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement