Skip to content

Commit

Permalink
Associate raw resource files with a doc ID (#1807)
Browse files Browse the repository at this point in the history
Fixes #1802. This lets multiple docs to load from raw resources, fixes
the design switcher, and allows live update to work even when loading
from raw resources.

---------

Co-authored-by: yiqunw700 <[email protected]>
  • Loading branch information
rylin8 and yiqunw700 authored Nov 21, 2024
1 parent ad2d77e commit e77b42b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
24 changes: 14 additions & 10 deletions designcompose/src/main/java/com/android/designcompose/DocServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.android.designcompose

import android.content.res.Resources
import android.os.Handler
import android.os.Looper
import android.util.Log
Expand Down Expand Up @@ -99,7 +98,7 @@ object DesignSettings {
internal var isDocumentLive = mutableStateOf(false)
private var fontDb: HashMap<String, FontFamily> = HashMap()
internal var fileFetchStatus: HashMap<DesignDocId, DesignDocStatus> = HashMap()
internal var rawResourceId: Int = Resources.ID_NULL
internal var rawResourceId: HashMap<DesignDocId, Int> = HashMap()

@VisibleForTesting
@RestrictTo(RestrictTo.Scope.TESTS)
Expand Down Expand Up @@ -158,15 +157,19 @@ object DesignSettings {
}

/**
* Sets the raw resource id for serialized doc. Once set, the design doc will only be read from
* res/raw/the_dcf_file. Live updates do not work when this is set. All other files will be
* ignored: downloaded/cached files, assets/figma/
* Sets the raw resource id for serialized doc associated with the designDocId. Once set, the
* design doc will be read from res/raw/the_dcf_file instead of assets.
*
* @param designDocId the Figma
* @param resourceId of the raw dcf file placed in res/raw
* @sample R.raw.the_dcf_file
*/
fun setRawResourceId(@RawRes resourceId: Int) {
rawResourceId = resourceId
fun setRawResourceId(designDocId: DesignDocId, @RawRes resourceId: Int) {
rawResourceId[designDocId] = resourceId
}

fun clearRawResources() {
rawResourceId.clear()
}

fun addFontFamily(name: String, family: FontFamily) {
Expand Down Expand Up @@ -533,14 +536,15 @@ internal fun DocServer.doc(

// Use the LocalContext to locate this doc in the precompiled DesignComposeDefinitionuments
try {
val rawResource = DesignSettings.rawResourceId[docId]
val assetDoc: InputStream =
if (DesignSettings.rawResourceId != Resources.ID_NULL) {
if (rawResource != null) {
Log.i(
TAG,
"Loaded design doc from R.raw." +
context.resources.getResourceEntryName(DesignSettings.rawResourceId),
context.resources.getResourceEntryName(rawResource),
)
context.resources.openRawResource(DesignSettings.rawResourceId)
context.resources.openRawResource(rawResource)
} else {
val fileName = "figma/$id.dcf"
Log.i(TAG, "Loaded design doc from assets/$fileName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.android.designcompose.TestUtils.ClearStateTestRule
import com.android.designcompose.annotation.Design
import com.android.designcompose.annotation.DesignComponent
import com.android.designcompose.annotation.DesignDoc
import com.android.designcompose.common.DesignDocId
import com.android.designcompose.test.R
import com.android.designcompose.test.assertRenderStatus
import com.android.designcompose.test.onDCDoc
Expand Down Expand Up @@ -58,7 +59,10 @@ class DesignRawResourceTest {
@Test
fun testHelloWorldDoc_setRawResourceId_passes() {
with(composeTestRule) {
DesignSettings.setRawResourceId(R.raw.raw_resource_test_hello_world_doc)
DesignSettings.setRawResourceId(
DesignDocId("pxVlixodJqZL95zo2RzTHl"),
R.raw.raw_resource_test_hello_world_doc,
)
composeTestRule.setContent { HelloWorld() }

onDCDoc(HelloWorldDoc).assertRenderStatus(DocRenderStatus.Rendered)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static void clearDocServer() {

private static void clearDesignSettings() {
DesignSettings.INSTANCE.testOnlyClearFileFetchStatus();
DesignSettings.INSTANCE.setRawResourceId(Resources.ID_NULL);
DesignSettings.INSTANCE.clearRawResources();
}


Expand Down

0 comments on commit e77b42b

Please sign in to comment.