From 42e28d3a2d556f18ce5664a63d56085d96500cb0 Mon Sep 17 00:00:00 2001 From: oliverhnat Date: Sat, 6 Jan 2024 18:20:29 +0100 Subject: [PATCH] Renamed ContntView to HomeView, cleaned up SettingsView --- SleepDebt.xcodeproj/project.pbxproj | 8 +-- SleepDebt/ContentView.swift | 42 ---------------- SleepDebt/HomeView.swift | 26 ++++++++++ SleepDebt/SettingsView.swift | 76 +++++++++++++---------------- SleepDebt/SleepDebtTabView.swift | 2 +- 5 files changed, 66 insertions(+), 88 deletions(-) delete mode 100644 SleepDebt/ContentView.swift create mode 100644 SleepDebt/HomeView.swift diff --git a/SleepDebt.xcodeproj/project.pbxproj b/SleepDebt.xcodeproj/project.pbxproj index 7699e1e..09ba48c 100644 --- a/SleepDebt.xcodeproj/project.pbxproj +++ b/SleepDebt.xcodeproj/project.pbxproj @@ -8,7 +8,7 @@ /* Begin PBXBuildFile section */ 6C560B852B495DED00FDB70C /* SleepDebtApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C560B842B495DED00FDB70C /* SleepDebtApp.swift */; }; - 6C560B872B495DED00FDB70C /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C560B862B495DED00FDB70C /* ContentView.swift */; }; + 6C560B872B495DED00FDB70C /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C560B862B495DED00FDB70C /* HomeView.swift */; }; 6C560B892B495DEE00FDB70C /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6C560B882B495DEE00FDB70C /* 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 */; }; @@ -19,7 +19,7 @@ /* Begin PBXFileReference section */ 6C560B812B495DED00FDB70C /* SleepDebt.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SleepDebt.app; sourceTree = BUILT_PRODUCTS_DIR; }; 6C560B842B495DED00FDB70C /* SleepDebtApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SleepDebtApp.swift; sourceTree = ""; }; - 6C560B862B495DED00FDB70C /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; + 6C560B862B495DED00FDB70C /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = ""; }; 6C560B882B495DEE00FDB70C /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 6C560B8B2B495DEE00FDB70C /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 6C560B922B495E3A00FDB70C /* HealthKitManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HealthKitManager.swift; sourceTree = ""; }; @@ -60,7 +60,7 @@ children = ( 6C560B942B49605000FDB70C /* SleepDebt.entitlements */, 6C560B842B495DED00FDB70C /* SleepDebtApp.swift */, - 6C560B862B495DED00FDB70C /* ContentView.swift */, + 6C560B862B495DED00FDB70C /* HomeView.swift */, 6CD7B4BA2B49AA5100D1D8B8 /* SleepDebtTabView.swift */, 6CD7B4BC2B49AB3F00D1D8B8 /* SettingsView.swift */, 6C560B922B495E3A00FDB70C /* HealthKitManager.swift */, @@ -148,7 +148,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 6C560B872B495DED00FDB70C /* ContentView.swift in Sources */, + 6C560B872B495DED00FDB70C /* HomeView.swift in Sources */, 6CD7B4BB2B49AA5100D1D8B8 /* SleepDebtTabView.swift in Sources */, 6CD7B4BD2B49AB3F00D1D8B8 /* SettingsView.swift in Sources */, 6C560B852B495DED00FDB70C /* SleepDebtApp.swift in Sources */, diff --git a/SleepDebt/ContentView.swift b/SleepDebt/ContentView.swift deleted file mode 100644 index 6f70b56..0000000 --- a/SleepDebt/ContentView.swift +++ /dev/null @@ -1,42 +0,0 @@ -// -// ContentView.swift -// SleepDebt -// -// Created by Oliver Hnát on 06.01.2024. -// - -import SwiftUI - -struct ContentView: View { - @EnvironmentObject var manager: HealthKitManager - var desiredAmountOfSleep = 8 - var body: some View { - VStack { - Image(systemName: "globe") - .imageScale(.large) - .foregroundStyle(.tint) -// Button(action: { -// print(manager.sleepForLastXDays) -// var seconds = Int(manager.getSleepForLast7Days()) -// 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!") -// } - } - .padding() -// .onAppear { -// print(manager.sleepForLastXDays) -// var seconds = Int(manager.getSleepForLast7Days()) -// 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) -// } - } -} - -#Preview { - ContentView() - .environmentObject(HealthKitManager()) -} diff --git a/SleepDebt/HomeView.swift b/SleepDebt/HomeView.swift new file mode 100644 index 0000000..0347d13 --- /dev/null +++ b/SleepDebt/HomeView.swift @@ -0,0 +1,26 @@ +// +// ContentView.swift +// SleepDebt +// +// Created by Oliver Hnát on 06.01.2024. +// + +import SwiftUI + +struct HomeView: View { + @EnvironmentObject var manager: HealthKitManager + var desiredAmountOfSleep = 8 + var body: some View { + VStack { + Image(systemName: "globe") + .imageScale(.large) + .foregroundStyle(.tint) + } + .padding() + } +} + +#Preview { + HomeView() + .environmentObject(HealthKitManager()) +} diff --git a/SleepDebt/SettingsView.swift b/SleepDebt/SettingsView.swift index f83a70a..c0b6bf0 100644 --- a/SleepDebt/SettingsView.swift +++ b/SleepDebt/SettingsView.swift @@ -10,61 +10,55 @@ import SwiftUI struct SettingsView: View { @State var sleepDebtPeriod = 30 @State var repaymentPeriod = 7 + @State var desiredHoursOfSleep = 8 @FocusState private var focusItem: Bool private var repaymentString = "Repayment period (days)" private var sleepDebtString = "Sleep debt period (days)" - let formatter: NumberFormatter = { - let formatter = NumberFormatter() - formatter.numberStyle = .decimal - return formatter - }() + private var desiredHoursOfSleepString = "Desired hours of sleep" var body: some View { NavigationStack { - List { Section { - LabeledContent { - TextField("", value: $sleepDebtPeriod, formatter: formatter) - .keyboardType(.numberPad) - .onSubmit { - focusItem = false - } - .focused($focusItem) - .multilineTextAlignment(.trailing) - .foregroundStyle(.blue) - } label: { - Text(sleepDebtString) - .fixedSize() - } - - .lineLimit(1) - LabeledContent { - TextField("", value: $repaymentPeriod, formatter: formatter) - .keyboardType(.numberPad) - .onSubmit { - focusItem = false - } - .focused($focusItem) - .multilineTextAlignment(.trailing) - .foregroundStyle(.blue) - - } label: { - Text(repaymentString) - .fixedSize() - } + SettingsNumberField(value: $sleepDebtPeriod, focusItem: $focusItem, text: sleepDebtString) + SettingsNumberField(value: $repaymentPeriod, focusItem: $focusItem, text: repaymentString) + SettingsNumberField(value: $desiredHoursOfSleep, focusItem: $focusItem, text: desiredHoursOfSleepString) } + } - .onTapGesture{ - focusItem = false - } - .navigationTitle("Settings") + .navigationTitle("Settings") + } + .onTapGesture{ + focusItem = false } - - } + + } #Preview { SettingsView() } + +struct SettingsNumberField: View { + @Binding var value: Int + var focusItem: FocusState.Binding + var text: String + let formatter: NumberFormatter = { + let formatter = NumberFormatter() + formatter.numberStyle = .decimal + return formatter + }() + var body: some View { + LabeledContent { + TextField("", value: $value, formatter: formatter) + .keyboardType(.numberPad) + .focused(focusItem) + .multilineTextAlignment(.trailing) + .foregroundStyle(.blue) + } label: { + Text(text) + .fixedSize() + } + } +} diff --git a/SleepDebt/SleepDebtTabView.swift b/SleepDebt/SleepDebtTabView.swift index 9190d44..da82aed 100644 --- a/SleepDebt/SleepDebtTabView.swift +++ b/SleepDebt/SleepDebtTabView.swift @@ -12,7 +12,7 @@ struct SleepDebtTabView: View { @StateObject var healthKitManager = HealthKitManager() var body: some View { TabView(selection: $selectedTab) { - ContentView() + HomeView() .tag("Home") .tabItem { Image(systemName: "house")