1 import ABI49_0_0ExpoModulesCore
2 
3 public class ExpoPrintModule: Module {
4   var renderTasks: [ExpoWKPDFRenderer] = []
5   var delegate = ExpoPrintModuleDelegate()
6 
7   lazy var printerSelector = {
8     ExpoPrinterSelector(delegate: self.delegate, addPrinterToCache: { key, printer in
9       self.printWithPrinter.cachedPrinters[key] = printer
10     })
11   }()
12 
13   lazy var printToFile = {
14     ExpoPrintToFile(appContext: self.appContext)
15   }()
16 
17   lazy var printWithPrinter = {
18     ExpoPrintWithPrinter(delegate: self.delegate)
19   }()
20 
21   public func definition() -> ModuleDefinition {
22     Name("ExpoPrint")
23 
24     Constants {
25       [
26         "Orientation": [
27           "portrait": PrintOrientation.portrait.rawValue,
28           "landscape": PrintOrientation.landscape.rawValue
29         ]
30       ]
31     }
32 
33     AsyncFunction("print") { (options: PrintOptions, promise: Promise) in
34       printWithPrinter.startPrint(options: options, promise: promise)
35     }
36     .runOnQueue(.main)
37 
38     AsyncFunction("selectPrinter") { (promise: Promise) in
39       self.printerSelector.selectPrinter(promise: promise)
40     }
41     .runOnQueue(.main)
42 
43     AsyncFunction("printToFileAsync") { (options: PrintOptions, promise: Promise) in
44       printToFile.printToFile(options: options, promise: promise)
45     }
46     .runOnQueue(.main)
47   }
48 }
49 
50 // ExpoPrintModule cannot inherit from UIPrinterPickerControllerDelegate due to inheritance conflicts with Module. Create a helper class
51 class ExpoPrintModuleDelegate: NSObject, UIPrinterPickerControllerDelegate, UIPrintInteractionControllerDelegate {
52   private func getViewController() -> UIViewController? {
53     let provider = ModuleRegistryProvider()
54     let moduleRegistry = provider.moduleRegistry()
55     let utils = moduleRegistry.getModuleImplementingProtocol(ABI49_0_0EXUtilitiesInterface.self) as? ABI49_0_0EXUtilitiesInterface
56     return utils?.currentViewController()
57   }
58 
59   func printerPickerControllerParentViewController(_ printerPickerController: UIPrinterPickerController) -> UIViewController? {
60     return getViewController()
61   }
62 
63   // UIPrintInteractionControllerDelegate
64   func printInteractionControllerParentViewController(_ printInteractionController: UIPrintInteractionController) -> UIViewController? {
65     return getViewController()
66   }
67 }
68