// // CheckListSheet.swift // APITestApp // // Created by Mohammad Tahreem Qadri on 19/04/21. // import SwiftUI struct ChecklistModel: Codable, Hashable { var description, remarks, status: String? } struct CheckListCard: View { @Binding var checkListData : ChecklistModel @State var statusList: [String] = [] @State var currentStatus : String = "" @State var currentRemark: String = "" @Binding var enableButtonBool: Bool var body: some View { VStack{ Text("Description").padding() .onAppear(){ addData() } if checkListData.description != nil{ Text(checkListData.description!) .padding() } Text("Status").padding() if checkListData.status != nil { Picker("Status", selection: $currentStatus ) { ForEach(statusList, id: \.self){ status in Text(status) } }.pickerStyle(SegmentedPickerStyle()) .padding() .disabled(!enableButtonBool) } Text("Remarks") if checkListData.remarks != nil{ TextField("Remarks", text: $currentRemark) .disabled(!enableButtonBool) .padding() .background(Color(.white)) .cornerRadius(8) .accentColor(.gray) }else{ TextField("Remarks", text: $currentRemark) .disabled(!enableButtonBool) .padding() .background(Color(.white)) .cornerRadius(8) } } .padding() .background(Color("light_gray")) .foregroundColor(.black) .cornerRadius(8) .shadow(radius: 10) .padding() } func addData() { statusList.append("Yes") statusList.append("No") statusList.append("NA") if checkListData.status != nil { statusList.append(checkListData.status!) currentStatus = checkListData.status! } if checkListData.remarks != nil { currentRemark = checkListData.remarks! } } } struct CheckListSheet: View { @State var taskId: Int @State var checklistResponse: [ChecklistModel] = [ChecklistModel()] @State var enableButtonBool: Bool = false @State var currentRemarks = "" @State var currentStatus = "" @State var statusList : [String] = [] var body: some View { Text("Checklist Items") .padding() .font(.title) ScrollView{ ForEach(checklistResponse, id:\.self){ checklist in CheckListCard(checkListData: Binding(get: { return checklist }, set: { (newValue) in print("this is the new value \(newValue)") checklistResponse.append(newValue) }) , enableButtonBool: $enableButtonBool) } if enableButtonBool{ Button("Update"){ updateCheckList() } } } .onAppear(){ getChecklists() } } func updateCheckList() { print("this is the checklistresponse \(checklistResponse)") } func addItems(checkList: ChecklistModel) { statusList.append("Yes") statusList.append("No") statusList.append("NA") if checkList.status != nil { statusList.append(checkList.status!) currentStatus = checkList.status! } if checkList.remarks != nil { currentRemarks = checkList.remarks! } } func createCard(checkList: ChecklistModel) -> some View { return VStack{ if checkList.description != nil{ Text(checkList.description!) .padding() } Picker("Status", selection: $currentStatus) { ForEach(statusList, id: \.self) { status in Text(status) } }.pickerStyle(SegmentedPickerStyle()) TextField("Remarks", text: $currentRemarks) }.padding() } func getChecklists() { checklistResponse = [ChecklistModel(description: "ist item", remarks: nil , status: "Yes"), ChecklistModel(description: "2nd item", remarks: "Fine" , status: "no")] } } struct CheckListSheet_Previews: PreviewProvider { static var previews: some View { CheckListSheet(taskId: 1) } }