WarningView.swift 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // WarningView.swift
  3. // LoopKitUI
  4. //
  5. // Created by Michael Pangburn on 7/24/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. public enum WarningSeverity: Int, Comparable {
  10. case `default`
  11. case critical
  12. public static func < (lhs: WarningSeverity, rhs: WarningSeverity) -> Bool {
  13. lhs.rawValue < rhs.rawValue
  14. }
  15. }
  16. public struct WarningView: View {
  17. @Environment(\.guidanceColors) var guidanceColors
  18. var title: Text
  19. var caption: Text
  20. var severity: WarningSeverity
  21. public init(title: Text, caption: Text, severity: WarningSeverity = .default) {
  22. self.title = title
  23. self.caption = caption
  24. self.severity = severity
  25. }
  26. public var body: some View {
  27. HStack {
  28. VStack(alignment: .leading) {
  29. HStack(alignment: .firstTextBaseline) {
  30. Image(systemName: "exclamationmark.triangle.fill")
  31. .foregroundColor(warningColor)
  32. title
  33. .font(Font(UIFont.preferredFont(forTextStyle: .title3)))
  34. .bold()
  35. .fixedSize()
  36. .animation(nil)
  37. }
  38. caption
  39. .font(.callout)
  40. .foregroundColor(Color(.secondaryLabel))
  41. .fixedSize(horizontal: false, vertical: true)
  42. .animation(nil)
  43. }
  44. .accessibilityElement(children: .combine)
  45. Spacer()
  46. }
  47. }
  48. private var warningColor: Color {
  49. switch severity {
  50. case .default:
  51. return guidanceColors.warning
  52. case .critical:
  53. return guidanceColors.critical
  54. }
  55. }
  56. }