Skip to content

Commit

Permalink
Feat: SettingView Data Binding 성공
Browse files Browse the repository at this point in the history
- 파이리 덕분
  • Loading branch information
teabag759 committed May 23, 2024
1 parent f43feca commit 418990a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
1 change: 1 addition & 0 deletions MC2-Team3-Pilling/Extentsion/model+extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extension Config{
dateFormatter.dateFormat = format
return dateFormatter.string(from: date)
}

static func StringToDate(dateString: String,format:String) -> Date? {

let dateFormatter = DateFormatter()
Expand Down
8 changes: 4 additions & 4 deletions MC2-Team3-Pilling/View/MainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ struct MainView: View {
.padding()
.presentationCompactAdaptation(.popover)
}
// NavigationLink(destination: SettingView(selectedPill: $selectedPill, showingMedicineSheet: $showingMedicineSheet), label: {
// Image(systemName: "gearshape")
// .Icon()
// })
NavigationLink(destination: SettingView(selectedPill: $selectedPill, showingMedicineSheet: $showingMedicineSheet), label: {
Image(systemName: "gearshape")
.Icon()
})
}

// status header
Expand Down
4 changes: 4 additions & 0 deletions MC2-Team3-Pilling/View/MedicineSheetView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import SwiftUI
import SwiftData

struct MedicineSheetView: View {
@State private var searchText = ""
Expand All @@ -8,6 +9,7 @@ struct MedicineSheetView: View {

@Environment(\.presentationMode) var presentationMode
@Binding var selectedPill: PillInfo?
@Query var user:[UserInfo]

// let BirthControlNames = [
// "쎄스콘정", "미뉴렛정", "에이리스정", "머시론정",
Expand All @@ -28,9 +30,11 @@ struct MedicineSheetView: View {
List(filteredBirthControl) { pill in
Button(action: {
selectedPill = pill

print(pill.pillName)

if selectedPill == pill {
user.first?.curPill?.pillInfo=selectedPill!
self.presentationMode.wrappedValue.dismiss()
}

Expand Down
28 changes: 25 additions & 3 deletions MC2-Team3-Pilling/View/SettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
//

import SwiftUI
import SwiftData

struct SettingView: View {
@State private var selectedAlarmTime = Date.now
@Environment(\.modelContext) private var modelContext
@Query var user:[UserInfo]

@State private var selectedAlarmTime = Date()
@State private var isSoundOn = false
// @State private var selectedPill = 0
// @State private var selectedPill = 0
@Binding var selectedPill: PillInfo?
@State private var isShowingPills = false
@Binding var showingMedicineSheet: Bool
Expand All @@ -35,7 +39,7 @@ struct SettingView: View {

NavigationLink(destination: MedicineSheetView(showingMedicineSheet: $showingMedicineSheet, selectedPill: $selectedPill)) {
HStack {
Text("Select Pill")
Text("약 선택")
Spacer()
if let pill = selectedPill {
Text(pill.pillName)
Expand All @@ -50,11 +54,29 @@ struct SettingView: View {

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

Toggle("알람", isOn: $isSoundOn)
.onChange(of: isSoundOn) { oldValue, newValueAlarm in
user.first?.isAlarm = newValueAlarm
}
// Optional, unWrapped

}
}
.onAppear{
if let userInfo = user.first {
selectedPill = userInfo.curPill?.pillInfo
selectedAlarmTime = Config.StringToDate(dateString: userInfo.scheduleTime, format: Hourformat)!
// selectedAlarmTime = Config.DateToString(date: selectedAlarmTime, format: Hourformat)
isSoundOn = userInfo.isAlarm
}

}
.navigationTitle("Settings")
// .sheet(isPresented: $isShowingPills, content: {
// Text("Pills")
Expand Down

0 comments on commit 418990a

Please sign in to comment.