Skip to content

Commit

Permalink
fix class visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-chung committed Nov 15, 2024
1 parent 6ab52a7 commit 62f2556
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import com.sunnychung.application.multiplatform.hellohttp.ux.viewmodel.ResponseV
import com.sunnychung.application.multiplatform.hellohttp.ux.viewmodel.UserPreferenceViewModel
import java.io.File

internal class AppContext {
class AppContext {
val MetadataManager = MetadataManager()
val SingleInstanceProcessService = SingleInstanceProcessService()
val NetworkClientManager = NetworkClientManager()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.sunnychung.application.multiplatform.hellohttp.manager

import com.sunnychung.application.hello_http.generated.resources.Res
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.compose.resources.ExperimentalResourceApi
import java.util.concurrent.ConcurrentHashMap

/**
* Why this class exists? Why can't I just use `Res.font.xxx`?
*
* Response: |
* Why the generated `Res` class does not contain the resources placed correctly inside the composeResources dir?
* I have spent a half day to investigate by changing Kotlin versions, Gradle versions, dependencies, source sets,
* and looking at release notes and tickets of Compose Multiplatform. Finally, I found that `Res.readBytes(path)`
* can actually find the resources but not the Compose Gradle plugin. Why???
* And why somebody else does not encounter the issue? Why my older project (ComposableTable's demo) works? Why I
* need to provide a 100% reproducible code to report an issue?
* I don't want to dig further to help JetBrains debugging their buggy products. Jetpack Compose is the source of
* frustrations and full of bugs. Not to mention how buggy their TextField and Compose Test are.
* But most of Kotlin developers think they are amazing. The Compose Test random blocking bugs I reported was
* finally reported by someone else after 9 months and got attentions. Looks like I am the only one to try out
* their new features and suffer. I have to document this frustration as a record for myself.
*
*/
class ResourceManager {

private val cache = ConcurrentHashMap<String, ByteArray>()

@OptIn(ExperimentalResourceApi::class)
suspend fun loadAllResources() {
val resources = listOf(AppRes.Font.PitagonSansMonoRegular, AppRes.Font.PitagonSansMonoBold)
withContext(Dispatchers.IO) {
resources.forEach {
launch {
cache[it.key] = Res.readBytes(it.path)
// cache[it.key] = javaClass.classLoader.getResourceAsStream(it.path).use { it.readBytes() }
}
}
}
}

fun getResource(resource: AppRes.Resource) = cache[resource.key]!!
}

object AppRes {

object Font {
val PitagonSansMonoRegular = Resource("font:PitagonSansMonoRegular", "font/pitagon_sans_mono/PitagonSansMono-Regular.ttf")
val PitagonSansMonoBold = Resource("font:PitagonSansMonoBold", "font/pitagon_sans_mono/PitagonSansMono-Bold.ttf")
}

data class Resource(val key: String, val path: String)
}

0 comments on commit 62f2556

Please sign in to comment.