Made multiple functions to get sleep data
This commit is contained in:
@@ -12,6 +12,8 @@
|
|||||||
6C560B892B495DEE00FDB70C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6C560B882B495DEE00FDB70C /* Assets.xcassets */; };
|
6C560B892B495DEE00FDB70C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6C560B882B495DEE00FDB70C /* Assets.xcassets */; };
|
||||||
6C560B8C2B495DEE00FDB70C /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6C560B8B2B495DEE00FDB70C /* Preview Assets.xcassets */; };
|
6C560B8C2B495DEE00FDB70C /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6C560B8B2B495DEE00FDB70C /* Preview Assets.xcassets */; };
|
||||||
6C560B932B495E3A00FDB70C /* HealthKitManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C560B922B495E3A00FDB70C /* HealthKitManager.swift */; };
|
6C560B932B495E3A00FDB70C /* HealthKitManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C560B922B495E3A00FDB70C /* HealthKitManager.swift */; };
|
||||||
|
6CD7B4BB2B49AA5100D1D8B8 /* SleepDebtTabView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CD7B4BA2B49AA5100D1D8B8 /* SleepDebtTabView.swift */; };
|
||||||
|
6CD7B4BD2B49AB3F00D1D8B8 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CD7B4BC2B49AB3F00D1D8B8 /* SettingsView.swift */; };
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
@@ -22,6 +24,8 @@
|
|||||||
6C560B8B2B495DEE00FDB70C /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
|
6C560B8B2B495DEE00FDB70C /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
|
||||||
6C560B922B495E3A00FDB70C /* HealthKitManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthKitManager.swift; sourceTree = "<group>"; };
|
6C560B922B495E3A00FDB70C /* HealthKitManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthKitManager.swift; sourceTree = "<group>"; };
|
||||||
6C560B942B49605000FDB70C /* SleepDebt.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SleepDebt.entitlements; sourceTree = "<group>"; };
|
6C560B942B49605000FDB70C /* SleepDebt.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = SleepDebt.entitlements; sourceTree = "<group>"; };
|
||||||
|
6CD7B4BA2B49AA5100D1D8B8 /* SleepDebtTabView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SleepDebtTabView.swift; sourceTree = "<group>"; };
|
||||||
|
6CD7B4BC2B49AB3F00D1D8B8 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
@@ -57,6 +61,8 @@
|
|||||||
6C560B942B49605000FDB70C /* SleepDebt.entitlements */,
|
6C560B942B49605000FDB70C /* SleepDebt.entitlements */,
|
||||||
6C560B842B495DED00FDB70C /* SleepDebtApp.swift */,
|
6C560B842B495DED00FDB70C /* SleepDebtApp.swift */,
|
||||||
6C560B862B495DED00FDB70C /* ContentView.swift */,
|
6C560B862B495DED00FDB70C /* ContentView.swift */,
|
||||||
|
6CD7B4BA2B49AA5100D1D8B8 /* SleepDebtTabView.swift */,
|
||||||
|
6CD7B4BC2B49AB3F00D1D8B8 /* SettingsView.swift */,
|
||||||
6C560B922B495E3A00FDB70C /* HealthKitManager.swift */,
|
6C560B922B495E3A00FDB70C /* HealthKitManager.swift */,
|
||||||
6C560B882B495DEE00FDB70C /* Assets.xcassets */,
|
6C560B882B495DEE00FDB70C /* Assets.xcassets */,
|
||||||
6C560B8A2B495DEE00FDB70C /* Preview Content */,
|
6C560B8A2B495DEE00FDB70C /* Preview Content */,
|
||||||
@@ -143,6 +149,8 @@
|
|||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
6C560B872B495DED00FDB70C /* ContentView.swift in Sources */,
|
6C560B872B495DED00FDB70C /* ContentView.swift in Sources */,
|
||||||
|
6CD7B4BB2B49AA5100D1D8B8 /* SleepDebtTabView.swift in Sources */,
|
||||||
|
6CD7B4BD2B49AB3F00D1D8B8 /* SettingsView.swift in Sources */,
|
||||||
6C560B852B495DED00FDB70C /* SleepDebtApp.swift in Sources */,
|
6C560B852B495DED00FDB70C /* SleepDebtApp.swift in Sources */,
|
||||||
6C560B932B495E3A00FDB70C /* HealthKitManager.swift in Sources */,
|
6C560B932B495E3A00FDB70C /* HealthKitManager.swift in Sources */,
|
||||||
);
|
);
|
||||||
@@ -282,6 +290,7 @@
|
|||||||
DEVELOPMENT_TEAM = SSJBLTMP95;
|
DEVELOPMENT_TEAM = SSJBLTMP95;
|
||||||
ENABLE_PREVIEWS = YES;
|
ENABLE_PREVIEWS = YES;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
|
INFOPLIST_KEY_LSApplicationCategoryType = "";
|
||||||
INFOPLIST_KEY_NSHealthShareUsageDescription = "Please allow access to your sleep data to enjoy the features of SleepDebt";
|
INFOPLIST_KEY_NSHealthShareUsageDescription = "Please allow access to your sleep data to enjoy the features of SleepDebt";
|
||||||
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
||||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||||
@@ -313,6 +322,7 @@
|
|||||||
DEVELOPMENT_TEAM = SSJBLTMP95;
|
DEVELOPMENT_TEAM = SSJBLTMP95;
|
||||||
ENABLE_PREVIEWS = YES;
|
ENABLE_PREVIEWS = YES;
|
||||||
GENERATE_INFOPLIST_FILE = YES;
|
GENERATE_INFOPLIST_FILE = YES;
|
||||||
|
INFOPLIST_KEY_LSApplicationCategoryType = "";
|
||||||
INFOPLIST_KEY_NSHealthShareUsageDescription = "Please allow access to your sleep data to enjoy the features of SleepDebt";
|
INFOPLIST_KEY_NSHealthShareUsageDescription = "Please allow access to your sleep data to enjoy the features of SleepDebt";
|
||||||
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
|
||||||
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
|
||||||
|
|||||||
@@ -9,29 +9,30 @@ import SwiftUI
|
|||||||
|
|
||||||
struct ContentView: View {
|
struct ContentView: View {
|
||||||
@EnvironmentObject var manager: HealthKitManager
|
@EnvironmentObject var manager: HealthKitManager
|
||||||
|
var desiredAmountOfSleep = 8
|
||||||
var body: some View {
|
var body: some View {
|
||||||
VStack {
|
VStack {
|
||||||
Image(systemName: "globe")
|
Image(systemName: "globe")
|
||||||
.imageScale(.large)
|
.imageScale(.large)
|
||||||
.foregroundStyle(.tint)
|
.foregroundStyle(.tint)
|
||||||
Button(action: {
|
// Button(action: {
|
||||||
manager.getSleepForLast7Days()
|
// print(manager.sleepForLastXDays)
|
||||||
print(manager.sleepForLast7Days)
|
// var seconds = Int(manager.getSleepForLast7Days())
|
||||||
var seconds = manager.sleepForLast7Days
|
// print("hours: ", seconds / 3600, " minutes: ", (seconds % 3600) / 60, " seconds: ", (seconds % 3600) % 60)
|
||||||
print("hours: ", seconds / 3600, " minutes: ", (seconds % 3600) / 60, " seconds: ", (seconds % 3600) % 60)
|
// seconds = Int(manager.getSleepForLast30Days())
|
||||||
}) {
|
// print("hours: ", seconds / 3600, " minutes: ", (seconds % 3600) / 60, " seconds: ", (seconds % 3600) % 60)
|
||||||
Text("Hello, world!")
|
// }) {
|
||||||
}
|
// Text("Hello, world!")
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
.padding()
|
.padding()
|
||||||
.onAppear {
|
// .onAppear {
|
||||||
manager.getSleepForLast7Days()
|
// print(manager.sleepForLastXDays)
|
||||||
print(manager.sleepForLast7Days)
|
// var seconds = Int(manager.getSleepForLast7Days())
|
||||||
var seconds = manager.sleepForLast7Days
|
// print("hours: ", seconds / 3600, " minutes: ", (seconds % 3600) / 60, " seconds: ", (seconds % 3600) % 60)
|
||||||
print("hours: ", seconds / 3600, " minutes: ", (seconds % 3600) / 60, " seconds: ", (seconds % 3600) % 60)
|
// seconds = Int(manager.getSleepForLast30Days())
|
||||||
|
// print("hours: ", seconds / 3600, " minutes: ", (seconds % 3600) / 60, " seconds: ", (seconds % 3600) % 60)
|
||||||
|
// }
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,17 +8,11 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import HealthKit
|
import HealthKit
|
||||||
|
|
||||||
extension Date {
|
|
||||||
static var last7Days: Date {
|
|
||||||
Date().addingTimeInterval(TimeInterval(-60 * 60 * 24 * 7)) // last 7 days
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class HealthKitManager: ObservableObject {
|
class HealthKitManager: ObservableObject {
|
||||||
|
|
||||||
let healthStore = HKHealthStore()
|
let healthStore = HKHealthStore()
|
||||||
|
|
||||||
var sleepForLast7Days: Int = 0
|
var sleepForLastXDays: Dictionary<Int, Int> = [:]
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
let sleep = HKCategoryType(.sleepAnalysis)
|
let sleep = HKCategoryType(.sleepAnalysis)
|
||||||
@@ -32,14 +26,25 @@ class HealthKitManager: ObservableObject {
|
|||||||
print(error)
|
print(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.getSleepForLast7Days()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSleepForLast7Days() {
|
func getSleepForLast7Days() -> Int {
|
||||||
|
self.getSleepForLastXDays(days: 7)
|
||||||
|
return self.sleepForLastXDays[7] ?? 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func getSleepForLast30Days() -> Int{
|
||||||
|
self.getSleepForLastXDays(days: 30)
|
||||||
|
return self.sleepForLastXDays[30] ?? 0
|
||||||
|
}
|
||||||
|
|
||||||
|
private func getSleepForLastXDays(days: Int) {
|
||||||
var totalSleep = 0
|
var totalSleep = 0
|
||||||
let sleep = HKCategoryType(.sleepAnalysis)
|
let sleep = HKCategoryType(.sleepAnalysis)
|
||||||
let sleepPredicate = HKCategoryValueSleepAnalysis.predicateForSamples(equalTo: HKCategoryValueSleepAnalysis.allAsleepValues)
|
let sleepPredicate = HKCategoryValueSleepAnalysis.predicateForSamples(equalTo: HKCategoryValueSleepAnalysis.allAsleepValues)
|
||||||
let datePredicate = HKQuery.predicateForSamples(withStart: .last7Days, end: .now)
|
let lastXDays = Date().addingTimeInterval(TimeInterval(-60 * 60 * 24 * days))
|
||||||
|
|
||||||
|
let datePredicate = HKQuery.predicateForSamples(withStart: lastXDays, end: .now)
|
||||||
let predicates = NSCompoundPredicate(andPredicateWithSubpredicates: [sleepPredicate, datePredicate])
|
let predicates = NSCompoundPredicate(andPredicateWithSubpredicates: [sleepPredicate, datePredicate])
|
||||||
let sortByDate = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
|
let sortByDate = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)
|
||||||
let query = HKSampleQuery(
|
let query = HKSampleQuery(
|
||||||
@@ -56,9 +61,8 @@ class HealthKitManager: ObservableObject {
|
|||||||
let seconds = Int(endDate.timeIntervalSince(startDate))
|
let seconds = Int(endDate.timeIntervalSince(startDate))
|
||||||
totalSleep += seconds
|
totalSleep += seconds
|
||||||
}
|
}
|
||||||
self.sleepForLast7Days = totalSleep
|
self.sleepForLastXDays[days] = totalSleep
|
||||||
}
|
}
|
||||||
healthStore.execute(query)
|
healthStore.execute(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user