From 5daf92c5bb82071e1783fd87894e63916e5b3c10 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 31 Jan 2024 11:03:10 +0100 Subject: [PATCH] Create goal type --- HealthImport.xcodeproj/project.pbxproj | 3 ++ HealthImport/Model/Goal.swift | 46 +++++++++++++++++++++++++ HealthImport/Model/Workout+SQLite.swift | 5 ++- HealthImport/Model/Workout.swift | 9 ++--- HealthImport/WorkoutDetailView.swift | 1 - 5 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 HealthImport/Model/Goal.swift diff --git a/HealthImport.xcodeproj/project.pbxproj b/HealthImport.xcodeproj/project.pbxproj index 0905d7d..5bc0bf9 100644 --- a/HealthImport.xcodeproj/project.pbxproj +++ b/HealthImport.xcodeproj/project.pbxproj @@ -41,6 +41,7 @@ E201EC7B2B6275CA005B83D3 /* Metadata.swift in Sources */ = {isa = PBXBuildFile; fileRef = E201EC7A2B6275CA005B83D3 /* Metadata.swift */; }; E201EC7D2B62930E005B83D3 /* Sample+SQLite.swift in Sources */ = {isa = PBXBuildFile; fileRef = E201EC7C2B62930E005B83D3 /* Sample+SQLite.swift */; }; E201EC7F2B629B4C005B83D3 /* SampleListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E201EC7E2B629B4C005B83D3 /* SampleListView.swift */; }; + E201EC812B631708005B83D3 /* Goal.swift in Sources */ = {isa = PBXBuildFile; fileRef = E201EC802B631708005B83D3 /* Goal.swift */; }; E27BC67A2B5D99AC003A8873 /* LocationSample.swift in Sources */ = {isa = PBXBuildFile; fileRef = E27BC6792B5D99AC003A8873 /* LocationSample.swift */; }; E27BC67E2B5E6CE3003A8873 /* Sequence+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = E27BC67D2B5E6CE3003A8873 /* Sequence+Extensions.swift */; }; E27BC6802B5E74D7003A8873 /* LocationSampleListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E27BC67F2B5E74D7003A8873 /* LocationSampleListView.swift */; }; @@ -90,6 +91,7 @@ E201EC7A2B6275CA005B83D3 /* Metadata.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Metadata.swift; sourceTree = ""; }; E201EC7C2B62930E005B83D3 /* Sample+SQLite.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Sample+SQLite.swift"; sourceTree = ""; }; E201EC7E2B629B4C005B83D3 /* SampleListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SampleListView.swift; sourceTree = ""; }; + E201EC802B631708005B83D3 /* Goal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Goal.swift; sourceTree = ""; }; E27BC6792B5D99AC003A8873 /* LocationSample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSample.swift; sourceTree = ""; }; E27BC67D2B5E6CE3003A8873 /* Sequence+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Sequence+Extensions.swift"; sourceTree = ""; }; E27BC67F2B5E74D7003A8873 /* LocationSampleListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSampleListView.swift; sourceTree = ""; }; @@ -194,6 +196,7 @@ 885002812B5C37B700E7D4DB /* Model */ = { isa = PBXGroup; children = ( + E201EC802B631708005B83D3 /* Goal.swift */, E27BC6792B5D99AC003A8873 /* LocationSample.swift */, E201EC7A2B6275CA005B83D3 /* Metadata.swift */, E201EC762B626FC1005B83D3 /* MetadataKey.swift */, diff --git a/HealthImport/Model/Goal.swift b/HealthImport/Model/Goal.swift new file mode 100644 index 0000000..557ba9c --- /dev/null +++ b/HealthImport/Model/Goal.swift @@ -0,0 +1,46 @@ +import Foundation + +enum Goal { + + case time(TimeInterval) + + init?(goalType: Int?, goal: Double?) { + switch goalType { + case .none: + return nil + case 0: + return nil + case 2: + guard let goal else { + print("Time goal, but no goal value set") + return nil + } + self = .time(goal) + default: + return nil + } + } + + var gaol: Double { + switch self { + case .time(let timeInterval): + return timeInterval + } + } + + var goalType: Int { + switch self { + case .time: return 2 + } + } +} + +extension Goal: CustomStringConvertible { + + var description: String { + switch self { + case .time(let seconds): + return seconds.durationString + } + } +} diff --git a/HealthImport/Model/Workout+SQLite.swift b/HealthImport/Model/Workout+SQLite.swift index 2173af6..5e7d5b7 100644 --- a/HealthImport/Model/Workout+SQLite.swift +++ b/HealthImport/Model/Workout+SQLite.swift @@ -64,9 +64,8 @@ extension Workout { private static func insert(_ element: Workout, in database: Database) throws { let rowid = try database.run(table.insert( columnTotalDistance <- element.totalDistance, - columnGoalType <- element.goalType, - columnGoalType <- element.goalType, - columnGoal <- element.goal, + columnGoalType <- element.goal?.goalType, + columnGoal <- element.goal?.gaol, columnCondenserVersion <- element.condenserVersion, columnCondenserDate <- element.condenserDate?.timeIntervalSinceReferenceDate) ) diff --git a/HealthImport/Model/Workout.swift b/HealthImport/Model/Workout.swift index 62bca95..d679076 100644 --- a/HealthImport/Model/Workout.swift +++ b/HealthImport/Model/Workout.swift @@ -16,10 +16,8 @@ struct Workout { /// The distance in km (?) let totalDistance: Double? - let goalType: Int? - - let goal: Double? - + let goal: Goal? + let condenserVersion: Int? let condenserDate: Date? @@ -56,8 +54,7 @@ struct Workout { init(id: Int, totalDistance: Double? = nil, goalType: Int? = nil, goal: Double? = nil, condenserVersion: Int? = nil, condenserDate: Date? = nil, events: [WorkoutEvent] = [], activities: [WorkoutActivity] = [], metadata: [Metadata.Key : Metadata.Value] = [:]) { self.id = id self.totalDistance = totalDistance - self.goalType = goalType - self.goal = goal + self.goal = .init(goalType: goalType, goal: goal) self.condenserVersion = condenserVersion self.condenserDate = condenserDate self.events = events diff --git a/HealthImport/WorkoutDetailView.swift b/HealthImport/WorkoutDetailView.swift index 52265fe..b55346b 100644 --- a/HealthImport/WorkoutDetailView.swift +++ b/HealthImport/WorkoutDetailView.swift @@ -13,7 +13,6 @@ struct WorkoutDetailView: View { Section("Info") { DetailRow("ID", value: workout.id) DetailRow("Total Distance", kilometer: workout.totalDistance) - DetailRow("Goal Type", value: workout.goalType) DetailRow("Goal", value: workout.goal) DetailRow("Condenser Version", value: workout.condenserVersion) DetailRow("Condenser Date", date: workout.condenserDate)