diff --git a/AvH Plan.xcodeproj/project.pbxproj b/AvH Plan.xcodeproj/project.pbxproj index 2224bb6..06a60ee 100644 --- a/AvH Plan.xcodeproj/project.pbxproj +++ b/AvH Plan.xcodeproj/project.pbxproj @@ -9,7 +9,7 @@ /* Begin PBXBuildFile section */ 1BF3A6CA97BA41C38482C1A6 /* Pods_AvH_PlanTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D9D6269E96544A9242A29611 /* Pods_AvH_PlanTests.framework */; }; 2E65263AFD35D6B542E89AE4 /* Pods_AvH_Plan.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7605BB0678C9E27A1D3E9DAA /* Pods_AvH_Plan.framework */; }; - A949F26322D7938700D4BFFD /* db.sqlite3 in Resources */ = {isa = PBXBuildFile; fileRef = A949F26222D7938700D4BFFD /* db.sqlite3 */; }; + A9128EDD22D88E8E006976C8 /* MenuViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A9128EDC22D88E8E006976C8 /* MenuViewCell.swift */; }; A96133802299867400B541D4 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A961337F2299867400B541D4 /* AppDelegate.swift */; }; A96133822299867400B541D4 /* PlanViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A96133812299867400B541D4 /* PlanViewController.swift */; }; A96133842299867400B541D4 /* PersonalViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A96133832299867400B541D4 /* PersonalViewController.swift */; }; @@ -47,7 +47,7 @@ 589FBD2CE6A9C73CD9B81441 /* Pods-AvH PlanUITests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AvH PlanUITests.release.xcconfig"; path = "Target Support Files/Pods-AvH PlanUITests/Pods-AvH PlanUITests.release.xcconfig"; sourceTree = ""; }; 74EDE3D8134812AB76BEB988 /* Pods_AvH_PlanUITests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AvH_PlanUITests.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 7605BB0678C9E27A1D3E9DAA /* Pods_AvH_Plan.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AvH_Plan.framework; sourceTree = BUILT_PRODUCTS_DIR; }; - A949F26222D7938700D4BFFD /* db.sqlite3 */ = {isa = PBXFileReference; lastKnownFileType = text; path = db.sqlite3; sourceTree = ""; }; + A9128EDC22D88E8E006976C8 /* MenuViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuViewCell.swift; sourceTree = ""; }; A961337C2299867400B541D4 /* AvH Plan.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "AvH Plan.app"; sourceTree = BUILT_PRODUCTS_DIR; }; A961337F2299867400B541D4 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; A96133812299867400B541D4 /* PlanViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlanViewController.swift; sourceTree = ""; }; @@ -162,8 +162,8 @@ A961338D2299867A00B541D4 /* Info.plist */, A96133AF229986DF00B541D4 /* CollectionViewCell.swift */, A9A3496B22D4859200F35AE0 /* SubstModel.swift */, + A9128EDC22D88E8E006976C8 /* MenuViewCell.swift */, A9C1D30122D7237200139043 /* DataFetcher.swift */, - A949F26222D7938700D4BFFD /* db.sqlite3 */, ); path = "AvH Plan"; sourceTree = ""; @@ -297,7 +297,6 @@ A961338C2299867A00B541D4 /* LaunchScreen.storyboard in Resources */, A96133892299867A00B541D4 /* Assets.xcassets in Resources */, A96133872299867400B541D4 /* Main.storyboard in Resources */, - A949F26322D7938700D4BFFD /* db.sqlite3 in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -424,6 +423,7 @@ A9A3496C22D4859200F35AE0 /* SubstModel.swift in Sources */, A9C1D30022D656DD00139043 /* MenuViewController.swift in Sources */, A96133B0229986DF00B541D4 /* CollectionViewCell.swift in Sources */, + A9128EDD22D88E8E006976C8 /* MenuViewCell.swift in Sources */, A9C1D2FE22D656A400139043 /* InfoViewController.swift in Sources */, A96133822299867400B541D4 /* PlanViewController.swift in Sources */, A9C1D30222D7237200139043 /* DataFetcher.swift in Sources */, diff --git a/AvH Plan/Assets.xcassets/ic_german.imageset/ic_german-1.png b/AvH Plan/Assets.xcassets/ic_german.imageset/ic_german-1.png new file mode 100644 index 0000000..8fcae5a Binary files /dev/null and b/AvH Plan/Assets.xcassets/ic_german.imageset/ic_german-1.png differ diff --git a/AvH Plan/Assets.xcassets/ic_german.imageset/ic_german.png b/AvH Plan/Assets.xcassets/ic_german.imageset/ic_german.png new file mode 100644 index 0000000..1a04def Binary files /dev/null and b/AvH Plan/Assets.xcassets/ic_german.imageset/ic_german.png differ diff --git a/AvH Plan/Assets.xcassets/ic_politics.imageset/ic_politics.png b/AvH Plan/Assets.xcassets/ic_politics.imageset/ic_politics.png new file mode 100644 index 0000000..f457c69 Binary files /dev/null and b/AvH Plan/Assets.xcassets/ic_politics.imageset/ic_politics.png differ diff --git a/AvH Plan/Assets.xcassets/ic_religion.imageset/ic_religion.png b/AvH Plan/Assets.xcassets/ic_religion.imageset/ic_religion.png new file mode 100644 index 0000000..bdac747 Binary files /dev/null and b/AvH Plan/Assets.xcassets/ic_religion.imageset/ic_religion.png differ diff --git a/AvH Plan/Base.lproj/Main.storyboard b/AvH Plan/Base.lproj/Main.storyboard index b89bc26..7ca1b71 100644 --- a/AvH Plan/Base.lproj/Main.storyboard +++ b/AvH Plan/Base.lproj/Main.storyboard @@ -166,34 +166,144 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + + + + + + + - + @@ -202,7 +312,21 @@ + + + + + + + + + + + + + + @@ -218,14 +342,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/AvH Plan/DataFetcher.swift b/AvH Plan/DataFetcher.swift index 5ec27f3..0c80a4f 100644 --- a/AvH Plan/DataFetcher.swift +++ b/AvH Plan/DataFetcher.swift @@ -7,15 +7,19 @@ // import UIKit +import Foundation import Alamofire import SwiftSoup import SQLite class DataFetcher { - let path = Bundle.main.path(forResource: "db", ofType: "sqlite3") + let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! let substitutions = Table("substitutions") + let personal = Table("personal") + let information = Table("information") + let foodmenu = Table("foodmenu") let id = Expression("id") let group = Expression("group") let course = Expression("course") @@ -23,28 +27,46 @@ class DataFetcher { let date = Expression("date") let time = Expression("time") let room = Expression("room") + let text = Expression("text") - func doAsync(completionHandler: @escaping (_ substitutions: Array) -> ()) { + func doAsync(do task: String, completionHandler: @escaping (_ substitutions: Array) -> ()) { DispatchQueue(label: "work-queue").async { - let url = "https://djd4rkn355.github.io/subst_test.html" + var url = "" + switch task { + case "menu": url = "https://djd4rkn355.github.io/food_test.html" + default: 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)) + switch task { + case "plan": completionHandler(self.parseHTML(html: html, isPersonal: false)) + case "personal": completionHandler(self.parseHTML(html: html, isPersonal: true)) + case "info": completionHandler(self.parseInformation(html: html)) + default: completionHandler(self.parseMenu(html: html)) + } + } } } } - func parseHTML(html: String) -> Array { + func parseHTML(html: String, isPersonal: Bool) -> Array { var subst = Array() + var personalSubst = Array() do { let doc: Document = try SwiftSoup.parse(html) let rows: Elements = try doc.select("tr") - let db = try Connection(path!) +// let prefs = UserDefaults.standard +// let courses = prefs.string(forKey: "courses") +// let classes = prefs.string(forKey: "classes") + let courses: String? = "GES7" + let classes: String? = "17" - try db.run(substitutions.create { t in + let db = try Connection("\(path)/db.sqlite3") + + try db.run(substitutions.create(ifNotExists: true) { t in t.column(id, primaryKey: true) t.column(group) t.column(course) @@ -54,6 +76,19 @@ class DataFetcher { t.column(room) }) + try db.run(personal.create(ifNotExists: true) { t in + t.column(id, primaryKey: true) + t.column(group) + t.column(course) + t.column(additional) + t.column(date) + t.column(time) + t.column(room) + }) + + try db.run(substitutions.delete()) + try db.run(personal.delete()) + for i in (0.. [SubstModel] { var substs = [SubstModel]() do { - let db = try Connection(path!) + let db = try Connection("\(path)/db.sqlite3") for subst in try db.prepare(substitutions) { substs.append(SubstModel(group: subst[group], course: subst[course], additional: subst[additional], date: subst[date], time: subst[time], room: subst[room])) } @@ -92,6 +158,103 @@ class DataFetcher { return substs } + func getPersonalFromDatabase() -> [SubstModel] { + var substs = [SubstModel]() + do { + let db = try Connection("\(path)/db.sqlite3") + for subst in try db.prepare(personal) { + substs.append(SubstModel(group: subst[group], course: subst[course], additional: subst[additional], date: subst[date], time: subst[time], room: subst[room])) + } + } catch {} + return substs + } + + func parseInformation(html: String) -> [String] { + var list = [String]() + do { + let doc: Document = try SwiftSoup.parse(html) + let p: Elements = try doc.select("p") + + let db = try Connection("\(path)/db.sqlite3") + + try db.run(information.create(ifNotExists: true) { t in + t.column(id, primaryKey: true) + t.column(text) + }) + + try db.run(information.delete()) + + for item in p { + + let t = try item.text() + + list.append(t) + + let insert = information.insert(text <- t) + _ = try db.run(insert) + + + + } + + } catch Exception.Error(let type, let message) { + print(type) + print(message) + } catch { + print("error") + } + return list + } + + func readInformation() -> [String] { + var list = [String]() + do { + let db = try Connection("\(path)/db.sqlite3") + for item in try db.prepare(information) { + list.append(item[text]) + } + } catch {} + return list + } + + func parseMenu(html: String) -> [String] { + + var items = [String]() + do { + let doc: Document = try SwiftSoup.parse(html) + let e: Elements = try doc.select("th") + + let db = try Connection("\(path)/db.sqlite3") + + try db.run(foodmenu.create(ifNotExists: true) { t in + t.column(id, primaryKey: true) + t.column(text) + }) + + try db.run(foodmenu.delete()) + + for i in 0.. [String] { + var menu = [String]() + do { + let db = try Connection("\(path)/db.sqlite3") + for item in try db.prepare(foodmenu) { + menu.append(item[text]) + } + } catch {} + return menu + } + func getImage(from icon: String) -> UIImage? { var imagePath = "" let i = icon.lowercased() diff --git a/AvH Plan/MenuViewController.swift b/AvH Plan/MenuViewController.swift index 05aecf9..297b251 100644 --- a/AvH Plan/MenuViewController.swift +++ b/AvH Plan/MenuViewController.swift @@ -2,12 +2,68 @@ // MenuViewController.swift // AvH Plan // -// Created by Deniz Duezgoeren on 10.07.19. +// Created by Deniz Duezgoeren on 11.07.19. // Copyright © 2019 Deniz Duezgoeren. All rights reserved. // import UIKit -class MenuViewController : UIViewController { +class MenuViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { + let reuseIdentifier = "cell" + @IBOutlet weak var collectionView: UICollectionView! + var items = [String]() + private let refreshControl = UIRefreshControl() + let df = DataFetcher() + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + if #available(iOS 10.0, *) { + collectionView.refreshControl = refreshControl + } else { + collectionView.addSubview(refreshControl) + } + 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 food menu...") + } + + @objc private func objDoAsync(_ sender: Any) { + df.doAsync(do: "menu") { menuItems in + self.items = menuItems as! [String] + 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) + + self.items = self.df.readMenu() + self.collectionView.reloadData() + } + + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + return self.items.count + } + + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MenuViewCell + if self.items.count != 0 { + cell.content.text = self.items[indexPath.item] + } + + cell.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) + return cell + } + + func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + print("You selected cell#\(indexPath.item)!") + } + + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { + return CGSize(width: view.frame.size.width, height: 64) + } } + diff --git a/AvH Plan/PersonalViewController.swift b/AvH Plan/PersonalViewController.swift index 6a3bb55..2cecefc 100644 --- a/AvH Plan/PersonalViewController.swift +++ b/AvH Plan/PersonalViewController.swift @@ -1,20 +1,91 @@ // -// SecondViewController.swift +// PersonalViewController.swift // AvH Plan // -// Created by Deniz Duezgoeren on 25.05.19. +// Created by Deniz Duezgoeren on 11.07.19. // Copyright © 2019 Deniz Duezgoeren. All rights reserved. // import UIKit -class PersonalViewController: UIViewController { - +class PersonalViewController : UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { + + let reuseIdentifier = "cell" + @IBOutlet weak var collectionView: UICollectionView! + var substs = Array() + private let refreshControl = UIRefreshControl() + let df = DataFetcher() + + override func viewWillAppear(_ animated: Bool) { + super.viewWillAppear(animated) + if #available(iOS 10.0, *) { + collectionView.refreshControl = refreshControl + } else { + collectionView.addSubview(refreshControl) + } + 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 your plan...") + } + + @objc private func objDoAsync(_ sender: Any) { + df.doAsync(do: "personal") { substitutions in + self.substs = substitutions as! [SubstModel] + self.collectionView.reloadData() + self.refreshControl.endRefreshing() + } + } + override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. + collectionView.backgroundColor = UIColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1.0) + + self.substs = self.df.getPersonalFromDatabase() + self.collectionView.reloadData() + } + + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { + 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.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.substs[indexPath.item].course + let image = df.getImage(from: course) + + if image != nil { + let attachment: NSTextAttachment = NSTextAttachment() + attachment.bounds = CGRect(x: 0, y: 0, width: cell.course.frame.height - 6, height: cell.course.frame.height - 6) + attachment.image = image + + let courseImage = NSMutableAttributedString(attachment: attachment) + let courseString = NSAttributedString(string: " " + course) + courseImage.append(courseString) + + cell.course.attributedText = courseImage + } else { + cell.course.text = course + } + } + + cell.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) + cell.tintView.backgroundColor = cell.backgroundColor + return cell + } + + // func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { + // print("You selected cell#\(indexPath.item)!") + // } + + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { + return CGSize(width: view.frame.size.width, height: 108) } - - } diff --git a/AvH Plan/PlanViewController.swift b/AvH Plan/PlanViewController.swift index 2c7069d..d578dbb 100644 --- a/AvH Plan/PlanViewController.swift +++ b/AvH Plan/PlanViewController.swift @@ -29,8 +29,8 @@ class PlanViewController : UIViewController, UICollectionViewDataSource, UIColle } @objc private func objDoAsync(_ sender: Any) { - df.doAsync() { substitutions in - self.substs = substitutions + df.doAsync(do: "plan") { substitutions in + self.substs = substitutions as! [SubstModel] self.collectionView.reloadData() self.refreshControl.endRefreshing() }