PluginManager.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import Foundation
  2. import LoopKit
  3. import LoopKitUI
  4. import Swinject
  5. protocol PluginManager {
  6. var availablePumpManagers: [PumpManagerDescriptor] { get }
  7. var availableCGMManagers: [CGMManagerDescriptor] { get }
  8. func getPumpManagerTypeByIdentifier(_ identifier: String) -> PumpManagerUI.Type?
  9. func getCGMManagerTypeByIdentifier(_ identifier: String) -> CGMManagerUI.Type?
  10. }
  11. class BasePluginManager: Injectable, PluginManager {
  12. let pluginBundles: [Bundle]
  13. init(resolver: Resolver) {
  14. let pluginsURL: URL? = Bundle.main.privateFrameworksURL
  15. var bundles = [Bundle]()
  16. if let pluginsURL = pluginsURL {
  17. do {
  18. for pluginURL in try FileManager.default.contentsOfDirectory(at: pluginsURL, includingPropertiesForKeys: nil)
  19. .filter({ $0.path.hasSuffix(".framework") })
  20. {
  21. if let bundle = Bundle(url: pluginURL) {
  22. if let bname = bundle.object(forInfoDictionaryKey: "CFBundleName") as? String {
  23. debug(.deviceManager, "bundle name2:\(bname)")
  24. }
  25. if let bcgm = bundle.object(forInfoDictionaryKey: "com.loopkit.Loop.CGMManagerIdentifier") as? String {
  26. debug(.deviceManager, "bundle is CGM")
  27. }
  28. if bundle.isLoopPlugin {
  29. debug(.deviceManager, "Found loop plugin:\(pluginURL.absoluteString)")
  30. bundles.append(bundle)
  31. }
  32. }
  33. }
  34. } catch {
  35. debug(.deviceManager, "Error loading plugin: \(error)")
  36. }
  37. }
  38. pluginBundles = bundles
  39. injectServices(resolver)
  40. }
  41. func getPumpManagerTypeByIdentifier(_ identifier: String) -> PumpManagerUI.Type? {
  42. for bundle in pluginBundles {
  43. if let name = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.pumpManagerIdentifier.rawValue) as? String,
  44. name == identifier
  45. {
  46. do {
  47. try bundle.loadAndReturnError()
  48. if let principalClass = bundle.principalClass as? NSObject.Type {
  49. if let plugin = principalClass.init() as? PumpManagerUIPlugin {
  50. return plugin.pumpManagerType
  51. } else {
  52. fatalError("PrincipalClass does not conform to PumpManagerUIPlugin")
  53. }
  54. } else {
  55. fatalError("PrincipalClass not found")
  56. }
  57. } catch {
  58. debug(.deviceManager, "Error loading plugin: \(error)")
  59. }
  60. }
  61. }
  62. return nil
  63. }
  64. var availablePumpManagers: [PumpManagerDescriptor] {
  65. pluginBundles.compactMap({ (bundle) -> PumpManagerDescriptor? in
  66. guard let title = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.pumpManagerDisplayName.rawValue) as? String,
  67. let identifier = bundle
  68. .object(forInfoDictionaryKey: LoopPluginBundleKey.pumpManagerIdentifier.rawValue) as? String
  69. else {
  70. return nil
  71. }
  72. return PumpManagerDescriptor(identifier: identifier, localizedTitle: title)
  73. })
  74. }
  75. func getCGMManagerTypeByIdentifier(_ identifier: String) -> CGMManagerUI.Type? {
  76. for bundle in pluginBundles {
  77. if let name = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.cgmManagerIdentifier.rawValue) as? String,
  78. name == identifier
  79. {
  80. do {
  81. try bundle.loadAndReturnError()
  82. if let principalClass = bundle.principalClass as? NSObject.Type {
  83. if let plugin = principalClass.init() as? CGMManagerUIPlugin {
  84. return plugin.cgmManagerType
  85. } else {
  86. fatalError("PrincipalClass does not conform to CGMManagerUIPlugin")
  87. }
  88. } else {
  89. fatalError("PrincipalClass not found")
  90. }
  91. } catch {
  92. debug(.deviceManager, "Error loading plugin: \(error)")
  93. }
  94. }
  95. }
  96. return nil
  97. }
  98. var availableCGMManagers: [CGMManagerDescriptor] {
  99. pluginBundles.compactMap({ (bundle) -> CGMManagerDescriptor? in
  100. guard let title = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.cgmManagerDisplayName.rawValue) as? String,
  101. let identifier = bundle
  102. .object(forInfoDictionaryKey: LoopPluginBundleKey.cgmManagerIdentifier.rawValue) as? String
  103. else {
  104. return nil
  105. }
  106. return CGMManagerDescriptor(identifier: identifier, localizedTitle: title)
  107. })
  108. }
  109. func getServiceTypeByIdentifier(_ identifier: String) -> ServiceUI.Type? {
  110. for bundle in pluginBundles {
  111. if let name = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.serviceIdentifier.rawValue) as? String,
  112. name == identifier
  113. {
  114. do {
  115. try bundle.loadAndReturnError()
  116. if let principalClass = bundle.principalClass as? NSObject.Type {
  117. if let plugin = principalClass.init() as? ServiceUIPlugin {
  118. return plugin.serviceType
  119. } else {
  120. fatalError("PrincipalClass does not conform to ServiceUIPlugin")
  121. }
  122. } else {
  123. fatalError("PrincipalClass not found")
  124. }
  125. } catch {
  126. debug(.deviceManager, "Error loading plugin: \(error)")
  127. }
  128. }
  129. }
  130. return nil
  131. }
  132. var availableServices: [ServiceDescriptor] {
  133. pluginBundles.compactMap({ (bundle) -> ServiceDescriptor? in
  134. guard let title = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.serviceDisplayName.rawValue) as? String,
  135. let identifier = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.serviceIdentifier.rawValue) as? String
  136. else {
  137. return nil
  138. }
  139. return ServiceDescriptor(identifier: identifier, localizedTitle: title)
  140. })
  141. }
  142. func getStatefulPluginTypeByIdentifier(_ identifier: String) -> StatefulPluggable.Type? {
  143. for bundle in pluginBundles {
  144. if let name = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.statefulPluginIdentifier.rawValue) as? String,
  145. name == identifier
  146. {
  147. do {
  148. try bundle.loadAndReturnError()
  149. if let principalClass = bundle.principalClass as? NSObject.Type {
  150. if let plugin = principalClass.init() as? StatefulPlugin {
  151. return plugin.pluginType
  152. } else {
  153. fatalError("PrincipalClass does not conform to StatefulPlugin")
  154. }
  155. } else {
  156. fatalError("PrincipalClass not found")
  157. }
  158. } catch {
  159. debug(.deviceManager, "Error loading plugin: \(error)")
  160. }
  161. }
  162. }
  163. return nil
  164. }
  165. var availableStatefulPluginIdentifiers: [String] {
  166. pluginBundles.compactMap({ (bundle) -> String? in
  167. bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.statefulPluginIdentifier.rawValue) as? String
  168. })
  169. }
  170. func getOnboardingTypeByIdentifier(_ identifier: String) -> OnboardingUI.Type? {
  171. for bundle in pluginBundles {
  172. if let name = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.onboardingIdentifier.rawValue) as? String,
  173. name == identifier
  174. {
  175. do {
  176. try bundle.loadAndReturnError()
  177. if let principalClass = bundle.principalClass as? NSObject.Type {
  178. if let plugin = principalClass.init() as? OnboardingUIPlugin {
  179. return plugin.onboardingType
  180. } else {
  181. fatalError("PrincipalClass does not conform to OnboardingUIPlugin")
  182. }
  183. } else {
  184. fatalError("PrincipalClass not found")
  185. }
  186. } catch {
  187. debug(.deviceManager, "Error loading plugin: \(error)")
  188. }
  189. }
  190. }
  191. return nil
  192. }
  193. var availableOnboardingIdentifiers: [String] {
  194. pluginBundles.compactMap({ (bundle) -> String? in
  195. bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.onboardingIdentifier.rawValue) as? String
  196. })
  197. }
  198. func getSupportUITypeByIdentifier(_ identifier: String) -> SupportUI.Type? {
  199. for bundle in pluginBundles {
  200. if let name = bundle.object(forInfoDictionaryKey: LoopPluginBundleKey.supportIdentifier.rawValue) as? String,
  201. name == identifier
  202. {
  203. do {
  204. try bundle.loadAndReturnError()
  205. if let principalClass = bundle.principalClass as? NSObject.Type {
  206. if let plugin = principalClass.init() as? SupportUIPlugin {
  207. return type(of: plugin.support)
  208. } else {
  209. fatalError("PrincipalClass does not conform to SupportUIPlugin")
  210. }
  211. } else {
  212. fatalError("PrincipalClass not found")
  213. }
  214. } catch {
  215. debug(.deviceManager, "Error loading plugin: \(error)")
  216. }
  217. }
  218. }
  219. return nil
  220. }
  221. }
  222. extension Bundle {
  223. var isPumpManagerPlugin: Bool {
  224. object(forInfoDictionaryKey: LoopPluginBundleKey.pumpManagerIdentifier.rawValue) as? String != nil }
  225. var isCGMManagerPlugin: Bool {
  226. object(forInfoDictionaryKey: LoopPluginBundleKey.cgmManagerIdentifier.rawValue) as? String != nil }
  227. var isStatefulPlugin: Bool {
  228. object(forInfoDictionaryKey: LoopPluginBundleKey.statefulPluginIdentifier.rawValue) as? String != nil }
  229. var isServicePlugin: Bool { object(forInfoDictionaryKey: LoopPluginBundleKey.serviceIdentifier.rawValue) as? String != nil }
  230. var isOnboardingPlugin: Bool {
  231. object(forInfoDictionaryKey: LoopPluginBundleKey.onboardingIdentifier.rawValue) as? String != nil }
  232. var isSupportPlugin: Bool { object(forInfoDictionaryKey: LoopPluginBundleKey.supportIdentifier.rawValue) as? String != nil }
  233. var isLoopPlugin: Bool {
  234. isPumpManagerPlugin || isCGMManagerPlugin || isStatefulPlugin || isServicePlugin || isOnboardingPlugin || isSupportPlugin
  235. }
  236. var isLoopExtension: Bool { object(forInfoDictionaryKey: LoopPluginBundleKey.extensionIdentifier.rawValue) as? String != nil }
  237. var isSimulator: Bool { object(forInfoDictionaryKey: LoopPluginBundleKey.pluginIsSimulator.rawValue) as? Bool == true }
  238. }