fix related to restrictreadings : fixing filter in Array for BgReading's, also WatchManager and HealthKitManager changed so that last disconnect is not taken in account

This commit is contained in:
Johan Degraeve 2021-01-01 22:15:00 +01:00
parent 78b00a56a9
commit 2f9d7acf21
5 changed files with 41 additions and 20 deletions

View File

@ -3544,7 +3544,7 @@
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 4.4.15;
PRODUCT_BUNDLE_IDENTIFIER = "net.johandegraeve.xdripswifttest.xDrip4iOS-Widget";
PRODUCT_BUNDLE_IDENTIFIER = "net.johandegraeve.xdripswift.xDrip4iOS-Widget";
PRODUCT_NAME = xDrip4iOS;
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
@ -3569,7 +3569,7 @@
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 4.4.15;
PRODUCT_BUNDLE_IDENTIFIER = "net.johandegraeve.xdripswifttest.xDrip4iOS-Widget";
PRODUCT_BUNDLE_IDENTIFIER = "net.johandegraeve.xdripswift.xDrip4iOS-Widget";
PRODUCT_NAME = xDrip4iOS;
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
@ -3717,7 +3717,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 4.4.15;
PRODUCT_BUNDLE_IDENTIFIER = net.johandegraeve.xdripswifttest;
PRODUCT_BUNDLE_IDENTIFIER = net.johandegraeve.xdripswift;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "xdrip/xdrip-Bridging-Header.h";
@ -3747,7 +3747,7 @@
"@executable_path/Frameworks",
);
MARKETING_VERSION = 4.4.15;
PRODUCT_BUNDLE_IDENTIFIER = net.johandegraeve.xdripswifttest;
PRODUCT_BUNDLE_IDENTIFIER = net.johandegraeve.xdripswift;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
SWIFT_OBJC_BRIDGING_HEADER = "xdrip/xdrip-Bridging-Header.h";

View File

@ -120,7 +120,8 @@ extension Array where Element: GlucoseData {
extension Array where Element: BgReading {
/// Filter out readings that are too close to each other
/// - Filter out readings that are too close to each other
/// - BgReadings array must be sorted by timeStamp of the BgReading, ascending, ie youngest first
/// - parameters:
/// - minimumTimeBetweenTwoReadingsInMinutes : filter out readings that are to close to each other in time, minimum difference in time between two readings = minimumTimeBetweenTwoReadingsInMinutes
/// - lastConnectionStatusChangeTimeStamp : lastConnectionStatusChangeTimeStamp > timeStampLastProcessedBgReading then the first connection will be returned, even if it's less than minimumTimeBetweenTwoReadingsInMinutes away from timeStampLastProcessedBgReading
@ -133,15 +134,21 @@ extension Array where Element: BgReading {
var timeStampLatestCheckedReading = timeStampLastProcessedBgReading
return self.filter({
// create a copy of self, reversed, because the filter algorithm assumes the first is the oldest element, while self is order by youngest first
var arrayReversed = Array(self.reversed())
// do the required filtering
arrayReversed = arrayReversed.filter({
if let lastConnectionStatusChangeTimeStamp = lastConnectionStatusChangeTimeStamp, let timeStampLastProcessedBgReading = timeStampLastProcessedBgReading, !didCheckLastConnectionStatusChangeTimeStamp {
didCheckLastConnectionStatusChangeTimeStamp = true
// if there was a disconnect or reconnect after the latest processed reading, then add this reading - this will only apply to the first reading
if lastConnectionStatusChangeTimeStamp.timeIntervalSince(timeStampLastProcessedBgReading) > 0.0 {
timeStampLatestCheckedReading = $0.timeStamp
return true
}
@ -154,12 +161,16 @@ extension Array where Element: BgReading {
returnValue = $0.timeStamp.timeIntervalSince(timeStampLatestCheckedReading) > minimumTimeBetweenTwoReadingsInMinutes * 60.0
}
timeStampLatestCheckedReading = $0.timeStamp
if returnValue {
timeStampLatestCheckedReading = $0.timeStamp
}
return returnValue
})
return Array(arrayReversed.reversed())
}

View File

@ -50,7 +50,7 @@ public class HealthKitManager:NSObject {
healthKitInitialized = initializeHealthKit()
// do first store
storeBgReadings(lastConnectionStatusChangeTimeStamp: nil)
storeBgReadings()
}
@ -95,9 +95,7 @@ public class HealthKitManager:NSObject {
}
/// stores latest readings in healthkit, only if HK supported, authorized, enabled in settings
/// - parameters:
/// - lastConnectionStatusChangeTimeStamp : when was the last transmitter dis/reconnect - if nil then 1 1 1970 is used
public func storeBgReadings(lastConnectionStatusChangeTimeStamp: Date?) {
public func storeBgReadings() {
// healthkit setting must be on, and healthkit must be initialized successfully
if !UserDefaults.standard.storeReadingsInHealthkit || !healthKitInitialized {
@ -108,7 +106,7 @@ public class HealthKitManager:NSObject {
guard let bloodGlucoseType = bloodGlucoseType else {return}
// get readings to store, limit to 2016 = maximum 1 week - just to avoid a huge array is being returned here, applying minimumTimeBetweenTwoReadingsInMinutes filter
let bgReadingsToStore = bgReadingsAccessor.getLatestBgReadings(limit: 2016, fromDate: UserDefaults.standard.timeStampLatestHealthKitStoreBgReading, forSensor: nil, ignoreRawData: true, ignoreCalculatedValue: false).filter(minimumTimeBetweenTwoReadingsInMinutes: ConstantsHealthKit.minimiumTimeBetweenTwoReadingsInMinutes, lastConnectionStatusChangeTimeStamp: lastConnectionStatusChangeTimeStamp, timeStampLastProcessedBgReading: UserDefaults.standard.timeStampLatestHealthKitStoreBgReading)
let bgReadingsToStore = bgReadingsAccessor.getLatestBgReadings(limit: 2016, fromDate: UserDefaults.standard.timeStampLatestHealthKitStoreBgReading, forSensor: nil, ignoreRawData: true, ignoreCalculatedValue: false).filter(minimumTimeBetweenTwoReadingsInMinutes: ConstantsHealthKit.minimiumTimeBetweenTwoReadingsInMinutes, lastConnectionStatusChangeTimeStamp: nil, timeStampLastProcessedBgReading: UserDefaults.standard.timeStampLatestHealthKitStoreBgReading)
let bloodGlucoseUnit = HKUnit.init(from: "mg/dL")
@ -151,7 +149,7 @@ public class HealthKitManager:NSObject {
healthKitInitialized = initializeHealthKit()
// doesn't matter which if the two settings got changed, it's ok to call initialize
storeBgReadings(lastConnectionStatusChangeTimeStamp: nil)
storeBgReadings()
}

View File

@ -18,6 +18,9 @@ class WatchManager: NSObject {
/// to create and delete events
private let eventStore = EKEventStore()
/// timestamp of last reading for which calendar event is created, initially set to 1 jan 1970
private var timeStampLastProcessedReading = Date(timeIntervalSince1970: 0.0)
// MARK: - initializer
init(coreDataManager: CoreDataManager) {
@ -31,7 +34,7 @@ class WatchManager: NSObject {
/// process new readings
/// - lastConnectionStatusChangeTimeStamp : when was the last transmitter dis/reconnect - if nil then 1 1 1970 is used
public func processNewReading(lastConnectionStatusChangeTimeStamp: Date?) {
public func processNewReading(lastConnectionStatusChangeTimeStamp: Date) {
// check if createCalenderEvent is enabled in the settings and if so create calender event
if UserDefaults.standard.createCalendarEvent {
@ -42,7 +45,7 @@ class WatchManager: NSObject {
// MARK: - private functions
private func createCalendarEvent(lastConnectionStatusChangeTimeStamp: Date?) {
private func createCalendarEvent(lastConnectionStatusChangeTimeStamp: Date) {
// check that access to calendar is authorized by the user
guard EKEventStore.authorizationStatus(for: .event) == .authorized else {
@ -57,7 +60,7 @@ class WatchManager: NSObject {
}
// get 2 last Readings, with a calculatedValue
let lastReading = bgReadingsAccessor.get2LatestBgReadings(minimumTimeIntervalInMinutes: 4.0).filter(minimumTimeBetweenTwoReadingsInMinutes: ConstantsWatch.minimiumTimeBetweenTwoReadingsInMinutes, lastConnectionStatusChangeTimeStamp: lastConnectionStatusChangeTimeStamp, timeStampLastProcessedBgReading: nil)
let lastReading = bgReadingsAccessor.get2LatestBgReadings(minimumTimeIntervalInMinutes: 4.0)
// there should be at least one reading
guard lastReading.count > 0 else {
@ -65,6 +68,15 @@ class WatchManager: NSObject {
return
}
// check if timeStampLastProcessedReading is at least minimiumTimeBetweenTwoReadingsInMinutes earlier than now (or it's at least minimiumTimeBetweenTwoReadingsInMinutes minutes ago that event was created for reading) - otherwise don't create event
// exception : there's been a disconnect/reconnect after the last spoken reading
if (abs(timeStampLastProcessedReading.timeIntervalSince(Date())) < ConstantsWatch.minimiumTimeBetweenTwoReadingsInMinutes * 60.0 && lastConnectionStatusChangeTimeStamp.timeIntervalSince(timeStampLastProcessedReading) < 0) {
trace("in createCalendarEvent, no new calendar event will be created because it's too close to previous event", log: log, category: ConstantsLog.categoryWatchManager, type: .info)
return
}
// latest reading should be less than 5 minutes old
guard abs(lastReading[0].timeStamp.timeIntervalSinceNow) < 5 * 60 else {
trace("in createCalendarEvent, the latest reading is older than 5 minutes", log: log, category: ConstantsLog.categoryWatchManager, type: .info)

View File

@ -779,7 +779,7 @@ final class RootViewController: UIViewController {
nightScoutUploadManager?.upload(lastConnectionStatusChangeTimeStamp: lastConnectionStatusChangeTimeStamp())
healthKitManager?.storeBgReadings(lastConnectionStatusChangeTimeStamp: lastConnectionStatusChangeTimeStamp())
healthKitManager?.storeBgReadings()
bgReadingSpeaker?.speakNewReading(lastConnectionStatusChangeTimeStamp: lastConnectionStatusChangeTimeStamp())
@ -1815,7 +1815,7 @@ extension RootViewController:NightScoutFollowerDelegate {
checkAlertsCreateNotificationAndSetAppBadge()
if let healthKitManager = healthKitManager {
healthKitManager.storeBgReadings(lastConnectionStatusChangeTimeStamp: lastConnectionStatusChangeTimeStamp())
healthKitManager.storeBgReadings()
}
if let bgReadingSpeaker = bgReadingSpeaker {