TrioWatchComplication.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 .accessoryCircular:
  30. TrioAccessoryCircularView(entry: entry)
  31. case .accessoryCorner:
  32. TrioAccessoryCornerView(entry: entry)
  33. default:
  34. Image("ComplicationIcon")
  35. .widgetAccentable()
  36. .widgetBackground(backgroundView: Color.clear)
  37. }
  38. }
  39. }
  40. /// Corner Complication
  41. struct TrioAccessoryCornerView: View {
  42. var entry: TrioWatchComplicationProvider.Entry
  43. var body: some View {
  44. Text("")
  45. .widgetCurvesContent()
  46. .widgetLabel {
  47. Text("Trio")
  48. }
  49. .widgetBackground(backgroundView: Color.clear)
  50. }
  51. }
  52. /// Circular Complication
  53. struct TrioAccessoryCircularView: View {
  54. var entry: TrioWatchComplicationProvider.Entry
  55. var body: some View {
  56. Image("ComplicationIcon")
  57. .resizable()
  58. .widgetAccentable()
  59. .widgetBackground(backgroundView: Color.clear)
  60. }
  61. }
  62. // MARK: - Widget Configuration
  63. @main struct TrioWatchComplication: Widget {
  64. let kind: String = "TrioWatchComplication"
  65. var body: some WidgetConfiguration {
  66. StaticConfiguration(kind: kind, provider: TrioWatchComplicationProvider()) { entry in
  67. TrioWatchComplicationEntryView(entry: entry)
  68. }
  69. .configurationDisplayName("Trio")
  70. .description("Displays Trio app icon as complication")
  71. .supportedFamilies([
  72. .accessoryCorner,
  73. .accessoryCircular
  74. ])
  75. }
  76. }
  77. extension View {
  78. func widgetBackground(backgroundView: some View) -> some View {
  79. if #available(watchOS 10.0, iOSApplicationExtension 17.0, iOS 17.0, *) {
  80. return containerBackground(for: .widget) {
  81. backgroundView
  82. }
  83. } else {
  84. return background(backgroundView)
  85. }
  86. }
  87. }