Thanks, it was long code, here are the details
in the view of all the radio buttons, here is the
`//
// RadioButtons.swift
// De-Indexed GFR
//
// Created by Loveleen Parmar on 2021-09-21.
//
import SwiftUI
//MARK:- Single Radio Button Field
struct RadioButtonField: View {
let id: String
let label: String
let size: CGFloat
let color: Color
let textSize: CGFloat
let isMarked:Bool
let callback: (String)->()
init(
id: String,
label:String,
size: CGFloat = 20,
color: Color = Color.black,
textSize: CGFloat = 14,
isMarked: Bool = false,
callback: @escaping (String)->()
) {
self.id = id
self.label = label
self.size = size
self.color = color
self.textSize = textSize
self.isMarked = isMarked
self.callback = callback
}
var body: some View {
Button(action:{
self.callback(self.id)
}) {
HStack(alignment: .center, spacing: 10) {
Image(systemName: self.isMarked ? "largecircle.fill.circle" : "circle")
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: self.size, height: self.size)
Text(label)
//.font(Font.system(size: textSize))
.font(.caption)
Spacer()
}.foregroundColor(self.color)
}
.foregroundColor(Color.white)
}
}
//MARK:- Group of weight Radio Buttons
enum Weight: String {
case kg = "Kgs"
case pounds = "lbs"
}
struct weightRadioButtonGroups: View {
let callback: (String) -> ()
@State var selectedId: String = ""
var body: some View {
HStack {
weightRadioKgMajority
weightRadioPoundsMajority
}.padding(.leading, 6)
}
var weightRadioKgMajority: some View {
RadioButtonField(
id: Weight.kg.rawValue,
label: Weight.kg.rawValue,
isMarked: selectedId == Weight.kg.rawValue ? true : false,
callback: weightRadioGroupCallback
)
}
var weightRadioPoundsMajority: some View {
RadioButtonField(
id: Weight.pounds.rawValue,
label: Weight.pounds.rawValue,
isMarked: selectedId == Weight.pounds.rawValue ? true : false,
callback: weightRadioGroupCallback
)
}
func weightRadioGroupCallback(id: String) {
selectedId = id
callback(id)
}
}
//MARK:- Group of height Radio Buttons
enum Height: String {
case cms = "cms"
case inches = "inches"
}
struct heightRadioButtonGroups: View {
let callback: (String) -> ()
@State var selectedId: String = ""
var body: some View {
HStack {
heightRadioCmsMajority
heightRadioInchesMajority
}.padding(.leading, 8)
}
var heightRadioCmsMajority: some View {
RadioButtonField(
id: Height.cms.rawValue,
label: Height.cms.rawValue,
isMarked: selectedId == Height.cms.rawValue ? true : false,
callback: heightRadioGroupCallback
)
}
var heightRadioInchesMajority: some View {
RadioButtonField(
id: Height.inches.rawValue,
label: Height.inches.rawValue,
isMarked: selectedId == Height.inches.rawValue ? true : false,
callback: heightRadioGroupCallback
)
}
func heightRadioGroupCallback(id: String) {
selectedId = id
callback(id)
}
}
//MARK:- Group of creatinine Radio Buttons
enum Creatinine: String {
case mg = "mg/dL"
case umol = "\u{03BC}mol/L"
}
struct creatinineRadioButtonGroups: View {
let callback: (String) -> ()
@State var selectedId: String = ""
var body: some View {
HStack {
creatinineRadioMgMajority
creatinineRadioUmolMajority
}
}
var creatinineRadioMgMajority: some View {
RadioButtonField(
id: Creatinine.mg.rawValue,
label: Creatinine.mg.rawValue,
isMarked: selectedId == Creatinine.mg.rawValue ? true : false,
callback: creatinineRadioGroupCallback
)
}
var creatinineRadioUmolMajority: some View {
RadioButtonField(
id: Creatinine.umol.rawValue,
label: Creatinine.umol.rawValue,
isMarked: selectedId == Creatinine.umol.rawValue ? true : false,
callback: creatinineRadioGroupCallback
)
}
func creatinineRadioGroupCallback(id: String) {
selectedId = id
callback(id)
}
}
struct RadioButtonFieldView: View {
var body: some View {
HStack {
Text("Sex:")
.font(Font.headline)
RadioButtonGroups { selected in
return
//print("Selected Sex is: (selected)")
}
}.padding()
HStack {
Text("Race:")
.font(Font.headline)
raceRadioButtonGroups { selected in
return
//print("Selected Race is: (selected)")
}
}.padding()
HStack {
Text("Weight:")
.font(Font.headline)
weightRadioButtonGroups { selected in
return
//print("Selected Weight is: (selected)")
}
}.padding()
HStack {
Text("Height:")
.font(Font.headline)
heightRadioButtonGroups { selected in
return
// print("Selected Height is: (selected)")
}
}.padding()
HStack {
Text("Creatinine:")
.font(Font.headline)
creatinineRadioButtonGroups { selected in
return
// print("Selected Creatinine is: (selected)")
}
}.padding()
}
}
struct RadioButtonField_Previews: PreviewProvider {