1. import SwiftUI 2. //MARK:- Single Radio Button Field 3. struct RadioButtonField: View { 4. let id: String 5. let label: String 6. let size: CGFloat 7. let color: Color 8. let textSize: CGFloat 9. let isMarked:Bool 10. let callback: (String)->() 11. 12. init( 13. id: String, 14. label:String, 15. size: CGFloat = 20, 16. color: Color = Color.black, 17. textSize: CGFloat = 14, 18. isMarked: Bool = false, 19. callback: @escaping (String)->() 20. ) { 21. self.id = id 22. self.label = label 23. self.size = size 24. self.color = color 25. self.textSize = textSize 26. self.isMarked = isMarked 27. self.callback = callback 28. } 29. 30. var body: some View { 31. Button(action:{ 32. self.callback(self.id) 33. }) { 34. HStack(alignment: .center, spacing: 10) { 35. Image(systemName: self.isMarked ? "largecircle.fill.circle" : "circle") 36. .renderingMode(.original) 37. .resizable() 38. .aspectRatio(contentMode: .fit) 39. .frame(width: self.size, height: self.size) 40. 41. Text(label) 42. //.font(Font.system(size: textSize)) 43. .font(.caption) 44. 45. Spacer() 46. }.foregroundColor(self.color) 47. } 48. .foregroundColor(Color.white) 49. } 50. } 51. 52. //MARK:- Group of weight Radio Buttons 53. enum Weight: String { 54. case kg = "Kgs" 55. case pounds = "lbs" 56. } 57. 58. struct weightRadioButtonGroups: View { 59. let callback: (String) -> () 60. 61. @State var selectedId: String = "" 62. 63. var body: some View { 64. HStack { 65. weightRadioKgMajority 66. weightRadioPoundsMajority 67. }.padding(.leading, 6) 68. } 69. 70. var weightRadioKgMajority: some View { 71. RadioButtonField( 72. id: Weight.kg.rawValue, 73. label: Weight.kg.rawValue, 74. isMarked: selectedId == Weight.kg.rawValue ? true : false, 75. callback: weightRadioGroupCallback 76. ) 77. } 78. 79. var weightRadioPoundsMajority: some View { 80. RadioButtonField( 81. id: Weight.pounds.rawValue, 82. label: Weight.pounds.rawValue, 83. isMarked: selectedId == Weight.pounds.rawValue ? true : false, 84. callback: weightRadioGroupCallback 85. ) 86. } 87. 88. func weightRadioGroupCallback(id: String) { 89. selectedId = id 90. callback(id) 91. } 92. } 93. //MARK:- Group of height Radio Buttons 94. enum Height: String { 95. case cms = "cms" 96. case inches = "inches" 97. } 98. 99. struct heightRadioButtonGroups: View { 100. let callback: (String) -> () 101. 102. @State var selectedId: String = "" 103. 104. var body: some View { 105. HStack { 106. heightRadioCmsMajority 107. heightRadioInchesMajority 108. }.padding(.leading, 8) 109. } 110. 111. var heightRadioCmsMajority: some View { 112. RadioButtonField( 113. id: Height.cms.rawValue, 114. label: Height.cms.rawValue, 115. isMarked: selectedId == Height.cms.rawValue ? true : false, 116. callback: heightRadioGroupCallback 117. ) 118. } 119. 120. var heightRadioInchesMajority: some View { 121. RadioButtonField( 122. id: Height.inches.rawValue, 123. label: Height.inches.rawValue, 124. isMarked: selectedId == Height.inches.rawValue ? true : false, 125. callback: heightRadioGroupCallback 126. ) 127. } 128. 129. func heightRadioGroupCallback(id: String) { 130. selectedId = id 131. callback(id) 132. } 133. } 134. 135. //MARK:- Group of creatinine Radio Buttons 136. enum Creatinine: String { 137. case mg = "mg/dL" 138. case umol = "\u{03BC}mol/L" 139. } 140. 141. struct creatinineRadioButtonGroups: View { 142. let callback: (String) -> () 143. 144. @State var selectedId: String = "" 145. 146. var body: some View { 147. HStack { 148. creatinineRadioMgMajority 149. creatinineRadioUmolMajority 150. } 151. } 152. 153. var creatinineRadioMgMajority: some View { 154. RadioButtonField( 155. id: Creatinine.mg.rawValue, 156. label: Creatinine.mg.rawValue, 157. isMarked: selectedId == Creatinine.mg.rawValue ? true : false, 158. callback: creatinineRadioGroupCallback 159. ) 160. } 161. 162. var creatinineRadioUmolMajority: some View { 163. RadioButtonField( 164. id: Creatinine.umol.rawValue, 165. label: Creatinine.umol.rawValue, 166. isMarked: selectedId == Creatinine.umol.rawValue ? true : false, 167. callback: creatinineRadioGroupCallback 168. ) 169. } 170. 171. func creatinineRadioGroupCallback(id: String) { 172. selectedId = id 173. callback(id) 174. } 175. } 176. 177. struct RadioButtonFieldView: View { 178. var body: some View { 179. HStack { 180. Text("***:") 181. .font(Font.headline) 182. RadioButtonGroups { selected in 183. return 184. //print("Selected *** is: (selected)") 185. 186. } 187. }.padding() 188. 189. HStack { 190. Text("Race:") 191. .font(Font.headline) 192. raceRadioButtonGroups { selected in 193. return 194. //print("Selected Race is: (selected)") 195. } 196. }.padding() 197. 198. 199. HStack { 200. Text("Weight:") 201. .font(Font.headline) 202. 203. weightRadioButtonGroups { selected in 204. return 205. //print("Selected Weight is: (selected)") 206. } 207. }.padding() 208. 209. HStack { 210. Text("Height:") 211. .font(Font.headline) 212. 213. heightRadioButtonGroups { selected in 214. return 215. // print("Selected Height is: (selected)") 216. } 217. }.padding() 218. 219. HStack { 220. Text("Creatinine:") 221. .font(Font.headline) 222. creatinineRadioButtonGroups { selected in 223. return 224. // print("Selected Creatinine is: (selected)") 225. } 226. }.padding() 227. } 228. 229. } 230. 231. struct ContentView: View { 232. @State var age = "" 233. @State var *** = "" 234. @State var race = "" 235. @State var height = "" 236. @State var weight = "" 237. @State var creatinine = "" 238. 239. var body: some View { 240. VStack { 241. Text("") 242. Spacer() 243. Group{ 244. HStack { 245. Text("Age (Years):") 246. .fontWeight(/@START_MENU_TOKEN@/.bold/@END_MENU_TOKEN@/) 247. .frame(width: 200, height: 40, alignment: .leading) 248. .padding(.leading, 20) 249. 250. Spacer() 251. 252. TextField("Enter age:", text: $age) 253. .frame(width: 100, height: 40, alignment: .leading) 254. .multilineTextAlignment(.trailing) 255. .padding(.trailing, 20) 256. } 257. HStack { 258. Text("Height:") 259. .fontWeight(/@START_MENU_TOKEN@/.bold/@END_MENU_TOKEN@/) 260. .frame(width: 100, height: 40, alignment: .leading) 261. .padding(.leading, 20) 262. Spacer() 263. heightRadioButtonGroups { selected in 264. return 265. 266. } 267. 268. TextField("Enter Height:", text: $height) 269. .frame(width: 80, height: 40, alignment: .leading) 270. .multilineTextAlignment(.trailing) 271. .padding(.trailing, 20) 272. } 273. HStack { 274. Text("Weight:") 275. .fontWeight(/@START_MENU_TOKEN@/.bold/@END_MENU_TOKEN@/) 276. .frame(width: 100, height: 40, alignment: .leading) 277. .padding(.leading, 20) 278. 279. Spacer() 280. 281. weightRadioButtonGroups { selected in 282. return 283. 284. } 285. 286. TextField("Enter weight", text: $weight) 287. .frame(width: 80, height: 40, alignment: .leading) 288. .multilineTextAlignment(.trailing) 289. .padding(.trailing, 20) 290. 291. 292. } 293. 294. Spacer() 295. 296. Button(action: { 297. 298. //calculate() 299. 300. }, label: { 301. Text("CALCULATE") 302. .font(.largeTitle) 303. .fontWeight(.bold) 304. .foregroundColor(Color.white) 305. .background(Color("teal")) 306. .frame(width: 200, height: 40, alignment: /@START_MENU_TOKEN@/.center/@END_MENU_TOKEN@/) 307. .clipShape(RoundedRectangle(cornerRadius: 12)) 308. .shadow(radius: 14) 309. }) 310. 311. Spacer() 312. 313. Button(action: { 314. // reset() 315. }, label: { 316. Text("RESET") 317. .font(.largeTitle) 318. .fontWeight(.bold) 319. .foregroundColor(Color.white) 320. .background(Color("teal")) 321. .frame(width: 140, height: 40, alignment: /@START_MENU_TOKEN@/.center/@END_MENU_TOKEN@/) 322. .clipShape(RoundedRectangle(cornerRadius: 12)) 323. .shadow(radius: 14) 324. }) 325. } 326. } 327. } 328. } 329. 330. func calculate() -> Double { 331. 332. var heightCms: Double = 0.0 333. 334. if (heightRadioButtonGroups(callback: heightRadioInchesMajority).isSelected) { 335. return Double(height) ?? 1 * 2.54 336. } else { 337. return Double(height) ?? 1 338. } 339. }