InformationView.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. ScrollView {
  45. bodyWithCancelButtonIfNeeded
  46. .navigationBarTitle(title, displayMode: .large)
  47. .padding()
  48. }
  49. }
  50. private var bodyWithCancelButtonIfNeeded: some View {
  51. switch mode {
  52. case .acceptanceFlow:
  53. return AnyView(bodyWithBottomButton)
  54. case .settings:
  55. return AnyView(bodyWithCancelButton)
  56. }
  57. }
  58. private var bodyWithBottomButton: some View {
  59. VStack(alignment: .leading, spacing: 20) {
  60. informationalContent
  61. Spacer()
  62. nextPageButton
  63. }
  64. }
  65. private var bodyWithCancelButton: some View {
  66. VStack(alignment: .leading, spacing: 20) {
  67. informationalContent
  68. Spacer()
  69. }
  70. .navigationBarItems(trailing: cancelButton)
  71. }
  72. private var cancelButton: some View {
  73. Button(action: onExit, label: { Text(LocalizedString("Close", comment: "Text to close informational page")) })
  74. }
  75. private var nextPageButton: some View {
  76. Button(action: onExit) {
  77. buttonText
  78. .actionButtonStyle(.primary)
  79. }
  80. }
  81. }