Extracted SettingsNumberFieldView into its own file, fixed environment objects and added settings as an environment object
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
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 */; };
|
||||
6CD7B4C12B49C45400D1D8B8 /* SettingsNumberFieldView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CD7B4C02B49C45400D1D8B8 /* SettingsNumberFieldView.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
@@ -26,6 +27,7 @@
|
||||
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>"; };
|
||||
6CD7B4C02B49C45400D1D8B8 /* SettingsNumberFieldView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsNumberFieldView.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@@ -63,6 +65,7 @@
|
||||
6C560B862B495DED00FDB70C /* HomeView.swift */,
|
||||
6CD7B4BA2B49AA5100D1D8B8 /* SleepDebtTabView.swift */,
|
||||
6CD7B4BC2B49AB3F00D1D8B8 /* SettingsView.swift */,
|
||||
6CD7B4C02B49C45400D1D8B8 /* SettingsNumberFieldView.swift */,
|
||||
6C560B922B495E3A00FDB70C /* HealthKitManager.swift */,
|
||||
6C560B882B495DEE00FDB70C /* Assets.xcassets */,
|
||||
6C560B8A2B495DEE00FDB70C /* Preview Content */,
|
||||
@@ -149,6 +152,7 @@
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
6C560B872B495DED00FDB70C /* HomeView.swift in Sources */,
|
||||
6CD7B4C12B49C45400D1D8B8 /* SettingsNumberFieldView.swift in Sources */,
|
||||
6CD7B4BB2B49AA5100D1D8B8 /* SleepDebtTabView.swift in Sources */,
|
||||
6CD7B4BD2B49AB3F00D1D8B8 /* SettingsView.swift in Sources */,
|
||||
6C560B852B495DED00FDB70C /* SleepDebtApp.swift in Sources */,
|
||||
|
||||
@@ -26,6 +26,8 @@ class HealthKitManager: ObservableObject {
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
self.getSleepForLastXDays(days: 7)
|
||||
self.getSleepForLastXDays(days: 30)
|
||||
}
|
||||
|
||||
func getSleepForLast7Days() -> Int {
|
||||
|
||||
@@ -15,6 +15,7 @@ struct HomeView: View {
|
||||
Image(systemName: "globe")
|
||||
.imageScale(.large)
|
||||
.foregroundStyle(.tint)
|
||||
Text(String(manager.getSleepForLast7Days()))
|
||||
}
|
||||
.padding()
|
||||
}
|
||||
@@ -23,4 +24,5 @@ struct HomeView: View {
|
||||
#Preview {
|
||||
HomeView()
|
||||
.environmentObject(HealthKitManager())
|
||||
.environmentObject(SleepDebtSettings())
|
||||
}
|
||||
|
||||
46
SleepDebt/SettingsNumberFieldView.swift
Normal file
46
SleepDebt/SettingsNumberFieldView.swift
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// SettingsNumberFieldView.swift
|
||||
// SleepDebt
|
||||
//
|
||||
// Created by Oliver Hnát on 06.01.2024.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct SettingsNumberFieldView: View {
|
||||
@Binding var value: Int
|
||||
var focusItem: FocusState<Bool>.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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SettingsNumberFieldView_Preview: PreviewProvider {
|
||||
|
||||
struct ContainerView: View {
|
||||
@State var sleepDebtPeriod = 30
|
||||
@FocusState var focusItem: Bool
|
||||
var sleepDebtString = "Sleep debt period (days)"
|
||||
var body: some View {
|
||||
SettingsNumberFieldView(value: $sleepDebtPeriod, focusItem: $focusItem, text: sleepDebtString)
|
||||
}
|
||||
}
|
||||
static var previews: some View {
|
||||
ContainerView()
|
||||
}
|
||||
}
|
||||
@@ -8,9 +8,7 @@
|
||||
import SwiftUI
|
||||
|
||||
struct SettingsView: View {
|
||||
@State var sleepDebtPeriod = 30
|
||||
@State var repaymentPeriod = 7
|
||||
@State var desiredHoursOfSleep = 8
|
||||
@EnvironmentObject var settings: SleepDebtSettings
|
||||
@FocusState private var focusItem: Bool
|
||||
private var repaymentString = "Repayment period (days)"
|
||||
private var sleepDebtString = "Sleep debt period (days)"
|
||||
@@ -20,9 +18,9 @@ struct SettingsView: View {
|
||||
NavigationStack {
|
||||
List {
|
||||
Section {
|
||||
SettingsNumberField(value: $sleepDebtPeriod, focusItem: $focusItem, text: sleepDebtString)
|
||||
SettingsNumberField(value: $repaymentPeriod, focusItem: $focusItem, text: repaymentString)
|
||||
SettingsNumberField(value: $desiredHoursOfSleep, focusItem: $focusItem, text: desiredHoursOfSleepString)
|
||||
SettingsNumberFieldView(value: $settings.sleepDebtPeriod, focusItem: $focusItem, text: sleepDebtString)
|
||||
SettingsNumberFieldView(value: $settings.repaymentPeriod, focusItem: $focusItem, text: repaymentString)
|
||||
SettingsNumberFieldView(value: $settings.desiredHoursOfSleep, focusItem: $focusItem, text: desiredHoursOfSleepString)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,6 +29,7 @@ struct SettingsView: View {
|
||||
.onTapGesture{
|
||||
focusItem = false
|
||||
}
|
||||
.environmentObject(settings)
|
||||
}
|
||||
|
||||
|
||||
@@ -38,27 +37,7 @@ struct SettingsView: View {
|
||||
|
||||
#Preview {
|
||||
SettingsView()
|
||||
.environmentObject(HealthKitManager())
|
||||
.environmentObject(SleepDebtSettings())
|
||||
}
|
||||
|
||||
struct SettingsNumberField: View {
|
||||
@Binding var value: Int
|
||||
var focusItem: FocusState<Bool>.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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,10 +10,18 @@ import SwiftUI
|
||||
@main
|
||||
struct SleepDebtApp: App {
|
||||
@StateObject var healthKitManager = HealthKitManager()
|
||||
@StateObject var settings = SleepDebtSettings()
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
SleepDebtTabView()
|
||||
.environmentObject(healthKitManager)
|
||||
.environmentObject(settings)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class SleepDebtSettings: ObservableObject {
|
||||
@Published var sleepDebtPeriod = 30
|
||||
@Published var repaymentPeriod = 7
|
||||
@Published var desiredHoursOfSleep = 8
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import SwiftUI
|
||||
|
||||
struct SleepDebtTabView: View {
|
||||
@State var selectedTab = "Home"
|
||||
@StateObject var healthKitManager = HealthKitManager()
|
||||
var body: some View {
|
||||
TabView(selection: $selectedTab) {
|
||||
HomeView()
|
||||
@@ -18,18 +17,18 @@ struct SleepDebtTabView: View {
|
||||
Image(systemName: "house")
|
||||
Text("Home")
|
||||
}
|
||||
.environmentObject(healthKitManager)
|
||||
SettingsView()
|
||||
.tag("Settings")
|
||||
.tabItem {
|
||||
Image(systemName: "gear")
|
||||
Text("Settings")
|
||||
}
|
||||
.environmentObject(healthKitManager)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
SleepDebtTabView()
|
||||
.environmentObject(SleepDebtSettings())
|
||||
.environmentObject(HealthKitManager())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user