1

I am showing daily step information in one of the tabs. Unfortunately, when I select the steps tab again it adds one more of the same data below the previous one.

enter image description here

I have tried to solve it via toggle a boolean. But it did not help either.

import SwiftUI
import HealthKit

struct StepView: View {
    private var healthStore: HealthStore?
    @State private var presentClipboardView = true
    @State private var steps: [Step] = [Step]()
    init() {
        healthStore = HealthStore()
    }
    private func updateUIFromStatistics(_ statisticsCollection: HKStatisticsCollection) {
        let now = Date()
        let startOfDay = Calendar.current.startOfDay(for: now)
        statisticsCollection.enumerateStatistics(from: startOfDay, to: now) { (statistics, stop) in
            let count = statistics.sumQuantity()?.doubleValue(for: .count())
            let step = Step(count: Int(count ?? 0), date: statistics.startDate, wc: Double(count ?? 0 / 1000 ))
            steps.append(step)
        }
    }
    var body: some View {  
        VStack {
            ForEach(steps, id: \.id) { step in
                VStack {
                        HStack{
                            Text("WC")
                            Text("\(step.wc)")
                        }
                        HStack {
                            Text("\(step.count)")
                            Text("Total Steps")
                        }
                        Text(step.date, style: .date)
                            .opacity(0.5)
                        Spacer()
                }
            }
            .navigationBarBackButtonHidden(true)
        }
        .onAppear() {
            if let healthStore = healthStore {
                healthStore.requestAuthorization { (success) in
                    if success {
                        healthStore.calculateSteps { (statisticsCollection) in
                            if let statisticsCollection = statisticsCollection {
                                updateUIFromStatistics(statisticsCollection)
                            }
                        }
                    }
                }
            }
        }
        .onDisappear() {
            self.presentClipboardView.toggle()
        }
    }
}

Step Model

struct Step: Identifiable {
    let id = UUID()
    let count: Int?
    let date: Date
    let wc: Double
}

HealthStore file

class HealthStore {
    var healthStore: HKHealthStore?
    var query: HKStatisticsCollectionQuery?
    init() {
        if HKHealthStore.isHealthDataAvailable() {
            healthStore = HKHealthStore()
        }
    }
    func calculateSteps(completion: @escaping (HKStatisticsCollection?) -> Void ) {
        let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
        let now = Date()
        let startOfDay = Calendar.current.startOfDay(for: now)
        let daily = DateComponents(day:1)
        let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: Date(), options: .strictStartDate)
        query = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: startOfDay, intervalComponents: daily)
        query!.initialResultsHandler = { query, statisticCollection, error in
            completion(statisticCollection)
        }
        if let healthStore = healthStore, let query = self.query {
            healthStore.execute(query)
        }
    }
    
    func requestAuthorization(completion: @escaping (Bool) -> Void) {
        let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
        guard let healthStore = self.healthStore else { return completion (false) }
        healthStore.requestAuthorization(toShare: [], read: [stepType]) { (success, error) in
            completion(success)
        }
    }
}

Those are all related files according to my problem. Thanks in advance.

pawello2222
  • 46,897
  • 22
  • 145
  • 209
Mert Köksal
  • 817
  • 1
  • 7
  • 26

1 Answers1

1

The steps is annotated with @State which means it will be persisted even when the view is redrawn.

And you never reset it. You only append new steps. Try clearing steps in updateUIFromStatistics:

private func updateUIFromStatistics(_ statisticsCollection: HKStatisticsCollection) {
    steps = [] // remove previous values
    let now = Date()
    let startOfDay = Calendar.current.startOfDay(for: now)
    statisticsCollection.enumerateStatistics(from: startOfDay, to: now) { (statistics, stop) in
        let count = statistics.sumQuantity()?.doubleValue(for: .count())
        let step = Step(count: Int(count ?? 0), date: statistics.startDate, wc: Double(count ?? 0 / 1000 ))
        steps.append(step)
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209