Skip to content

Instantly share code, notes, and snippets.

@alexbaramilis
Last active April 16, 2019 13:53
Show Gist options
  • Save alexbaramilis/a10c1b19dc6a5a8151cdca4745f5faa2 to your computer and use it in GitHub Desktop.
Save alexbaramilis/a10c1b19dc6a5a8151cdca4745f5faa2 to your computer and use it in GitHub Desktop.
Populating the Breather UI with sample data.
// MARK: - Properties
private let data = CityConditions.sampleData()
// MARK: - Actions
@IBAction func aqiStandardSegmentedControlValueChanged(_ sender: UISegmentedControl) {
updateAirQualityUI()
}
// MARK: - Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
// MARK: - Methods
private func updateUI() {
cityLabel.text = data.city
updateWeatherUI()
updateAirQualityUI()
updateAsthmaUI()
}
private func updateWeatherUI() {
weatherImageView.image = UIImage(named: Asset.forIconCode(data.weather.iconCode))
temperatureLabel.text = "\(data.weather.temperature)℃"
temperatureLabel.textColor = Color.forTemperature(data.weather.temperature)
humidityLabel.text = "Humidity: \(data.weather.humidity)%"
pressureLabel.text = "Pressure: \(data.weather.pressure) hPa"
windLabel.text = "Wind: \(data.weather.windSpeed) m/s"
let rotationAngle = (CGFloat(data.weather.windDirection) * .pi) / 180.0
windDirectionImageView.transform = CGAffineTransform(rotationAngle: rotationAngle)
}
private func updateAirQualityUI() {
if aqiStandardSegmentedControl.selectedSegmentIndex == 0 {
airQualityLabel.text = Text.forAQI(data.pollution.aqiUS)
airQualityLabel.textColor = Color.forAQI(data.pollution.aqiUS)
aqiLabel.text = "AQI: \(data.pollution.aqiUS)"
let text = "Main pollutant: \(Text.forMainPollutant(data.pollution.mainPollutantUS))"
mainPollutantLabel.setAttributedTextWithSubscripts(text: text,
indicesOfSubscripts: text.indicesOfNumbers)
} else if aqiStandardSegmentedControl.selectedSegmentIndex == 1 {
airQualityLabel.text = Text.forAQI(data.pollution.aqiChina)
airQualityLabel.textColor = Color.forAQI(data.pollution.aqiChina)
aqiLabel.text = "AQI: \(data.pollution.aqiChina)"
let text = "Main pollutant: \(Text.forMainPollutant(data.pollution.mainPollutantChina))"
mainPollutantLabel.setAttributedTextWithSubscripts(text: text,
indicesOfSubscripts: text.indicesOfNumbers)
}
}
private func updateAsthmaUI() {
asthmaRiskLabel.text = data.asthma.risk.capitalized
asthmaRiskLabel.textColor = Color.forAsthmaRisk(data.asthma.risk)
asthmaProbabilityLabel.text = "Probability: \(data.asthma.probability)%"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment