WarningView.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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: .center) {
  30. Image(systemName: "exclamationmark.triangle.fill")
  31. .foregroundColor(warningColor)
  32. title
  33. .font(Font(UIFont.preferredFont(forTextStyle: .title3)))
  34. .bold()
  35. .fixedSize(horizontal: false, vertical: true)
  36. }
  37. .padding(.bottom, 2)
  38. caption
  39. .font(.callout)
  40. .foregroundColor(Color(.secondaryLabel))
  41. .fixedSize(horizontal: false, vertical: true)
  42. }
  43. .accessibilityElement(children: .combine)
  44. Spacer()
  45. }
  46. }
  47. private var warningColor: Color {
  48. switch severity {
  49. case .default:
  50. return guidanceColors.warning
  51. case .critical:
  52. return guidanceColors.critical
  53. }
  54. }
  55. }