ComplicationController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import ClockKit
  2. import SwiftUI
  3. class ComplicationController: NSObject, CLKComplicationDataSource {
  4. // MARK: - Complication Configuration
  5. func getComplicationDescriptors(handler: @escaping ([CLKComplicationDescriptor]) -> Void) {
  6. let descriptors = [
  7. CLKComplicationDescriptor(
  8. identifier: "complication",
  9. displayName: "FreeAPS X",
  10. supportedFamilies: [
  11. .graphicCorner,
  12. .graphicCircular,
  13. .modularSmall,
  14. .utilitarianSmall,
  15. .circularSmall
  16. ]
  17. )
  18. ]
  19. // Call the handler with the currently supported complication descriptors
  20. handler(descriptors)
  21. }
  22. func handleSharedComplicationDescriptors(_: [CLKComplicationDescriptor]) {
  23. // Do any necessary work to support these newly shared complication descriptors
  24. }
  25. // MARK: - Timeline Configuration
  26. func getTimelineEndDate(for _: CLKComplication, withHandler handler: @escaping (Date?) -> Void) {
  27. // Call the handler with the last entry date you can currently provide or nil if you can't support future timelines
  28. handler(nil)
  29. }
  30. func getPrivacyBehavior(for _: CLKComplication, withHandler handler: @escaping (CLKComplicationPrivacyBehavior) -> Void) {
  31. // Call the handler with your desired behavior when the device is locked
  32. handler(.showOnLockScreen)
  33. }
  34. // MARK: - Timeline Population
  35. func getCurrentTimelineEntry(
  36. for complication: CLKComplication,
  37. withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void
  38. ) {
  39. switch complication.family {
  40. case .graphicCorner:
  41. guard let image = UIImage(named: "Complication/Graphic Corner") else {
  42. handler(nil)
  43. return
  44. }
  45. let template = CLKComplicationTemplateGraphicCornerTextImage(
  46. textProvider: CLKTextProvider(format: "%@", "FreeAPS X"),
  47. imageProvider: CLKFullColorImageProvider(fullColorImage: image)
  48. )
  49. let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
  50. handler(timelineEntry)
  51. case .modularSmall:
  52. let template = CLKComplicationTemplateModularSmallRingText(
  53. textProvider: CLKTextProvider(format: "%@", "FAX"),
  54. fillFraction: 1,
  55. ringStyle: .closed
  56. )
  57. let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
  58. handler(timelineEntry)
  59. case .utilitarianSmall:
  60. guard let image = UIImage(named: "Complication/Utilitarian") else {
  61. handler(nil)
  62. return
  63. }
  64. let template = CLKComplicationTemplateUtilitarianSmallSquare(
  65. imageProvider: CLKImageProvider(onePieceImage: image)
  66. )
  67. let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
  68. handler(timelineEntry)
  69. case .circularSmall:
  70. let template =
  71. CLKComplicationTemplateCircularSmallSimpleText(textProvider: CLKTextProvider(format: "%@", "FAX"))
  72. let timelineEntry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template)
  73. handler(timelineEntry)
  74. default:
  75. handler(nil)
  76. }
  77. }
  78. func getTimelineEntries(
  79. for _: CLKComplication,
  80. after _: Date,
  81. limit _: Int,
  82. withHandler handler: @escaping ([CLKComplicationTimelineEntry]?) -> Void
  83. ) {
  84. handler(nil)
  85. }
  86. // MARK: - Sample Templates
  87. func getLocalizableSampleTemplate(
  88. for _: CLKComplication,
  89. withHandler handler: @escaping (CLKComplicationTemplate?) -> Void
  90. ) {
  91. handler(nil)
  92. }
  93. }