Skip to content

Commit

Permalink
feat(sdds-acore/theme-builder): Json theme decoding was fixed. Unused…
Browse files Browse the repository at this point in the history
… attrs were removed from playground:theme-builder. Typography generator was changed to support textStyle and lineHeight attributes.
  • Loading branch information
malilex committed Mar 29, 2024
1 parent 5977746 commit 80ea125
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 35 deletions.
5 changes: 5 additions & 0 deletions playground/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ buildscript {
}

dependencies {
configurations.all {
resolutionStrategy {
force(libs.base.kotlin.serialization.json)
}
}
classpath(libs.base.gradle.android)
classpath(libs.base.gradle.kotlin)
classpath(libs.base.gradle.detekt)
Expand Down
6 changes: 0 additions & 6 deletions playground/theme-builder/src/main/res/values/attrs.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import com.sdds.plugin.themebuilder.internal.token.Theme
import com.sdds.plugin.themebuilder.internal.token.TypographyToken
import com.sdds.plugin.themebuilder.internal.utils.ResourceReferenceProvider
import com.sdds.plugin.themebuilder.internal.utils.unsafeLazy
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.decodeFromStream
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
Expand Down Expand Up @@ -112,10 +112,8 @@ abstract class GenerateThemeTask : DefaultTask() {
dimensGenerator.generate()
}

private fun decodeTheme(): Theme {
val readFile = themeFile.get().asFile.readText()
val theme = Serializer.instance.decodeFromString<Theme>(readFile)
logger.debug("decoded theme $theme")
return theme
}
private fun decodeTheme(): Theme =
themeFile.get().asFile.inputStream().use { stream ->
Serializer.instance.decodeFromStream<Theme>(stream)
}.also { logger.debug("decoded theme $it") }
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ internal class KtFileBuilder(
}

/**
* Добавляет kotlin свойство с именем [name], типом [typeName] и инициализатором [initializer]
* Добавляет kotlin свойство с именем [name], типом [typeName], инициализатором [initializer]
* и описанием (документацией) [description]
* @return [TypeSpec.Builder]
*/
fun TypeSpec.Builder.appendProperty(
Expand All @@ -50,7 +51,8 @@ internal class KtFileBuilder(
) = appendProperty(name, typeName.asTypeName(), initializer, description, this)

/**
* Добавляет kotlin свойство с именем [name], типом [typeName] и инициализатором [initializer]
* Добавляет kotlin свойство с именем [name], типом [typeName], инициализатором [initializer]
* и описанием (документацией) [description]
* @return [TypeSpec.Builder]
*/
fun TypeSpec.Builder.appendProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal open class XmlDocumentBuilder(private val tokenPrefix: String) {

private val rootContent: Element by unsafeLazy {
document.createElement("resources")
.apply { setAttribute("xmlns:tools", "http://schemas.android.com/tools") }
.also { document.appendChild(it) }
}

Expand All @@ -38,16 +39,18 @@ internal open class XmlDocumentBuilder(private val tokenPrefix: String) {
* @param value значение токена
* @param format формат xml элемента
* @param type тип xml элемента
* @param targetApi целевая версия
*/
fun appendElement(
elementName: ElementName,
tokenName: String,
value: String,
format: ElementFormat? = null,
type: ElementType? = null,
targetApi: TargetApi? = null,
usePrefix: Boolean = true,
) {
rootContent.appendElement(elementName, tokenName, value, format, type, usePrefix)
rootContent.appendElement(elementName, tokenName, value, format, type, targetApi, usePrefix)
}

/**
Expand All @@ -57,6 +60,7 @@ internal open class XmlDocumentBuilder(private val tokenPrefix: String) {
* @param value значение токена
* @param format формат xml элемента
* @param type тип xml элемента
* @param targetApi целевая версия
* @param usePrefix если true - добавляет префикс к имени токена
*/
fun Element.appendElement(
Expand All @@ -65,13 +69,15 @@ internal open class XmlDocumentBuilder(private val tokenPrefix: String) {
value: String,
format: ElementFormat? = null,
type: ElementType? = null,
targetApi: TargetApi? = null,
usePrefix: Boolean = true,
) {
val nameAttr = tokenName.withPrefixIfNeed(tokenPrefix.takeIf { usePrefix })
val element = document.createElement(elementName.value).apply {
setAttribute("name", nameAttr)
format?.let { setAttribute("format", it.value) }
type?.let { setAttribute("type", it.value) }
targetApi?.let { setAttribute("tools:targetApi", targetApi.value) }
textContent = value
}
appendChild(element)
Expand Down Expand Up @@ -147,4 +153,16 @@ internal open class XmlDocumentBuilder(private val tokenPrefix: String) {
enum class ElementType(val value: String) {
DIMEN("dimen"),
}

/**
* Версия api
* @param value строковое название
*/
enum class TargetApi(val value: String) {

/**
* Android SDK 28
*/
P("p"),
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.sdds.plugin.themebuilder.internal.factory

import com.sdds.plugin.themebuilder.internal.builder.KtFileBuilder
import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder

/**
* Фабрика для [XmlDocumentBuilder]
* Фабрика для [KtFileBuilder]
* @param packageName название пакета, куда будет сохранен файл
* @author Малышев Александр on 07.03.2024
*/
Expand All @@ -13,7 +12,7 @@ internal class KtFileBuilderFactory(
) {

/**
* Создает [XmlDocumentBuilder]
* Создает [KtFileBuilder]
*/
fun create(fileName: String): KtFileBuilder = KtFileBuilder(packageName, fileName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.sdds.plugin.themebuilder.ThemeBuilderTarget
import com.sdds.plugin.themebuilder.internal.builder.KtFileBuilder
import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder
import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder.ElementName
import com.sdds.plugin.themebuilder.internal.builder.XmlDocumentBuilder.TargetApi
import com.sdds.plugin.themebuilder.internal.dimens.DimenData
import com.sdds.plugin.themebuilder.internal.dimens.DimensAggregator
import com.sdds.plugin.themebuilder.internal.factory.KtFileBuilderFactory
Expand Down Expand Up @@ -142,7 +143,7 @@ internal class TypographyGenerator(
appendStyle(textAppearanceName) {
appendElement(ElementName.ITEM, "fontFamily", tokenValue.fontFamily, usePrefix = false)
appendElement(ElementName.ITEM, "fontWeight", tokenValue.fontWeight.toString(), usePrefix = false)
appendElement(ElementName.ITEM, "android:fontStyle", tokenValue.fontStyle, usePrefix = false)
appendElement(ElementName.ITEM, "android:textStyle", tokenValue.fontStyle, usePrefix = false)
appendElement(
ElementName.ITEM,
"android:letterSpacing",
Expand All @@ -159,6 +160,7 @@ internal class TypographyGenerator(
ElementName.ITEM,
"android:lineHeight",
resourceReferenceProvider.dimen(lineHeightDimen),
targetApi = TargetApi.P,
usePrefix = false,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<!--Прозрачная акцентная поверхность-->
<color name="thmbldr_dark_on_light_surface_transparent_accent">#FFFFFF1F</color>
</resources>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<!--region Акцентный sweep градиент поверхности-->
<color name="thmbldr_dark_inverse_surface_accent_color_0">#000</color>
<color name="thmbldr_dark_inverse_surface_accent_color_1">#fff</color>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<!--region Тень downHardL для android-->
<dimen name="thmbldr_shadow_down_hard_l_dx">0.0dp</dimen>
<dimen name="thmbldr_shadow_down_hard_l_dY">1.0dp</dimen>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Thmbldr.Shape"/>
<style name="Thmbldr.Shape.Round">
<item name="cornerFamily">rounded</item>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<dimen name="thmbldr_test_dimen">128.0dp</dimen>
</resources>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<!--шрифт android-->
<style name="Thmbldr.TextAppearance.DisplayL">
<item name="fontFamily">sans</item>
<item name="fontWeight">300</item>
<item name="android:fontStyle">normal</item>
<item name="android:textStyle">normal</item>
<item name="android:letterSpacing">0.02</item>
<item name="android:textSize">@dimen/thmbldr_screen_l_display_l_text_size</item>
<item name="android:lineHeight">@dimen/thmbldr_screen_l_display_l_line_height</item>
<item name="android:lineHeight" tools:targetApi="p">@dimen/thmbldr_screen_l_display_l_line_height</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Thmbldr.TextAppearance"/>
<!--шрифт android-->
<style name="Thmbldr.TextAppearance.DisplayL">
<item name="fontFamily">sans</item>
<item name="fontWeight">300</item>
<item name="android:fontStyle">normal</item>
<item name="android:textStyle">normal</item>
<item name="android:letterSpacing">0.02</item>
<item name="android:textSize">@dimen/thmbldr_screen_m_display_l_text_size</item>
<item name="android:lineHeight">@dimen/thmbldr_screen_m_display_l_line_height</item>
<item name="android:lineHeight" tools:targetApi="p">@dimen/thmbldr_screen_m_display_l_line_height</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<!--шрифт android-->
<style name="Thmbldr.TextAppearance.DisplayL">
<item name="fontFamily">sans</item>
<item name="fontWeight">300</item>
<item name="android:fontStyle">normal</item>
<item name="android:textStyle">normal</item>
<item name="android:letterSpacing">0.02</item>
<item name="android:textSize">@dimen/thmbldr_screen_s_display_l_text_size</item>
<item name="android:lineHeight">@dimen/thmbldr_screen_s_display_l_line_height</item>
<item name="android:lineHeight" tools:targetApi="p">@dimen/thmbldr_screen_s_display_l_line_height</item>
</style>
</resources>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Thmbldr.Typography"/>
<!--шрифт android-->
<style name="Thmbldr.Typography.DisplayL">
Expand Down

0 comments on commit 80ea125

Please sign in to comment.