From e7ee9e531962ec4831d24f7dd7c6aa89cbbdf6b7 Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Fri, 26 May 2023 16:48:31 +1000 Subject: [PATCH 1/3] Add extension property for core gallery v2 support This can be used as a fallback when the API response omits the galleryWithImageBlocks property. --- .../java/org/wordpress/android/fluxc/model/EditorTheme.kt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/model/EditorTheme.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/model/EditorTheme.kt index ba0d68a017..cd276cff73 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/model/EditorTheme.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/model/EditorTheme.kt @@ -11,8 +11,11 @@ import com.google.gson.reflect.TypeToken import org.wordpress.android.fluxc.persistence.EditorThemeElementType import org.wordpress.android.fluxc.persistence.EditorThemeSqlUtils.EditorThemeBuilder import org.wordpress.android.fluxc.persistence.EditorThemeSqlUtils.EditorThemeElementBuilder +import org.wordpress.android.util.VersionUtils import java.lang.reflect.Type +private const val GALLERY_V2_WP_VERSION = "5.9" + const val MAP_KEY_ELEMENT_DISPLAY_NAME: String = "name" const val MAP_KEY_ELEMENT_SLUG: String = "slug" const val MAP_KEY_ELEMENT_COLORS: String = "colors" @@ -182,3 +185,6 @@ class EditorThemeElementListSerializer : JsonDeserializer Date: Fri, 26 May 2023 16:50:42 +1000 Subject: [PATCH 2/3] Declare gallery v2 field as optional By doing this, Gson won't coerce the missing value to false when the property is omitted in the response, instead leaving it `null`. This allows us to fallback to a reasonable value based on the WP version. --- .../wordpress/android/fluxc/model/EditorTheme.kt | 13 ++++++------- .../fluxc/persistence/EditorThemeSqlUtils.kt | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/model/EditorTheme.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/model/EditorTheme.kt index cd276cff73..1d2c4c5fe5 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/model/EditorTheme.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/model/EditorTheme.kt @@ -49,15 +49,15 @@ data class EditorTheme( version = null ) - fun toBuilder(siteId: Int): EditorThemeBuilder { + fun toBuilder(site: SiteModel): EditorThemeBuilder { val element = EditorThemeBuilder() - element.localSiteId = siteId + element.localSiteId = site.id element.stylesheet = stylesheet element.version = version element.rawStyles = themeSupport.rawStyles element.rawFeatures = themeSupport.rawFeatures element.isBlockBasedTheme = themeSupport.isBlockBasedTheme - element.galleryWithImageBlocks = themeSupport.galleryWithImageBlocks + element.galleryWithImageBlocks = themeSupport.galleryWithImageBlocks ?: site.coreSupportsGalleryV2 element.quoteBlockV2 = themeSupport.quoteBlockV2 element.listBlockV2 = themeSupport.listBlockV2 element.hasBlockTemplates = themeSupport.hasBlockTemplates ?: false @@ -97,11 +97,11 @@ data class EditorThemeSupport( val rawStyles: String?, val rawFeatures: String?, val isBlockBasedTheme: Boolean, - val galleryWithImageBlocks: Boolean, + val galleryWithImageBlocks: Boolean?, val quoteBlockV2: Boolean, val listBlockV2: Boolean ) { - fun toBundle(): Bundle { + fun toBundle(site: SiteModel): Bundle { val bundle = Bundle() colors?.map { it.toBundle() }?.let { @@ -121,7 +121,7 @@ data class EditorThemeSupport( } bundle.putBoolean(MAP_KEY_IS_BLOCK_BASED_THEME, isBlockBasedTheme) - bundle.putBoolean(MAP_KEY_GALLERY_WITH_IMAGE_BLOCKS, galleryWithImageBlocks) + bundle.putBoolean(MAP_KEY_GALLERY_WITH_IMAGE_BLOCKS, galleryWithImageBlocks ?: site.coreSupportsGalleryV2) bundle.putBoolean(MAP_KEY_QUOTE_BLOCK_V2, quoteBlockV2) bundle.putBoolean(MAP_KEY_LIST_BLOCK_V2, listBlockV2) bundle.putBoolean(MAP_KEY_HAS_BLOCK_TEMPLATES, hasBlockTemplates ?: false) @@ -131,7 +131,6 @@ data class EditorThemeSupport( fun isEditorThemeBlockBased(): Boolean = isBlockBasedTheme || (hasBlockTemplates ?: false) } - data class EditorThemeElement( val name: String?, val slug: String?, diff --git a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt index 9e7ee7975d..5b9100cd0b 100644 --- a/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt +++ b/fluxc/src/main/java/org/wordpress/android/fluxc/persistence/EditorThemeSqlUtils.kt @@ -65,7 +65,7 @@ class EditorThemeSqlUtils { } private fun makeEditorTheme(site: SiteModel, editorTheme: EditorTheme) { - val editorThemeBuilder = editorTheme.toBuilder(site.id) + val editorThemeBuilder = editorTheme.toBuilder(site) val items = (editorTheme.themeSupport.colors ?: emptyList()) + (editorTheme.themeSupport.gradients ?: emptyList()) From 3a14c028ab13e6802f282f4dd4e140a10db80685 Mon Sep 17 00:00:00 2001 From: Matthew Kevins Date: Fri, 26 May 2023 16:52:39 +1000 Subject: [PATCH 3/3] Fix tests --- .../fluxc/mocked/MockedStack_EditorThemeStoreTest.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_EditorThemeStoreTest.kt b/example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_EditorThemeStoreTest.kt index e9d0c50c9a..bc765bf45e 100644 --- a/example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_EditorThemeStoreTest.kt +++ b/example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_EditorThemeStoreTest.kt @@ -63,7 +63,7 @@ class MockedStack_EditorThemeStoreTest : MockedStack_Base() { assertNotEmpty(cachedTheme) // Validate Bundle - val themeBundle = editorTheme!!.themeSupport.toBundle() + val themeBundle = editorTheme!!.themeSupport.toBundle(site) assertNotEmpty(themeBundle) } @@ -83,7 +83,7 @@ class MockedStack_EditorThemeStoreTest : MockedStack_Base() { assertEmpty(cachedTheme) // Validate Bundle - val themeBundle = editorTheme!!.themeSupport.toBundle() + val themeBundle = editorTheme!!.themeSupport.toBundle(site) assertEmpty(themeBundle) } @@ -135,7 +135,7 @@ class MockedStack_EditorThemeStoreTest : MockedStack_Base() { assertEmpty(cachedTheme) // Validate Bundle - val themeBundle = editorTheme!!.themeSupport.toBundle() + val themeBundle = editorTheme!!.themeSupport.toBundle(site) assertEmpty(themeBundle) } @@ -155,7 +155,7 @@ class MockedStack_EditorThemeStoreTest : MockedStack_Base() { assertNotEmpty(cachedTheme) // Validate Bundle - val themeBundle = editorTheme!!.themeSupport.toBundle() + val themeBundle = editorTheme!!.themeSupport.toBundle(site) assertNotEmpty(themeBundle) } @@ -177,7 +177,7 @@ class MockedStack_EditorThemeStoreTest : MockedStack_Base() { Assert.assertNotNull(cachedTheme?.themeSupport?.rawStyles) // Validate Bundle - val themeBundle = editorTheme!!.themeSupport.toBundle() + val themeBundle = editorTheme!!.themeSupport.toBundle(site) assertEmpty(themeBundle) val styles = themeBundle.getString("rawStyles") val features = themeBundle.getString("rawFeatures")