Skip to content

Commit 8d18f9b

Browse files
umberto-sonninoumberto-sonnino
umberto-sonnino
andcommitted
Asset types in Kotlin
- As discussed, expose a way to determine the AssetType in Kotlin - Cleans up a couple of things highlighted by @dragostis previously Diffs= 69396dd2b Asset types for Images and Fonts in Kotlin (#7039) Co-authored-by: Umberto Sonnino <[email protected]>
1 parent 3398b4c commit 8d18f9b

30 files changed

+551
-110
lines changed

Diff for: .rive_head

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2828b7b013811f1f4b0b0e2ee0f6d9b23c73878b
1+
69396dd2bb3931cefee324201db34a366e3baefa

Diff for: app/src/main/java/app/rive/runtime/example/AssetLoaderFragment.kt

+92-40
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import app.rive.runtime.kotlin.RiveAnimationView
1515
import app.rive.runtime.kotlin.core.BytesRequest
1616
import app.rive.runtime.kotlin.core.FileAsset
1717
import app.rive.runtime.kotlin.core.FileAssetLoader
18+
import app.rive.runtime.kotlin.core.FontAsset
19+
import app.rive.runtime.kotlin.core.ImageAsset
1820
import app.rive.runtime.kotlin.core.Loop
21+
import app.rive.runtime.kotlin.core.RiveFont
22+
import app.rive.runtime.kotlin.core.RiveRenderImage
1923
import com.android.volley.RequestQueue
2024
import com.android.volley.toolbox.Volley
2125
import kotlin.random.Random
@@ -33,6 +37,21 @@ private fun makeContainer(context: Context): FrameLayout {
3337
}
3438
}
3539

40+
private fun makeButton(
41+
context: Context,
42+
label: String,
43+
clickListener: View.OnClickListener
44+
): Button {
45+
return Button(context).apply {
46+
layoutParams = ViewGroup.LayoutParams(
47+
ViewGroup.LayoutParams.WRAP_CONTENT,
48+
ViewGroup.LayoutParams.WRAP_CONTENT,
49+
)
50+
text = label
51+
setOnClickListener(clickListener)
52+
}
53+
}
54+
3655
class AssetLoaderFragment : Fragment() {
3756
private var _binding: FragmentAssetLoaderBinding? = null
3857
private val binding get() = _binding!!
@@ -76,36 +95,28 @@ class AssetButtonFragment : Fragment() {
7695

7796
private val loremImage = "https://picsum.photos"
7897
private lateinit var queue: RequestQueue
79-
private lateinit var assetStore: AssetStore
80-
private val minSize = 150
81-
private val maxSize = 500
98+
private lateinit var assetStore: ImageAssetStore
99+
private val minSize = 750
100+
private val maxSize = 1000
82101
private val maxId = 1084
83-
private var lastAssetChanged = 0
84102

85-
private fun makeButton(context: Context): Button {
86-
return Button(context).apply {
87-
layoutParams = ViewGroup.LayoutParams(
88-
ViewGroup.LayoutParams.WRAP_CONTENT,
89-
ViewGroup.LayoutParams.WRAP_CONTENT,
103+
private fun randomImageButton(context: Context): Button {
104+
return makeButton(context, "Change Image") {
105+
val randomWidth = Random.nextInt(minSize, maxSize)
106+
val randomHeight = Random.nextInt(minSize, maxSize)
107+
val imgId = Random.nextInt(maxId)
108+
val url = "$loremImage/id/$imgId/$randomWidth/$randomHeight"
109+
val request = BytesRequest(
110+
url,
111+
{ bytes ->
112+
assetStore.nextAsset.image = RiveRenderImage.make(bytes)
113+
},
114+
{
115+
Log.e("Request", "onAssetLoaded: failed to load $url.")
116+
it.printStackTrace()
117+
}
90118
)
91-
text = "Change Image"
92-
setOnClickListener {
93-
val randomAsset =
94-
assetStore.assetList[lastAssetChanged++ % assetStore.assetList.size]
95-
val randomWidth = Random.nextInt(minSize, maxSize)
96-
val randomHeight = Random.nextInt(minSize, maxSize)
97-
val imgId = Random.nextInt(maxId)
98-
val url = "$loremImage/id/$imgId/$randomWidth/$randomHeight"
99-
val request = BytesRequest(
100-
url,
101-
{ bytes -> randomAsset.decode(bytes) },
102-
{
103-
Log.e("Request", "onAssetLoaded: failed to load $url.")
104-
it.printStackTrace()
105-
}
106-
)
107-
queue.add(request)
108-
}
119+
queue.add(request)
109120
}
110121
}
111122

@@ -119,39 +130,73 @@ class AssetButtonFragment : Fragment() {
119130
val ctx = view.context
120131
queue = Volley.newRequestQueue(ctx)
121132

122-
assetStore = AssetStore()
133+
assetStore = ImageAssetStore()
123134
val riveView = RiveAnimationView.Builder(ctx)
124135
.setAssetLoader(assetStore)
125-
.setResource(R.raw.walle)
136+
.setResource(R.raw.asset_load_check)
126137
.build()
127138

128139
makeContainer(ctx).let {
129140
it.addView(riveView)
130141
binding.fragmentButtonContainer.addView(it)
131142
}
132143

133-
makeButton(ctx).let {
144+
randomImageButton(ctx).let {
134145
binding.fragmentButtonContainer.addView(it)
135146
}
136147

137148

138149
return view
139150
}
140151

141-
private class AssetStore : FileAssetLoader() {
142-
val assetList = mutableListOf<FileAsset>()
152+
private class ImageAssetStore : FileAssetLoader() {
153+
private var lastAssetChanged = 0
154+
val assetList = mutableListOf<ImageAsset>()
155+
156+
val nextAsset: ImageAsset
157+
get() = assetList[lastAssetChanged++ % assetList.size]
158+
143159
override fun loadContents(asset: FileAsset, inBandBytes: ByteArray): Boolean {
144-
return assetList.add(asset)
160+
if (asset is ImageAsset) {
161+
return assetList.add(asset)
162+
}
163+
return false
145164
}
146165
}
147166
}
148167

149168
class FontAssetFragment : Fragment() {
150169
private var _binding: FragmentAssetButtonBinding? = null
170+
private lateinit var queue: RequestQueue
151171
private val binding get() = _binding!!
152172

153173
private lateinit var fontDecoder: RandomFontDecoder
154174

175+
private val fontUrls = listOf(
176+
"https://cdn.rive.app/runtime/flutter/IndieFlower-Regular.ttf",
177+
"https://cdn.rive.app/runtime/flutter/comic-neue.ttf",
178+
"https://cdn.rive.app/runtime/flutter/inter.ttf",
179+
"https://cdn.rive.app/runtime/flutter/inter-tight.ttf",
180+
"https://cdn.rive.app/runtime/flutter/josefin-sans.ttf",
181+
"https://cdn.rive.app/runtime/flutter/send-flowers.ttf",
182+
)
183+
184+
185+
private fun randomFontButton(context: Context): Button {
186+
return makeButton(context, "Change Font") {
187+
val url = fontUrls.random()
188+
val request = BytesRequest(
189+
url,
190+
{ bytes -> fontDecoder.fontAsset.font = RiveFont.make(bytes) },
191+
{
192+
Log.e("Request", "onAssetLoaded: failed to load $url.")
193+
it.printStackTrace()
194+
}
195+
)
196+
queue.add(request)
197+
}
198+
}
199+
155200
override fun onCreateView(
156201
inflater: LayoutInflater, container: ViewGroup?,
157202
savedInstanceState: Bundle?
@@ -160,6 +205,7 @@ class FontAssetFragment : Fragment() {
160205
_binding = FragmentAssetButtonBinding.inflate(inflater, container, false)
161206
val view = binding.root
162207
val ctx = view.context
208+
queue = Volley.newRequestQueue(ctx)
163209

164210
fontDecoder = RandomFontDecoder(ctx)
165211
val riveView = RiveAnimationView.Builder(ctx)
@@ -173,20 +219,26 @@ class FontAssetFragment : Fragment() {
173219
it.addView(riveView)
174220
binding.fragmentButtonContainer.addView(it)
175221
}
222+
randomFontButton(ctx).let {
223+
binding.fragmentButtonContainer.addView(it)
224+
}
176225

177226
return view
178227
}
179228

180229
private class RandomFontDecoder(private val context: Context) : FileAssetLoader() {
181-
lateinit var fontAsset: FileAsset
230+
lateinit var fontAsset: FontAsset
182231
override fun loadContents(asset: FileAsset, inBandBytes: ByteArray): Boolean {
183-
// Store a reference to the asset.
184-
fontAsset = asset
185-
186-
// Load the original font
187-
context.resources.openRawResource(R.raw.roboto).use {
188-
return asset.decode(it.readBytes())
232+
if (asset is FontAsset) {
233+
// Store a reference to the asset.
234+
fontAsset = asset
235+
236+
// Load the original font
237+
context.resources.openRawResource(R.raw.roboto).use {
238+
return asset.decode(it.readBytes())
239+
}
189240
}
241+
return false
190242
}
191243
}
192244
}

Diff for: app/src/main/res/layout/fragment_asset_button.xml

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
android:id="@+id/fragmentButtonContainer"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
android:background="#57A5E0"
87
android:gravity="center"
98
android:orientation="vertical"
109
android:padding="16dp"

Diff for: app/src/main/res/layout/fragment_asset_loader.xml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
android:id="@+id/fragmentLoaderContainer"
66
android:layout_width="wrap_content"
77
android:layout_height="wrap_content"
8-
android:background="#57A5E0"
98
android:gravity="center"
109
android:orientation="vertical"
1110
android:padding="16dp"

Diff for: app/src/main/res/raw/asset_load_check.riv

190 KB
Binary file not shown.

Diff for: build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ext {
1111
}
1212
buildscript {
1313
ext {
14-
gradle_version = '8.3.1'
14+
gradle_version = '8.3.2'
1515
kotlin_version = '1.9.22'
1616
dokkaVersion = "1.4.30"
1717
compose_version = '1.6.2'

Diff for: gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ org.gradle.jvmargs=-Xmx2048m
1616
# https://developer.android.com/topic/libraries/support-library/androidx-rn
1717
android.useAndroidX=true
1818
# Automatically convert third-party libraries to use AndroidX
19-
android.enableJetifier=true
19+
android.enableJetifier=false
2020
# Kotlin code style for this project: "official" or "obsolete":
2121
kotlin.code.style=official

Diff for: kotlin/build.gradle

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99

1010
defaultConfig {
1111
minSdkVersion 21
12-
targetSdkVersion 33
12+
targetSdkVersion 34
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
consumerProguardFiles "consumer-rules.pro"
@@ -38,7 +38,7 @@ android {
3838
dokkaSourceSets {
3939
named("main") {
4040
noAndroidSdkLink.set(false)
41-
outputDirectory.set(buildDir.resolve("dokka"))
41+
outputDirectory.set(layout.buildDirectory.resolve("dokka"))
4242
reportUndocumented.set(true)
4343
}
4444
}
@@ -64,6 +64,7 @@ dependencies {
6464

6565
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
6666
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
67+
implementation "org.jetbrains.kotlin:kotlin-reflect:1.9.22"
6768
}
6869

6970
// Commenting this out, this only really helps when you're doing local dev, but it means our git actions need abd!

0 commit comments

Comments
 (0)