fix for iphone 10 devices and crash during reading batteryinfo

This commit is contained in:
Johan Degraeve 2019-05-28 23:43:44 +02:00
parent e117e2d207
commit 59ea845462
2 changed files with 66 additions and 30 deletions

View File

@ -106,20 +106,20 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<pickerView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Obg-wT-x7O">
<rect key="frame" x="0.0" y="96" width="320" height="216"/>
<rect key="frame" x="0.0" y="84" width="320" height="216"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="216" id="GHY-FH-oVO"/>
</constraints>
</pickerView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="iIy-f4-6Ww">
<rect key="frame" x="130.5" y="58" width="59" height="30"/>
<rect key="frame" x="130.5" y="46" width="59" height="30"/>
<fontDescription key="fontDescription" type="system" pointSize="25"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" buttonType="roundedRect" adjustsImageSizeForAccessibilityContentSizeCategory="YES" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="0oo-Gz-k2G">
<rect key="frame" x="8" y="320" width="78" height="42"/>
<rect key="frame" x="8" y="308" width="78" height="42"/>
<fontDescription key="fontDescription" name=".SFNSDisplay" family=".SF NS Display" pointSize="25"/>
<state key="normal" title="Cancel">
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
@ -129,7 +129,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="249" contentHorizontalAlignment="right" contentVerticalAlignment="center" buttonType="roundedRect" adjustsImageSizeForAccessibilityContentSizeCategory="YES" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="uqL-UP-KZF">
<rect key="frame" x="256" y="320" width="56" height="42"/>
<rect key="frame" x="256" y="308" width="56" height="42"/>
<fontDescription key="fontDescription" name=".SFNSDisplay" family=".SF NS Display" pointSize="25"/>
<state key="normal" title="Ok">
<color key="titleColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
@ -139,7 +139,7 @@
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bT9-HM-Wcp">
<rect key="frame" x="118.5" y="20" width="83" height="30"/>
<rect key="frame" x="118.5" y="8" width="83" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="wDu-Ww-9fD"/>
</constraints>
@ -151,15 +151,15 @@
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="Obg-wT-x7O" firstAttribute="top" secondItem="iIy-f4-6Ww" secondAttribute="bottom" constant="8" id="BXm-QO-DsV"/>
<constraint firstItem="EeK-ZH-OUl" firstAttribute="bottom" secondItem="0oo-Gz-k2G" secondAttribute="bottom" constant="8" id="DI1-Uz-raw"/>
<constraint firstItem="iIy-f4-6Ww" firstAttribute="top" secondItem="bT9-HM-Wcp" secondAttribute="bottom" constant="8" id="Ecv-M2-lBk" userLabel="Picker View Sub Title.top = Picker View Main Title.bottom + 30"/>
<constraint firstItem="iIy-f4-6Ww" firstAttribute="centerX" secondItem="FhS-oK-gA5" secondAttribute="centerX" id="MqF-Fb-uay"/>
<constraint firstItem="EeK-ZH-OUl" firstAttribute="bottom" secondItem="uqL-UP-KZF" secondAttribute="bottom" constant="8" id="NRt-D9-z33"/>
<constraint firstItem="bT9-HM-Wcp" firstAttribute="top" secondItem="EeK-ZH-OUl" secondAttribute="top" constant="8" id="NT8-5a-jNK"/>
<constraint firstItem="Obg-wT-x7O" firstAttribute="leading" secondItem="EeK-ZH-OUl" secondAttribute="leading" id="OdZ-8Q-Ejb"/>
<constraint firstItem="Obg-wT-x7O" firstAttribute="trailing" secondItem="EeK-ZH-OUl" secondAttribute="trailing" id="WKt-Jy-Djx"/>
<constraint firstItem="0oo-Gz-k2G" firstAttribute="leading" secondItem="EeK-ZH-OUl" secondAttribute="leading" constant="8" id="ZsA-c1-jos"/>
<constraint firstItem="0oo-Gz-k2G" firstAttribute="top" secondItem="Obg-wT-x7O" secondAttribute="bottom" constant="8" id="cDx-iF-l0I"/>
<constraint firstItem="EeK-ZH-OUl" firstAttribute="trailing" secondItem="uqL-UP-KZF" secondAttribute="trailing" constant="8" id="ftH-al-hrH"/>
<constraint firstItem="uqL-UP-KZF" firstAttribute="baseline" secondItem="0oo-Gz-k2G" secondAttribute="baseline" id="je2-Ub-IFg"/>
<constraint firstItem="uqL-UP-KZF" firstAttribute="leading" relation="lessThanOrEqual" secondItem="0oo-Gz-k2G" secondAttribute="trailing" constant="170" id="kaj-af-CvU"/>
<constraint firstItem="bT9-HM-Wcp" firstAttribute="centerX" secondItem="FhS-oK-gA5" secondAttribute="centerX" id="ugA-v5-U8o"/>
</constraints>

View File

@ -28,35 +28,71 @@ enum TransmitterBatteryInfo {
/// used to store TransmitterBatteryInfo as NSData in UserDefaults
///
/// would need to be extended if new cases are added to TransmitterBatteryInfo
init(data:Data) {
if data.count < 1 {fatalError("in TransmitterBatteryInfo init with data, length < 1")}
init?(data:Data) {
if data.count < 1 {return nil}
let type = data.uint8(position: 0)
let uint64size = 8
switch type {
case 0:// percentage
if data.count < 5 {fatalError("in TransmitterBatteryInfo init with data, type is percentage but length < 5")}
let percentage = Int(data.uint64(position: 1))
self = .percentage(percentage: percentage)
case 0,2:// percentage or DexcomG4
// get value,
var percentageOrLevel:Int?
switch data.count {// check total length 5 or 9, if values are stored with 4 bytes, then it will be 5 otherwise 9
case 5:
percentageOrLevel = Int(data.uint32(position: 1))
case 9:
percentageOrLevel = Int(data.uint64(position: 1))
default:
break
}
// if percentageOrLevel found, return it as percentage or DexcomG4 batterylevel
if let percentageOrLevel = percentageOrLevel {
if type == 0 {//percentage
self = .percentage(percentage: percentageOrLevel)
} else {//dexcomG4
self = .DexcomG4(level: percentageOrLevel)
}
} else {
return nil
}
case 1://dexcomg5
if data.count < 21 {fatalError("in TransmitterBatteryInfo init with data, type is dexcomg5 but length < 21")}
let voltageA = Int(data.uint64(position: 1))
let voltageB = Int(data.uint64(position: 1 + uint64size * 1))
let resist = Int(data.uint64(position: 1 + uint64size * 2))
let runtime = Int(data.uint64(position: 1 + uint64size * 3))
let temperature = Int(data.uint64(position: 1 + uint64size * 4))
self = .DexcomG5(voltageA: voltageA, voltageB: voltageB, resist: resist, runtime: runtime, temperature: temperature)
case 2://dexcomG4
if data.count < 5 {fatalError("in TransmitterBatteryInfo init with data, type is dexcomG4 but length < 5")}
let level = Int(data.uint64(position: 1))
self = .DexcomG4(level: level)
// intialize values as nil
var voltageA:Int?
var voltageB:Int?
var resist:Int?
var runtime:Int?
var temperature:Int?
switch data.count {// check total length 5 or 9, if values are stored with 4 bytes, then it will be 5 otherwise 9
case 21:// if values are stored with 4 bytes per it
voltageA = Int(data.uint32(position: 1))
voltageB = Int(data.uint32(position: 1 + 4))
resist = Int(data.uint32(position: 1 + 8))
runtime = Int(data.uint32(position: 1 + 12))
temperature = Int(data.uint32(position: 1 + 16))
case 41:// if values are stored with 8 bytes per it
voltageA = Int(data.uint64(position: 1))
voltageB = Int(data.uint64(position: 1 + 8))
resist = Int(data.uint64(position: 1 + 16))
runtime = Int(data.uint64(position: 1 + 24))
temperature = Int(data.uint64(position: 1 + 32))
default:
break
}
default://assume percentage
if data.count < 5 {fatalError("in TransmitterBatteryInfo init with data, type is percentage but length < 5")}
let percentage = Int(data.uint64(position: 1))
self = .percentage(percentage: percentage)
if let voltageA = voltageA, let voltageB = voltageB, let resist = resist, let runtime = runtime, let temperature = temperature {
self = .DexcomG5(voltageA: voltageA, voltageB: voltageB, resist: resist, runtime: runtime, temperature: temperature)
} else {
return nil
}
default:
return nil
}