@onekeyfe/react-native-native-list 6.7.0-alpha.466 → 6.8.0-10
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 +312 -379
- package/android/src/main/java/com/onekey/nativelist/NativeListActivityDetailsView.kt +15 -6
- package/android/src/main/java/com/onekey/nativelist/NativeListMediaPlaceholderView.kt +244 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListMediaPreviewSlot.kt +66 -31
- package/android/src/main/java/com/onekey/nativelist/NativeListMediaTileRenderer.kt +32 -11
- package/android/src/main/java/com/onekey/nativelist/NativeListModels.kt +60 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListResolvedText.kt +5 -1
- package/android/src/main/java/com/onekey/nativelist/NativeListTextView.kt +75 -0
- package/android/src/main/java/com/onekey/nativelist/NativeListView.kt +25 -19
- package/android/src/test/java/com/margelo/nitro/nativelist/NativeListDescriptionTouchPolicyTest.kt +153 -0
- package/docs/ROW_TEMPLATES.md +11 -4
- package/docs/SPEC.md +296 -20
- package/docs/STYLE_SPEC.md +12 -0
- package/ios/NativeListActivityDetailsView.swift +13 -5
- package/ios/NativeListMediaPlaceholderView.swift +189 -0
- package/ios/NativeListMediaPreviewSlot.swift +122 -61
- package/ios/NativeListMediaTileRenderer.swift +13 -13
- package/ios/NativeListResolvedText.swift +15 -4
- package/ios/NativeListTextLabel.swift +106 -0
- package/ios/RNCNativeListView.swift +129 -32
- package/ios/tests/NativeListActivityPresentationTests.swift +270 -0
- package/ios/tests/NativeListLifecycleTests.swift +369 -40
- package/ios/tests/NativeListMediaVisibilityTests.swift +41 -7
- package/lib/module/NativeList.js +17 -4
- package/lib/module/NativeList.web.js +3 -1
- package/lib/module/validation.js +53 -4
- package/lib/module/web/NativeListGeneratedIcons.js +1 -0
- package/lib/module/web/NativeListWebEngine.js +13 -11
- package/lib/module/web/templates/ActivityRowRenderer.js +29 -23
- package/lib/module/web/templates/DataRowRenderer.js +54 -1
- package/lib/module/web/templates/IdentityRowRenderer.js +3 -1
- package/lib/module/web/templates/MediaTileRowRenderer.js +84 -15
- package/lib/typescript/src/models.d.ts +18 -1
- package/package.json +1 -1
- package/scripts/generate-icons.test.mjs +1 -0
- package/scripts/native-list-icons.json +1 -1
- package/src/NativeList.tsx +25 -4
- package/src/NativeList.web.tsx +1 -1
- package/src/models.ts +18 -0
- package/src/validation.ts +96 -1
- package/src/web/NativeListGeneratedIcons.ts +1 -0
- package/src/web/NativeListWebEngine.ts +16 -11
- package/src/web/templates/ActivityRowRenderer.ts +42 -32
- package/src/web/templates/DataRowRenderer.ts +76 -1
- package/src/web/templates/IdentityRowRenderer.ts +3 -3
- package/src/web/templates/MediaTileRowRenderer.ts +90 -15
|
@@ -78,9 +78,9 @@ internal class NativeListActivityDetailsView(private val reactContext: ThemedRea
|
|
|
78
78
|
return parseNativeListColor(theme?.optString(key, fallback) ?: fallback)
|
|
79
79
|
}
|
|
80
80
|
private fun text(label: NativeListTextView, value: String, style: JSONObject?, size: Float,
|
|
81
|
-
ink: Int, line: Int, weight: String = "regular", lines: Int = 1)
|
|
82
|
-
NativeListResolvedText.resolve(context, fonts, value, style, size, weight, ink, line, lines, sourceScale)
|
|
83
|
-
|
|
81
|
+
ink: Int, line: Int, weight: String = "regular", lines: Int = 1): NativeListResolvedText =
|
|
82
|
+
NativeListResolvedText.resolve(context, fonts, value, style, size, weight, ink, line, lines, sourceScale)
|
|
83
|
+
.also { it.bind(label) }
|
|
84
84
|
fun bind(item: NativeListItem, theme: JSONObject?, sourceScale: Boolean) {
|
|
85
85
|
this.sourceScale = sourceScale
|
|
86
86
|
val data = item.json
|
|
@@ -98,12 +98,20 @@ internal class NativeListActivityDetailsView(private val reactContext: ThemedRea
|
|
|
98
98
|
setClipping(nativeListActivityHostClips(leadingOverhangs))
|
|
99
99
|
amounts.layoutParams = if (table) LayoutParams(0, -2, 1f).apply { marginStart = gap } else LayoutParams(-2, -2).apply { marginStart = gap }
|
|
100
100
|
text(title, data.optString("title"), style.optJSONObject("title"), 16f, color("primary", theme), 24, "medium")
|
|
101
|
-
text(description, data.optString("description"), style.optJSONObject("description"), 14f, color("secondary", theme), 20, lines = 2)
|
|
101
|
+
val descriptionText = text(description, data.optString("description"), style.optJSONObject("description"), 14f, color("secondary", theme), 20, lines = 2)
|
|
102
102
|
text(status, data.optString("status"), style.optJSONObject("status"), 12f, color("secondary", theme), 16)
|
|
103
103
|
description.setOnClickListener(null)
|
|
104
104
|
val descriptionKey = data.optString("descriptionActionKey")
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
// The view spans the identity column, so with an action only its text from
|
|
106
|
+
// the caller's `descriptionActionStart` on takes touches. The start is
|
|
107
|
+
// never read out of the string, and one at the end leaves no action.
|
|
108
|
+
val actionStart =
|
|
109
|
+
if (descriptionKey.isEmpty()) null
|
|
110
|
+
else descriptionText.displayOffset(data.optLong("descriptionActionStart"), data.optString("description"))
|
|
111
|
+
val hasActionText = actionStart != null && actionStart < descriptionText.text.length
|
|
112
|
+
description.touchableTextStart = actionStart
|
|
113
|
+
description.isClickable = hasActionText
|
|
114
|
+
if (hasActionText) description.setOnClickListener { onAction?.invoke(descriptionKey, description, "description", 0) }
|
|
107
115
|
for (view in listOf(description, status)) (view.layoutParams as LayoutParams).topMargin = if (style.has("lineGap")) px(style.optDouble("lineGap")) else 0
|
|
108
116
|
badges.forEach(titleLine::removeView); badges.clear()
|
|
109
117
|
val badgeData = data.optJSONArray("badges")
|
|
@@ -172,6 +180,7 @@ internal class NativeListActivityDetailsView(private val reactContext: ThemedRea
|
|
|
172
180
|
}
|
|
173
181
|
fun recycle() {
|
|
174
182
|
visual.recycle(); visual.visibility = VISIBLE; amountViews.forEach { it.recycle() }; description.setOnClickListener(null); actions.removeAllViews()
|
|
183
|
+
description.touchableTextStart = null
|
|
175
184
|
leadingOverhangs = false
|
|
176
185
|
setClipping(true)
|
|
177
186
|
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
package com.margelo.nitro.nativelist
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.graphics.Canvas
|
|
5
|
+
import android.graphics.Color
|
|
6
|
+
import android.graphics.LinearGradient
|
|
7
|
+
import android.graphics.Matrix
|
|
8
|
+
import android.graphics.Paint
|
|
9
|
+
import android.graphics.Path
|
|
10
|
+
import android.graphics.PorterDuff
|
|
11
|
+
import android.graphics.PorterDuffXfermode
|
|
12
|
+
import android.graphics.RadialGradient
|
|
13
|
+
import android.graphics.RectF
|
|
14
|
+
import android.graphics.Shader
|
|
15
|
+
import android.os.SystemClock
|
|
16
|
+
import android.provider.Settings
|
|
17
|
+
import android.view.View
|
|
18
|
+
import android.view.animation.PathInterpolator
|
|
19
|
+
import kotlin.math.sqrt
|
|
20
|
+
import kotlin.math.tan
|
|
21
|
+
import org.json.JSONObject
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Loading sheen and failed hatch drawn over a media tile while it has no picture. Geometry mirrors
|
|
25
|
+
* the Web renderer (`MediaTileRowRenderer.ts`) and iOS (`NativeListMediaPlaceholderView.swift`) so
|
|
26
|
+
* every platform draws the same placeholder.
|
|
27
|
+
*/
|
|
28
|
+
internal class NativeListMediaPlaceholderView(context: Context) : View(context) {
|
|
29
|
+
enum class State {
|
|
30
|
+
Hidden,
|
|
31
|
+
Loading,
|
|
32
|
+
Failed,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
var state = State.Hidden
|
|
36
|
+
private set
|
|
37
|
+
|
|
38
|
+
var cornerRadius = 0f
|
|
39
|
+
set(value) {
|
|
40
|
+
if (field == value) return
|
|
41
|
+
field = value
|
|
42
|
+
invalidate()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private val density = resources.displayMetrics.density
|
|
46
|
+
private val clip = Path()
|
|
47
|
+
private val bounds = RectF()
|
|
48
|
+
private val hatchPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.STROKE }
|
|
49
|
+
private val hatchMaskPaint =
|
|
50
|
+
Paint(Paint.ANTI_ALIAS_FLAG).apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN) }
|
|
51
|
+
private val sheenPaint = Paint(Paint.ANTI_ALIAS_FLAG)
|
|
52
|
+
private val fillPaint = Paint(Paint.ANTI_ALIAS_FLAG)
|
|
53
|
+
private val glyphPaint =
|
|
54
|
+
Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
55
|
+
style = Paint.Style.STROKE
|
|
56
|
+
strokeCap = Paint.Cap.ROUND
|
|
57
|
+
strokeJoin = Paint.Join.ROUND
|
|
58
|
+
}
|
|
59
|
+
private val borderPaint =
|
|
60
|
+
Paint(Paint.ANTI_ALIAS_FLAG).apply {
|
|
61
|
+
style = Paint.Style.STROKE
|
|
62
|
+
strokeWidth = 1f
|
|
63
|
+
}
|
|
64
|
+
private val picture = Path()
|
|
65
|
+
private val slash = Path()
|
|
66
|
+
private val pictureClip = Path()
|
|
67
|
+
private var shine = Color.argb(191, 255, 255, 255)
|
|
68
|
+
private var laidOutWidth = -1
|
|
69
|
+
private var laidOutHeight = -1
|
|
70
|
+
private var reduceMotion = false
|
|
71
|
+
|
|
72
|
+
init {
|
|
73
|
+
visibility = GONE
|
|
74
|
+
setWillNotDraw(false)
|
|
75
|
+
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_NO
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fun applyTheme(theme: JSONObject?) {
|
|
79
|
+
fun tint(name: String, fallback: String) =
|
|
80
|
+
parseNativeListColor(theme?.optString(name, fallback)?.takeIf(String::isNotEmpty) ?: fallback)
|
|
81
|
+
glyphPaint.color = withAlpha(tint("disabledText", "#8C8C8C"), 0.7f)
|
|
82
|
+
fillPaint.color = tint("strongBackground", "#0000000F")
|
|
83
|
+
borderPaint.color = withAlpha(tint("separator", "#E5E7EB"), 0.6f)
|
|
84
|
+
val primary = tint("primaryText", "#202020")
|
|
85
|
+
hatchPaint.color = Color.argb(11, Color.red(primary), Color.green(primary), Color.blue(primary))
|
|
86
|
+
val row = tint("rowBackground", "#FFFFFF")
|
|
87
|
+
val dark = Color.red(row) * 299 + Color.green(row) * 587 + Color.blue(row) * 114 < 128_000
|
|
88
|
+
val nextShine = Color.argb(if (dark) 18 else 191, 255, 255, 255)
|
|
89
|
+
if (nextShine != shine) {
|
|
90
|
+
shine = nextShine
|
|
91
|
+
laidOutWidth = -1
|
|
92
|
+
}
|
|
93
|
+
invalidate()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fun setState(next: State) {
|
|
97
|
+
if (next == state) return
|
|
98
|
+
state = next
|
|
99
|
+
if (next == State.Loading) reduceMotion = animatorScale() == 0f
|
|
100
|
+
visibility = if (next == State.Hidden) GONE else VISIBLE
|
|
101
|
+
invalidate()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
|
|
105
|
+
super.onSizeChanged(w, h, oldw, oldh)
|
|
106
|
+
laidOutWidth = -1
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private fun layoutShapes() {
|
|
110
|
+
if (laidOutWidth == width && laidOutHeight == height) return
|
|
111
|
+
laidOutWidth = width
|
|
112
|
+
laidOutHeight = height
|
|
113
|
+
val w = width.toFloat()
|
|
114
|
+
val h = height.toFloat()
|
|
115
|
+
val center = 0.5f * sqrt(w * w + h * h)
|
|
116
|
+
hatchMaskPaint.shader =
|
|
117
|
+
RadialGradient(
|
|
118
|
+
w / 2,
|
|
119
|
+
h / 2,
|
|
120
|
+
center.coerceAtLeast(1f),
|
|
121
|
+
intArrayOf(Color.TRANSPARENT, Color.TRANSPARENT, Color.BLACK, Color.BLACK),
|
|
122
|
+
floatArrayOf(0f, 0.22f, 0.7f, 1f),
|
|
123
|
+
Shader.TileMode.CLAMP,
|
|
124
|
+
)
|
|
125
|
+
val transparentShine = shine and 0x00FFFFFF
|
|
126
|
+
sheenPaint.shader =
|
|
127
|
+
LinearGradient(
|
|
128
|
+
0f,
|
|
129
|
+
0f,
|
|
130
|
+
w * 0.6f,
|
|
131
|
+
0f,
|
|
132
|
+
intArrayOf(transparentShine, shine, transparentShine),
|
|
133
|
+
floatArrayOf(0f, 0.5f, 1f),
|
|
134
|
+
Shader.TileMode.CLAMP,
|
|
135
|
+
)
|
|
136
|
+
hatchPaint.strokeWidth = density
|
|
137
|
+
// Glyph geometry in a 24-unit box, scaled to 28% of the tile and capped at 44pt.
|
|
138
|
+
val size = minOf(w * 0.28f, 44f * density)
|
|
139
|
+
val scale = size / 24f
|
|
140
|
+
val matrix =
|
|
141
|
+
Matrix().apply {
|
|
142
|
+
setScale(scale, scale)
|
|
143
|
+
postTranslate((w - size) / 2, (h - size) / 2)
|
|
144
|
+
}
|
|
145
|
+
glyphPaint.strokeWidth = 1.5f * scale
|
|
146
|
+
picture.reset()
|
|
147
|
+
picture.addRoundRect(RectF(3f, 3f, 21f, 21f), 4f, 4f, Path.Direction.CW)
|
|
148
|
+
picture.addCircle(9f, 9f, 1.6f, Path.Direction.CW)
|
|
149
|
+
picture.moveTo(5f, 20.5f)
|
|
150
|
+
picture.lineTo(14.5f, 11f)
|
|
151
|
+
picture.lineTo(21f, 17.5f)
|
|
152
|
+
picture.transform(matrix)
|
|
153
|
+
slash.reset()
|
|
154
|
+
slash.moveTo(3f, 3f)
|
|
155
|
+
slash.lineTo(21f, 21f)
|
|
156
|
+
slash.transform(matrix)
|
|
157
|
+
val band =
|
|
158
|
+
Path().apply {
|
|
159
|
+
moveTo(2.6f, -0.6f)
|
|
160
|
+
lineTo(24.6f, 21.4f)
|
|
161
|
+
lineTo(21.4f, 24.6f)
|
|
162
|
+
lineTo(-0.6f, 2.6f)
|
|
163
|
+
close()
|
|
164
|
+
transform(matrix)
|
|
165
|
+
}
|
|
166
|
+
pictureClip.reset()
|
|
167
|
+
pictureClip.addRect(0f, 0f, w, h, Path.Direction.CW)
|
|
168
|
+
pictureClip.op(band, Path.Op.DIFFERENCE)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
override fun onDraw(canvas: Canvas) {
|
|
172
|
+
if (state == State.Hidden || width == 0 || height == 0) return
|
|
173
|
+
layoutShapes()
|
|
174
|
+
val w = width.toFloat()
|
|
175
|
+
val h = height.toFloat()
|
|
176
|
+
bounds.set(0f, 0f, w, h)
|
|
177
|
+
clip.reset()
|
|
178
|
+
clip.addRoundRect(bounds, cornerRadius, cornerRadius, Path.Direction.CW)
|
|
179
|
+
canvas.save()
|
|
180
|
+
canvas.clipPath(clip)
|
|
181
|
+
// The placeholder owns the slot fill: the image view underneath is empty while
|
|
182
|
+
// loading and hidden once every probe failed.
|
|
183
|
+
canvas.drawRect(0f, 0f, w, h, fillPaint)
|
|
184
|
+
if (state == State.Failed) drawHatch(canvas, w, h) else drawSheen(canvas, w, h)
|
|
185
|
+
if (state == State.Failed) {
|
|
186
|
+
canvas.save()
|
|
187
|
+
canvas.clipPath(pictureClip)
|
|
188
|
+
canvas.drawPath(picture, glyphPaint)
|
|
189
|
+
canvas.restore()
|
|
190
|
+
canvas.drawPath(slash, glyphPaint)
|
|
191
|
+
} else canvas.drawPath(picture, glyphPaint)
|
|
192
|
+
canvas.restore()
|
|
193
|
+
val inset = borderPaint.strokeWidth / 2
|
|
194
|
+
bounds.inset(inset, inset)
|
|
195
|
+
canvas.drawRoundRect(bounds, cornerRadius - inset, cornerRadius - inset, borderPaint)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private fun drawHatch(canvas: Canvas, w: Float, h: Float) {
|
|
199
|
+
val layer = canvas.saveLayer(0f, 0f, w, h, null)
|
|
200
|
+
// Lines along x + y = c, 7pt apart measured perpendicular to the stripes.
|
|
201
|
+
val step = 7f * density * sqrt(2f)
|
|
202
|
+
var c = 0f
|
|
203
|
+
while (c <= w + h) {
|
|
204
|
+
canvas.drawLine(c, 0f, 0f, c, hatchPaint)
|
|
205
|
+
c += step
|
|
206
|
+
}
|
|
207
|
+
canvas.drawRect(0f, 0f, w, h, hatchMaskPaint)
|
|
208
|
+
canvas.restoreToCount(layer)
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
private fun drawSheen(canvas: Canvas, w: Float, h: Float) {
|
|
212
|
+
// Band left edge travels from -80% to 130% of the tile, then rests for 30% of the period.
|
|
213
|
+
// Every tile derives its phase from the shared uptime clock so the grid sweeps in step.
|
|
214
|
+
val phase = (SystemClock.uptimeMillis() % SWEEP_PERIOD_MS).toFloat() / SWEEP_PERIOD_MS
|
|
215
|
+
val progress = if (phase < 0.7f) SWEEP_EASING.getInterpolation(phase / 0.7f) else 1f
|
|
216
|
+
val left = if (reduceMotion) w * 0.2f else w * (-0.8f + 2.1f * progress)
|
|
217
|
+
sheenPaint.alpha = if (reduceMotion) 153 else 255
|
|
218
|
+
canvas.save()
|
|
219
|
+
canvas.translate(left + w * 0.3f, h / 2)
|
|
220
|
+
canvas.skew(SKEW, 0f)
|
|
221
|
+
canvas.translate(-w * 0.3f, 0f)
|
|
222
|
+
canvas.drawRect(0f, -h, w * 0.6f, h, sheenPaint)
|
|
223
|
+
canvas.restore()
|
|
224
|
+
if (!reduceMotion && isAttachedToWindow) postInvalidateOnAnimation()
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private fun animatorScale() =
|
|
228
|
+
Settings.Global.getFloat(context.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f)
|
|
229
|
+
|
|
230
|
+
private fun withAlpha(color: Int, factor: Float) =
|
|
231
|
+
Color.argb(
|
|
232
|
+
(Color.alpha(color) * factor).toInt(),
|
|
233
|
+
Color.red(color),
|
|
234
|
+
Color.green(color),
|
|
235
|
+
Color.blue(color),
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
private companion object {
|
|
239
|
+
const val SWEEP_PERIOD_MS = 1600L
|
|
240
|
+
// CSS skewX(-12deg).
|
|
241
|
+
val SKEW = tan(Math.toRadians(-12.0)).toFloat()
|
|
242
|
+
val SWEEP_EASING = PathInterpolator(0.4f, 0f, 0.2f, 1f)
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -26,6 +26,8 @@ import com.facebook.react.modules.network.OkHttpClientProvider
|
|
|
26
26
|
import com.facebook.react.uimanager.ThemedReactContext
|
|
27
27
|
import okhttp3.JavaNetCookieJar
|
|
28
28
|
import org.json.JSONObject
|
|
29
|
+
import com.margelo.nitro.onekeyimage.OneKeyMediaPreviewCache
|
|
30
|
+
import java.util.UUID
|
|
29
31
|
|
|
30
32
|
/**
|
|
31
33
|
* Uses Media3 like native Video, but never starts playback or requests audio focus. A player
|
|
@@ -35,9 +37,10 @@ import org.json.JSONObject
|
|
|
35
37
|
internal class NativeListMediaPreviewSlot(private val context: ThemedReactContext) {
|
|
36
38
|
val view: FrameLayout = object : FrameLayout(context) {
|
|
37
39
|
override fun onAttachedToWindow() { super.onAttachedToWindow(); observeViewport(); updateVisibility() }
|
|
38
|
-
override fun onDetachedFromWindow() { stopObservingViewport();
|
|
40
|
+
override fun onDetachedFromWindow() { stopObservingViewport(); cancelPreview(); super.onDetachedFromWindow() }
|
|
41
|
+
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh); updateVisibility() }
|
|
39
42
|
override fun onWindowVisibilityChanged(visibility: Int) { super.onWindowVisibilityChanged(visibility); updateVisibility() }
|
|
40
|
-
override fun onVisibilityAggregated(isVisible: Boolean) { super.onVisibilityAggregated(isVisible); if (isVisible) updateVisibility() else
|
|
43
|
+
override fun onVisibilityAggregated(isVisible: Boolean) { super.onVisibilityAggregated(isVisible); if (isVisible) updateVisibility() else cancelPreview() }
|
|
41
44
|
}
|
|
42
45
|
private val image = NativeListImageSlot(context)
|
|
43
46
|
private val video = AspectRatioFrameLayout(context)
|
|
@@ -51,6 +54,10 @@ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContex
|
|
|
51
54
|
private var completion: ((Boolean) -> Unit)? = null
|
|
52
55
|
private var pendingCompletion: Runnable? = null
|
|
53
56
|
private var player: ExoPlayer? = null
|
|
57
|
+
private var subscription: OneKeyMediaPreviewCache.Subscription? = null
|
|
58
|
+
private var extractionLease: UUID? = null
|
|
59
|
+
private var requestedEdge: Int? = null
|
|
60
|
+
private val previewCache by lazy { OneKeyMediaPreviewCache.get(context) }
|
|
54
61
|
private var resumeVideo: (() -> Unit)? = null
|
|
55
62
|
private var viewportObserver: ViewTreeObserver? = null
|
|
56
63
|
private val visibleRect = Rect()
|
|
@@ -99,7 +106,7 @@ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContex
|
|
|
99
106
|
val candidate = candidateGeneration
|
|
100
107
|
resumeVideo = null
|
|
101
108
|
stopObservingViewport()
|
|
102
|
-
|
|
109
|
+
cancelPreview()
|
|
103
110
|
if (attempt >= order.size) {
|
|
104
111
|
result = false
|
|
105
112
|
completion?.invoke(false)
|
|
@@ -133,8 +140,36 @@ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContex
|
|
|
133
140
|
val uri = source.optString("uri")
|
|
134
141
|
if (uri.isBlank()) { finished(false); return }
|
|
135
142
|
val headers = source.optJSONObject("headers")?.let { values -> values.keys().asSequence().associateWith { values.optString(it) } } ?: emptyMap()
|
|
143
|
+
val fitKey = if ((fit ?: source.optString("contentFit")) == "cover") "cover" else "contain"
|
|
136
144
|
resumeVideo = start@{
|
|
137
|
-
if (epoch != token || candidateGeneration != candidate ||
|
|
145
|
+
if (epoch != token || candidateGeneration != candidate || subscription != null || frame.drawable != null ||
|
|
146
|
+
video.width <= 0 || video.height <= 0) return@start
|
|
147
|
+
val edge = OneKeyMediaPreviewCache.targetEdge(maxOf(view.width, view.height))
|
|
148
|
+
requestedEdge = edge
|
|
149
|
+
subscription = previewCache.request(uri, headers, edge, fitKey, source.optString("cachePolicy", "memory-disk"),
|
|
150
|
+
onImage = { still ->
|
|
151
|
+
if (epoch == token && candidateGeneration == candidate) {
|
|
152
|
+
subscription = null
|
|
153
|
+
video.setAspectRatio(still.width.toFloat() / still.height)
|
|
154
|
+
frame.setImageBitmap(still); frame.visibility = View.VISIBLE
|
|
155
|
+
stopObservingViewport(); finished(true)
|
|
156
|
+
}
|
|
157
|
+
}, onExtract = { lease ->
|
|
158
|
+
if (epoch == token && candidateGeneration == candidate) {
|
|
159
|
+
extractionLease = lease
|
|
160
|
+
updateVisibility()
|
|
161
|
+
if (extractionLease == lease && requestedEdge == edge) startPlayer(uri, headers, token, candidate, edge, lease)
|
|
162
|
+
}
|
|
163
|
+
}, onRevoked = { extractionLease = null; releasePlayer() },
|
|
164
|
+
onFailure = {
|
|
165
|
+
if (epoch == token && candidateGeneration == candidate) { subscription = null; finished(false) }
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
observeViewport()
|
|
169
|
+
updateVisibility()
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
private fun startPlayer(uri: String, headers: Map<String, String>, token: Int, candidate: Int, edge: Int, lease: UUID) {
|
|
138
173
|
playerGeneration++
|
|
139
174
|
val generation = playerGeneration
|
|
140
175
|
try {
|
|
@@ -158,10 +193,11 @@ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContex
|
|
|
158
193
|
next.addListener(object : Player.Listener {
|
|
159
194
|
override fun onRenderedFirstFrame() {
|
|
160
195
|
if (playerGeneration != generation) return
|
|
161
|
-
|
|
162
|
-
|
|
196
|
+
captureFirstFrame(token, candidate, generation, FIRST_FRAME_CAPTURE_ATTEMPTS, edge, lease)
|
|
197
|
+
}
|
|
198
|
+
override fun onPlayerError(error: PlaybackException) {
|
|
199
|
+
view.post { if (playerGeneration == generation) previewCache.fail(lease) }
|
|
163
200
|
}
|
|
164
|
-
override fun onPlayerError(error: PlaybackException) { if (playerGeneration == generation) finished(false, generation) }
|
|
165
201
|
override fun onVideoSizeChanged(size: VideoSize) {
|
|
166
202
|
if (playerGeneration == generation && size.height > 0) video.setAspectRatio(size.width * size.pixelWidthHeightRatio / size.height)
|
|
167
203
|
}
|
|
@@ -170,39 +206,26 @@ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContex
|
|
|
170
206
|
next.prepare()
|
|
171
207
|
next.pause()
|
|
172
208
|
} catch (_: Exception) {
|
|
173
|
-
|
|
174
|
-
finished(false)
|
|
209
|
+
previewCache.fail(lease)
|
|
175
210
|
}
|
|
176
|
-
}
|
|
177
|
-
observeViewport()
|
|
178
|
-
updateVisibility()
|
|
179
211
|
}
|
|
180
212
|
|
|
181
|
-
/**
|
|
182
|
-
|
|
183
|
-
* retried on later frames. If no frame can be copied the paused player stays attached.
|
|
184
|
-
*/
|
|
185
|
-
private fun captureFirstFrame(token: Int, candidate: Int, generation: Int, attemptsLeft: Int) {
|
|
213
|
+
/** TextureView requires UI access; request bounded pixels before background encoding. */
|
|
214
|
+
private fun captureFirstFrame(token: Int, candidate: Int, generation: Int, attemptsLeft: Int, edge: Int, lease: UUID) {
|
|
186
215
|
view.postOnAnimation {
|
|
187
216
|
if (epoch != token || candidateGeneration != candidate || playerGeneration != generation) return@postOnAnimation
|
|
188
|
-
// Releasing first would make the queued success completion drop itself as stale.
|
|
189
|
-
if (result != true) {
|
|
190
|
-
captureFirstFrame(token, candidate, generation, attemptsLeft)
|
|
191
|
-
return@postOnAnimation
|
|
192
|
-
}
|
|
193
217
|
val still = if (texture.isAvailable && texture.width > 0 && texture.height > 0) {
|
|
194
|
-
|
|
218
|
+
val scale = minOf(1.0, edge.toDouble() / maxOf(texture.width, texture.height))
|
|
219
|
+
runCatching { texture.getBitmap(maxOf(1, (texture.width * scale).toInt()), maxOf(1, (texture.height * scale).toInt())) }.getOrNull()
|
|
195
220
|
} else null
|
|
196
221
|
if (still == null || !hasVisiblePixels(still)) {
|
|
197
222
|
still?.recycle()
|
|
198
|
-
if (attemptsLeft > 1) captureFirstFrame(token, candidate, generation, attemptsLeft - 1)
|
|
223
|
+
if (attemptsLeft > 1) captureFirstFrame(token, candidate, generation, attemptsLeft - 1, edge, lease)
|
|
224
|
+
else previewCache.fail(lease)
|
|
199
225
|
return@postOnAnimation
|
|
200
226
|
}
|
|
201
|
-
frame.setImageBitmap(still)
|
|
202
|
-
frame.visibility = View.VISIBLE
|
|
203
|
-
resumeVideo = null
|
|
204
|
-
stopObservingViewport()
|
|
205
227
|
releasePlayer()
|
|
228
|
+
previewCache.complete(lease, still)
|
|
206
229
|
}
|
|
207
230
|
}
|
|
208
231
|
|
|
@@ -213,7 +236,7 @@ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContex
|
|
|
213
236
|
}
|
|
214
237
|
|
|
215
238
|
private fun observeViewport() {
|
|
216
|
-
if (viewportObserver != null || resumeVideo == null || !view.isAttachedToWindow) return
|
|
239
|
+
if (viewportObserver != null || resumeVideo == null || frame.drawable != null || !view.isAttachedToWindow) return
|
|
217
240
|
view.viewTreeObserver.takeIf { it.isAlive }?.let {
|
|
218
241
|
it.addOnScrollChangedListener(scrollListener)
|
|
219
242
|
it.addOnGlobalLayoutListener(layoutListener)
|
|
@@ -230,6 +253,13 @@ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContex
|
|
|
230
253
|
viewportObserver = null
|
|
231
254
|
}
|
|
232
255
|
private fun updateVisibility() {
|
|
256
|
+
requestedEdge?.let { previous ->
|
|
257
|
+
if (video.width > 0 && video.height > 0 && OneKeyMediaPreviewCache.targetEdge(maxOf(view.width, view.height)) != previous) {
|
|
258
|
+
cancelPreview(); requestedEdge = null
|
|
259
|
+
frame.setImageDrawable(null); frame.visibility = View.GONE; result = null
|
|
260
|
+
observeViewport()
|
|
261
|
+
}
|
|
262
|
+
}
|
|
233
263
|
var ancestor: View? = view
|
|
234
264
|
var opaque = true
|
|
235
265
|
while (ancestor != null) {
|
|
@@ -238,7 +268,11 @@ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContex
|
|
|
238
268
|
}
|
|
239
269
|
if (opaque && view.isAttachedToWindow && view.windowVisibility == View.VISIBLE && view.isShown &&
|
|
240
270
|
view.getGlobalVisibleRect(visibleRect) && !visibleRect.isEmpty) resumeVideo?.invoke()
|
|
241
|
-
else
|
|
271
|
+
else cancelPreview()
|
|
272
|
+
}
|
|
273
|
+
private fun cancelPreview() {
|
|
274
|
+
val previous = subscription; subscription = null
|
|
275
|
+
previewCache.cancel(previous); extractionLease = null; releasePlayer()
|
|
242
276
|
}
|
|
243
277
|
private fun releasePlayer() {
|
|
244
278
|
playerGeneration++
|
|
@@ -251,11 +285,12 @@ internal class NativeListMediaPreviewSlot(private val context: ThemedReactContex
|
|
|
251
285
|
pendingCompletion = null
|
|
252
286
|
resumeVideo = null
|
|
253
287
|
stopObservingViewport()
|
|
254
|
-
|
|
288
|
+
cancelPreview()
|
|
255
289
|
image.recycle()
|
|
256
290
|
image.view.visibility = View.VISIBLE
|
|
257
291
|
video.visibility = View.GONE
|
|
258
292
|
video.setAspectRatio(0f)
|
|
293
|
+
requestedEdge = null
|
|
259
294
|
frame.setImageDrawable(null)
|
|
260
295
|
frame.visibility = View.GONE
|
|
261
296
|
identity = null; result = null; completion = null
|
|
@@ -16,7 +16,7 @@ internal class NativeListMediaTileRowView(context: ThemedReactContext, fonts: Na
|
|
|
16
16
|
private val picture = FrameLayout(context)
|
|
17
17
|
private val image = NativeListMediaPreviewSlot(context)
|
|
18
18
|
private val network = NativeListImageSlot(context)
|
|
19
|
-
private val
|
|
19
|
+
private val placeholderView = NativeListMediaPlaceholderView(context)
|
|
20
20
|
private val title = NativeListTextView(context)
|
|
21
21
|
private val subtitle = NativeListTextView(context)
|
|
22
22
|
private val badge = NativeListTextView(context)
|
|
@@ -43,7 +43,7 @@ internal class NativeListMediaTileRowView(context: ThemedReactContext, fonts: Na
|
|
|
43
43
|
init {
|
|
44
44
|
orientation = VERTICAL
|
|
45
45
|
picture.addView(image.view, FrameLayout.LayoutParams(-1, -1))
|
|
46
|
-
picture.addView(
|
|
46
|
+
picture.addView(placeholderView, FrameLayout.LayoutParams(-1, -1))
|
|
47
47
|
picture.addView(badge)
|
|
48
48
|
subtitleLine.addView(subtitle, LayoutParams(0, -2, 1f))
|
|
49
49
|
subtitleLine.addView(network.view)
|
|
@@ -136,17 +136,20 @@ internal class NativeListMediaTileRowView(context: ThemedReactContext, fonts: Na
|
|
|
136
136
|
metadata.layoutParams = LayoutParams(-1, -2).apply { topMargin = size("leadingGap", 8) }
|
|
137
137
|
(subtitle.layoutParams as LayoutParams).marginEnd = dp(8)
|
|
138
138
|
val state = item.json.optString("imageState")
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
139
|
+
placeholderView.applyTheme(theme)
|
|
140
|
+
placeholderView.cornerRadius =
|
|
141
|
+
if (imageStyle.optString("shape") == "circle" && !imageStyle.has("cornerRadius")) 10_000f
|
|
142
|
+
else if (imageStyle.has("cornerRadius")) stylePx(imageStyle.optDouble("cornerRadius")).toFloat()
|
|
143
|
+
else if (imageStyle.optString("shape") == "square") 0f
|
|
144
|
+
else dp(10).toFloat()
|
|
144
145
|
val media = item.json.optJSONObject("media")
|
|
145
146
|
val source = media?.optJSONObject("source") ?: item.json.optJSONObject("image")
|
|
146
147
|
val order = media?.optJSONArray("probeOrder")?.let { values ->
|
|
147
148
|
(0 until values.length()).map { values.optString(it) }
|
|
148
149
|
} ?: listOf("image")
|
|
149
|
-
if (state != "empty" && state != "error" && source != null)
|
|
150
|
+
if (state != "empty" && state != "error" && source != null) {
|
|
151
|
+
// Set before binding: a cached result completes synchronously inside bind.
|
|
152
|
+
showPlaceholder(NativeListMediaPlaceholderView.State.Loading)
|
|
150
153
|
image.bind(
|
|
151
154
|
source,
|
|
152
155
|
"${item.key}:media",
|
|
@@ -156,17 +159,26 @@ internal class NativeListMediaTileRowView(context: ThemedReactContext, fonts: Na
|
|
|
156
159
|
?: "#0000000F",
|
|
157
160
|
probeOrder = order,
|
|
158
161
|
) { loaded ->
|
|
159
|
-
|
|
162
|
+
showPlaceholder(
|
|
163
|
+
if (loaded) NativeListMediaPlaceholderView.State.Hidden
|
|
164
|
+
else NativeListMediaPlaceholderView.State.Failed
|
|
165
|
+
)
|
|
160
166
|
if (!loaded) image.view.setBackgroundColor(tint("strongBackground", "#0000000F"))
|
|
161
167
|
}
|
|
162
|
-
else
|
|
168
|
+
} else {
|
|
169
|
+
image.recycle()
|
|
170
|
+
showPlaceholder(
|
|
171
|
+
if (state == "error") NativeListMediaPlaceholderView.State.Failed
|
|
172
|
+
else NativeListMediaPlaceholderView.State.Hidden
|
|
173
|
+
)
|
|
174
|
+
}
|
|
163
175
|
// Legacy: only the empty/error states paint a slot fill; loaded images show
|
|
164
176
|
// their own placeholder without a white backing.
|
|
165
177
|
image.view.background =
|
|
166
178
|
when (state) {
|
|
167
179
|
"error" -> GradientDrawable().apply { setColor(tint("strongBackground", "#0000000F")) }
|
|
168
180
|
"empty" -> GradientDrawable().apply { setColor(Color.WHITE) }
|
|
169
|
-
else -> if (
|
|
181
|
+
else -> if (placeholderView.state == NativeListMediaPlaceholderView.State.Failed) GradientDrawable().apply { setColor(tint("strongBackground", "#0000000F")) } else null
|
|
170
182
|
}
|
|
171
183
|
image.view.outlineProvider =
|
|
172
184
|
object : ViewOutlineProvider() {
|
|
@@ -229,8 +241,17 @@ internal class NativeListMediaTileRowView(context: ThemedReactContext, fonts: Na
|
|
|
229
241
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
|
|
230
242
|
}
|
|
231
243
|
|
|
244
|
+
private fun showPlaceholder(state: NativeListMediaPlaceholderView.State) {
|
|
245
|
+
placeholderView.setState(state)
|
|
246
|
+
// OneKeyImage may paint its own terminal symbol once a probe fails; the failed
|
|
247
|
+
// placeholder replaces the slot entirely instead of drawing over it.
|
|
248
|
+
image.view.visibility =
|
|
249
|
+
if (state == NativeListMediaPlaceholderView.State.Failed) INVISIBLE else VISIBLE
|
|
250
|
+
}
|
|
251
|
+
|
|
232
252
|
override fun recycleContent() {
|
|
233
253
|
image.recycle()
|
|
254
|
+
showPlaceholder(NativeListMediaPlaceholderView.State.Hidden)
|
|
234
255
|
network.recycle()
|
|
235
256
|
closeKey = ""
|
|
236
257
|
picture.alpha = 1f
|
|
@@ -62,6 +62,66 @@ internal fun nativeListLeadingVisualOverhangsFrame(
|
|
|
62
62
|
*/
|
|
63
63
|
internal fun nativeListActivityHostClips(leadingOverhangs: Boolean): Boolean = !leadingOverhangs
|
|
64
64
|
|
|
65
|
+
/** A one-line text view shows each line break of its text as one space. */
|
|
66
|
+
internal fun nativeListFoldLineBreaks(text: String): String = text.replace(Regex("\\r\\n|[\\r\\n]"), " ")
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Offset into the displayed text for [sourceOffset], a UTF-16 offset into the
|
|
70
|
+
* caller's [source], kept within the text. Folding line breaks
|
|
71
|
+
* ([nativeListFoldLineBreaks]) moves every offset after a CRLF. The offset is a
|
|
72
|
+
* Long because JSON carries any safe integer; narrowing it first would wrap a
|
|
73
|
+
* value past Int range instead of clamping it to the end.
|
|
74
|
+
*/
|
|
75
|
+
internal fun nativeListDisplayOffset(source: String, sourceOffset: Long, foldsLineBreaks: Boolean): Int {
|
|
76
|
+
val clamped = sourceOffset.coerceIn(0L, source.length.toLong()).toInt()
|
|
77
|
+
return if (foldsLineBreaks) nativeListFoldLineBreaks(source.substring(0, clamped)).length else clamped
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A horizontal run of laid-out text on one line, in view coordinates. */
|
|
81
|
+
internal data class NativeListTextRun(val left: Float, val right: Float)
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* One laid-out line of a text view, in view coordinates: its vertical extent,
|
|
85
|
+
* the runs of [touchable] text and the runs of [plain] text before it. A line
|
|
86
|
+
* holds one run of each, or several where mixed-direction text puts plain text
|
|
87
|
+
* between touchable text.
|
|
88
|
+
*/
|
|
89
|
+
internal data class NativeListTextLine(
|
|
90
|
+
val top: Float,
|
|
91
|
+
val bottom: Float,
|
|
92
|
+
val touchable: List<NativeListTextRun>,
|
|
93
|
+
val plain: List<NativeListTextRun> = emptyList(),
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
internal data class NativeListTouchRegion(val left: Float, val top: Float, val right: Float, val bottom: Float) {
|
|
97
|
+
fun contains(x: Float, y: Float): Boolean = x in left..right && y in top..bottom
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Runs are read back from pixel regions, so neighbours can be a pixel apart. */
|
|
101
|
+
private const val TEXT_RUN_EDGE = 1f
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Parts of a stretched text view that take touches: every run of touchable
|
|
105
|
+
* text, widened by [slop] into blank space only. The widening stops at plain
|
|
106
|
+
* text on the same line, so text before the touchable text never takes the
|
|
107
|
+
* touch, however narrow it is. Touches outside the regions fall through to the
|
|
108
|
+
* row behind the view (OK-67219).
|
|
109
|
+
*/
|
|
110
|
+
internal fun nativeListTouchableTextRegions(
|
|
111
|
+
lines: List<NativeListTextLine>,
|
|
112
|
+
slop: Float,
|
|
113
|
+
): List<NativeListTouchRegion> = lines.flatMap { line ->
|
|
114
|
+
line.touchable.map { run ->
|
|
115
|
+
var left = run.left - slop
|
|
116
|
+
var right = run.right + slop
|
|
117
|
+
for (other in line.plain) {
|
|
118
|
+
if (other.right <= run.left + TEXT_RUN_EDGE) left = maxOf(left, other.right)
|
|
119
|
+
if (other.left >= run.right - TEXT_RUN_EDGE) right = minOf(right, other.left)
|
|
120
|
+
}
|
|
121
|
+
NativeListTouchRegion(left, line.top, right, line.bottom)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
65
125
|
internal data class NativeListSlotFrame(val x: Int, val y: Int, val width: Int, val height: Int)
|
|
66
126
|
|
|
67
127
|
/** Side of one member of an Activity token pair inside a [frame]-unit box (24 in 40). */
|
|
@@ -43,6 +43,10 @@ internal data class NativeListResolvedText(
|
|
|
43
43
|
val gravity: Int,
|
|
44
44
|
val offset: Float,
|
|
45
45
|
) {
|
|
46
|
+
/** Offset into [text] for a UTF-16 offset into the [source] it was resolved from. */
|
|
47
|
+
fun displayOffset(sourceOffset: Long, source: String): Int =
|
|
48
|
+
nativeListDisplayOffset(source, sourceOffset, foldsLineBreaks = lines == 1 && explicitTruncation)
|
|
49
|
+
|
|
46
50
|
fun bind(view: NativeListTextView) {
|
|
47
51
|
view.includeFontPadding = false
|
|
48
52
|
view.fontFeatureSettings = "tnum"
|
|
@@ -119,7 +123,7 @@ internal data class NativeListResolvedText(
|
|
|
119
123
|
else -> Gravity.TOP
|
|
120
124
|
}
|
|
121
125
|
return NativeListResolvedText(
|
|
122
|
-
if (maxLines == 1 && truncation) text
|
|
126
|
+
if (maxLines == 1 && truncation) nativeListFoldLineBreaks(text) else text,
|
|
123
127
|
fontSize,
|
|
124
128
|
face,
|
|
125
129
|
style
|