InformationView.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // InformationView.swift
  3. // LoopKitUI
  4. //
  5. // Created by Anna Quinlan on 7/1/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. struct InformationView<InformationalContent: View> : View {
  10. var informationalContent: InformationalContent
  11. var title: Text
  12. var buttonText: Text
  13. var onExit: (() -> Void)
  14. let mode: SettingsPresentationMode
  15. init(
  16. title: Text,
  17. buttonText: Text,
  18. @ViewBuilder informationalContent: () -> InformationalContent,
  19. onExit: @escaping () -> Void,
  20. mode: SettingsPresentationMode
  21. ) {
  22. self.title = title
  23. self.buttonText = buttonText
  24. self.informationalContent = informationalContent()
  25. self.onExit = onExit
  26. self.mode = mode
  27. }
  28. // Convenience initializer for info view w/next page button
  29. init(
  30. title: Text,
  31. @ViewBuilder informationalContent: () -> InformationalContent,
  32. onExit: @escaping () -> Void,
  33. mode: SettingsPresentationMode = .acceptanceFlow
  34. ) {
  35. self.init(
  36. title: title,
  37. buttonText: Text(LocalizedString("Continue", comment: "Button to advance to setting editor")),
  38. informationalContent: informationalContent,
  39. onExit: onExit,
  40. mode: mode
  41. )
  42. }
  43. var body: some View {
  44. GeometryReader { geometry in
  45. ScrollView {
  46. bodyForMode
  47. .padding()
  48. .frame(minHeight: geometry.size.height)
  49. }
  50. }
  51. }
  52. @ViewBuilder
  53. private var bodyForMode: some View {
  54. switch mode {
  55. case .acceptanceFlow:
  56. bodyForAcceptanceFlow
  57. case .settings:
  58. bodyForSettings
  59. }
  60. }
  61. private var bodyForAcceptanceFlow: some View {
  62. VStack(alignment: .leading, spacing: 20) {
  63. titleView
  64. Divider()
  65. informationalContent
  66. Spacer()
  67. nextPageButton
  68. }
  69. }
  70. private var bodyForSettings: some View {
  71. VStack(alignment: .leading, spacing: 20) {
  72. informationalContent
  73. Spacer()
  74. }
  75. .toolbar {
  76. ToolbarItem(placement: .navigationBarTrailing) {
  77. cancelButton
  78. }
  79. }
  80. .navigationBarTitle(title, displayMode: .large)
  81. }
  82. private var titleView: some View {
  83. title
  84. .font(.largeTitle)
  85. .bold()
  86. .fixedSize(horizontal: false, vertical: true)
  87. }
  88. private var cancelButton: some View {
  89. Button(action: onExit, label: { Text(LocalizedString("Close", comment: "Text to close informational page")) })
  90. }
  91. private var nextPageButton: some View {
  92. Button(action: onExit) {
  93. buttonText
  94. .actionButtonStyle(.primary)
  95. }
  96. }
  97. }