Skip to content

Commit 80f85e2

Browse files
authored
Merge pull request #68 from DeveloperAcademy-POSTECH/feat/#42
Feat/#42 SettingView DataBinding
2 parents 4aa8910 + 418990a commit 80f85e2

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

MC2-Team3-Pilling.xcodeproj/xcshareddata/xcschemes/MC2-Team3-Pilling.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
33
LastUpgradeVersion = "1530"
4-
version = "1.7">
4+
version = "1.8">
55
<BuildAction
66
parallelizeBuildables = "YES"
77
buildImplicitDependencies = "YES"

MC2-Team3-Pilling/Extentsion/model+extension.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extension Config{
2424
dateFormatter.dateFormat = format
2525
return dateFormatter.string(from: date)
2626
}
27+
2728
static func StringToDate(dateString: String,format:String) -> Date? {
2829

2930
let dateFormatter = DateFormatter()

MC2-Team3-Pilling/View/MedicineSheetView.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import SwiftUI
3+
import SwiftData
34

45
struct MedicineSheetView: View {
56
@State private var searchText = ""
@@ -8,6 +9,7 @@ struct MedicineSheetView: View {
89

910
@Environment(\.presentationMode) var presentationMode
1011
@Binding var selectedPill: PillInfo?
12+
@Query var user:[UserInfo]
1113

1214
// let BirthControlNames = [
1315
// "쎄스콘정", "미뉴렛정", "에이리스정", "머시론정",
@@ -28,9 +30,11 @@ struct MedicineSheetView: View {
2830
List(filteredBirthControl) { pill in
2931
Button(action: {
3032
selectedPill = pill
33+
3134
print(pill.pillName)
3235

3336
if selectedPill == pill {
37+
user.first?.curPill?.pillInfo=selectedPill!
3438
self.presentationMode.wrappedValue.dismiss()
3539
}
3640

MC2-Team3-Pilling/View/SettingView.swift

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,20 @@
66
//
77

88
import SwiftUI
9+
import SwiftData
910

1011
struct SettingView: View {
11-
@State private var selectedAlarmTime = Date.now
12+
@Environment(\.modelContext) private var modelContext
13+
@Query var user:[UserInfo]
14+
15+
@State private var selectedAlarmTime = Date()
1216
@State private var isSoundOn = false
13-
// @State private var selectedPill = 0
17+
// @State private var selectedPill = 0
1418
@Binding var selectedPill: PillInfo?
1519
@State private var isShowingPills = false
1620
@Binding var showingMedicineSheet: Bool
1721

1822

19-
20-
21-
22-
// let pills = [(name: "야즈", type: "24일/4일"), (name: "머쉬론", type: "21일/7일")]
23-
2423
var body: some View {
2524
NavigationStack {
2625
Form {
@@ -40,7 +39,7 @@ struct SettingView: View {
4039

4140
NavigationLink(destination: MedicineSheetView(showingMedicineSheet: $showingMedicineSheet, selectedPill: $selectedPill)) {
4241
HStack {
43-
Text("Select Pill")
42+
Text("약 선택")
4443
Spacer()
4544
if let pill = selectedPill {
4645
Text(pill.pillName)
@@ -55,11 +54,29 @@ struct SettingView: View {
5554

5655
Section("알림") {
5756
DatePicker("시간", selection: $selectedAlarmTime, displayedComponents: .hourAndMinute)
57+
.onChange(of: selectedAlarmTime) { oldValue, newValue in
58+
let newValueToString = Config.DateToString(date: newValue, format: Hourformat)
59+
user.first?.scheduleTime = newValueToString
60+
print(newValueToString)
61+
}
5862

5963
Toggle("알람", isOn: $isSoundOn)
64+
.onChange(of: isSoundOn) { oldValue, newValueAlarm in
65+
user.first?.isAlarm = newValueAlarm
66+
}
67+
// Optional, unWrapped
6068

6169
}
6270
}
71+
.onAppear{
72+
if let userInfo = user.first {
73+
selectedPill = userInfo.curPill?.pillInfo
74+
selectedAlarmTime = Config.StringToDate(dateString: userInfo.scheduleTime, format: Hourformat)!
75+
// selectedAlarmTime = Config.DateToString(date: selectedAlarmTime, format: Hourformat)
76+
isSoundOn = userInfo.isAlarm
77+
}
78+
79+
}
6380
.navigationTitle("Settings")
6481
// .sheet(isPresented: $isShowingPills, content: {
6582
// Text("Pills")

0 commit comments

Comments
 (0)