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
- import StoreKit
- class AllCoursesViewController: UITableViewController {
- // MARK: - Properties
- var allCoursesArray = [Course]()
- var ownedCoursesArray = [Course]()
- var coursesToShow = [Course]()
- var IDArray = [Int]()
- var mycourse: Course!
- var IAPArray = [SKProduct]()
- let dispatchGroup = DispatchGroup()
- override func viewWillAppear(_ animated: Bool) {
- let url = URL(string: "http://fe01.kilosierracharlie.me/user")
- let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
- guard error == nil else {
- print(error?.localizedDescription ?? "")
- return
- }
- // Check if the user is already logged in
- if let httpStatus = response as? HTTPURLResponse {
- print("status code = \(httpStatus.statusCode)")
- // If logged in, then go straight to "All Courses"
- if httpStatus.statusCode == 200 {
- DispatchQueue.main.async {
- //self.performSegue(withIdentifier: "loginToAllCoursesSegue", sender: self)
- }
- }
- // If not logged in, then ask to login
- else {
- // Segue to Login screen
- print("You need to log in")
- }
- }
- }
- task.resume()
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- // Pull all owned courses, setting flags
- parseOwnedCoursesJSON()
- // Pull all courses
- parseAllCoursesJSON()
- dispatchGroup.notify(queue: .main) {
- // Merge both arrays, removing any duplicates where ID is present twice and flag is 0
- for cou in self.ownedCoursesArray {
- self.IDArray.append(cou.id)
- }
- for var cou in self.allCoursesArray {
- cou.owned = self.IDArray.contains(cou.id)
- self.coursesToShow.append(cou)
- }
- self.tableView.reloadData()
- }
- // IAP CODE
- //IAPHandler.shared.setProductIDs(IDs: self.productIDs)
- IAPHandler.shared.fetchAvailableProducts { [weak self](products)
- in
- guard let sSelf = self else {return}
- sSelf.IAPArray = products
- }
- }
- // PULL ALL COURSES AND FILL ARRAY
- func parseAllCoursesJSON() {
- dispatchGroup.enter()
- let url = URL(string: "http://fe01.kilosierracharlie.me/courses")
- 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
- }
- // Using Codable, fill allCoursesArray
- let decoder = JSONDecoder()
- do {
- for cou in try decoder.decode([Course].self, from: content) {
- self.allCoursesArray.append(cou)
- }
- self.dispatchGroup.leave()
- return
- }
- catch {
- print(error.localizedDescription)
- print(error)
- return
- }
- }
- task.resume()
- }
- // PULL ALL OWNED COURSES AND FILL THE ARRAY
- func parseOwnedCoursesJSON() {
- dispatchGroup.enter()
- let url = URL(string: "http://fe01.kilosierracharlie.me/courses/my")
- 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
- }
- // Using Codable, fill ownedCoursesArray and set the owned flag to true
- let decoder = JSONDecoder()
- do {
- for cou in try decoder.decode([Course].self, from: content) {
- self.ownedCoursesArray.append(cou)
- print(cou)
- }
- for index in 0 ..< self.ownedCoursesArray.count {
- self.ownedCoursesArray[index].owned = true
- }
- self.dispatchGroup.leave()
- return
- }
- catch {
- print(error.localizedDescription)
- print(error)
- return
- }
- }
- task.resume()
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return self.coursesToShow.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "CourseCell", for: indexPath) as! CourseCell
- cell.course = coursesToShow[indexPath.row]
- return cell
- }
- @objc func buttonClicked(sender : UIButton!) {
- print("Clicked!")
- }
- // When the cell is pressed
- override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- // Determine which Course was selected
- let selectedCourse = coursesToShow[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