TrioWatchComplication.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import SwiftUI
  2. import WidgetKit
  3. // MARK: - Timeline Entry
  4. struct TrioWatchComplicationEntry: TimelineEntry {
  5. let date: Date
  6. }
  7. // MARK: - Provider
  8. struct TrioWatchComplicationProvider: TimelineProvider {
  9. func placeholder(in _: Context) -> TrioWatchComplicationEntry {
  10. TrioWatchComplicationEntry(date: Date())
  11. }
  12. func getSnapshot(in _: Context, completion: @escaping (TrioWatchComplicationEntry) -> Void) {
  13. let entry = TrioWatchComplicationEntry(date: Date())
  14. completion(entry)
  15. }
  16. func getTimeline(in _: Context, completion: @escaping (Timeline<TrioWatchComplicationEntry>) -> Void) {
  17. let entry = TrioWatchComplicationEntry(date: Date())
  18. let timeline = Timeline(entries: [entry], policy: .never)
  19. completion(timeline)
  20. }
  21. }
  22. // MARK: - Views
  23. //// Displayed View Wrapper
  24. struct TrioWatchComplicationEntryView: View {
  25. @Environment(\.widgetFamily) private var widgetFamily
  26. var entry: TrioWatchComplicationEntry
  27. var body: some View {
  28. switch widgetFamily {
  29. case .accessoryRectangular:
  30. TrioAccessoryRectangularView(entry: entry)
  31. case .accessoryCircular:
  32. TrioAccessoryCircularView(entry: entry)
  33. case .accessoryCorner:
  34. TrioAccessoryCornerView(entry: entry)
  35. case .accessoryInline:
  36. TrioAccessoryInlineView(entry: entry)
  37. default:
  38. Image("ComplicationIcon")
  39. }
  40. }
  41. }
  42. /// Corner Complication
  43. struct TrioAccessoryCornerView: View {
  44. var entry: TrioWatchComplicationProvider.Entry
  45. var body: some View {
  46. ZStack {
  47. Circle()
  48. .fill(Color.white.opacity(0.2))
  49. Image("ComplicationIcon")
  50. .resizable()
  51. .scaledToFit()
  52. .padding(5)
  53. }
  54. }
  55. }
  56. /// Circular Complication
  57. struct TrioAccessoryCircularView: View {
  58. var entry: TrioWatchComplicationProvider.Entry
  59. var body: some View {
  60. if let uiImage = UIImage(named: "ComplicationIcon") {
  61. Image(uiImage: uiImage)
  62. .resizable()
  63. .scaledToFit()
  64. .padding(5)
  65. } else {
  66. ZStack {
  67. Circle().fill(Color.red.opacity(0.2))
  68. Text("No Image!")
  69. .font(.caption)
  70. .foregroundColor(.white)
  71. }
  72. }
  73. }
  74. }
  75. /// Rectangular Complication
  76. struct TrioAccessoryRectangularView: View {
  77. var entry: TrioWatchComplicationProvider.Entry
  78. var body: some View {
  79. HStack {
  80. Image("ComplicationIcon")
  81. .resizable()
  82. .scaledToFit()
  83. .frame(width: 30, height: 30)
  84. Text("Trio")
  85. .font(.headline)
  86. .foregroundColor(.primary)
  87. }
  88. }
  89. }
  90. /// Inline Complication
  91. struct TrioAccessoryInlineView: View {
  92. var entry: TrioWatchComplicationProvider.Entry
  93. var body: some View {
  94. HStack {
  95. Image("ComplicationIcon")
  96. .resizable()
  97. .scaledToFit()
  98. .frame(width: 12, height: 12)
  99. Text("Trio")
  100. .font(.caption)
  101. .foregroundColor(.primary)
  102. }
  103. }
  104. }
  105. // MARK: - Widget Configuration
  106. @main struct TrioWatchComplication: Widget {
  107. let kind: String = "TrioWatchComplication"
  108. var body: some WidgetConfiguration {
  109. StaticConfiguration(kind: kind, provider: TrioWatchComplicationProvider()) { entry in
  110. TrioWatchComplicationEntryView(entry: entry)
  111. }
  112. .configurationDisplayName("Trio")
  113. .description("Displays Trio app icon as complication")
  114. .supportedFamilies([
  115. .accessoryCorner,
  116. .accessoryCircular,
  117. .accessoryRectangular,
  118. .accessoryInline
  119. ])
  120. }
  121. }