-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from florianpreknya/BasicStats
Daily stats feature
- Loading branch information
Showing
25 changed files
with
2,107 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// | ||
// A1cView.swift | ||
// nightguard | ||
// | ||
// Created by Florian Preknya on 3/18/19. | ||
// Copyright © 2019 private. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension UIColor { | ||
|
||
// Creates a color-meter like UIColor by placing the given value in correspondence with its good-bad range, resulting a green color if the value is very good on that scale, a red color if is very bad or yellow & compositions with red-green if is in between | ||
static func redYellowGreen(for value: CGFloat, bestValue: CGFloat, worstValue: CGFloat) -> UIColor { | ||
if bestValue < worstValue { | ||
let power = max(min((worstValue - CGFloat(value)) / (worstValue - bestValue), 1), 0) | ||
return UIColor(red: 1 - power, green: power, blue: 0, alpha: 1) | ||
} else { | ||
let power = max(min((bestValue - CGFloat(value)) / (bestValue - worstValue), 1), 0) | ||
return UIColor(red: power, green: 1 - power, blue: 0, alpha: 1) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
A stats view that displays the A1c value, the average BG value, standard deviation & variation values. It appreciates the values by giving a colored feedback to user: green - good values, yellow - okish, red - pretty bad. | ||
*/ | ||
class A1cView: BasicStatsControl { | ||
|
||
override func createPages() -> [StatsPage] { | ||
return [ | ||
StatsPage(name: "A1c", formattedValue: model?.formattedA1c), | ||
StatsPage(name: "Average", formattedValue: model?.formattedAverageGlucose?.replacingOccurrences(of: " ", with: "\n")), | ||
StatsPage(name: "Std Deviation", formattedValue: model?.formattedStandardDeviation?.replacingOccurrences(of: " ", with: "\n")), | ||
StatsPage(name: "Coefficient of Variation", formattedValue: model?.formattedCoefficientOfVariation) | ||
] | ||
} | ||
|
||
fileprivate var a1cColor: UIColor? { | ||
|
||
guard let a1c = model?.a1c else { | ||
return nil | ||
} | ||
|
||
return UIColor.redYellowGreen(for: CGFloat(a1c), bestValue: 5.5, worstValue: 8.5) | ||
} | ||
|
||
fileprivate var variationColor: UIColor? { | ||
|
||
guard let coefficientOfVariation = model?.coefficientOfVariation else { | ||
return nil | ||
} | ||
|
||
return UIColor.redYellowGreen(for: CGFloat(coefficientOfVariation), bestValue: 0.3, worstValue: 0.5) | ||
} | ||
|
||
fileprivate var modelColor: UIColor? { | ||
return (currentPageIndex < 2) ? a1cColor : variationColor | ||
} | ||
|
||
override func commonInit() { | ||
super.commonInit() | ||
|
||
diagramView.dataSource = self | ||
// diagramView.separatorWidh = 8 | ||
// diagramView.separatorColor = .black | ||
// diagramView.startAngle = .pi * 0.75 | ||
// diagramView.endAngle = 2 * .pi + .pi * 0.75 | ||
} | ||
|
||
override func modelWasSet() { | ||
super.modelWasSet() | ||
diagramView.backgroundColor = modelColor?.withAlphaComponent(0.1) | ||
} | ||
|
||
override func pageChanged() { | ||
super.pageChanged() | ||
diagramView.backgroundColor = modelColor?.withAlphaComponent(0.1) | ||
} | ||
} | ||
|
||
extension A1cView: SMDiagramViewDataSource { | ||
|
||
@objc func numberOfSegmentsIn(diagramView: SMDiagramView) -> Int { | ||
return 2 | ||
} | ||
|
||
func diagramView(_ diagramView: SMDiagramView, colorForSegmentAtIndex index: NSInteger, angle: CGFloat) -> UIColor? { | ||
// return (index == 1) ? a1cColor : variationColor | ||
return modelColor | ||
} | ||
|
||
func diagramView(_ diagramView: SMDiagramView, radiusForSegmentAtIndex index: NSInteger, proportion: CGFloat, angle: CGFloat) -> CGFloat { | ||
return (diagramView.frame.size.height - 2/*diagramView.arcWidth*/) / 2 | ||
} | ||
|
||
func diagramView(_ diagramView: SMDiagramView, lineWidthForSegmentAtIndex index: NSInteger, angle: CGFloat) -> CGFloat { | ||
//not called for SMDiagramViewModeSegment | ||
return 2.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.