diff --git a/AvH Plan.xcodeproj/project.pbxproj b/AvH Plan.xcodeproj/project.pbxproj index f5a5910..37476a8 100644 --- a/AvH Plan.xcodeproj/project.pbxproj +++ b/AvH Plan.xcodeproj/project.pbxproj @@ -21,6 +21,7 @@ A9A3496C22D4859200F35AE0 /* SubstModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9A3496B22D4859200F35AE0 /* SubstModel.swift */; }; A9C1D2FE22D656A400139043 /* InfoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C1D2FD22D656A400139043 /* InfoViewController.swift */; }; A9C1D30022D656DD00139043 /* MenuViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C1D2FF22D656DD00139043 /* MenuViewController.swift */; }; + A9C1D30222D7237200139043 /* DataFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9C1D30122D7237200139043 /* DataFetcher.swift */; }; C736A19A44C64423F3A695B7 /* Pods_AvH_PlanUITests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 74EDE3D8134812AB76BEB988 /* Pods_AvH_PlanUITests.framework */; }; /* End PBXBuildFile section */ @@ -63,6 +64,7 @@ A9A3496B22D4859200F35AE0 /* SubstModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SubstModel.swift; sourceTree = ""; }; A9C1D2FD22D656A400139043 /* InfoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoViewController.swift; sourceTree = ""; }; A9C1D2FF22D656DD00139043 /* MenuViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuViewController.swift; sourceTree = ""; }; + A9C1D30122D7237200139043 /* DataFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataFetcher.swift; sourceTree = ""; }; C6EC6ACE7893F2BB0A157817 /* Pods-AvH Plan.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AvH Plan.release.xcconfig"; path = "Target Support Files/Pods-AvH Plan/Pods-AvH Plan.release.xcconfig"; sourceTree = ""; }; D9D6269E96544A9242A29611 /* Pods_AvH_PlanTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AvH_PlanTests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; E596AED9E99670CBF1B6F49E /* Pods-AvH Plan.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AvH Plan.debug.xcconfig"; path = "Target Support Files/Pods-AvH Plan/Pods-AvH Plan.debug.xcconfig"; sourceTree = ""; }; @@ -158,6 +160,7 @@ A961338D2299867A00B541D4 /* Info.plist */, A96133AF229986DF00B541D4 /* CollectionViewCell.swift */, A9A3496B22D4859200F35AE0 /* SubstModel.swift */, + A9C1D30122D7237200139043 /* DataFetcher.swift */, ); path = "AvH Plan"; sourceTree = ""; @@ -417,6 +420,7 @@ A96133B0229986DF00B541D4 /* CollectionViewCell.swift in Sources */, A9C1D2FE22D656A400139043 /* InfoViewController.swift in Sources */, A96133822299867400B541D4 /* PlanViewController.swift in Sources */, + A9C1D30222D7237200139043 /* DataFetcher.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/AvH Plan/Assets.xcassets/ic_history.imageset/ic_history-1.png b/AvH Plan/Assets.xcassets/ic_history.imageset/ic_history-1.png new file mode 100644 index 0000000..5ab828e Binary files /dev/null and b/AvH Plan/Assets.xcassets/ic_history.imageset/ic_history-1.png differ diff --git a/AvH Plan/DataFetcher.swift b/AvH Plan/DataFetcher.swift new file mode 100644 index 0000000..e5c6374 --- /dev/null +++ b/AvH Plan/DataFetcher.swift @@ -0,0 +1,110 @@ +// +// DataFetcher.swift +// AvH Plan +// +// Created by Deniz Duezgoeren on 11.07.19. +// Copyright © 2019 Deniz Duezgoeren. All rights reserved. +// + +import Foundation +import UIKit +import Alamofire +import SwiftSoup + +class DataFetcher { + + func doAsync(completionHandler: @escaping (_ substitutions: Array) -> ()) { + DispatchQueue(label: "work-queue").async { + let url = "https://djd4rkn355.github.io/subst_test.html" + Alamofire.request(url).responseString { response in + if let html = response.result.value { + completionHandler(self.parseHTML(html: html)) + } + } + + } + } + + func parseHTML(html: String) -> Array { + var subst = Array() + do { + let doc: Document = try SwiftSoup.parse(html) + let rows: Elements = try doc.select("tr") + + for i in (0.. UIImage? { + var imagePath = "" + let i = icon.lowercased() + if i.contains("deu") || i.contains("dep") || i.contains("daz") { + imagePath = "ic_german" + } else if i.contains("mat") || i.contains("map") { + imagePath = "ic_maths" + } else if i.contains("eng") || i.contains("enp") || i.contains("ena") { + imagePath = "ic_english" + } else if i.contains("spo") || i.contains("spp") || i.contains("spth") { + imagePath = "ic_pe" + } else if i.contains("pol") || i.contains("pop") { + imagePath = "ic_politics" + } else if i.contains("dar") || i.contains("dap") { + imagePath = "ic_drama" + } else if i.contains("phy") || i.contains("php") { + imagePath = "ic_physics" + } else if i.contains("bio") || i.contains("bip") || i.contains("nw") { + imagePath = "ic_biology" + } else if i.contains("che") || i.contains("chp") { + imagePath = "ic_chemistry" + } else if i.contains("phi") || i.contains("psp") { + imagePath = "ic_philosophy" + } else if i.contains("laa") || i.contains("laf") || i.contains("lat") { + imagePath = "ic_latin" + } else if i.contains("spa") || i.contains("spf") { + imagePath = "ic_spanish" + } else if i.contains("fra") || i.contains("frf") || i.contains("frz") { + imagePath = "ic_french" + } else if i.contains("inf") { + imagePath = "ic_compsci" + } else if i.contains("ges") { + imagePath = "ic_history" + } else if i.contains("rel") { + imagePath = "ic_religion" + } else if i.contains("geg") || i.contains("wuk") { + imagePath = "ic_geography" + } else if i.contains("kun") { + imagePath = "ic_arts" + } else if i.contains("mus") { + imagePath = "ic_music" + } else if i.contains("tue") { + imagePath = "ic_turkish" + } else if i.contains("chi") { + imagePath = "ic_chinese" + } else if i.contains("gll") { + imagePath = "ic_gll" + } else if i.contains("wat") { + imagePath = "ic_wat" + } else if i.contains("för") { + imagePath = "ic_help" + } else if i.contains("wp") || i.contains("met") { + imagePath = "ic_pencil" + } else { + return nil + } + return UIImage(named: imagePath) + } +} diff --git a/AvH Plan/PlanViewController.swift b/AvH Plan/PlanViewController.swift index 6fe6739..08d7ec5 100644 --- a/AvH Plan/PlanViewController.swift +++ b/AvH Plan/PlanViewController.swift @@ -15,15 +15,10 @@ class PlanViewController : UIViewController, UICollectionViewDataSource, UIColle let reuseIdentifier = "cell" @IBOutlet weak var collectionView: UICollectionView! - let queue = DispatchQueue(label: "work-queue") - var group: Array = Array() - var course: Array = Array() - var additional: Array = Array() - var date: Array = Array() - var time: Array = Array() - var room: Array = Array() + var substs = Array() private let refreshControl = UIRefreshControl() var db: OpaquePointer? + let df = DataFetcher() override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) @@ -35,47 +30,36 @@ class PlanViewController : UIViewController, UICollectionViewDataSource, UIColle refreshControl.addTarget(self, action: #selector(objDoAsync(_:)), for: .valueChanged) refreshControl.tintColor = UIColor(red: 0.39, green: 0.71, blue: 0.96, alpha: 1.0) refreshControl.attributedTitle = NSAttributedString(string: "Fetching the plan...") -// self.refreshControl.beginRefreshing() -// self.doAsync() } @objc private func objDoAsync(_ sender: Any) { - self.doAsync() + df.doAsync() { substitutions in + self.substs = substitutions + self.collectionView.reloadData() + self.refreshControl.endRefreshing() + } } override func viewDidLoad() { super.viewDidLoad() - - collectionView.backgroundColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1.0) - -// let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout -// layout.itemSize = CGSize(width: view.frame.size.width, height: 129) - -// let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) -// .appendingPathComponent("SubstDB.sqlite") -// -// if (sqlite3_open(fileURL.path, &db) != SQLITE_OK) { -// print("Error opening database connection") -// } -// sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS subst_table (id INTEGER PRIMARY KEY AUTOINCREMENT, group TEXT, course TEXT, add TEXT, date TEXT, time TEXT, room TEXT)", nil, nil, nil) } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return self.group.count + return self.substs.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! CollectionViewCell - if self.group.count != 0 { - cell.group.text = self.group[indexPath.item] - cell.additional.text = self.additional[indexPath.item] - cell.date.text = self.date[indexPath.item] - cell.time.text = self.time[indexPath.item] - cell.room.text = self.room[indexPath.item] + if self.substs.count != 0 { + cell.group.text = self.substs[indexPath.item].group + cell.additional.text = self.substs[indexPath.item].additional + cell.date.text = self.substs[indexPath.item].date + cell.time.text = self.substs[indexPath.item].time + cell.room.text = self.substs[indexPath.item].room - let course = self.course[indexPath.item] - let image = getImage(from: course) + let course = self.substs[indexPath.item].course + let image = df.getImage(from: course) if image != nil { let attachment: NSTextAttachment = NSTextAttachment() @@ -100,65 +84,6 @@ class PlanViewController : UIViewController, UICollectionViewDataSource, UIColle return cell } - func getImage(from icon: String) -> UIImage? { - var imagePath = "" - let i = icon.lowercased() - if i.contains("deu") || i.contains("dep") || i.contains("daz") { - imagePath = "ic_german" - } else if i.contains("mat") || i.contains("map") { - imagePath = "ic_maths" - } else if i.contains("eng") || i.contains("enp") || i.contains("ena") { - imagePath = "ic_english" - } else if i.contains("spo") || i.contains("spp") || i.contains("spth") { - imagePath = "ic_pe" - } else if i.contains("pol") || i.contains("pop") { - imagePath = "ic_politics" - } else if i.contains("dar") || i.contains("dap") { - imagePath = "ic_drama" - } else if i.contains("phy") || i.contains("php") { - imagePath = "ic_physics" - } else if i.contains("bio") || i.contains("bip") || i.contains("nw") { - imagePath = "ic_biology" - } else if i.contains("che") || i.contains("chp") { - imagePath = "ic_chemistry" - } else if i.contains("phi") || i.contains("psp") { - imagePath = "ic_philosophy" - } else if i.contains("laa") || i.contains("laf") || i.contains("lat") { - imagePath = "ic_latin" - } else if i.contains("spa") || i.contains("spf") { - imagePath = "ic_spanish" - } else if i.contains("fra") || i.contains("frf") || i.contains("frz") { - imagePath = "ic_french" - } else if i.contains("inf") { - imagePath = "ic_compsci" - } else if i.contains("ges") { - imagePath = "ic_history" - } else if i.contains("rel") { - imagePath = "ic_religion" - } else if i.contains("geg") || i.contains("wuk") { - imagePath = "ic_geography" - } else if i.contains("kun") { - imagePath = "ic_arts" - } else if i.contains("mus") { - imagePath = "ic_music" - } else if i.contains("tue") { - imagePath = "ic_turkish" - } else if i.contains("chi") { - imagePath = "ic_chinese" - } else if i.contains("gll") { - imagePath = "ic_gll" - } else if i.contains("wat") { - imagePath = "ic_wat" - } else if i.contains("för") { - imagePath = "ic_help" - } else if i.contains("wp") || i.contains("met") { - imagePath = "ic_pencil" - } else { - return nil - } - return UIImage(named: imagePath) - } - func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { print("You selected cell#\(indexPath.item)!") } @@ -166,82 +91,5 @@ class PlanViewController : UIViewController, UICollectionViewDataSource, UIColle func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: view.frame.size.width, height: 108) } - - func doAsync() { - queue.async { - let url = "https://djd4rkn355.github.io/subst_test.html" - Alamofire.request(url).responseString { response in - if let html = response.result.value { - self.parseHTML(html: html) - } - } - - } - } - - func insertInDb() { - -// let insertString = "INSERT INTO subst_table VALUES (?, ?, ?, ?, ?, ?)" -// -// if (sqlite3_prepare_v2(db, insertString, nil, nil, nil) != SQLITE_OK) { -// print("error db") -// } -// if ( - } - - func parseHTML(html: String) { - do { - let doc: Document = try SwiftSoup.parse(html) - let rows: Elements = try doc.select("tr") - - self.group.removeAll() - self.date.removeAll() - self.time.removeAll() - self.course.removeAll() - self.room.removeAll() - self.additional.removeAll() - - var group: String - var date: String - var time: String - var course: String - var room: String - var add: String - -// sqlite3_exec(db, "DELETE FROM subst_table", nil, nil, nil) - - for i in (0..