-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathEnrichedTextImageSpan.kt
More file actions
79 lines (68 loc) · 2.3 KB
/
Copy pathEnrichedTextImageSpan.kt
File metadata and controls
79 lines (68 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.swmansion.enriched.text.spans
import android.graphics.drawable.Drawable
import android.os.Handler
import android.os.Looper
import android.view.View
import com.facebook.react.bridge.ReactContext
import com.facebook.react.uimanager.UIManagerHelper
import com.swmansion.enriched.R
import com.swmansion.enriched.common.AsyncDrawable
import com.swmansion.enriched.common.ResourceManager
import com.swmansion.enriched.common.spans.EnrichedImageSpan
import com.swmansion.enriched.text.EnrichedTextStyle
import com.swmansion.enriched.text.events.OnImagePressEvent
import com.swmansion.enriched.text.spans.interfaces.EnrichedTextClickableSpan
import com.swmansion.enriched.text.spans.interfaces.EnrichedTextSpan
class EnrichedTextImageSpan(
drawable: Drawable,
source: String,
width: Int,
height: Int,
) : EnrichedImageSpan(drawable, source, width, height),
EnrichedTextSpan,
EnrichedTextClickableSpan {
override val dependsOnHtmlStyle = false
override var isPressed = false
override fun rebuildWithStyle(style: EnrichedTextStyle) = this
override fun onClick(view: View) {
val context = view.context as? ReactContext ?: return
val surfaceId = UIManagerHelper.getSurfaceId(context)
val dispatcher = UIManagerHelper.getEventDispatcherForReactTag(context, view.id)
dispatcher?.dispatchEvent(
OnImagePressEvent(
surfaceId,
view.id,
source ?: "",
getWidth().toDouble(),
getHeight().toDouble(),
),
)
}
// We use a callback to trigger a layout rebuild because ForceRedrawSpan/SpanWatcher
// (used in EnrichedImageSpan) doesn’t work in EnrichedTextView with SpannedString.
fun observeAsyncDrawableLoaded(onLoaded: () -> Unit) {
val d = drawable
if (d !is AsyncDrawable) {
return
}
d.onLoaded = {
Handler(Looper.getMainLooper()).post { onLoaded() }
}
if (d.isLoaded) {
d.onLoaded?.invoke()
}
}
companion object {
fun createEnrichedImageSpan(
src: String,
width: Int,
height: Int,
): EnrichedImageSpan {
var imgDrawable = prepareDrawableForImage(src, width, height)
if (imgDrawable == null) {
imgDrawable = ResourceManager.getDrawableResource(R.drawable.broken_image)
}
return EnrichedTextImageSpan(imgDrawable, src, width, height)
}
}
}