expo-h3 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  apply plugin: 'com.android.library'
2
2
 
3
3
  group = 'expo.modules.h3'
4
- version = '0.1.0'
4
+ version = '0.1.2'
5
5
 
6
6
  def expoModulesCorePlugin = new File(project(":expo-modules-core").projectDir.absolutePath, "ExpoModulesCorePlugin.gradle")
7
7
  apply from: expoModulesCorePlugin
@@ -35,7 +35,7 @@ android {
35
35
  namespace "expo.modules.h3"
36
36
  defaultConfig {
37
37
  versionCode 1
38
- versionName "0.1.0"
38
+ versionName "0.1.2"
39
39
  }
40
40
  lintOptions {
41
41
  abortOnError false
@@ -1,3 +1,3 @@
1
- <manifest>
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
2
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3
3
  </manifest>
@@ -2,7 +2,6 @@ package expo.modules.h3
2
2
 
3
3
  import expo.modules.kotlin.modules.Module
4
4
  import expo.modules.kotlin.modules.ModuleDefinition
5
-
6
5
  import android.content.Intent
7
6
  import android.os.CancellationSignal
8
7
  import android.os.ParcelFileDescriptor
@@ -19,7 +18,9 @@ import java.io.File
19
18
  import java.io.IOException
20
19
 
21
20
  class ExpoH3Module : Module() {
21
+
22
22
  override fun definition() = ModuleDefinition {
23
+
23
24
  Name("ExpoH3")
24
25
 
25
26
  // Function for printing HTML as PDF
@@ -36,12 +37,17 @@ class ExpoH3Module : Module() {
36
37
  AsyncFunction("printUrlAsync") { url: String, promise: Promise ->
37
38
  sendPrintIntent(url, "text/plain", promise)
38
39
  }
40
+
39
41
  }
40
42
 
41
43
  private fun silentPrintHtml(html: String, promise: Promise) {
42
- val context = appContext.reactContext.applicationContext
43
- val handler = Handler(Looper.getMainLooper())
44
+ val context = appContext.reactContext?.applicationContext
45
+ if (context == null) {
46
+ promise.reject("CONTEXT_ERROR", "Application context is not available", null)
47
+ return
48
+ }
44
49
 
50
+ val handler = Handler(Looper.getMainLooper())
45
51
  handler.post {
46
52
  val webView = WebView(context)
47
53
  webView.webViewClient = object : WebViewClient() {
@@ -56,7 +62,12 @@ class ExpoH3Module : Module() {
56
62
  private fun createPdfFromWebView(webView: WebView, promise: Promise) {
57
63
  val pdfPath = "/storage/emulated/0/temp_print.pdf"
58
64
  val pdfFile = File(pdfPath)
59
- val context = appContext.reactContext.applicationContext
65
+ val context = appContext.reactContext?.applicationContext
66
+
67
+ if (context == null) {
68
+ promise.reject("CONTEXT_ERROR", "Application context is not available", null)
69
+ return
70
+ }
60
71
 
61
72
  try {
62
73
  // Set up print attributes (A4, high res, no margins)
@@ -68,37 +79,40 @@ class ExpoH3Module : Module() {
68
79
 
69
80
  val adapter = webView.createPrintDocumentAdapter("SilentPrintDoc")
70
81
 
71
- // Layout the document
82
+ // Layout the document - use anonymous object instead of constructor
72
83
  adapter.onLayout(null, attributes, null, object : PrintDocumentAdapter.LayoutResultCallback() {
73
84
  override fun onLayoutFinished(info: PrintDocumentInfo?, changed: Boolean) {
74
85
  // Write to file
75
- val fd = ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_WRITE)
76
- adapter.onWrite(arrayOf(PageRange.ALL_PAGES), fd, CancellationSignal(), object : PrintDocumentAdapter.WriteResultCallback() {
77
- override fun onWriteFinished(pages: Array<out PageRange>?) {
78
- try {
79
- fd.close()
80
- // Now send the print intent
81
- val intent = Intent()
82
- intent.action = Intent.ACTION_SEND
83
- intent.setPackage("comb.bld.settings.print")
84
- intent.putExtra(Intent.EXTRA_TEXT, pdfPath)
85
- intent.type = "application/pdf"
86
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
87
- context.startActivity(intent)
88
-
89
- // Delete the temp file (assume print service has it now; add delay if needed for testing)
90
- pdfFile.delete()
91
-
92
- promise.resolve("Printed successfully")
93
- } catch (e: Exception) {
94
- promise.reject("PRINT_ERROR", e.message, e)
86
+ try {
87
+ val fd = ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_WRITE or ParcelFileDescriptor.MODE_CREATE)
88
+
89
+ adapter.onWrite(arrayOf(PageRange.ALL_PAGES), fd, CancellationSignal(), object : PrintDocumentAdapter.WriteResultCallback() {
90
+ override fun onWriteFinished(pages: Array<out PageRange>?) {
91
+ try {
92
+ fd.close()
93
+ // Now send the print intent
94
+ val intent = Intent()
95
+ intent.action = Intent.ACTION_SEND
96
+ intent.setPackage("comb.bld.settings.print")
97
+ intent.putExtra(Intent.EXTRA_TEXT, pdfPath)
98
+ intent.type = "application/pdf"
99
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
100
+ context.startActivity(intent)
101
+ // Delete the temp file
102
+ pdfFile.delete()
103
+ promise.resolve("Printed successfully")
104
+ } catch (e: Exception) {
105
+ promise.reject("PRINT_ERROR", e.message, e)
106
+ }
95
107
  }
96
- }
97
108
 
98
- override fun onWriteFailed(error: CharSequence?) {
99
- promise.reject("PDF_WRITE_ERROR", error.toString(), null)
100
- }
101
- })
109
+ override fun onWriteFailed(error: CharSequence?) {
110
+ promise.reject("PDF_WRITE_ERROR", error.toString(), null)
111
+ }
112
+ })
113
+ } catch (e: IOException) {
114
+ promise.reject("FILE_ERROR", e.message, e)
115
+ }
102
116
  }
103
117
 
104
118
  override fun onLayoutFailed(error: CharSequence?) {
@@ -111,9 +125,13 @@ class ExpoH3Module : Module() {
111
125
  }
112
126
 
113
127
  private fun sendPrintIntent(content: String, mimeType: String, promise: Promise) {
114
- val context = appContext.reactContext.applicationContext
115
- val handler = Handler(Looper.getMainLooper())
128
+ val context = appContext.reactContext?.applicationContext
129
+ if (context == null) {
130
+ promise.reject("CONTEXT_ERROR", "Application context is not available", null)
131
+ return
132
+ }
116
133
 
134
+ val handler = Handler(Looper.getMainLooper())
117
135
  handler.post {
118
136
  try {
119
137
  val intent = Intent()
@@ -129,4 +147,5 @@ class ExpoH3Module : Module() {
129
147
  }
130
148
  }
131
149
  }
150
+
132
151
  }
@@ -1,5 +1,5 @@
1
1
  {
2
- "platforms": ["apple", "android", "web"],
2
+ "platforms": ["android"],
3
3
  "apple": {
4
4
  "modules": ["ExpoH3Module"]
5
5
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expo-h3",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "My new module",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",