Commit d735d7db by Dmitriy Stepanets

Performance changes

parent 4656d2b3
......@@ -696,9 +696,9 @@
CD5D231B2615C6CA00B549DA /* Buttons */ = {
isa = PBXGroup;
children = (
CD3F6E6B25FA5A90002DB99B /* PeriodButtonProtocol.swift */,
CDEE8AD625DA882200C289DE /* ForecastPeriodButton.swift */,
CD3F6E6825FA59D4002DB99B /* ForecastDetailPeriodButton.swift */,
CD3F6E6B25FA5A90002DB99B /* PeriodButtonProtocol.swift */,
CDE2BF242609D9140085C930 /* ForecastWindButton.swift */,
);
path = Buttons;
......
......@@ -12,7 +12,7 @@
<key>OneWeatherNotificationServiceExtension.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>49</integer>
<integer>51</integer>
</dict>
<key>PG (Playground) 1.xcscheme</key>
<dict>
......
......@@ -35,7 +35,6 @@ public class PushNotificationsManager: NSObject {
// TODO: forced re-register on timeout
public func registerForRemoteNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { [weak self]
(granted, error) in
......@@ -47,7 +46,9 @@ public class PushNotificationsManager: NSObject {
self.log.info("Granted: \(granted)")
}
MoEngage.sharedInstance().registerForRemoteNotification(withCategories: nil, withUserNotificationCenterDelegate: self)
onMain {
MoEngage.sharedInstance().registerForRemoteNotification(withCategories: nil, withUserNotificationCenterDelegate: self)
}
}
}
......
......@@ -57,6 +57,11 @@ class ForecastDetailPeriodButton: UIControl, PeriodButtonProtocol {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: 12).cgPath
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
updateUI()
......
......@@ -62,6 +62,11 @@ class ForecastPeriodButton: UIControl, PeriodButtonProtocol {
updateUI()
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: 12).cgPath
}
override var isSelected: Bool {
didSet {
if isSelected {
......
......@@ -50,6 +50,11 @@ class ForecastWindButton: UIControl, PeriodButtonProtocol {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: 12).cgPath
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
updateUI()
......
......@@ -107,8 +107,6 @@ class ForecastTimePeriodView: UIView {
}
private func rebuildButtons(buttonType: PeriodButtonProtocol.Type) {
stackView.removeAll()
let numberOfButtons:Int
switch currentForecastType {
case .daily:
......@@ -117,22 +115,49 @@ class ForecastTimePeriodView: UIView {
numberOfButtons = hourly.count
}
for index in 0..<numberOfButtons {
let forecastButton = buttonType.init()
switch currentForecastType {
case .daily:
forecastButton.configure(dailyWeather: daily[index])
case .hourly, .wind:
forecastButton.configure(hourlyWeather: hourly[index])
if stackView.arrangedSubviews.count == numberOfButtons {
for (index, arrangedSubview) in stackView.arrangedSubviews.enumerated() {
guard let periodButton = arrangedSubview as? PeriodButtonProtocol else {
continue
}
switch currentForecastType {
case .daily:
periodButton.configure(dailyWeather: daily[index])
case .hourly, .wind:
periodButton.configure(hourlyWeather: hourly[index])
}
}
}
else {
let diff = stackView.arrangedSubviews.count - numberOfButtons
for _ in 0..<abs(diff) {
if diff > 0 {
//Remove present
stackView.arrangedSubviews[0].removeFromSuperview()
}
else {
//Add new
let forecastButton = buttonType.init()
forecastButton.addTarget(self, action: #selector(handleForecastButton(button:)), for: .touchUpInside)
stackView.addArrangedSubview(forecastButton)
forecastButton.snp.makeConstraints { (make) in
make.height.equalToSuperview()
}
}
}
forecastButton.index = index
forecastButton.addTarget(self, action: #selector(handleForecastButton(button:)), for: .touchUpInside)
forecastButton.isSelected = index == 0
stackView.addArrangedSubview(forecastButton)
forecastButton.snp.makeConstraints { (make) in
make.height.equalToSuperview()
//Rebuild buttons index
for (index, subview) in stackView.arrangedSubviews.enumerated() {
guard let forecastButton = subview as? PeriodButtonProtocol else {
continue
}
forecastButton.isSelected = index == 0
forecastButton.index = index
}
self.rebuildButtons(buttonType: buttonType)
}
stackView.layoutIfNeeded()
......
......@@ -33,33 +33,79 @@ class PrecipitationCell: UITableViewCell {
}
public func configure(with dayily:[DailyWeather]) {
self.stackView.removeAll()
//TODO: Hide button for now
headingButton.isHidden = true
for index in 0..<dayily.count {
let precipButton = PrecipButton()
precipButton.isSelected = index == 1
if stackView.arrangedSubviews.count != dayily.count {
let diff = stackView.arrangedSubviews.count - dayily.count
for _ in 0..<abs(diff) {
if diff > 0 {
//Remove present
stackView.arrangedSubviews[0].removeFromSuperview()
}
else {
//Add new
let precipButton = PrecipButton()
precipButton.addTarget(self, action: #selector(handlePrecipButton(button:)), for: .touchUpInside)
stackView.addArrangedSubview(precipButton)
}
}
//Reset selected index
stackView.arrangedSubviews.enumerated().forEach {
if let button = $1 as? PrecipButton {
button.isSelected = $0 == 1
}
}
stackView.layoutIfNeeded()
}
for (index, button) in stackView.arrangedSubviews.enumerated() {
guard let precipButton = button as? PrecipButton else {
continue
}
precipButton.configure(with: dayily[index])
precipButton.addTarget(self, action: #selector(handlePrecipButton(button:)), for: .touchUpInside)
stackView.addArrangedSubview(precipButton)
}
stackView.layoutIfNeeded()
}
public func configure(with hourly:[HourlyWeather]) {
self.stackView.removeAll()
self.headingLabel.font = AppFont.SFPro.bold(size: 18)
self.headingButton.isHidden = true
self.headingLabel.text = "precipitation.title".localized().capitalized
self.headingLabel.textColor = ThemeManager.currentTheme.primaryTextColor
for index in 0..<hourly.count {
let precipButton = PrecipButton()
precipButton.isSelected = index == 0
if stackView.arrangedSubviews.count != hourly.count {
let diff = stackView.arrangedSubviews.count - hourly.count
for _ in 0..<abs(diff) {
if diff > 0 {
//Remove present
stackView.arrangedSubviews[0].removeFromSuperview()
}
else {
//Add new
let precipButton = PrecipButton()
precipButton.addTarget(self, action: #selector(handlePrecipButton(button:)), for: .touchUpInside)
stackView.addArrangedSubview(precipButton)
}
}
//Reset selected index
stackView.arrangedSubviews.enumerated().forEach {
if let button = $1 as? PrecipButton {
button.isSelected = $0 == 1
}
}
stackView.layoutIfNeeded()
}
for (index, button) in stackView.arrangedSubviews.enumerated() {
guard let precipButton = button as? PrecipButton else {
continue
}
precipButton.configure(with: hourly[index])
precipButton.addTarget(self, action: #selector(handlePrecipButton(button:)), for: .touchUpInside)
stackView.addArrangedSubview(precipButton)
}
stackView.layoutIfNeeded()
}
//Private
......
......@@ -65,22 +65,6 @@ class TodayAirQualityCell: UITableViewCell {
airDescLabel.text = health?.airQuality?.advice
//Fill pollutions
stackView.removeAll()
if let pollutants = health?.pollutants.values, let maxPollutantProgress: CGFloat = pollutants.max(by: { $0.progress < $1.progress })?.progress {
var coefficient: CGFloat = 1.0
let desiredMinProgress: CGFloat = 0.3
if maxPollutantProgress < desiredMinProgress {
coefficient = desiredMinProgress / maxPollutantProgress
}
for pollutant in pollutants {
let pollutionView = PollutantView()
pollutionView.configure(pollutant: pollutant, progressCoefficient: coefficient)
stackView.addArrangedSubview(pollutionView)
}
}
stackView.layoutIfNeeded()
//Progress
guard let airQuality = health?.airQuality else {
return
......@@ -91,6 +75,60 @@ class TodayAirQualityCell: UITableViewCell {
value: Double(airQuality.progress / 2)),
AirQualityDataType(color: airQuality.status.gradientColorEnd,
value: Double(airQuality.progress / 2))]
//Fill pollutions
guard
let pollutants = health?.pollutants.values,
let maxPollutantProgress: CGFloat = (pollutants.max{$0.progress < $1.progress})?.progress
else {
return
}
if stackView.arrangedSubviews.count != pollutants.count {
let diff = stackView.arrangedSubviews.count - pollutants.count
for _ in 0..<abs(diff) {
if diff > 0 {
//Remove present
stackView.arrangedSubviews[0].removeFromSuperview()
}
else {
//Add new
let pollutionView = PollutantView()
stackView.addArrangedSubview(pollutionView)
}
}
stackView.layoutIfNeeded()
}
var coefficient: CGFloat = 1.0
let desiredMinProgress: CGFloat = 0.3
if maxPollutantProgress < desiredMinProgress {
coefficient = desiredMinProgress / maxPollutantProgress
}
for (index, pollutant) in pollutants.enumerated() {
guard let pollutantView = stackView.arrangedSubviews[index] as? PollutantView else {
continue
}
pollutantView.configure(pollutant: pollutant, progressCoefficient: coefficient)
}
// stackView.removeAll()
// if let pollutants = health?.pollutants.values,
// let maxPollutantProgress: CGFloat = pollutants.max(by: { $0.progress < $1.progress })?.progress {
// var coefficient: CGFloat = 1.0
// let desiredMinProgress: CGFloat = 0.3
// if maxPollutantProgress < desiredMinProgress {
// coefficient = desiredMinProgress / maxPollutantProgress
// }
// for pollutant in pollutants {
// let pollutionView = PollutantView()
// pollutionView.configure(pollutant: pollutant, progressCoefficient: coefficient)
// stackView.addArrangedSubview(pollutionView)
// }
// }
// stackView.layoutIfNeeded()
}
}
......
......@@ -28,6 +28,11 @@ class TodayConditionButton: UIControl {
prepareLabels()
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.shadowPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: 12).cgPath
}
public func configure(with location:Location) {
switch conditionType {
case .precipitation:
......
......@@ -53,23 +53,38 @@ class TodayDayTimesCell: UITableViewCell {
//Public
public func configure(with location: Location) {
stackView.removeAll()
let maxNumberOfItems = 4
var currentlyShowing = 0
var i = 0
while i < location.dayTimeForecast.count && currentlyShowing < maxNumberOfItems {
let dayTimeWeather = location.dayTimeForecast[i]
i += 1
if dayTimeWeather.date >= Date() {
let view = DayTimeView(dayTimeWeather: dayTimeWeather,
withSeparator: currentlyShowing != maxNumberOfItems - 1)
guard !location.dayTimeForecast.isEmpty else {
return
}
var validDayTimeWeather = [DayTimeWeather]()
for index in 0..<location.dayTimeForecast.count {
if location.dayTimeForecast[index].date >= Date() {
validDayTimeWeather.append(location.dayTimeForecast[index])
}
}
if stackView.arrangedSubviews.isEmpty {
for index in 0..<maxNumberOfItems {
let view = DayTimeView(dayTimeWeather: validDayTimeWeather[index],
withSeparator: index != maxNumberOfItems - 1)
stackView.addArrangedSubview(view)
currentlyShowing += 1
}
stackView.layoutIfNeeded()
return
}
if stackView.arrangedSubviews.count == maxNumberOfItems {
for (index, arrangedSubview) in stackView.arrangedSubviews.enumerated() {
guard let dayTimeView = arrangedSubview as? DayTimeView else {
continue
}
dayTimeView.configure(with: validDayTimeWeather[index])
}
}
stackView.layoutIfNeeded()
}
}
......
......@@ -41,6 +41,11 @@ class TodayForecastCell: UITableViewCell {
updateUI()
}
override func layoutSubviews() {
super.layoutSubviews()
container.layer.shadowPath = UIBezierPath(roundedRect: container.bounds, cornerRadius: 12).cgPath
}
//Public
public func configure(with location:Location) {
cityImageView.image = WeatherImageProvider().imageFor(location: location)
......
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