Mert Köksal
Mert Köksal

Reputation: 901

Cannot convert value of type 'Int?' to expected argument type 'Binding<Int>' SwiftUI

I created a circularprogress view to be able to show a progress bar according to the steps data. But for some reason I can not reach to the step.count inside my stepView file.

This is my StepView

    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 ?? 0)")
                                Text("Total Steps")
                            }
                            Text(step.date, style: .date)
                                .opacity(0.5)
                        CircularProgress(steps: step.count) //ERROR
                            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()
            }
        }
    }

and this is my circularprogress view

    struct CircularProgress: View {
    var steps: Binding<Int>
    var body: some View {
        ZStack {
            Color.progressBarColor
                .edgesIgnoringSafeArea(.all)
            VStack {
                ZStack {
                    Label()
                    Outline(steps: steps)
                }
            }
        }
    }
}

struct Label: View {
    var percentage: CGFloat = 20
    var body : some View {
        ZStack {
            Text(String(format: "%.0f", percentage))
                .font(Font.custom("SFCompactDisplay-Bold", size: 56))
        }
    }
}

struct Outline: View {
    var steps: Binding<Int>
    var percentage: CGFloat = 20
    var colors : [Color] = [Color.trackProgressBarColor]
    var body: some View {
        ZStack {
            Circle()
                .fill(Color.clear)
                .frame(width: 250, height: 250)
                .overlay(
                    Circle()
                        .trim(from: 0, to: percentage * 0.01)
                        .stroke(style: StrokeStyle(lineWidth: 20, lineCap: .round, lineJoin: .round))
                        .fill(AngularGradient(gradient: .init(colors: colors), center: .center, startAngle: .zero, endAngle: .init(degrees: 360)))
                ).animation(.spring(response: 2.0, dampingFraction: 1.0, blendDuration: 1.0))
        }
    }
}

I am getting this error at stepview WHILE CALLING CIRCULARPROGRESS inside the stepview. I guess I am trying to get the data in the wrong way.

Upvotes: 2

Views: 3019

Answers (1)

Asperi
Asperi

Reputation: 257711

I don't see necessity of binding here, so just replace corresponding places with simple Int:

struct CircularProgress: View {
  var steps: Int

and

struct Outline: View {
    var steps: Int

Upvotes: 1

Related Questions