Skip to content

Commit

Permalink
[expo-print][ios] Added constants, added prevention of starting multi…
Browse files Browse the repository at this point in the history
…ple print requests (expo#23128)

# Why

While migrating `expo-print` to the new Modules API I forgot to add
constants to native swift code of the module. This causes
`Print.Orientation.landscape/portrait` to return null. I also noticed
that it is possible to start multiple print requests, which launches
multiple UIPrintInteractionController print interfaces, which causes the
app to crash.

# How

Added the constants to iOS module, added a check on JS side that checks
if another print request is being processed (it's still possible to add
multiple documents to the iOS print queue, it's just not possible to
launch multiple iOS print bottom sheets at once). Ideally this would be
added on native side, but it would add a lot more complexity to the code
than a simple check on JS side.

# Test Plan

Tested on Android emulator and iOS simulator.
  • Loading branch information
behenate authored Jun 27, 2023
1 parent 6c2144d commit c87ab94
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 16 deletions.
8 changes: 4 additions & 4 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ PODS:
- ABI49_0_0ExpoModulesCore
- ABI49_0_0EXPermissions (14.2.0):
- ABI49_0_0ExpoModulesCore
- ABI49_0_0Expo (49.0.0-alpha.9):
- ABI49_0_0Expo (49.0.0-alpha.10):
- ABI49_0_0ExpoModulesCore
- ABI49_0_0ExpoAppleAuthentication (6.1.0):
- ABI49_0_0ExpoModulesCore
Expand Down Expand Up @@ -1611,7 +1611,7 @@ PODS:
- ABI49_0_0EXUpdates
- ABI49_0_0ExpoNetwork (5.4.0):
- ABI49_0_0ExpoModulesCore
- ABI49_0_0ExpoPrint (12.4.0):
- ABI49_0_0ExpoPrint (12.4.1):
- ABI49_0_0ExpoModulesCore
- ABI49_0_0ExpoRandom (13.2.0):
- ABI49_0_0ExpoModulesCore
Expand Down Expand Up @@ -4784,7 +4784,7 @@ SPEC CHECKSUMS:
ABI49_0_0EXMediaLibrary: 0e0e244712394523c90e1bda78127d8c76814434
ABI49_0_0EXNotifications: b8a6fdc20a5c93452177fb980d07d8c1ca73138c
ABI49_0_0EXPermissions: f43bc5235e58a3bc698d3fadd6aa1ecb27020b05
ABI49_0_0Expo: b71ccedd80113fe654059abb8a0cf99a2646c7a8
ABI49_0_0Expo: 110e062a5a397647d2f71eaa3e438d82ce636bec
ABI49_0_0ExpoAppleAuthentication: af85867e111a296a561ddf9d3f659728ee8675d0
ABI49_0_0ExpoBattery: 9417f9897691a8af49a93047d99b27c2c6c13528
ABI49_0_0ExpoBlur: 9293857092c742630ef6dadbbd6378c0965cb317
Expand All @@ -4810,7 +4810,7 @@ SPEC CHECKSUMS:
ABI49_0_0ExpoModulesCore: 4f3ca2d3439d9c6293a036f0f731d858a3c100bf
ABI49_0_0ExpoModulesProvider: 6d5f656e0e7f821ae6aae15fd02290c88c45feab
ABI49_0_0ExpoNetwork: 028f02f5d15379f89e529b37982559e445f86ab9
ABI49_0_0ExpoPrint: 33aeee5e8b7464c77657b2c59218c9ea9542554c
ABI49_0_0ExpoPrint: 8065c3f088efcad86befd6ead6c2bc8adb425f24
ABI49_0_0ExpoRandom: 83829ccecffd4afbdd4f711e053639e2109df0b6
ABI49_0_0ExpoScreenOrientation: 246531771c12e546098bfbac9fb44b0efc174830
ABI49_0_0ExpoSecureStore: 05b26374e173878efda9267822d64825c80f652e
Expand Down
2 changes: 1 addition & 1 deletion ios/versioned/sdk49/Expo/ABI49_0_0Expo.podspec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ABI49_0_0Expo",
"version": "49.0.0-alpha.9",
"version": "49.0.0-alpha.10",
"summary": "The Expo SDK",
"description": "The Expo SDK",
"license": "MIT",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ABI49_0_0ExpoPrint",
"version": "12.4.0",
"version": "12.4.1",
"summary": "Provides an API for iOS (AirPrint) and Android printing functionality.",
"description": "Provides an API for iOS (AirPrint) and Android printing functionality.",
"license": "MIT",
Expand Down
9 changes: 9 additions & 0 deletions ios/versioned/sdk49/ExpoPrint/ExpoPrintModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ public class ExpoPrintModule: Module {
public func definition() -> ModuleDefinition {
Name("ExpoPrint")

Constants {
[
"Orientation": [
"portrait": PrintOrientation.portrait.rawValue,
"landscape": PrintOrientation.landscape.rawValue
]
]
}

AsyncFunction("print") { (options: PrintOptions, promise: Promise) in
printWithPrinter.startPrint(options: options, promise: promise)
}
Expand Down
9 changes: 6 additions & 3 deletions ios/versioned/sdk49/ExpoPrint/PrintOptions.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import ABI49_0_0ExpoModulesCore

enum PrintOrientation: String {
case portrait
case landscape
}

internal struct PrintOptions: Record {
@Field
var html: String?
Expand Down Expand Up @@ -35,8 +40,6 @@ internal struct PrintOptions: Record {
var markupFormatterIOS: String?

let kLetterPaperSize = CGSize(width: 612, height: 792)
let kExpoPrintOrientationLandscape = "landscape"
let kExpoPrintOrientationPortrait = "portrait"

func toPageSize() -> CGSize {
// defaults to pixel size for A4 paper format with 72 PPI
Expand Down Expand Up @@ -81,6 +84,6 @@ internal struct PrintOptions: Record {
}

func toUIPrintInfoOrientation() -> UIPrintInfo.Orientation {
orientation == kExpoPrintOrientationLandscape ? UIPrintInfo.Orientation.landscape : UIPrintInfo.Orientation.portrait
orientation == PrintOrientation.landscape.rawValue ? UIPrintInfo.Orientation.landscape : UIPrintInfo.Orientation.portrait
}
}
2 changes: 2 additions & 0 deletions packages/expo-print/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### 🐛 Bug fixes

- Fixed missing constants on iOS, restricted possibility of starting multiple print jobs at once, which would lead to crashes. ([#23128](https://github.com/expo/expo/pull/23128) by [@behenate](https://github.com/behenate))

### 💡 Others

## 12.4.1 — 2023-06-27
Expand Down
2 changes: 1 addition & 1 deletion packages/expo-print/build/Print.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion packages/expo-print/build/Print.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-print/build/Print.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions packages/expo-print/ios/ExpoPrintModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ public class ExpoPrintModule: Module {
public func definition() -> ModuleDefinition {
Name("ExpoPrint")

Constants {
[
"Orientation": [
"portrait": PrintOrientation.portrait.rawValue,
"landscape": PrintOrientation.landscape.rawValue
]
]
}

AsyncFunction("print") { (options: PrintOptions, promise: Promise) in
printWithPrinter.startPrint(options: options, promise: promise)
}
Expand Down
9 changes: 6 additions & 3 deletions packages/expo-print/ios/PrintOptions.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import ExpoModulesCore

enum PrintOrientation: String {
case portrait
case landscape
}

internal struct PrintOptions: Record {
@Field
var html: String?
Expand Down Expand Up @@ -35,8 +40,6 @@ internal struct PrintOptions: Record {
var markupFormatterIOS: String?

let kLetterPaperSize = CGSize(width: 612, height: 792)
let kExpoPrintOrientationLandscape = "landscape"
let kExpoPrintOrientationPortrait = "portrait"

func toPageSize() -> CGSize {
// defaults to pixel size for A4 paper format with 72 PPI
Expand Down Expand Up @@ -81,6 +84,6 @@ internal struct PrintOptions: Record {
}

func toUIPrintInfoOrientation() -> UIPrintInfo.Orientation {
orientation == kExpoPrintOrientationLandscape ? UIPrintInfo.Orientation.landscape : UIPrintInfo.Orientation.portrait
orientation == PrintOrientation.landscape.rawValue ? UIPrintInfo.Orientation.landscape : UIPrintInfo.Orientation.portrait
}
}
12 changes: 11 additions & 1 deletion packages/expo-print/src/Print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {

export { FilePrintOptions, FilePrintResult, OrientationType, PrintOptions, Printer };

let isPrinting = false;
// @needsAudit @docsMissing
/**
* The orientation of the printed content.
Expand Down Expand Up @@ -45,7 +46,16 @@ export async function printAsync(options: PrintOptions): Promise<void> {
if (options.markupFormatterIOS !== undefined) {
console.warn('The markupFormatterIOS option is deprecated. Use useMarkupFormatter instead.');
}
return await ExponentPrint.print(options);
if (isPrinting) {
throw new Error('Another print request is already in progress');
}

isPrinting = true;
try {
return await ExponentPrint.print(options);
} finally {
isPrinting = false;
}
}

// @needsAudit
Expand Down

0 comments on commit c87ab94

Please sign in to comment.