Skip to content

Commit

Permalink
Merge branch 'fix-385-deleteLocalTreatments' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanDegraeve committed Oct 8, 2022
2 parents 084b94a + 64bd7f6 commit b0d29ca
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions xdrip/Managers/NightScout/NightScoutUploadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,10 @@ public class NightScoutUploadManager: NSObject {
// query for treatments older than maxHoursTreatmentsToDownload
let queries = [URLQueryItem(name: "find[created_at][$gte]", value: String(Date(timeIntervalSinceNow: TimeInterval(hours: -ConstantsNightScout.maxHoursTreatmentsToDownload)).ISOStringFromDate()))]

/// treatments that are locally stored (and not marked as deleted), and that are not in the list of downloaded treatments will be locally deleted
/// - only for latest treatments less than maxHoursTreatmentsToDownload old
var didFindTreatmentInDownload = [Bool]( repeating: false, count: treatmentsToSync.count )

getOrDeleteRequest(path: nightScoutTreatmentPath, queries: queries, httpMethod: nil) { (data: Data?, nightScoutResult: NightScoutResult) in

guard nightScoutResult.successFull() else {
Expand Down Expand Up @@ -933,14 +937,34 @@ public class NightScoutUploadManager: NSObject {

trace(" %{public}@ treatmentEntries found in response which were not yet marked as uploaded", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .info, amountMarkedAsUploaded.description)

let amountOfUpdatedTreaments = self.checkIfChangedAtNightscout(forTreatmentEntries: treatmentsToSync, inTreatmentNSResponses: treatmentNSResponses)
let amountOfUpdatedTreaments = self.checkIfChangedAtNightscout(forTreatmentEntries: treatmentsToSync, inTreatmentNSResponses: treatmentNSResponses, didFindTreatmentInDownload: &didFindTreatmentInDownload)

trace(" %{public}@ treatmentEntries found that were updated at NS and updated locally", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .info, amountOfUpdatedTreaments.description)

// now for each treatmentEntry, less than maxHoursTreatmentsToDownload, check if it was found in the NS response
// if not, it means it's been deleted at NS, also do the local deletion
// only treatments that were successfully uploaded before

// to keep track of amount of locally deleted treatmentEntries
var amountOfLocallyDeletedTreatments = 0

for (index, entry) in treatmentsToSync.enumerated() {

if abs(entry.date.timeIntervalSinceNow) < ConstantsNightScout.maxHoursTreatmentsToDownload * 3600.0 {

if !didFindTreatmentInDownload[index] && !entry.treatmentdeleted && entry.uploaded {
entry.treatmentdeleted = true
amountOfLocallyDeletedTreatments = amountOfLocallyDeletedTreatments + 1
}

}
}
trace(" %{public}@ treatmentEntries that were not found anymore at NS and deleted locally", log: self.oslog, category: ConstantsLog.categoryNightScoutUploadManager, type: .info, amountOfLocallyDeletedTreatments.description)

self.coreDataManager.saveChanges()

// call completion handler with success, if amount and/or amountOfNewTreatments > 0 then it's success withlocalchanges
completionHandler(.success(amountOfUpdatedTreaments + amountOfNewTreatments))
// call completion handler with success, if amount and/or amountOfNewTreatments > 0 then it's success with localchanges
completionHandler(.success(amountOfUpdatedTreaments + amountOfNewTreatments + amountOfLocallyDeletedTreatments))

}

Expand Down Expand Up @@ -1340,13 +1364,14 @@ public class NightScoutUploadManager: NSObject {
/// - returns:amount of locally updated treatmentEntries
///
/// - !! does not save to coredata
private func checkIfChangedAtNightscout(forTreatmentEntries treatmentEntries: [TreatmentEntry], inTreatmentNSResponses treatmentNSResponses: [TreatmentNSResponse]) -> Int {
/// - while the function iterates through treatmentEntries, didFindTreatmentInDownload will be updated and corresponding value will be set to true if a treatment is found at NS
private func checkIfChangedAtNightscout(forTreatmentEntries treatmentEntries: [TreatmentEntry], inTreatmentNSResponses treatmentNSResponses: [TreatmentNSResponse], didFindTreatmentInDownload: inout [Bool]) -> Int {

// used to trace how many new treatmenEntries are locally updated
var amountOfUpdatedTreatmentEntries = 0

// iterate through treatmentEntries
for treatmentEntry in treatmentEntries {
for (index, treatmentEntry) in treatmentEntries.enumerated() {

// only handle treatmentEntries that are already uploaded
if treatmentEntry.uploaded && treatmentEntry.id != TreatmentEntry.EmptyId {
Expand All @@ -1357,6 +1382,9 @@ public class NightScoutUploadManager: NSObject {
// find matching id
if treatmentNSResponse.id == treatmentEntry.id {

// found the treatment in NS response, set didFindTreatmentInDownload to true for that treatment
didFindTreatmentInDownload[index] = true

var treatmentUpdated = false

// check value, type and date. If NS has any difference, then update locally
Expand Down

0 comments on commit b0d29ca

Please sign in to comment.