Skip to content

Commit

Permalink
feature: Follow selected resolution in all chart settings #1996 (#2010)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyonto authored May 2, 2024
1 parent ccec430 commit 37ef7cf
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,7 @@ class TagChartsMarkerView: MarkerImage {
}

override func refreshContent(entry: ChartDataEntry, highlight _: Highlight) {
var value = ""
switch type {
case .temperature:
value = measurementService.stringWithoutSign(temperature: entry.y)
case .humidity:
value = measurementService.stringWithoutSign(humidity: entry.y)
case .pressure:
value = measurementService.stringWithoutSign(pressure: entry.y)
default: break
}

let value = GlobalHelpers().formattedString(from: entry.y.round(to: 2))
labelText = value + " " + unit
+ "\n" +
AppDateFormatter.shared.graphMarkerDateString(from: entry.x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import RuuviOntology
import RuuviService
import UIKit

// swiftlint:disable file_length

protocol TagChartsViewDelegate: NSObjectProtocol {
func chartDidTranslate(_ chartView: TagChartsView)
func chartValueDidSelect(
Expand Down Expand Up @@ -310,16 +312,20 @@ extension TagChartsView {
min: Double,
max: Double,
avg: Double,
type _: MeasurementType
type: MeasurementType,
measurementService: RuuviServiceMeasurement
) {
let roundedTo = 2
let minText = RuuviLocalization.chartStatMin + ": " +
GlobalHelpers().formattedString(from: min.round(to: roundedTo))
let maxText = RuuviLocalization.chartStatMax + ": " +
GlobalHelpers().formattedString(from: max.round(to: roundedTo))
let avgText = RuuviLocalization.chartStatAvg + ": " +
GlobalHelpers().formattedString(from: avg.round(to: roundedTo))

let (
minText,
maxText,
avgText
) = createMeasurementStrings(
type: type,
min: min,
max: max,
avg: avg,
measurementService: measurementService
)
chartMinMaxAvgLabel.text = minText + " " + maxText + " " + avgText
}

Expand Down Expand Up @@ -355,3 +361,72 @@ extension TagChartsView {
return min(leftAxis.axisMaximum, Double(pt.y))
}
}

extension TagChartsView {

private func createMeasurementStrings(
type: MeasurementType,
min: Double?,
max: Double?,
avg: Double?,
measurementService: RuuviServiceMeasurement
) ->
// swiftlint:disable:next large_tuple
(
String,
String,
String
) {
let minValue = formattedMeasurementString(
for: type,
value: min,
measurementService: measurementService
)
let minText = RuuviLocalization.chartStatMin + ": " + minValue

let maxValue = formattedMeasurementString(
for: type,
value: max,
measurementService: measurementService
)
let maxText = RuuviLocalization.chartStatMax + ": " + maxValue

let avgValue = formattedMeasurementString(
for: type,
value: avg,
measurementService: measurementService
)
let avgText = RuuviLocalization.chartStatAvg + ": " + avgValue

return (
minText,
maxText,
avgText
)
}

private func formattedMeasurementString(
for type: MeasurementType,
value: Double?,
measurementService: RuuviServiceMeasurement
) -> String {
switch type {
case .temperature:
return measurementService.stringWithoutSign(
temperature: value
)
case .humidity:
return measurementService.stringWithoutSign(
humidity: value
)
case .pressure:
return measurementService.stringWithoutSign(
pressure: value
)
default:
return ""
}
}
}

// swiftlint:enable file_length
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,8 @@ extension TagChartsViewController {
min: minVisibleYValue,
max: maxVisibleYValue,
avg: averageYValue,
type: type
type: type,
measurementService: measurementService
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ struct GlobalHelpers {
}
}

func formattedString(from double: Double) -> String {
func formattedString(from double: Double, toPlace: Int = 2) -> String {
let formatter = NumberFormatter()
formatter.locale = Locale.autoupdatingCurrent
formatter.numberStyle = .decimal
formatter.minimumFractionDigits = toPlace
formatter.maximumFractionDigits = toPlace
return formatter.string(from: NSNumber(value: double)) ?? "0"
}
}
2 changes: 1 addition & 1 deletion station.localization

0 comments on commit 37ef7cf

Please sign in to comment.