ComplicationController.swift 3.8 KB

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