Skip to content

Commit 98da6a7

Browse files
mfazekasclaude
andcommitted
fix(kotlin): fix all ktlint violations and add lint:fix:kotlin script
- Auto-fixed code style violations via ktlint --format - Disabled if-else-wrapping and condition-wrapping rules in .editorconfig (non-bug style opinions inconsistent with project's permissive approach) - Added scripts/lint-fix-kotlin.sh and yarn lint:fix:kotlin for auto-fixing Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c3a13f0 commit 98da6a7

9 files changed

Lines changed: 89 additions & 43 deletions

File tree

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ ktlint_standard_string-template-indent = disabled
3333
ktlint_standard_backing-property-naming = disabled
3434
ktlint_standard_no-consecutive-comments = disabled
3535
ktlint_standard_no-empty-first-line-in-class-body = disabled
36+
ktlint_standard_condition-wrapping = disabled
37+
ktlint_standard_if-else-wrapping = disabled
3638

3739
[nitrogen/generated/**/*.kt]
3840
ktlint = disabled

android/src/experimental/java/com/margelo/nitro/rive/ExperimentalAssetLoader.kt

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ object ExperimentalAssetLoader {
2525
try {
2626
val loader = source.createLoader()
2727
val data = loader.load(source)
28-
val type = inferAssetType(name, data)
28+
val type = inferAssetType(name, data, assetData.type)
2929
registerAsset(data, name, type, riveWorker)
3030
} catch (e: Exception) {
3131
Log.e(TAG, "Failed to load asset '$name'", e)
@@ -47,7 +47,7 @@ object ExperimentalAssetLoader {
4747
try {
4848
val loader = source.createLoader()
4949
val data = loader.load(source)
50-
val type = inferAssetType(name, data)
50+
val type = inferAssetType(name, data, assetData.type)
5151
registerAsset(data, name, type, riveWorker)
5252
} catch (e: Exception) {
5353
Log.e(TAG, "Failed to update asset '$name'", e)
@@ -91,7 +91,20 @@ object ExperimentalAssetLoader {
9191
}
9292
}
9393

94-
private fun inferAssetType(name: String, data: ByteArray): AssetType {
94+
private fun inferAssetType(name: String, data: ByteArray, explicitType: String?): AssetType {
95+
// Explicit type provided by the caller — always preferred.
96+
when (explicitType?.lowercase()) {
97+
"image" -> return AssetType.IMAGE
98+
"font" -> return AssetType.FONT
99+
"audio" -> return AssetType.AUDIO
100+
}
101+
// No explicit type — fall back to extension / magic-byte inference.
102+
// Deprecated: provide `type` on your asset entry to avoid this.
103+
Log.w(
104+
TAG,
105+
"No type provided for '$name'. Falling back to extension/magic-byte inference — " +
106+
"set type: 'image' | 'font' | 'audio' on the asset to silence this warning."
107+
)
95108
val ext = name.substringAfterLast('.', "").lowercase()
96109
return when (ext) {
97110
"png", "jpg", "jpeg", "webp", "gif", "bmp", "svg" -> AssetType.IMAGE
@@ -102,33 +115,26 @@ object ExperimentalAssetLoader {
102115
}
103116

104117
private fun inferFromMagicBytes(data: ByteArray): AssetType {
105-
if (data.size < 4) return AssetType.IMAGE
118+
fun ByteArray.startsWith(vararg bytes: Int) =
119+
bytes.size <= size && bytes.indices.all { this[it] == bytes[it].toByte() }
106120

107-
// PNG: 89 50 4E 47
108-
if (data[0] == 0x89.toByte() && data[1] == 0x50.toByte() &&
109-
data[2] == 0x4E.toByte() && data[3] == 0x47.toByte()) return AssetType.IMAGE
110-
// JPEG: FF D8 FF
111-
if (data[0] == 0xFF.toByte() && data[1] == 0xD8.toByte() &&
112-
data[2] == 0xFF.toByte()) return AssetType.IMAGE
113-
// RIFF container: WebP (RIFF....WEBP) or WAV (RIFF....WAVE)
114-
if (data[0] == 0x52.toByte() && data[1] == 0x49.toByte() &&
115-
data[2] == 0x46.toByte() && data[3] == 0x46.toByte()) {
116-
if (data.size >= 12 &&
117-
data[8] == 0x57.toByte() && data[9] == 0x41.toByte() &&
118-
data[10] == 0x56.toByte() && data[11] == 0x45.toByte()) return AssetType.AUDIO // "WAVE"
119-
return AssetType.IMAGE // assume WebP for other RIFF
120-
}
121-
// ID3 (MP3): 49 44 33
122-
if (data[0] == 0x49.toByte() && data[1] == 0x44.toByte() &&
123-
data[2] == 0x33.toByte()) return AssetType.AUDIO
124-
// TrueType: 00 01 00 00
125-
if (data[0] == 0x00.toByte() && data[1] == 0x01.toByte() &&
126-
data[2] == 0x00.toByte() && data[3] == 0x00.toByte()) return AssetType.FONT
127-
// OpenType: 4F 54 54 4F ("OTTO")
128-
if (data[0] == 0x4F.toByte() && data[1] == 0x54.toByte() &&
129-
data[2] == 0x54.toByte() && data[3] == 0x4F.toByte()) return AssetType.FONT
121+
fun ByteArray.matchesAt(offset: Int, vararg bytes: Int) =
122+
offset + bytes.size <= size && bytes.indices.all { this[offset + it] == bytes[it].toByte() }
130123

131-
return AssetType.IMAGE
124+
return when {
125+
data.startsWith(0x89, 0x50, 0x4E, 0x47) -> AssetType.IMAGE // PNG
126+
data.startsWith(0xFF, 0xD8, 0xFF) -> AssetType.IMAGE // JPEG
127+
data.startsWith(0x49, 0x44, 0x33) -> AssetType.AUDIO // MP3 (ID3)
128+
data.startsWith(0x00, 0x01, 0x00, 0x00) -> AssetType.FONT // TrueType
129+
data.startsWith(0x4F, 0x54, 0x54, 0x4F) -> AssetType.FONT // OpenType (OTTO)
130+
data.startsWith(0x52, 0x49, 0x46, 0x46) -> // RIFF container
131+
if (data.matchesAt(8, 0x57, 0x41, 0x56, 0x45)) {
132+
AssetType.AUDIO // → WAV (WAVE)
133+
} else {
134+
AssetType.IMAGE // → WebP
135+
}
136+
else -> AssetType.IMAGE
137+
}
132138
}
133139

134140
enum class AssetType { IMAGE, FONT, AUDIO }

android/src/experimental/java/com/margelo/nitro/rive/HybridRiveFile.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ class HybridRiveFile(
169169
val file = riveFile ?: return Promise.resolved(emptyArray())
170170
return Promise.async {
171171
val enums = file.getEnums()
172-
enums.map { enum ->
172+
enums
173+
.map { enum ->
173174
RiveEnumDefinition(
174175
name = enum.name,
175176
values = enum.values.toTypedArray()
@@ -206,21 +207,26 @@ class HybridRiveFile(
206207
try {
207208
// Find a string property to use as identifier for value comparison
208209
val testPropName = vmNames.firstNotNullOfOrNull { name ->
209-
file.getViewModelProperties(name)
210+
file
211+
.getViewModelProperties(name)
210212
.firstOrNull { it.type == PropertyDataType.STRING }
211213
?.name
212214
} ?: return vmNames.first()
213215

214216
val artboardValue = try {
215217
artboardVmi.getStringFlow(testPropName).first()
216-
} catch (_: Exception) { return vmNames.first() }
218+
} catch (_: Exception) {
219+
return vmNames.first()
220+
}
217221

218222
for (name in vmNames) {
219223
val namedVmi = ViewModelInstance.fromFile(file, ViewModelSource.Named(name).defaultInstance())
220224
try {
221225
val namedValue = try {
222226
namedVmi.getStringFlow(testPropName).first()
223-
} catch (_: Exception) { continue }
227+
} catch (_: Exception) {
228+
continue
229+
}
224230
if (namedValue == artboardValue) return name
225231
} finally {
226232
namedVmi.close()

android/src/experimental/java/com/margelo/nitro/rive/HybridViewModel.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.util.Log
44
import androidx.annotation.Keep
55
import app.rive.RiveFile
66
import app.rive.ViewModelInstance
7-
import app.rive.ViewModelInstanceSource
87
import app.rive.ViewModelSource
98
import app.rive.core.CommandQueue
109
import com.facebook.proguard.annotations.DoNotStrip

android/src/experimental/java/com/rive/RiveReactNativeView.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
8080
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
8181
surfaceTextureListener = object : TextureView.SurfaceTextureListener {
8282
override fun onSurfaceTextureAvailable(st: SurfaceTexture, w: Int, h: Int) {
83-
Log.d(TAG, "onSurfaceTextureAvailable: ${w}x${h} worker=${this@RiveReactNativeView.riveWorker != null}")
83+
Log.d(TAG, "onSurfaceTextureAvailable: ${w}x$h worker=${this@RiveReactNativeView.riveWorker != null}")
8484
this@RiveReactNativeView.surfaceTexture = st
8585
this@RiveReactNativeView.surfaceWidth = w
8686
this@RiveReactNativeView.surfaceHeight = h
@@ -114,8 +114,11 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
114114
override fun doFrame(frameTimeNanos: Long) {
115115
if (!renderLoopRunning) return
116116

117-
val deltaTime = if (lastFrameTimeNs == 0L) Duration.ZERO
118-
else (frameTimeNanos - lastFrameTimeNs).nanoseconds
117+
val deltaTime = if (lastFrameTimeNs == 0L) {
118+
Duration.ZERO
119+
} else {
120+
(frameTimeNanos - lastFrameTimeNs).nanoseconds
121+
}
119122
lastFrameTimeNs = frameTimeNanos
120123

121124
val worker = riveWorker
@@ -156,7 +159,10 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
156159
fun configure(config: ViewConfiguration, dataBindingChanged: Boolean, reload: Boolean = false, initialUpdate: Boolean = false) {
157160
riveWorker = config.riveWorker
158161
activeFit = config.fit
159-
Log.d(TAG, "configure: reload=$reload initialUpdate=$initialUpdate fit=$activeFit surfaceTexture=${surfaceTexture != null} surfaceW=${surfaceWidth} surfaceH=${surfaceHeight}")
162+
Log.d(
163+
TAG,
164+
"configure: reload=$reload initialUpdate=$initialUpdate fit=$activeFit surfaceTexture=${surfaceTexture != null} surfaceW=$surfaceWidth surfaceH=$surfaceHeight"
165+
)
160166

161167
if (reload) {
162168
RiveErrorLogger.resetReportedErrors()
@@ -215,11 +221,20 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
215221
}
216222

217223
private fun handlePointerEvent(event: MotionEvent) {
218-
val worker = riveWorker ?: run { Log.w(TAG, "touch: no worker"); return }
219-
val smHandle = stateMachineHandle ?: run { Log.w(TAG, "touch: no smHandle"); return }
224+
val worker = riveWorker ?: run {
225+
Log.w(TAG, "touch: no worker")
226+
return
227+
}
228+
val smHandle = stateMachineHandle ?: run {
229+
Log.w(TAG, "touch: no smHandle")
230+
return
231+
}
220232
val w = surfaceWidth.toFloat()
221233
val h = surfaceHeight.toFloat()
222-
if (w <= 0 || h <= 0) { Log.w(TAG, "touch: invalid surface ${w}x${h}"); return }
234+
if (w <= 0 || h <= 0) {
235+
Log.w(TAG, "touch: invalid surface ${w}x$h")
236+
return
237+
}
223238

224239
val fit = activeFit
225240

android/src/legacy/java/com/margelo/nitro/rive/HybridViewModelInstance.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.margelo.nitro.rive
22

3-
import android.util.Log
43
import androidx.annotation.Keep
54
import app.rive.runtime.kotlin.core.ViewModelInstance
65
import app.rive.runtime.kotlin.core.errors.ViewModelException

android/src/legacy/java/com/margelo/nitro/rive/HybridViewModelListProperty.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ class HybridViewModelListProperty(private val listProperty: ViewModelListPropert
3434
override fun getInstanceAtAsync(index: Double): Promise<HybridViewModelInstanceSpec?> {
3535
return Promise.async {
3636
val idx = index.toInt()
37-
if (idx < 0 || idx >= listProperty.size) null
38-
else HybridViewModelInstance(listProperty.elementAt(idx))
37+
if (idx < 0 || idx >= listProperty.size) {
38+
null
39+
} else {
40+
HybridViewModelInstance(listProperty.elementAt(idx))
41+
}
3942
}
4043
}
4144

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"copy:nitrogen-config": "mkdir -p lib/nitrogen/generated/shared/json && cp nitrogen/generated/shared/json/RiveViewConfig.json lib/nitrogen/generated/shared/json/",
4747
"lint:swift": "./scripts/lint-swift.sh",
4848
"lint:kotlin": "./scripts/lint-kotlin.sh",
49+
"lint:fix:kotlin": "./scripts/lint-fix-kotlin.sh",
4950
"lint:native": "yarn lint:swift && yarn lint:kotlin"
5051
},
5152
"keywords": [

scripts/lint-fix-kotlin.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
set -e
3+
4+
KTLINT_VERSION="1.5.0"
5+
KTLINT_DIR=".ktlint"
6+
KTLINT_BIN="$KTLINT_DIR/ktlint"
7+
8+
if [ ! -f "$KTLINT_BIN" ]; then
9+
echo "Downloading ktlint $KTLINT_VERSION..."
10+
mkdir -p "$KTLINT_DIR"
11+
curl -sSL "https://github.com/pinterest/ktlint/releases/download/${KTLINT_VERSION}/ktlint" -o "$KTLINT_BIN"
12+
chmod +x "$KTLINT_BIN"
13+
fi
14+
15+
"$KTLINT_BIN" --format "android/src/**/*.kt" --reporter=plain

0 commit comments

Comments
 (0)