Post

Replies

Boosts

Views

Activity

Reply to I need to read multiple Queries of HKStatisticsCollectionQuery Like StepCount and Swimming Stroke
I have solved the problem by creating the two separate func for two separate queries like   func stepCount(completion: @escaping (HKStatisticsCollection?)-> Void){         let stepCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!         let startDate = Calendar.current.date(byAdding: .day,value: -7, to: Date())         let anchorDate = Date.monday12AM()         let daily = DateComponents(day : 1)         let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)         query = HKStatisticsCollectionQuery(quantityType: stepCount, quantitySamplePredicate: predicate,options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)   query!.initialResultsHandler = { query, statisticsCollection, error in             completion(statisticsCollection)         }         if let healthStore = healthStore,let query = self.query{             healthStore.execute(query)         }     }     func swimmingStroke(completion : @escaping (HKStatisticsCollection?)-> Void){         let swimmmingStrokeCount = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.swimmingStrokeCount)!         let startDate = Calendar.current.date(byAdding: .day,value: -7, to: Date())         let anchorDate = Date.monday12AM()         let daily = DateComponents(day : 1)         let predicate = HKQuery.predicateForSamples(withStart: startDate, end: Date(), options: .strictStartDate)         query1 = HKStatisticsCollectionQuery(quantityType: swimmmingStrokeCount, quantitySamplePredicate: predicate,options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily)         query1!.initialResultsHandler = { query1, statisticsCollection1, error in             completion(statisticsCollection1)         }         if let healthStore1 = healthStore,let query1 = self.query1{             healthStore1.execute(query1)         }     } and updating the UI by if let healthStore1 = healthStore {                 healthStore1.authorization { success in                     if success{                         healthStore1.swimmingStroke{ statisticsCollection1  in                             if let statisticsCollection1 = statisticsCollection1{                                 print(statisticsCollection1)                                 UpdateUIFromStatistics1(statisticsCollection1)                             }else{                                 print("no statistics Collection Data1")                             }                         }                     }                 }             }             if let healthStore = healthStore {                 healthStore.authorization { success in                     if success{                         healthStore.readData{ statisticsCollection  in                             if let statisticsCollection = statisticsCollection{                                 print(statisticsCollection)                                 UpdateUIFromStatistics(statisticsCollection)                             }else{                                 print("no statistics Collection Data")                             }                         }                     }                 }             }
Mar ’23
Reply to Cannot convert value of type 'Set<HKQuantityType>' to expected argument type 'HKQuantityType
yeah till this there is no problem the problem arises on below code query =  HKStatisticsCollectionQuery(quantityType: Activitysummary, quantitySamplePredicate: predicate,options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: daily) here in quantityType: Activitysummary throws an error saying "Cannot convert value of type 'Set' to expected argument type 'HKQuantityType "
Jan ’23
Reply to cannot find 'minutes' in the scope
inhave just added the code which caused the error label     public typealias PizzaDeliveryStatus = ContentState     public struct ContentState: Codable, Hashable {         var driverName: String         var deliveryTimer: ClosedRange<Date>     }     //var id = UUID()     var numberOfPizzas: Int     var totalAmount: String     var orderNumber: String } i think i have a doubt in syntax only i have used Text(timerInterval: context.state.deliveryTimer, countsDown: true) Where deliveryTimer is in ClosedRange Format like var deliveryTimer: ClosedRange<Date> Now if you go through this below code    var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ), to: Date())!     future = Calendar.current.date(byAdding: .second, value: (Int(seconds) ), to: future)!         let date = Date.now...future     let activityAttributes = PizzaDeliveryAttributes(numberOfPizzas: 10, totalAmount: "$100", orderNumber: "101")     let initialContentState = PizzaDeliveryAttributes.ContentState(driverName: "Makwin", deliveryTimer: date )     do {         let deliveryActivity = try Activity<PizzaDeliveryAttributes>.request(attributes: activityAttributes, contentState: initialContentState,pushType: nil)         print("Requested a pizza delivery Live Activity \(String(describing: deliveryActivity.id)).")     } catch (let error) {         print("Error requesting pizza delivery Live Activity \(error.localizedDescription).")     } } now you may get some clearance of my doubt if i give value for minutes and seconds which is not defined in the code as Int or String it will not accept do you know how to give input format for this Minutes and Seconds which is not defined in my code var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ), to: Date())! future = Calendar.current.date(byAdding: .second, value: (Int(seconds) ), to: future)!
Nov ’22
Reply to cannot find 'minutes' in the scope
import WidgetKit import SwiftUI import Foundation struct dynamicIsland : Widget {          var body: some WidgetConfiguration{         ActivityConfiguration(for: PizzaDeliveryAttributes.self) { context in                          LockScreenLiveActivityView(context: context)         } dynamicIsland: { context in             DynamicIsland{                                  DynamicIslandExpandedRegion(.leading){                     Label("\(context.attributes.numberOfPizzas) Pizzas", systemImage: "bag")                         .foregroundColor(.indigo)                         .font(.title2)                 }                 DynamicIslandExpandedRegion(.trailing) {                     Label {                         Text(timerInterval: context.state.deliveryTimer , countsDown: true)                             .multilineTextAlignment(.trailing)                             .frame(width: 50)                             .monospacedDigit()                     } icon: {                         Image(systemName: "timer")                             .foregroundColor(.indigo)                     }                     .font(.title2)                 }                                  DynamicIslandExpandedRegion(.center) {                     Text("\(context.state.driverName) is on their way!")                         .lineLimit(1)                         .font(.caption)                 }                                  DynamicIslandExpandedRegion(.bottom) {                     Button {                         // Deep link into your app.                     } label: {                         Label("Call driver", systemImage: "phone")                     }                     .foregroundColor(.indigo)                 }             } compactLeading: {                 // Create the compact leading presentation.                 // ...             } compactTrailing: {                 // Create the compact trailing presentation.                 // ...             } minimal: {                 // Create the minimal presentation.                 // ...             }             .contentMargins(.trailing, 8, for: .expanded)             .keylineTint(.yellow)         }     } }                           struct LockScreenLiveActivityView: View {             let context: ActivityViewContext<PizzaDeliveryAttributes>                          var body: some View {                 VStack {                     Spacer()                     Text("\(context.state.driverName) is on their way with your pizza!")                     Spacer()                     HStack {                         Spacer()                         Label {                             Text("\(context.attributes.numberOfPizzas) Pizzas")                         } icon: {                             Image(systemName: "bag")                                 .foregroundColor(.indigo)                         }                         .font(.title2)                         Spacer()                         Label {                             Text(timerInterval: context.state.deliveryTimer, countsDown: true)                                 .multilineTextAlignment(.center)                                 .frame(width: 50)                                 .monospacedDigit()                         } icon: {                             Image(systemName: "timer")                                 .foregroundColor(.indigo)                         }                         .font(.title2)                         Spacer()                     }                     Spacer()                 }                 .activitySystemActionForegroundColor(.indigo)                 .activityBackgroundTint(.cyan)             }         }     struct PizzaDeliveryAttributes: ActivityAttributes {     public typealias PizzaDeliveryStatus = ContentState     public struct ContentState: Codable, Hashable {         var driverName: String         var deliveryTimer: ClosedRange<Date>     }     //var id = UUID()     var numberOfPizzas: Int     var totalAmount: String     var orderNumber: String } now check the code above also I have this in my contentview //  ContentView.swift //  Widget_and_LiveActivities // //  Created by Makwin Santhosh K on 20/11/22. // import SwiftUI import WidgetKit import ActivityKit struct ContentView: View {       var body: some View {         ZStack {             VStack{                 Button("Start Activity"){                     AddLiveACtivity()                 }             }         }         .navigationTitle("Live Activities")     } } func AddLiveACtivity() {        var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ), to: Date())!     future = Calendar.current.date(byAdding: .second, value: (Int(seconds) ), to: future)!          let date = Date.now...future     let activityAttributes = PizzaDeliveryAttributes(numberOfPizzas: 10, totalAmount: "$100", orderNumber: "101")     let initialContentState = PizzaDeliveryAttributes.ContentState(driverName: "Makwin", deliveryTimer: date )               do {         let deliveryActivity = try Activity<PizzaDeliveryAttributes>.request(attributes: activityAttributes, contentState: initialContentState,pushType: nil)         print("Requested a pizza delivery Live Activity \(String(describing: deliveryActivity.id)).")     } catch (let error) {         print("Error requesting pizza delivery Live Activity \(error.localizedDescription).")     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } }
Nov ’22
Reply to cannot find 'minutes' in the scope
and I have clearly checked the code and here is my doubt I have set deliveryTimer to be ClosedRange data format so to get ClosedRange input so if I give Minutes and Seconds as variable it is showing error in Timer I have added the code below which the data transfers from Content view to this view seeing this might give you a idea import WidgetKit import SwiftUI import Foundation struct dynamicIsland : Widget {          var body: some WidgetConfiguration{         ActivityConfiguration(for: PizzaDeliveryAttributes.self) { context in                          LockScreenLiveActivityView(context: context)         } dynamicIsland: { context in             DynamicIsland{                                  DynamicIslandExpandedRegion(.leading){                     Label("\(context.attributes.numberOfPizzas) Pizzas", systemImage: "bag")                         .foregroundColor(.indigo)                         .font(.title2)                 }                 DynamicIslandExpandedRegion(.trailing) {                     Label {                         Text(timerInterval: context.state.deliveryTimer, countsDown: true)                             .multilineTextAlignment(.trailing)                             .frame(width: 50)                             .monospacedDigit()                     } icon: {                         Image(systemName: "timer")                             .foregroundColor(.indigo)                     }                     .font(.title2)                 }                                  DynamicIslandExpandedRegion(.center) {                     Text("\(context.state.driverName) is on their way!")                         .lineLimit(1)                         .font(.caption)                 }                                  DynamicIslandExpandedRegion(.bottom) {                     Button {                         // Deep link into your app.                     } label: {                         Label("Call driver", systemImage: "phone")                     }                     .foregroundColor(.indigo)                 }             } compactLeading: {                 // Create the compact leading presentation.                 // ...             } compactTrailing: {                 // Create the compact trailing presentation.                 // ...             } minimal: {                 // Create the minimal presentation.                 // ...             }             .contentMargins(.trailing, 8, for: .expanded)             .keylineTint(.yellow)         }     } }                           struct LockScreenLiveActivityView: View {             let context: ActivityViewContext<PizzaDeliveryAttributes>                          var body: some View {                 VStack {                     Spacer()                     Text("\(context.state.driverName) is on their way with your pizza!")                     Spacer()                     HStack {                         Spacer()                         Label {                             Text("\(context.attributes.numberOfPizzas) Pizzas")                         } icon: {                             Image(systemName: "bag")                                 .foregroundColor(.indigo)                         }                         .font(.title2)                         Spacer()                         Label {                             Text(timerInterval: context.state.deliveryTimer, countsDown: true) // im getting error here if I give var minutes = 10 something like this, it gives error (I think for ClosedRange<date> the input that we should give is in different format)                                 .multilineTextAlignment(.center)                                 .frame(width: 50)                                 .monospacedDigit()                         } icon: {                             Image(systemName: "timer")                                 .foregroundColor(.indigo)                         }                         .font(.title2)                         Spacer()                     }                     Spacer()                 }                 .activitySystemActionForegroundColor(.indigo)                 .activityBackgroundTint(.cyan)             }         }
Nov ’22