TrioWatchComplication.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. Text("Trio")
  47. .font(.caption)
  48. .foregroundColor(.white)
  49. }
  50. }
  51. /// Circular Complication
  52. struct TrioAccessoryCircularView: View {
  53. var entry: TrioWatchComplicationProvider.Entry
  54. var body: some View {
  55. Text("Trio")
  56. .font(.caption)
  57. .foregroundColor(.white)
  58. }
  59. }
  60. /// Rectangular Complication
  61. struct TrioAccessoryRectangularView: View {
  62. var entry: TrioWatchComplicationProvider.Entry
  63. var body: some View {
  64. Text("Trio")
  65. .font(.headline)
  66. .foregroundColor(.primary)
  67. }
  68. }
  69. /// Inline Complication
  70. struct TrioAccessoryInlineView: View {
  71. var entry: TrioWatchComplicationProvider.Entry
  72. var body: some View {
  73. Text("Trio")
  74. .font(.caption)
  75. .foregroundColor(.primary)
  76. }
  77. }
  78. // MARK: - Widget Configuration
  79. @main struct TrioWatchComplication: Widget {
  80. let kind: String = "TrioWatchComplication"
  81. var body: some WidgetConfiguration {
  82. StaticConfiguration(kind: kind, provider: TrioWatchComplicationProvider()) { entry in
  83. TrioWatchComplicationEntryView(entry: entry)
  84. }
  85. .configurationDisplayName("Trio")
  86. .description("Displays Trio app icon as complication")
  87. .supportedFamilies([
  88. .accessoryCorner,
  89. .accessoryCircular,
  90. .accessoryRectangular,
  91. .accessoryInline
  92. ])
  93. }
  94. }