Skip to content

Commit

Permalink
Merge pull request #460 from usefulness/updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski authored Jun 3, 2023
2 parents 577091f + d6a0025 commit 13487dc
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.io.Serializable

interface EasyLauncherFilter : Serializable {

fun apply(canvas: Canvas, modifier: Modifier? = null)
fun apply(canvas: Canvas, modifier: Modifier?)

enum class Modifier {
Adaptive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ abstract class EasyLauncherTask @Inject constructor(
modifier = EasyLauncherFilter.Modifier.Round,
)

is IconFile.Adaptive -> processIcon(adaptiveIcon = iconFile)
is IconFile.Adaptive -> iconFile.processAdaptiveIcon()
is IconFile.XmlDrawableResource -> iconFile.processDrawable()
}
}
}
Expand All @@ -78,15 +79,15 @@ abstract class EasyLauncherTask @Inject constructor(
.associate { (key, value) -> key to value }

private fun getIcons(iconNames: Map<String, IconType>): List<IconFile> {
log.info { "will process icons: ${iconNames.values.joinToString()}" }
log.info { "will process icons: ${iconNames.keys.joinToString()}" }

return resourceDirectories.get()
.filter { it.exists() }
.flatMap { resDir ->
iconNames.flatMap { (iconName, iconType) ->
objects.getIconFiles(parent = resDir, iconName = iconName)
.map { iconFile ->
iconFile.asAdaptiveIcon() ?: when (iconType) {
iconFile.tryParseXmlFile() ?: when (iconType) {
IconType.Default -> IconFile.Raster(iconFile)
IconType.Round -> IconFile.RasterRound(iconFile)
}
Expand All @@ -95,9 +96,9 @@ abstract class EasyLauncherTask @Inject constructor(
}
}

private fun processIcon(adaptiveIcon: IconFile.Adaptive) {
private fun IconFile.Adaptive.processAdaptiveIcon() {
resourceDirectories.get().forEach { resDir ->
val icons = objects.getIconFiles(parent = resDir, iconName = adaptiveIcon.foreground)
val icons = objects.getIconFiles(parent = resDir, iconName = foreground)
icons.forEach { iconFile ->
val outputFile = iconFile.getOutputFile()
if (iconFile.extension == "xml") {
Expand All @@ -113,5 +114,10 @@ abstract class EasyLauncherTask @Inject constructor(
}
}

private fun IconFile.XmlDrawableResource.processDrawable() {
val outputFile = file.getOutputFile()
file.transformXml(outputFile, minSdkVersion.get(), filters.get())
}

private fun File.getOutputFile(): File = File(outputDir.asFile.get(), "${parentFile.name}/$name")
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ internal fun File.transformXml(outputFile: File, minSdkVersion: Int, filters: Li

resourceName
}
.joinToString(separator = "\n") {
.joinToString(separator = "\n\n") {
"""
| <item android:drawable="@${outputFile.parentFile.normalizedName}/$it" />
|
""".trimMargin()
}
val versionSuffix = if (minSdkVersion >= ANDROID_OREO) "" else "-v26"
Expand All @@ -65,7 +64,7 @@ internal fun File.transformXml(outputFile: File, minSdkVersion: Int, filters: Li
|<?xml version="1.0" encoding="utf-8"?>
|<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
| <item android:drawable="@drawable/easy_$nameWithoutExtension" />
| <item android:drawable="@${drawableRoot.normalizedName}/easy_$nameWithoutExtension" />
|
|$layers
|</layer-list>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private val regex by lazy { "\\\$\\{([^{}]*)}".toRegex() }
private fun String.applyPlaceholders(manifestPlaceholders: Map<String, Any>): String =
replace(regex) { manifestPlaceholders[it.groups[1]?.value]?.toString() ?: it.value }

internal fun File.asAdaptiveIcon(): IconFile.Adaptive? {
internal fun File.tryParseXmlFile(): IconFile? {
if (extension != "xml") {
return null
}
Expand All @@ -41,7 +41,7 @@ internal fun File.asAdaptiveIcon(): IconFile.Adaptive? {
foreground = foregroundDrawable,
)
} else {
null
IconFile.XmlDrawableResource(file = this)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ internal sealed class IconFile {
data class RasterRound(val file: File) : IconFile()

data class Adaptive(val file: File, val background: String, val foreground: String) : IconFile()

data class XmlDrawableResource(val file: File) : IconFile()
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ internal class IconTransformerTest {
| <item android:drawable="@drawable/chromelikefilter_2_output" />
|
| <item android:drawable="@drawable/overlayfilter_3_output" />
|
|</layer-list>
|
""".trimMargin(),
Expand Down Expand Up @@ -91,7 +90,6 @@ internal class IconTransformerTest {
| <item android:drawable="@drawable/chromelikefilter_2_output" />
|
| <item android:drawable="@drawable/overlayfilter_3_output" />
|
|</layer-list>
|
""".trimMargin(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.project.starter.easylauncher.plugin

import com.project.starter.easylauncher.plugin.models.IconFile
import com.project.starter.easylauncher.plugin.models.IconType
import com.project.starter.easylauncher.plugin.utils.vectorFile
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
Expand Down Expand Up @@ -210,11 +212,21 @@ internal class XmlReaderTest {
""".trimIndent(),
)

val icon = adaptiveIcon.asAdaptiveIcon()
val icon = adaptiveIcon.tryParseXmlFile() as IconFile.Adaptive

assertThat(icon?.background).isEqualTo("@drawable/ic_launcher_background")
assertThat(icon?.foreground).isEqualTo("@mipmap/ic_launcher_foreground")
assertThat(icon?.file?.path).isEqualTo(adaptiveIcon.path)
assertThat(icon.background).isEqualTo("@drawable/ic_launcher_background")
assertThat(icon.foreground).isEqualTo("@mipmap/ic_launcher_foreground")
assertThat(icon.file.path).isEqualTo(adaptiveIcon.path)
}

@Test
fun `parses drawable resource`() {
val drawableResource = tempDir.resolve("ic_launcher.xml")
drawableResource.writeText(vectorFile())

val icon = drawableResource.tryParseXmlFile()

assertThat(icon).isEqualTo(IconFile.XmlDrawableResource(file = drawableResource))
}

@Test
Expand All @@ -231,7 +243,7 @@ internal class XmlReaderTest {
""".trimIndent(),
)

val icon = adaptiveIcon.asAdaptiveIcon()
val icon = adaptiveIcon.tryParseXmlFile()

assertThat(icon).isNull()
}
Expand Down
9 changes: 8 additions & 1 deletion sample/example-vector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ android {
register("usingResValues") {
dimension "reportedBugs"
}
register("customDrawable") {
dimension "reportedBugs"
}
}
}

Expand Down Expand Up @@ -136,8 +139,12 @@ easylauncher {
chromeLike(label: "aaa"),
overlayFilter(file("launcherOverlay/beta.png"))
}
usingResValues {
register("usingResValues") {
filters chromeLike(label: "res-values", ribbonColor: "#6600CC", labelColor: "#FFFFFF", position: "bottom")
}
register("customDrawable") {
iconNames = ["@mipmap/ic_launcher"]
filters chromeLike(label: "HEART")
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0"
>
<path
android:fillColor="#FF0000"
android:pathData="M12,21.35l-1.45,-1.32C5.4,15.36 2,12.28 2,8.5 2,5.42 4.42,3 7.5,3c1.74,0 3.41,0.81 4.5,2.09C13.09,3.81 14.76,3 16.5,3 19.58,3 22,5.42 22,8.5c0,3.78 -3.4,6.86 -8.55,11.54L12,21.35z"
/>
</vector>

0 comments on commit 13487dc

Please sign in to comment.