Skip to content

Commit

Permalink
Fix session loading on launch
Browse files Browse the repository at this point in the history
Requery if sessions array is empty
Make scope tag color readable
  • Loading branch information
nickswalker committed Feb 5, 2016
1 parent 01fd7b2 commit c1e3293
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions uMAD/View Controllers/SessionsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import SafariServices
class SessionsViewController: PFQueryTableViewController, UISearchControllerDelegate,
UISearchResultsUpdating, UISearchBarDelegate, ProfileViewControllerDelegate {

private var sessions: [Session]?
private var sessions = [Session]()
private var sections = [[Session]]()
private var filteredSessions: [Session]?
private var filteredSessions = [Session]()
private var filteredSections = [[Session]]()
private var searchController: UISearchController!
private let cellIdentifier = "SessionCell"
Expand All @@ -30,6 +30,7 @@ UISearchResultsUpdating, UISearchBarDelegate, ProfileViewControllerDelegate {
searchController.searchBar.placeholder = "Search Sessions"
searchController.searchBar.delegate = self
searchController.delegate = self
searchController.searchBar.tintColor = UIColor.grayColor()
tableView.tableHeaderView = searchController.searchBar
definesPresentationContext = true

Expand All @@ -53,6 +54,9 @@ UISearchResultsUpdating, UISearchBarDelegate, ProfileViewControllerDelegate {

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if sessions.count == 0 {
loadObjects()
}

navigationController?.setToolbarHidden(true, animated: true)
}
Expand Down Expand Up @@ -81,11 +85,11 @@ UISearchResultsUpdating, UISearchBarDelegate, ProfileViewControllerDelegate {
return
}

sessions?.removeAll()
sessions.removeAll()
sections.removeAll()

sessions = casted
sections = sessions!.createSectionedRepresentation()
sections = sessions.createSectionedRepresentation()

searchController.searchBar.scopeButtonTitles = getTopTags()
}
Expand Down Expand Up @@ -121,8 +125,9 @@ UISearchResultsUpdating, UISearchBarDelegate, ProfileViewControllerDelegate {
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let section = (searchController.active) ? filteredSections[section] : sections[section]
return section.count
let sectionList = (searchController.active) ? filteredSections[section] : sections[section]
print("Section \(section) has \(sectionList.count)")
return sectionList.count
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
Expand Down Expand Up @@ -193,12 +198,9 @@ UISearchResultsUpdating, UISearchBarDelegate, ProfileViewControllerDelegate {
}

func getTopTags() -> [String]? {
guard sessions != nil else {
return nil
}

var tags = [String: Int]()
for event in sessions! {
for event in sessions {
for tag in event.topicTagsSet {
let oldValue = tags[tag] ?? 0
tags[tag] = 1 + oldValue
Expand Down Expand Up @@ -229,7 +231,7 @@ UISearchResultsUpdating, UISearchBarDelegate, ProfileViewControllerDelegate {
func filterContentForSearchText(searchText: String, scope: Int) {
let buttonTitles = searchController.searchBar.scopeButtonTitles!
let scopeString = buttonTitles[scope]
filteredSessions = sessions!.filter { event in
filteredSessions = sessions.filter { event in
let categoryMatch = (scopeString == "All") || (event.topicTagsSet.contains(scopeString))
if searchText != "" {
let stringMatch = event.name.rangeOfString(searchText, options: .CaseInsensitiveSearch)
Expand All @@ -239,7 +241,7 @@ UISearchResultsUpdating, UISearchBarDelegate, ProfileViewControllerDelegate {
}
}

filteredSections = filteredSessions!.createSectionedRepresentation()
filteredSections = filteredSessions.createSectionedRepresentation()
}

}

0 comments on commit c1e3293

Please sign in to comment.