Commit 9626fbf2 authored by Philip Brechler's avatar Philip Brechler

Add sections for hatchery categories

parent c66fb577
......@@ -18,7 +18,7 @@ protocol HatcheryClientDelegate: class {
class HatcheryClient: NSObject, URLSessionDelegate {
let hatcheryBaseURL: URL = URL(string:"https://badge.team/")!
private(set) public var loadedEggs: Array<HatcheryEgg> = []
private(set) public var loadedEggs: Array<Array<HatcheryEgg>> = []
weak var delegate: HatcheryClientDelegate?
......@@ -44,10 +44,25 @@ class HatcheryClient: NSObject, URLSessionDelegate {
let json = try JSONSerialization.jsonObject(with: data!, options: [])
if let jsonArray = json as? [Any] {
self.loadedEggs.removeAll()
var loadedEggsUncategorized: Array<HatcheryEgg> = []
for case let dictionary as Dictionary<String, Any> in jsonArray {
self.loadedEggs.append(HatcheryEgg.init(dictionary: dictionary))
loadedEggsUncategorized.append(HatcheryEgg.init(dictionary: dictionary))
}
let categories = (loadedEggsUncategorized).compactMap { $0.category }
var uniqueCategories = Array(Set(categories))
uniqueCategories = uniqueCategories.sorted { $0.lowercased() < $1.lowercased() }
for category in uniqueCategories {
var eggsForCategory: Array<HatcheryEgg> = []
for egg in loadedEggsUncategorized {
if egg.category == category {
eggsForCategory.append(egg)
}
eggsForCategory = eggsForCategory.sorted { $0.name!.lowercased() < $1.name!.lowercased() }
}
self.loadedEggs.append(eggsForCategory)
}
self.loadedEggs = self.loadedEggs.sorted { $0.name!.lowercased() < $1.name!.lowercased() }
DispatchQueue.main.async {
self.delegate?.didRefreshListOfEggs()
}
......
......@@ -52,14 +52,18 @@ class HatcheryRootTableViewController: UITableViewController,HatcheryClientDeleg
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
if isFiltering() {
return 1
}
return (client?.loadedEggs.count)!
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isFiltering() {
return filteredEggs.count
}
return (client?.loadedEggs.count)!
let categoryArray = client?.loadedEggs[section]
return (categoryArray?.count)!
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
......@@ -74,7 +78,7 @@ class HatcheryRootTableViewController: UITableViewController,HatcheryClientDeleg
if (isFiltering()) {
eggForCell = self.filteredEggs[indexPath.row]
} else {
eggForCell = (self.client?.loadedEggs[indexPath.row])!
eggForCell = (self.client?.loadedEggs[indexPath.section][indexPath.row])!
}
cell.eggNameLabel?.text = eggForCell.name
......@@ -83,6 +87,30 @@ class HatcheryRootTableViewController: UITableViewController,HatcheryClientDeleg
return cell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if (isFiltering()) {
return nil
}
let firstEggForCategory = client?.loadedEggs[section][0]
let sectionHeader: UIVisualEffectView = UIVisualEffectView.init(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.width, height: 44.0))
sectionHeader.effect = UIBlurEffect.init(style: .dark)
let sectionLabel: UILabel = UILabel.init(frame: CGRect(x: self.tableView.separatorInset.left, y: 0, width: self.tableView.frame.width-self.tableView.separatorInset.left, height:44))
sectionLabel.text = firstEggForCategory?.category
sectionLabel.textColor = .lightGray
sectionLabel.font = UIFont.preferredFont(forTextStyle: .subheadline)
sectionHeader.contentView.addSubview(sectionLabel)
return sectionHeader
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if (isFiltering()) {
return 0
}
return 44
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.tableView.indexPathForSelectedRow!
......@@ -91,7 +119,7 @@ class HatcheryRootTableViewController: UITableViewController,HatcheryClientDeleg
if (isFiltering()) {
eggForCell = self.filteredEggs[indexPath.row]
} else {
eggForCell = (self.client?.loadedEggs[indexPath.row])!
eggForCell = (self.client?.loadedEggs[indexPath.section][indexPath.row])!
}
let eggDetailViewController = segue.destination as? HatcheryDetailViewController
eggDetailViewController?.eggToShow = eggForCell
......@@ -102,7 +130,12 @@ class HatcheryRootTableViewController: UITableViewController,HatcheryClientDeleg
func didRefreshListOfEggs() {
self.refreshControl?.endRefreshing()
self.tableView.reloadSections([0], with: .automatic)
if (self.tableView.numberOfSections > 0) {
let rangeToReload = IndexSet.init(integersIn: 0..<(client?.loadedEggs.count)!)
self.tableView.reloadSections(rangeToReload, with: .automatic)
} else {
self.tableView.reloadData()
}
}
func didDownloadEggToPath(_ path: URL) {}
......@@ -127,9 +160,10 @@ class HatcheryRootTableViewController: UITableViewController,HatcheryClientDeleg
}
func filterContentForSearchText(_ searchText: String) {
filteredEggs = (self.client?.loadedEggs.filter({( egg : HatcheryEgg) -> Bool in
let eggsToSearch = self.client?.loadedEggs.flatMap { $0 }
filteredEggs = (eggsToSearch!.filter({( egg : HatcheryEgg) -> Bool in
return (egg.name?.lowercased().contains(searchText.lowercased()))! || (egg.eggDescription?.lowercased().contains(searchText.lowercased()))!
}))!
}))
tableView.reloadData()
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment