@quran.ws/qvp-react-native 0.6.1 → 0.8.0
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.
- package/README.md +66 -6
- package/android/build.gradle +1 -0
- package/android/src/main/java/ws/quran/qvp/rn/Codes.kt +34 -0
- package/android/src/main/java/ws/quran/qvp/rn/Marshal.kt +128 -30
- package/android/src/main/java/ws/quran/qvp/rn/QvpModule.kt +149 -50
- package/android/src/main/java/ws/quran/qvp/rn/QvpPackage.kt +1 -1
- package/android/src/main/java/ws/quran/qvp/rn/QvpPageViewManager.kt +4 -6
- package/android/src/main/java/ws/quran/qvp/rn/QvpPassageViewManager.kt +39 -0
- package/android/src/main/java/ws/quran/qvp/rn/QvpRnBytes.kt +20 -0
- package/android/src/main/java/ws/quran/qvp/rn/QvpRnEvents.kt +28 -0
- package/android/src/main/java/ws/quran/qvp/rn/QvpRnPageView.kt +87 -41
- package/android/src/main/java/ws/quran/qvp/rn/QvpRnPassageView.kt +144 -0
- package/android/src/main/java/ws/quran/qvp/rn/QvpRnWords.kt +26 -0
- package/package.json +2 -1
- package/src/codes.ts +33 -0
- package/src/index.tsx +288 -48
|
@@ -3,27 +3,71 @@ package ws.quran.qvp.rn
|
|
|
3
3
|
import com.facebook.react.bridge.*
|
|
4
4
|
import com.facebook.react.module.annotations.ReactModule
|
|
5
5
|
import com.facebook.react.uimanager.UIManagerHelper
|
|
6
|
+
import java.util.concurrent.CountDownLatch
|
|
7
|
+
import java.util.concurrent.ExecutorService
|
|
8
|
+
import java.util.concurrent.Executors
|
|
9
|
+
import java.util.concurrent.RejectedExecutionException
|
|
6
10
|
import ws.quran.qvp.*
|
|
7
11
|
import ws.quran.qvp.Target
|
|
8
12
|
|
|
9
13
|
/**
|
|
10
14
|
* Promise-based queries on the page behind a `<QvpPageView />` (looked up by react tag) plus engine-wide
|
|
11
|
-
* helpers and the atlas.
|
|
12
|
-
*
|
|
15
|
+
* helpers and the atlas. Names follow docs/API.md.
|
|
16
|
+
*
|
|
17
|
+
* Two threads run the calls. The query thread, which this module owns, runs every call that reads
|
|
18
|
+
* only what a page fixed when it loaded: its words, ayahs, lines, text, search, metadata and crop
|
|
19
|
+
* box. It also runs the engine-wide helpers and owns the atlases. A long query on it does not hold
|
|
20
|
+
* up a frame. The view keeps the page open while a query reads it ([QvpRnPageView.read]). Every
|
|
21
|
+
* call that reads or changes the layout, the view, styles, colours, the selection, the mask, the
|
|
22
|
+
* reveal or the dress runs on the UI thread, where the view draws.
|
|
13
23
|
*/
|
|
14
24
|
@ReactModule(name = QvpModule.NAME)
|
|
15
25
|
class QvpModule(private val ctx: ReactApplicationContext) : ReactContextBaseJavaModule(ctx) {
|
|
16
26
|
override fun getName() = NAME
|
|
27
|
+
/** The query thread. The atlases below are touched only on it. */
|
|
28
|
+
private val queries: ExecutorService = Executors.newSingleThreadExecutor { r -> Thread(r, "qvp-rn-query").apply { isDaemon = true } }
|
|
17
29
|
private val atlases = HashMap<Int, QvpAtlas>()
|
|
18
30
|
private val atlasByUri = HashMap<String, Int>()
|
|
19
31
|
private var nextAtlas = 1
|
|
20
32
|
|
|
21
33
|
private fun view(tag: Int): QvpRnPageView? = QvpRegistry.get(tag) ?: runCatching { UIManagerHelper.getUIManagerForReactTag(ctx, tag)?.resolveView(tag) as? QvpRnPageView }.getOrNull()
|
|
22
34
|
// Every @ReactMethod must be `void` for the TurboModule interop layer: these helpers return Unit on purpose.
|
|
23
|
-
private fun
|
|
35
|
+
private fun settle(promise: Promise, block: () -> Any?) {
|
|
36
|
+
try { promise.resolve(Marshal.toJs(block())) } catch (e: Throwable) { promise.reject("qvp", e.message ?: e.toString(), e) }
|
|
37
|
+
}
|
|
38
|
+
private fun ui(promise: Promise, block: () -> Any?) { UiThreadUtil.runOnUiThread { settle(promise, block) } }
|
|
39
|
+
/** Run [task] on the query thread, or reject when the module is gone. */
|
|
40
|
+
private fun queue(promise: Promise, task: () -> Unit) {
|
|
41
|
+
try { queries.execute(task) } catch (e: RejectedExecutionException) { promise.reject("qvp", "QvpModule was invalidated", e) }
|
|
42
|
+
}
|
|
43
|
+
/** An engine-wide call, or an atlas call, on the query thread. */
|
|
44
|
+
private fun off(promise: Promise, block: () -> Any?) = queue(promise) { settle(promise, block) }
|
|
45
|
+
/**
|
|
46
|
+
* A read-only page query on the query thread, with the page held open until it returns. A view
|
|
47
|
+
* the registry does not hold (not attached yet, or detached) is found through the UIManager,
|
|
48
|
+
* which only the UI thread may call, so that query runs there as before.
|
|
49
|
+
*/
|
|
50
|
+
private fun readPage(tag: Int, promise: Promise, block: (QvpRnPageView, QvpPage) -> Any?) = queue(promise) {
|
|
51
|
+
val v = QvpRegistry.get(tag)
|
|
52
|
+
if (v == null) withPage(tag, promise, block)
|
|
53
|
+
else settle(promise) { v.read { p -> block(v, p ?: throw IllegalStateException("QvpPageView $tag has no page loaded")) } }
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* A page call on the UI thread that the queries queued before it see done, and that is done
|
|
57
|
+
* before the queries queued after it start: the query thread waits for it. It holds no page
|
|
58
|
+
* open while it waits, so the UI thread never waits for it in turn.
|
|
59
|
+
*/
|
|
60
|
+
private fun inOrder(tag: Int, promise: Promise, block: (QvpRnPageView, QvpPage) -> Any?) = queue(promise) {
|
|
61
|
+
val done = CountDownLatch(1)
|
|
24
62
|
UiThreadUtil.runOnUiThread {
|
|
25
|
-
try {
|
|
63
|
+
try {
|
|
64
|
+
settle(promise) {
|
|
65
|
+
val v = view(tag) ?: throw IllegalStateException("no QvpPageView with tag $tag")
|
|
66
|
+
block(v, v.page ?: throw IllegalStateException("QvpPageView $tag has no page loaded"))
|
|
67
|
+
}
|
|
68
|
+
} finally { done.countDown() }
|
|
26
69
|
}
|
|
70
|
+
try { done.await() } catch (_: InterruptedException) { Thread.currentThread().interrupt() }
|
|
27
71
|
}
|
|
28
72
|
private fun withPage(tag: Int, promise: Promise, block: (QvpRnPageView, QvpPage) -> Any?) {
|
|
29
73
|
ui(promise) {
|
|
@@ -32,60 +76,64 @@ class QvpModule(private val ctx: ReactApplicationContext) : ReactContextBaseJava
|
|
|
32
76
|
block(v, p)
|
|
33
77
|
}
|
|
34
78
|
}
|
|
35
|
-
private fun withAtlas(id: Int, promise: Promise, block: (QvpAtlas) -> Any?)
|
|
79
|
+
private fun withAtlas(id: Int, promise: Promise, block: (QvpAtlas) -> Any?) = off(promise) { block(atlases[id] ?: throw IllegalStateException("no atlas $id")) }
|
|
36
80
|
private fun opt(m: ReadableMap?): Map<String, Any?> = m?.toHashMap() ?: emptyMap()
|
|
37
|
-
|
|
38
|
-
|
|
81
|
+
/** A target argument as plain values. A query reads its arguments this way on the calling
|
|
82
|
+
* thread, before it is queued, since the bridge's argument objects belong to the call. */
|
|
83
|
+
private fun plain(d: Dynamic): Any? = when (d.type) {
|
|
84
|
+
ReadableType.String -> d.asString(); ReadableType.Number -> d.asDouble(); ReadableType.Array -> d.asArray()?.toArrayList(); ReadableType.Map -> d.asMap()?.toHashMap(); else -> null }
|
|
85
|
+
private fun tgt(d: Dynamic, p: QvpPage): Target = Marshal.target(plain(d), p)
|
|
39
86
|
private fun ints(a: ReadableArray?): IntArray = a?.toArrayList()?.mapNotNull { (it as? Number)?.toInt() }?.toIntArray() ?: IntArray(0)
|
|
40
87
|
|
|
41
88
|
// ── page facts ──
|
|
42
|
-
@ReactMethod fun info(tag: Int, promise: Promise) =
|
|
43
|
-
@ReactMethod fun words(tag: Int, promise: Promise) =
|
|
44
|
-
@ReactMethod fun word(tag: Int, index: Int, promise: Promise) =
|
|
45
|
-
@ReactMethod fun ayahs(tag: Int, promise: Promise) =
|
|
46
|
-
@ReactMethod fun lines(tag: Int, promise: Promise) =
|
|
47
|
-
@ReactMethod fun decorations(tag: Int, promise: Promise) =
|
|
48
|
-
@ReactMethod fun findWord(tag: Int, s: Int, a: Int, w: Int, promise: Promise) =
|
|
49
|
-
@ReactMethod fun targetWords(tag: Int, target: Dynamic, promise: Promise) =
|
|
50
|
-
@ReactMethod fun wordForm(tag: Int, index: Int, form: String?, promise: Promise) =
|
|
51
|
-
@ReactMethod fun hasForm(tag: Int, form: String?, promise: Promise) =
|
|
52
|
-
@ReactMethod fun attachWords(tag: Int, json: String, promise: Promise) =
|
|
89
|
+
@ReactMethod fun info(tag: Int, promise: Promise) = readPage(tag, promise) { v, p -> Marshal.pageInfo(p, v.words(p).forms) + mapOf("loadMs" to v.loadMs, "bytes" to v.pageBytes) }
|
|
90
|
+
@ReactMethod fun words(tag: Int, promise: Promise) = readPage(tag, promise) { v, p -> v.words(p).all() }
|
|
91
|
+
@ReactMethod fun word(tag: Int, index: Int, promise: Promise) = readPage(tag, promise) { v, p -> v.words(p).word(index) }
|
|
92
|
+
@ReactMethod fun ayahs(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.ayahs.map { Marshal.ayah(it) } }
|
|
93
|
+
@ReactMethod fun lines(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.lines.map { Marshal.line(it) } }
|
|
94
|
+
@ReactMethod fun decorations(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.decorations.map { Marshal.decoration(it) } }
|
|
95
|
+
@ReactMethod fun findWord(tag: Int, s: Int, a: Int, w: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.findWord(s, a, w) }
|
|
96
|
+
@ReactMethod fun targetWords(tag: Int, target: Dynamic, promise: Promise) { val t = plain(target); readPage(tag, promise) { _, p -> p.targetWords(Marshal.target(t, p)).toList() } }
|
|
97
|
+
@ReactMethod fun wordForm(tag: Int, index: Int, form: String?, promise: Promise) = readPage(tag, promise) { _, p -> p.wordForm(index, Marshal.form(form)) }
|
|
98
|
+
@ReactMethod fun hasForm(tag: Int, form: String?, promise: Promise) = readPage(tag, promise) { _, p -> p.hasForm(Marshal.form(form)) }
|
|
99
|
+
@ReactMethod fun attachWords(tag: Int, json: String, promise: Promise) = inOrder(tag, promise) { v, p -> v.attachWords(p, json) }
|
|
53
100
|
|
|
54
101
|
// ── metadata ──
|
|
55
|
-
@ReactMethod fun surahs(tag: Int, promise: Promise) =
|
|
56
|
-
@ReactMethod fun divisions(tag: Int, promise: Promise) =
|
|
102
|
+
@ReactMethod fun surahs(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.surahs().map { Marshal.surah(it) } }
|
|
103
|
+
@ReactMethod fun divisions(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.divisions().map { Marshal.division(it) } }
|
|
57
104
|
@ReactMethod fun surahHeaders(tag: Int, promise: Promise) = withPage(tag, promise) { _, p -> p.surahHeaders().map { Marshal.surahHeader(it) } }
|
|
58
105
|
@ReactMethod fun surahHeadersView(tag: Int, promise: Promise) = withPage(tag, promise) { _, p -> p.surahHeadersView().map { Marshal.surahHeader(it) } }
|
|
59
|
-
@ReactMethod fun ayahMarks(tag: Int, promise: Promise) =
|
|
60
|
-
@ReactMethod fun rosettes(tag: Int, promise: Promise) =
|
|
61
|
-
@ReactMethod fun sajdahs(tag: Int, promise: Promise) =
|
|
62
|
-
@ReactMethod fun ayahKeys(tag: Int, promise: Promise) =
|
|
63
|
-
@ReactMethod fun ayahWordCount(tag: Int, s: Int, a: Int, promise: Promise) =
|
|
64
|
-
@ReactMethod fun reciteMap(tag: Int, s: Int, a: Int, n: Int, promise: Promise) =
|
|
65
|
-
@ReactMethod fun wordLabel(tag: Int, i: Int, promise: Promise) =
|
|
66
|
-
@ReactMethod fun ayahLabel(tag: Int, i: Int, promise: Promise) =
|
|
106
|
+
@ReactMethod fun ayahMarks(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.ayahMarks().map { Marshal.ayahMark(it) } }
|
|
107
|
+
@ReactMethod fun rosettes(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.rosettes().map { Marshal.rosette(it) } }
|
|
108
|
+
@ReactMethod fun sajdahs(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.sajdahs().map { Marshal.sajdah(it) } }
|
|
109
|
+
@ReactMethod fun ayahKeys(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.ayahKeys().map { mapOf("surah" to it.first, "ayah" to it.second, "ayahKey" to "${it.first}:${it.second}") } }
|
|
110
|
+
@ReactMethod fun ayahWordCount(tag: Int, s: Int, a: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.ayahWordCount(s, a).let { mapOf("count" to it.first, "isComplete" to it.second) } }
|
|
111
|
+
@ReactMethod fun reciteMap(tag: Int, s: Int, a: Int, n: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.reciteMap(s, a, n)?.toList() }
|
|
112
|
+
@ReactMethod fun wordLabel(tag: Int, i: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.wordLabel(i) }
|
|
113
|
+
@ReactMethod fun ayahLabel(tag: Int, i: Int, promise: Promise) = readPage(tag, promise) { _, p -> p.ayahLabel(i) }
|
|
67
114
|
|
|
68
115
|
// ── text & search ──
|
|
69
|
-
@ReactMethod fun text(tag: Int, target: Dynamic, opts: ReadableMap?, promise: Promise) =
|
|
70
|
-
|
|
71
|
-
@ReactMethod fun search(tag: Int, query: String, opts: ReadableMap?, promise: Promise) =
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
@ReactMethod fun
|
|
75
|
-
@ReactMethod fun arabic(op: String, s: String, promise: Promise) = ui(promise) { when (op) { "strip" -> QvpEngine.strip(s); "fold" -> QvpEngine.fold(s); "normalize" -> QvpEngine.normalize(s); "loose", "looseKey" -> QvpEngine.looseKey(s); else -> s } }
|
|
116
|
+
@ReactMethod fun text(tag: Int, target: Dynamic, opts: ReadableMap?, promise: Promise) { val t = plain(target); val o = opt(opts)
|
|
117
|
+
readPage(tag, promise) { _, p -> p.text(Marshal.target(t, p), Marshal.form(o["form"]), (o["wordSep"] as? String) ?: " ", (o["lineSep"] as? String) ?: "\n") } }
|
|
118
|
+
@ReactMethod fun search(tag: Int, query: String, opts: ReadableMap?, promise: Promise) { val o = opt(opts)
|
|
119
|
+
readPage(tag, promise) { _, p -> p.search(query, Marshal.form(o["form"], Form.SEARCH), Marshal.searchMode(o["mode"]), o["normalize"] != false, o["looseMatch"] != false, (o["limit"] as? Number)?.toInt() ?: 0).map { Marshal.match(it) } } }
|
|
120
|
+
@ReactMethod fun citation(tag: Int, words: ReadableArray, promise: Promise) { val ws = ints(words); readPage(tag, promise) { _, p -> p.citation(ws) } }
|
|
121
|
+
@ReactMethod fun arabic(op: String, s: String, promise: Promise) = off(promise) { when (op) { "strip" -> QvpEngine.strip(s); "fold" -> QvpEngine.fold(s); "normalize" -> QvpEngine.normalize(s); "loose", "looseKey" -> QvpEngine.looseKey(s); else -> s } }
|
|
76
122
|
|
|
77
123
|
// ── hit testing / layout (the view already does gestures; these are for scroll-into-view etc.) ──
|
|
78
124
|
private fun hitOptions(o: Map<String, Any?>) = QvpHitOptions((o["maxDistance"] as? Number)?.toFloat() ?: QvpDefaults.TAP_DISTANCE, (o["gapBias"] as? Number)?.toFloat() ?: QvpDefaults.GAP_BIAS, o["preferExact"] != false)
|
|
79
|
-
/** Exact
|
|
125
|
+
/** Exact hit at a point in page units: the outline under the point, else the box of a word or a decoration. */
|
|
80
126
|
@ReactMethod fun hitTestExact(tag: Int, x: Double, y: Double, promise: Promise) = withPage(tag, promise) { _, p -> p.hitTestExact(x.toFloat(), y.toFloat())?.let { Marshal.hit(p, it) } }
|
|
81
127
|
/** Gap-aware hit at a point in page units. */
|
|
82
128
|
@ReactMethod fun hitTest(tag: Int, x: Double, y: Double, opts: ReadableMap?, promise: Promise) = withPage(tag, promise) { _, p -> p.hitTest(x.toFloat(), y.toFloat(), hitOptions(opt(opts)))?.let { Marshal.hit(p, it) } }
|
|
83
|
-
/** Exact
|
|
129
|
+
/** Exact hit at a point in view dp (density and pan/zoom removed here, the layout in the engine). */
|
|
84
130
|
@ReactMethod fun hitTestExactView(tag: Int, x: Double, y: Double, promise: Promise) = withPage(tag, promise) { v, p -> val d = v.resources.displayMetrics.density
|
|
85
|
-
p.
|
|
131
|
+
val (lx, ly) = p.viewToLayout(QvpView(v.inner.viewScale, v.inner.viewOx, v.inner.viewOy), (x * d).toFloat(), (y * d).toFloat())
|
|
132
|
+
p.hitTestExactView(lx, ly)?.let { Marshal.hit(p, it) } }
|
|
86
133
|
@ReactMethod fun hitTestView(tag: Int, x: Double, y: Double, opts: ReadableMap?, promise: Promise) = withPage(tag, promise) { v, p ->
|
|
87
134
|
val o = opt(opts); val d = v.resources.displayMetrics.density
|
|
88
|
-
p.
|
|
135
|
+
val (lx, ly) = p.viewToLayout(QvpView(v.inner.viewScale, v.inner.viewOx, v.inner.viewOy), (x * d).toFloat(), (y * d).toFloat())
|
|
136
|
+
p.hitTestView(lx, ly, hitOptions(o))?.let { Marshal.hit(p, it) } }
|
|
89
137
|
@ReactMethod fun wordBoundsView(tag: Int, i: Int, promise: Promise) = withPage(tag, promise) { v, p -> val d = v.resources.displayMetrics.density
|
|
90
138
|
p.wordBoundsView(i)?.let { b -> mapOf("x0" to (v.inner.viewOx + b[0] * v.inner.viewScale) / d, "y0" to (v.inner.viewOy + b[1] * v.inner.viewScale) / d, "x1" to (v.inner.viewOx + b[2] * v.inner.viewScale) / d, "y1" to (v.inner.viewOy + b[3] * v.inner.viewScale) / d) } }
|
|
91
139
|
@ReactMethod fun currentLayout(tag: Int, promise: Promise) = withPage(tag, promise) { _, p -> p.currentLayout?.let { Marshal.layout(it) } }
|
|
@@ -96,7 +144,7 @@ class QvpModule(private val ctx: ReactApplicationContext) : ReactContextBaseJava
|
|
|
96
144
|
@ReactMethod fun layoutWastedFraction(tag: Int, promise: Promise) = withPage(tag, promise) { v, p -> p.layoutWastedFraction(v.inner.layoutSpec()) }
|
|
97
145
|
/** The page's height at the printed pitch for the spec this view lays out with. */
|
|
98
146
|
@ReactMethod fun layoutPrintedHeight(tag: Int, promise: Promise) = withPage(tag, promise) { v, p -> p.layoutPrintedHeight(v.inner.layoutSpec()) }
|
|
99
|
-
@ReactMethod fun grid(tag: Int, promise: Promise) =
|
|
147
|
+
@ReactMethod fun grid(tag: Int, promise: Promise) = readPage(tag, promise) { _, p -> mapOf("lines" to p.grid.lines, "lineSpacing" to p.grid.lineSpacing) }
|
|
100
148
|
@ReactMethod fun stats(tag: Int, promise: Promise) = ui(promise) { view(tag)?.stats() }
|
|
101
149
|
@ReactMethod fun invalidate(tag: Int, promise: Promise) = ui(promise) { view(tag)?.inner?.invalidate(); null }
|
|
102
150
|
|
|
@@ -120,13 +168,13 @@ class QvpModule(private val ctx: ReactApplicationContext) : ReactContextBaseJava
|
|
|
120
168
|
@ReactMethod fun revealStepOf(tag: Int, i: Int, promise: Promise) = withPage(tag, promise) { _, p -> p.revealStepOf(i).toDouble() }
|
|
121
169
|
|
|
122
170
|
// ── crop ──
|
|
123
|
-
@ReactMethod fun cropBounds(tag: Int, target: Dynamic, opts: ReadableMap?, promise: Promise) =
|
|
124
|
-
|
|
171
|
+
@ReactMethod fun cropBounds(tag: Int, target: Dynamic, opts: ReadableMap?, promise: Promise) { val t = plain(target); val o = opt(opts)
|
|
172
|
+
readPage(tag, promise) { _, p -> p.cropBounds(Marshal.target(t, p), (o["pad"] as? Number)?.toFloat() ?: QvpDefaults.CROP_PAD, o["keepAyahMarks"] != false)?.let { Marshal.cropBounds(it) } } }
|
|
125
173
|
@ReactMethod fun cropSvg(tag: Int, target: Dynamic, opts: ReadableMap?, promise: Promise) = withPage(tag, promise) { _, p ->
|
|
126
174
|
val o = opt(opts); p.cropSvg(tgt(target, p), (o["pad"] as? Number)?.toFloat() ?: QvpDefaults.CROP_PAD, o["keepAyahMarks"] != false, Marshal.color(o["background"], 0)) }
|
|
127
175
|
|
|
128
176
|
// ── atlas ──
|
|
129
|
-
@ReactMethod fun loadAtlas(uri: String, promise: Promise) =
|
|
177
|
+
@ReactMethod fun loadAtlas(uri: String, promise: Promise) = off(promise) {
|
|
130
178
|
atlasByUri[uri] ?: run {
|
|
131
179
|
val bytes = when {
|
|
132
180
|
uri.startsWith("asset://") -> ctx.assets.open(uri.removePrefix("asset://")).use { it.readBytes() }
|
|
@@ -139,7 +187,7 @@ class QvpModule(private val ctx: ReactApplicationContext) : ReactContextBaseJava
|
|
|
139
187
|
val id = nextAtlas++; atlases[id] = QvpAtlas(bytes); atlasByUri[uri] = id; id
|
|
140
188
|
}
|
|
141
189
|
}
|
|
142
|
-
@ReactMethod fun freeAtlas(id: Int, promise: Promise) =
|
|
190
|
+
@ReactMethod fun freeAtlas(id: Int, promise: Promise) = off(promise) { atlases.remove(id)?.close(); atlasByUri.entries.removeAll { it.value == id }; null }
|
|
143
191
|
@ReactMethod fun atlasPageOf(id: Int, s: Int, a: Int, promise: Promise) = withAtlas(id, promise) { it.pageOf(s, a) }
|
|
144
192
|
@ReactMethod fun atlasPageRange(id: Int, page: Int, promise: Promise) = withAtlas(id, promise) { it.pageRange(page)?.let { r -> mapOf("first" to mapOf("surah" to r.first.first, "ayah" to r.first.second), "last" to mapOf("surah" to r.second.first, "ayah" to r.second.second)) } }
|
|
145
193
|
@ReactMethod fun atlasPageCount(id: Int, promise: Promise) = withAtlas(id, promise) { it.pageCount() }
|
|
@@ -152,18 +200,69 @@ class QvpModule(private val ctx: ReactApplicationContext) : ReactContextBaseJava
|
|
|
152
200
|
@ReactMethod fun atlasPagesOfJuz(id: Int, n: Int, promise: Promise) = withAtlas(id, promise) { it.pagesOfJuz(n)?.let { r -> listOf(r.first, r.second) } }
|
|
153
201
|
@ReactMethod fun atlasSearchSurahs(id: Int, text: String, promise: Promise) = withAtlas(id, promise) { it.searchSurahs(text).map { s -> Marshal.atlasSurah(s) } }
|
|
154
202
|
|
|
203
|
+
// ── dress ──
|
|
204
|
+
// An ornament set is held here by id, like an atlas. The C calls behind each method:
|
|
205
|
+
// qvp_ornaments_load, qvp_ornaments_free, qvp_ornament_count, qvp_ornament_info and
|
|
206
|
+
// qvp_ornament_part (one ornament and its parts), qvp_ornament_find, then on the page
|
|
207
|
+
// qvp_dress, qvp_undress and qvp_dress_info. qvp_dress_geometry is the view's: it paints the layer.
|
|
208
|
+
private fun withOrnaments(id: Int, promise: Promise, block: (QvpOrnaments) -> Any?) { ui(promise) { block(QvpOrnamentSets.get(id) ?: throw IllegalStateException("no ornament set $id")) } }
|
|
209
|
+
@ReactMethod fun loadOrnaments(uri: String, promise: Promise) = ui(promise) { QvpOrnamentSets.add(QvpOrnaments(QvpRnBytes.read(ctx, uri))) }
|
|
210
|
+
@ReactMethod fun freeOrnaments(id: Int, promise: Promise) = ui(promise) { QvpOrnamentSets.free(id); null }
|
|
211
|
+
@ReactMethod fun ornamentCount(id: Int, promise: Promise) = withOrnaments(id, promise) { it.count }
|
|
212
|
+
@ReactMethod fun ornamentInfo(id: Int, index: Int, promise: Promise) = withOrnaments(id, promise) { it.list.getOrNull(index)?.let { o -> Marshal.ornament(o) } }
|
|
213
|
+
@ReactMethod fun ornamentPart(id: Int, index: Int, part: Int, promise: Promise) = withOrnaments(id, promise) { it.list.getOrNull(index)?.parts?.getOrNull(part)?.let { q -> Marshal.ornamentPart(q) } }
|
|
214
|
+
@ReactMethod fun ornamentList(id: Int, promise: Promise) = withOrnaments(id, promise) { it.list.map { o -> Marshal.ornament(o) } }
|
|
215
|
+
@ReactMethod fun ornamentFind(id: Int, kind: String, style: String, promise: Promise) = withOrnaments(id, promise) { set ->
|
|
216
|
+
Marshal.ornamentKind(kind)?.let { k -> set.find(k, style)?.index } ?: -1 }
|
|
217
|
+
/** Dress the page behind a view. The `dress` prop does the same declaratively; this call lasts until the next page. */
|
|
218
|
+
@ReactMethod fun dress(tag: Int, id: Int, spec: ReadableMap?, promise: Promise) = withPage(tag, promise) { v, p ->
|
|
219
|
+
val set = QvpOrnamentSets.get(id) ?: throw IllegalStateException("no ornament set $id")
|
|
220
|
+
v.dressNow(p, set, Marshal.dressSpec(opt(spec)))?.let { Marshal.dressInfo(it) } }
|
|
221
|
+
@ReactMethod fun undress(tag: Int, promise: Promise) = withPage(tag, promise) { v, p -> v.undressNow(p); null }
|
|
222
|
+
/** The engine's clamp of a view over the content plus the dress overflow. Offsets and viewport are in dp, like the other view methods. */
|
|
223
|
+
@ReactMethod fun dressClamp(tag: Int, scale: Double, offsetX: Double, offsetY: Double, viewportW: Double, viewportH: Double, promise: Promise) = withPage(tag, promise) { v, p ->
|
|
224
|
+
val d = v.resources.displayMetrics.density
|
|
225
|
+
val c = p.dressClamp(QvpView(scale.toFloat(), (offsetX * d).toFloat(), (offsetY * d).toFloat()), (viewportW * d).toFloat(), (viewportH * d).toFloat())
|
|
226
|
+
mapOf("scale" to c.scale, "offsetX" to c.offsetX / d, "offsetY" to c.offsetY / d) }
|
|
227
|
+
/** The engine's view and dressed box size for a host that scrolls the page. The viewport, offsets and size are in dp. */
|
|
228
|
+
@ReactMethod fun dressView(tag: Int, scale: Double, viewportW: Double, promise: Promise) = withPage(tag, promise) { v, p ->
|
|
229
|
+
val d = v.resources.displayMetrics.density
|
|
230
|
+
val r = p.dressView(scale.toFloat(), (viewportW * d).toFloat())
|
|
231
|
+
mapOf("view" to mapOf("scale" to r.view.scale, "offsetX" to r.view.offsetX / d, "offsetY" to r.view.offsetY / d),
|
|
232
|
+
"width" to r.width / d, "height" to r.height / d) }
|
|
233
|
+
@ReactMethod fun dressInfo(tag: Int, promise: Promise) = withPage(tag, promise) { _, p -> p.dressInfo()?.let { Marshal.dressInfo(it) } }
|
|
234
|
+
@ReactMethod fun dressRevision(tag: Int, promise: Promise) = withPage(tag, promise) { _, p -> p.dressRevision() }
|
|
235
|
+
|
|
155
236
|
// ── names ──
|
|
156
|
-
@ReactMethod fun markName(m: Int, promise: Promise) =
|
|
157
|
-
@ReactMethod fun markFromName(s: String, promise: Promise) =
|
|
158
|
-
@ReactMethod fun markCategory(m: Int, promise: Promise) =
|
|
159
|
-
@ReactMethod fun engineName(promise: Promise) =
|
|
237
|
+
@ReactMethod fun markName(m: Int, promise: Promise) = off(promise) { QvpEngine.markName(m) }
|
|
238
|
+
@ReactMethod fun markFromName(s: String, promise: Promise) = off(promise) { QvpEngine.markFromName(s) }
|
|
239
|
+
@ReactMethod fun markCategory(m: Int, promise: Promise) = off(promise) { QvpEngine.markCategory(m) }
|
|
240
|
+
@ReactMethod fun engineName(promise: Promise) = off(promise) { QvpEngine.engineName() }
|
|
160
241
|
/** Every name table, read from the engine; the JavaScript side resolves names with these. */
|
|
161
242
|
override fun getConstants(): Map<String, Any> = mapOf("version" to QvpEngine.version(), "formatVersion" to QvpEngine.formatVersion(),
|
|
162
243
|
"marks" to QvpEngine.names(QvpEngine.Names.MARK), "kinds" to QvpEngine.names(QvpEngine.Names.KIND),
|
|
163
244
|
"families" to QvpEngine.names(QvpEngine.Names.FAMILY), "categories" to QvpEngine.names(QvpEngine.Names.CATEGORY),
|
|
164
245
|
"decorations" to QvpEngine.names(QvpEngine.Names.DECORATION), "divisions" to QvpEngine.names(QvpEngine.Names.DIVISION),
|
|
165
|
-
"places" to QvpEngine.names(QvpEngine.Names.PLACE)
|
|
246
|
+
"places" to QvpEngine.names(QvpEngine.Names.PLACE),
|
|
247
|
+
// the whole defaults table by camelCase name, as the decimals the header states
|
|
248
|
+
"defaults" to QvpDefaults.decimal)
|
|
166
249
|
|
|
167
|
-
override fun invalidate() {
|
|
250
|
+
override fun invalidate() {
|
|
251
|
+
// The atlases belong to the query thread: free them there, after the queries already queued.
|
|
252
|
+
runCatching { queries.execute { atlases.values.forEach { it.close() }; atlases.clear(); atlasByUri.clear() } }
|
|
253
|
+
queries.shutdown()
|
|
254
|
+
QvpOrnamentSets.clear(); super.invalidate()
|
|
255
|
+
}
|
|
168
256
|
companion object { const val NAME = "QvpModule" }
|
|
169
257
|
}
|
|
258
|
+
|
|
259
|
+
/** Ornament sets by id, shared by the module and the views: the `dress` prop names its set by this id.
|
|
260
|
+
* A dressed page keeps what it needs, so freeing a set never breaks a page that wears it. */
|
|
261
|
+
object QvpOrnamentSets {
|
|
262
|
+
private val sets = HashMap<Int, QvpOrnaments>()
|
|
263
|
+
private var next = 1
|
|
264
|
+
fun add(set: QvpOrnaments): Int { val id = next++; sets[id] = set; return id }
|
|
265
|
+
fun get(id: Int?): QvpOrnaments? = id?.let { sets[it] }
|
|
266
|
+
fun free(id: Int) { sets.remove(id)?.close() }
|
|
267
|
+
fun clear() { sets.values.forEach { it.close() }; sets.clear() }
|
|
268
|
+
}
|
|
@@ -7,5 +7,5 @@ import com.facebook.react.uimanager.ViewManager
|
|
|
7
7
|
|
|
8
8
|
class QvpPackage : ReactPackage {
|
|
9
9
|
override fun createNativeModules(ctx: ReactApplicationContext): List<NativeModule> = listOf(QvpModule(ctx))
|
|
10
|
-
override fun createViewManagers(ctx: ReactApplicationContext): List<ViewManager<*, *>> = listOf(QvpPageViewManager())
|
|
10
|
+
override fun createViewManagers(ctx: ReactApplicationContext): List<ViewManager<*, *>> = listOf(QvpPageViewManager(), QvpPassageViewManager())
|
|
11
11
|
}
|
|
@@ -15,11 +15,7 @@ class QvpPageViewManager : SimpleViewManager<QvpRnPageView>() {
|
|
|
15
15
|
override fun onDropViewInstance(view: QvpRnPageView) { view.destroy(); super.onDropViewInstance(view) }
|
|
16
16
|
override fun onAfterUpdateTransaction(view: QvpRnPageView) { super.onAfterUpdateTransaction(view); view.commit() }
|
|
17
17
|
|
|
18
|
-
override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any>
|
|
19
|
-
val m = HashMap<String, Any>()
|
|
20
|
-
for (e in EVENTS) m[e] = mapOf("registrationName" to e)
|
|
21
|
-
return m
|
|
22
|
-
}
|
|
18
|
+
override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any> = QvpRnEvents.constants(EVENTS)
|
|
23
19
|
|
|
24
20
|
@ReactProp(name = "pageUri") fun setPageUri(v: QvpRnPageView, s: String?) { v.pageUri = s }
|
|
25
21
|
@ReactProp(name = "pageBase64") fun setPageBase64(v: QvpRnPageView, s: String?) { v.pageBase64 = s }
|
|
@@ -28,6 +24,7 @@ class QvpPageViewManager : SimpleViewManager<QvpRnPageView>() {
|
|
|
28
24
|
@ReactProp(name = "padBottom", defaultFloat = 0f) fun setPadBottom(v: QvpRnPageView, f: Float) = v.setPadBottomDp(f)
|
|
29
25
|
@ReactProp(name = "padSide", defaultFloat = 0f) fun setPadSide(v: QvpRnPageView, f: Float) = v.setPadSideDp(f)
|
|
30
26
|
@ReactProp(name = "lineSpacing", defaultFloat = 1f) fun setLineSpacing(v: QvpRnPageView, f: Float) = v.setLineSpacingProp(f)
|
|
27
|
+
@ReactProp(name = "reflowBreaks") fun setReflowBreaks(v: QvpRnPageView, s: String?) = v.setReflowBreaksProp(s)
|
|
31
28
|
@ReactProp(name = "fillHeight", defaultBoolean = false) fun setFillHeight(v: QvpRnPageView, b: Boolean) = v.setFillHeightProp(b)
|
|
32
29
|
@ReactProp(name = "surahFrames", defaultBoolean = true) fun setSurahFrames(v: QvpRnPageView, b: Boolean) = v.setSurahFramesProp(b)
|
|
33
30
|
@ReactProp(name = "paperColor") fun setPaperColor(v: QvpRnPageView, s: String?) { v.paperColor = if (s.isNullOrBlank()) 0 else QvpColor.argb(QvpColor.parse(s)); v.invalidate() }
|
|
@@ -43,9 +40,10 @@ class QvpPageViewManager : SimpleViewManager<QvpRnPageView>() {
|
|
|
43
40
|
@ReactProp(name = "highlights") fun setHighlights(v: QvpRnPageView, a: ReadableArray?) { v.highlightsProp = Marshal.plain(a) }
|
|
44
41
|
@ReactProp(name = "mask") fun setMask(v: QvpRnPageView, m: ReadableMap?) { v.maskProp = Marshal.plain(m) }
|
|
45
42
|
@ReactProp(name = "reveal") fun setReveal(v: QvpRnPageView, m: ReadableMap?) { v.revealProp = Marshal.plain(m) }
|
|
43
|
+
@ReactProp(name = "dress") fun setDress(v: QvpRnPageView, m: ReadableMap?) { v.dressProp = Marshal.plain(m) }
|
|
46
44
|
|
|
47
45
|
companion object {
|
|
48
46
|
const val NAME = "QvpPageView"
|
|
49
|
-
val EVENTS = listOf("onWordTap", "onDecorationTap", "onEmptyTap", "onSelectionChanged", "onPageLoad", "onRevealChanged", "onSwipe", "onZoomChanged", "onError")
|
|
47
|
+
val EVENTS = listOf("onWordTap", "onDecorationTap", "onEmptyTap", "onSelectionChanged", "onPageLoad", "onRevealChanged", "onSwipe", "onZoomChanged", "onDressChanged", "onError")
|
|
50
48
|
}
|
|
51
49
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
package ws.quran.qvp.rn
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReadableArray
|
|
4
|
+
import com.facebook.react.uimanager.SimpleViewManager
|
|
5
|
+
import com.facebook.react.uimanager.ThemedReactContext
|
|
6
|
+
import com.facebook.react.uimanager.annotations.ReactProp
|
|
7
|
+
import ws.quran.qvp.QvpColor
|
|
8
|
+
|
|
9
|
+
/** `<QvpPassageView />`: props are marshalled onto [QvpRnPassageView]; [onAfterUpdateTransaction] commits them in one pass. */
|
|
10
|
+
class QvpPassageViewManager : SimpleViewManager<QvpRnPassageView>() {
|
|
11
|
+
override fun getName() = NAME
|
|
12
|
+
override fun createViewInstance(c: ThemedReactContext) = QvpRnPassageView(c)
|
|
13
|
+
override fun onDropViewInstance(view: QvpRnPassageView) { view.destroy(); super.onDropViewInstance(view) }
|
|
14
|
+
override fun onAfterUpdateTransaction(view: QvpRnPassageView) { super.onAfterUpdateTransaction(view); view.commit() }
|
|
15
|
+
|
|
16
|
+
override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any> = QvpRnEvents.constants(EVENTS)
|
|
17
|
+
|
|
18
|
+
@ReactProp(name = "pageUris") fun setPageUris(v: QvpRnPassageView, a: ReadableArray?) { v.pageUris = Marshal.plain(a)?.map { it.toString() } ?: emptyList() }
|
|
19
|
+
@ReactProp(name = "surah", defaultInt = 0) fun setSurah(v: QvpRnPassageView, i: Int) { v.surahProp = i }
|
|
20
|
+
@ReactProp(name = "from", defaultInt = 0) fun setFrom(v: QvpRnPassageView, i: Int) { v.fromProp = i }
|
|
21
|
+
@ReactProp(name = "to", defaultInt = 0) fun setTo(v: QvpRnPassageView, i: Int) { v.toProp = i }
|
|
22
|
+
@ReactProp(name = "basmalah", defaultBoolean = false) fun setBasmalah(v: QvpRnPassageView, b: Boolean) { v.basmalahProp = b }
|
|
23
|
+
@ReactProp(name = "scale", defaultFloat = 1f) fun setScale(v: QvpRnPassageView, f: Float) { v.scaleProp = f }
|
|
24
|
+
@ReactProp(name = "lineHeight", defaultFloat = 0f) fun setLineHeight(v: QvpRnPassageView, f: Float) { v.lineHeightProp = f }
|
|
25
|
+
@ReactProp(name = "lineSpacing", defaultFloat = 1f) fun setLineSpacing(v: QvpRnPassageView, f: Float) { v.lineSpacingProp = f }
|
|
26
|
+
@ReactProp(name = "padding", defaultFloat = 2f) fun setPadding(v: QvpRnPassageView, f: Float) { v.paddingProp = f }
|
|
27
|
+
@ReactProp(name = "align") fun setAlign(v: QvpRnPassageView, s: String?) { v.alignProp = s }
|
|
28
|
+
@ReactProp(name = "maxRows", defaultInt = 0) fun setMaxRows(v: QvpRnPassageView, i: Int) { v.maxRowsProp = i }
|
|
29
|
+
@ReactProp(name = "keepAyahMark", defaultBoolean = false) fun setKeepAyahMark(v: QvpRnPassageView, b: Boolean) { v.keepAyahMarkProp = b }
|
|
30
|
+
@ReactProp(name = "ellipsisWidth", defaultFloat = 0f) fun setEllipsisWidth(v: QvpRnPassageView, f: Float) { v.ellipsisWidthProp = f }
|
|
31
|
+
@ReactProp(name = "maxStretch", defaultFloat = 0f) fun setMaxStretch(v: QvpRnPassageView, f: Float) { v.maxStretchProp = f }
|
|
32
|
+
@ReactProp(name = "ink") fun setInk(v: QvpRnPassageView, s: String?) { v.inkProp = s }
|
|
33
|
+
@ReactProp(name = "paperColor") fun setPaperColor(v: QvpRnPassageView, s: String?) { v.paperColor = Marshal.color(s, 0).let { if (it == 0) 0 else QvpColor.argb(it) } }
|
|
34
|
+
|
|
35
|
+
companion object {
|
|
36
|
+
const val NAME = "QvpPassageView"
|
|
37
|
+
val EVENTS = listOf("onPassageLayout", "onError")
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
package ws.quran.qvp.rn
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.util.Base64
|
|
5
|
+
import java.io.File
|
|
6
|
+
|
|
7
|
+
/** The bytes a prop-given URI names: an asset, a file, an absolute path or inline base64. */
|
|
8
|
+
object QvpRnBytes {
|
|
9
|
+
fun read(ctx: Context, uri: String): ByteArray {
|
|
10
|
+
val u = uri.trim()
|
|
11
|
+
return when {
|
|
12
|
+
u.startsWith("asset://") -> ctx.assets.open(u.removePrefix("asset://")).use { it.readBytes() }
|
|
13
|
+
u.startsWith("asset:") -> ctx.assets.open(u.removePrefix("asset:")).use { it.readBytes() }
|
|
14
|
+
u.startsWith("file://") -> File(u.removePrefix("file://")).readBytes()
|
|
15
|
+
u.startsWith("/") -> File(u).readBytes()
|
|
16
|
+
u.startsWith("base64:") -> Base64.decode(u.removePrefix("base64:"), Base64.DEFAULT)
|
|
17
|
+
else -> ctx.assets.open(u).use { it.readBytes() }
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
package ws.quran.qvp.rn
|
|
2
|
+
|
|
3
|
+
import android.view.View
|
|
4
|
+
import com.facebook.react.bridge.WritableMap
|
|
5
|
+
import com.facebook.react.uimanager.ThemedReactContext
|
|
6
|
+
import com.facebook.react.uimanager.UIManagerHelper
|
|
7
|
+
import com.facebook.react.uimanager.events.Event
|
|
8
|
+
|
|
9
|
+
/** The event plumbing both native views share: a named event with a map payload, sent to the view's tag. */
|
|
10
|
+
object QvpRnEvents {
|
|
11
|
+
private class QvpEvent(surfaceId: Int, viewTag: Int, private val name: String, private val payload: WritableMap) : Event<QvpEvent>(surfaceId, viewTag) {
|
|
12
|
+
override fun getEventName() = name
|
|
13
|
+
override fun getEventData(): WritableMap = payload
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
fun emit(ctx: ThemedReactContext, view: View, name: String, payload: Map<String, Any?>) {
|
|
17
|
+
if (view.id <= 0) return
|
|
18
|
+
val dispatcher = UIManagerHelper.getEventDispatcherForReactTag(ctx, view.id) ?: return
|
|
19
|
+
dispatcher.dispatchEvent(QvpEvent(UIManagerHelper.getSurfaceId(view), view.id, name, Marshal.toMap(payload)))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The direct event map a manager exports: each name registers under itself. */
|
|
23
|
+
fun constants(names: List<String>): MutableMap<String, Any> {
|
|
24
|
+
val m = HashMap<String, Any>()
|
|
25
|
+
for (e in names) m[e] = mapOf("registrationName" to e)
|
|
26
|
+
return m
|
|
27
|
+
}
|
|
28
|
+
}
|