Skip to content

Commit

Permalink
Added the ability to search for country/city.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhdhejazi committed Mar 14, 2020
1 parent 20a63c7 commit 3c25aec
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Corona.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
11672A1C24114922000B2466 /* PercentValueFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11672A1B24114922000B2466 /* PercentValueFormatter.swift */; };
11672A1E2411494F000B2466 /* DayAxisValueFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11672A1D2411494F000B2466 /* DayAxisValueFormatter.swift */; };
116EC0A82410DF48006E4559 /* RegionContainerController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 116EC0A72410DF48006E4559 /* RegionContainerController.swift */; };
1195F19E241D2E7900FE718F /* RegionListController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1195F19D241D2E7900FE718F /* RegionListController.swift */; };
11BF32AA24156AE500270130 /* FloatingPanel in Frameworks */ = {isa = PBXBuildFile; productRef = 11BF32A924156AE500270130 /* FloatingPanel */; };
11BF32AF2415F83100270130 /* CSV in Frameworks */ = {isa = PBXBuildFile; productRef = 11BF32AE2415F83100270130 /* CSV */; };
11D637152418D83900BE6557 /* Coordinate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11D637142418D83900BE6557 /* Coordinate.swift */; };
Expand Down Expand Up @@ -71,6 +72,7 @@
11672A1B24114922000B2466 /* PercentValueFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PercentValueFormatter.swift; sourceTree = "<group>"; };
11672A1D2411494F000B2466 /* DayAxisValueFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DayAxisValueFormatter.swift; sourceTree = "<group>"; };
116EC0A72410DF48006E4559 /* RegionContainerController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegionContainerController.swift; sourceTree = "<group>"; };
1195F19D241D2E7900FE718F /* RegionListController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegionListController.swift; sourceTree = "<group>"; };
11BF32B024166E5C00270130 /* App.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = App.entitlements; sourceTree = "<group>"; };
11D637142418D83900BE6557 /* Coordinate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Coordinate.swift; sourceTree = "<group>"; };
11EB2C9C2413F7E9001769B2 /* CurrentStateChartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentStateChartView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -207,8 +209,9 @@
isa = PBXGroup;
children = (
1162D12F240D05F600647002 /* MapController.swift */,
1162D16D240FCDAB00647002 /* RegionController.swift */,
116EC0A72410DF48006E4559 /* RegionContainerController.swift */,
1162D16D240FCDAB00647002 /* RegionController.swift */,
1195F19D241D2E7900FE718F /* RegionListController.swift */,
);
path = Controller;
sourceTree = "<group>";
Expand Down Expand Up @@ -327,6 +330,7 @@
116EC0A82410DF48006E4559 /* RegionContainerController.swift in Sources */,
11D637152418D83900BE6557 /* Coordinate.swift in Sources */,
1162D12C240D05F600647002 /* AppDelegate.swift in Sources */,
1195F19E241D2E7900FE718F /* RegionListController.swift in Sources */,
1162D15A240FA46D00647002 /* DataManager.swift in Sources */,
1162D12E240D05F600647002 /* SceneDelegate.swift in Sources */,
1162D156240F934100647002 /* BalloonMarker.swift in Sources */,
Expand Down
191 changes: 188 additions & 3 deletions Corona/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions Corona/Controller/MapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ class MapController: UIViewController {
panelController.move(to: .half, animated: true)
}

func showRegionOnMap(report: Report) {
let region = MKCoordinateRegion(center: report.region.location.clLocation,
span: MKCoordinateSpan(latitudeDelta: 12, longitudeDelta: 12))
mapView.setRegion(region, animated: true)

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
if let annotation = self.currentAnnotations.first(where: { $0.report.region == report.region }) {
self.mapView.selectAnnotation(annotation, animated: true)
}
}
}

private func update() {
allAnnotations = DataManager.instance.allReports
.filter({ $0.stat.confirmedCount > 0 })
Expand Down
67 changes: 67 additions & 0 deletions Corona/Controller/RegionContainerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,43 @@
import UIKit

class RegionContainerController: UIViewController {
var regionListController: RegionListController!
var regionController: RegionController!
var isUpdating: Bool = false {
didSet {
updateTime()
}
}

var isSearching: Bool = false {
didSet {
view.transition(duration: 0.25) {
self.labelTitle.isHidden = self.isSearching
self.labelUpdated.isHidden = self.isSearching
self.buttonSearch.isHidden = self.isSearching
self.searchBar.isHidden = !self.isSearching
self.regionListController.view.isHidden = !self.isSearching
self.regionController.view.isHidden = self.isSearching

if self.isSearching {
self.regionListController.reports = DataManager.instance.allReports
self.searchBar.text = ""
self.searchBar.becomeFirstResponder()
MapController.instance.showRegionScreen()
} else {
self.regionListController.reports = []
self.searchBar.resignFirstResponder()
}
}
}
}

@IBOutlet var effectViewBackground: UIVisualEffectView!
@IBOutlet var effectViewHeader: UIVisualEffectView!
@IBOutlet var labelTitle: UILabel!
@IBOutlet var labelUpdated: UILabel!
@IBOutlet var buttonSearch: UIButton!
@IBOutlet var searchBar: UISearchBar!

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -41,12 +67,18 @@ class RegionContainerController: UIViewController {
Timer.scheduledTimer(withTimeInterval: 20, repeats: true) { _ in
self.updateTime()
}

regionListController.tableView.isHidden = true
regionListController.tableView.delegate = self
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is RegionController {
regionController = segue.destination as? RegionController
}
else if segue.destination is RegionListController {
regionListController = segue.destination as? RegionListController
}
}

func update(report: Report?) {
Expand All @@ -65,4 +97,39 @@ class RegionContainerController: UIViewController {

self.labelUpdated.text = self.regionController.report?.lastUpdate.relativeTimeString
}

@IBAction func buttonSearchTapped(_ sender: Any) {
isSearching = true
}
}

extension RegionContainerController: UISearchBarDelegate, UITableViewDelegate {
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
isSearching = false
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
var reports = DataManager.instance.allReports

let query = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
if !query.isEmpty {
reports = reports.filter({ report in
report.region.name.contains(query)
})
}

regionListController.reports = reports
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let report = regionListController.reports[indexPath.row]

regionController.report = report
regionController.update()

isSearching = false

MapController.instance.hideRegionScreen()
MapController.instance.showRegionOnMap(report: report)
}
}
66 changes: 66 additions & 0 deletions Corona/Controller/RegionListController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// RegionListController.swift
// Corona Tracker
//
// Created by Mohammad on 3/14/20.
// Copyright © 2020 Samabox. All rights reserved.
//

import UIKit

class RegionListController: UITableViewController {
var reports: [Report] = [] {
didSet {
tableView.reloadData()
}
}

override func viewDidLoad() {
super.viewDidLoad()

tableView.rowHeight = 55
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return reports.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = String(describing: RegionCell.self)
let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! RegionCell

let report = reports[indexPath.row]
cell.report = report

return cell
}
}

class RegionCell: UITableViewCell {
var report: Report? {
didSet {
labelName.text = report?.region.name
labelStats.text = report?.stat.confirmedCountString
}
}

@IBOutlet var labelName: UILabel!
@IBOutlet var labelStats: UILabel!

override func awakeFromNib() {
super.awakeFromNib()

backgroundColor = .clear
}
}
12 changes: 6 additions & 6 deletions Corona/Map/ReportAnnotationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ class ReportAnnotationView: MKAnnotationView {
let boldFont = UIFont(descriptor: descriptor!, size: 0)

let string = NSMutableAttributedString()
string.append(NSAttributedString(string: "Confirmed: "))
string.append(NSAttributedString(string: report?.stat.confirmedCountString ?? "",
string.append(.init(string: "Confirmed: "))
string.append(.init(string: report?.stat.confirmedCountString ?? "",
attributes: [.foregroundColor: UIColor.systemOrange, .font: boldFont]))

string.append(NSAttributedString(string: "\nRecovered: "))
string.append(NSAttributedString(string: report?.stat.recoveredCountString ?? "",
string.append(.init(string: "\nRecovered: "))
string.append(.init(string: report?.stat.recoveredCountString ?? "",
attributes: [.foregroundColor : UIColor.systemGreen, .font: boldFont]))

string.append(NSAttributedString(string: "\nDeath: "))
string.append(NSAttributedString(string: report?.stat.deathCountString ?? "",
string.append(.init(string: "\nDeath: "))
string.append(.init(string: report?.stat.deathCountString ?? "",
attributes: [.foregroundColor : UIColor.systemRed, .font: boldFont]))

return string
Expand Down
2 changes: 2 additions & 0 deletions Corona/Service/JHUWebDataService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class JHUWebDataService: DataService {

func fetchReports(completion: @escaping FetchReportsBlock) {
print("Calling API")
URLCache.shared.removeAllCachedResponses()
let request = URLRequest(url: Self.reportsURL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)
_ = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let response = response as? HTTPURLResponse,
Expand Down Expand Up @@ -69,6 +70,7 @@ class JHUWebDataService: DataService {

func fetchTimeSerieses(completion: @escaping FetchTimeSeriesesBlock) {
print("Calling API")
URLCache.shared.removeAllCachedResponses()
let request = URLRequest(url: Self.globalTimeSeriesURL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)
_ = URLSession.shared.dataTask(with: request) { (data, response, error) in
guard let response = response as? HTTPURLResponse,
Expand Down

0 comments on commit 3c25aec

Please sign in to comment.