SettingInputSection.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import SwiftUI
  2. struct SettingInputSection: View {
  3. enum InputType {
  4. case decimal
  5. case boolean
  6. case conditionalDecimal
  7. }
  8. @Binding var decimalValue: Decimal
  9. @Binding var booleanValue: Bool
  10. @Binding var shouldDisplayHint: Bool
  11. @Binding var selectedVerboseHint: String?
  12. var type: InputType
  13. var label: String
  14. var conditionalLabel: String?
  15. var miniHint: String
  16. var verboseHint: String
  17. var headerText: String?
  18. var footerText: String?
  19. private var formatter: NumberFormatter {
  20. let formatter = NumberFormatter()
  21. formatter.numberStyle = .decimal
  22. return formatter
  23. }
  24. var body: some View {
  25. Section(
  26. content: {
  27. VStack {
  28. if type == .decimal {
  29. HStack {
  30. Text(label)
  31. TextFieldWithToolBar(
  32. text: Binding(
  33. get: { decimalValue },
  34. set: { decimalValue = $0 }
  35. ),
  36. placeholder: decimalValue.description,
  37. numberFormatter: formatter
  38. )
  39. }.padding(.top)
  40. } else if type == .boolean {
  41. HStack {
  42. Toggle(isOn: $booleanValue) {
  43. Text(label)
  44. }
  45. }.padding(.top)
  46. } else if type == .conditionalDecimal, let secondLabel = conditionalLabel {
  47. HStack {
  48. Toggle(isOn: $booleanValue) {
  49. Text(label)
  50. }
  51. }.padding(.vertical)
  52. if $booleanValue.wrappedValue {
  53. HStack {
  54. Text(secondLabel)
  55. TextFieldWithToolBar(
  56. text: Binding(
  57. get: { decimalValue },
  58. set: { decimalValue = $0 }
  59. ),
  60. placeholder: decimalValue.description,
  61. numberFormatter: formatter
  62. )
  63. }.padding(.bottom)
  64. }
  65. }
  66. HStack(alignment: .top) {
  67. Text(miniHint)
  68. .font(.footnote)
  69. .foregroundColor(.secondary)
  70. .lineLimit(nil)
  71. Spacer()
  72. Button(
  73. action: {
  74. shouldDisplayHint.toggle()
  75. selectedVerboseHint = shouldDisplayHint ? verboseHint : nil
  76. },
  77. label: {
  78. HStack {
  79. Image(systemName: "questionmark.circle")
  80. }
  81. }
  82. ).buttonStyle(BorderlessButtonStyle())
  83. }.padding(type == .boolean ? .vertical : .bottom)
  84. }
  85. },
  86. header: {
  87. if let headerText = headerText {
  88. Text(headerText)
  89. }
  90. },
  91. footer: {
  92. if let footerText = footerText {
  93. Text(footerText)
  94. }
  95. }
  96. ).listRowBackground(Color.chart)
  97. }
  98. }