Skip to content

Commit

Permalink
Add ribbon to monochrome icons
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Aug 6, 2023
1 parent d4e9992 commit db9cf3d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ abstract class EasyLauncherTask @Inject constructor(

private fun IconFile.Adaptive.processAdaptiveIcon() {
resourceDirectories.get().forEach { resDir ->
val icons = objects.getIconFiles(parent = resDir, iconName = foreground)
icons.forEach { iconFile ->
val foregroundFiles = objects.getIconFiles(parent = resDir, iconName = foreground)
val monochromeFiles = monochrome?.let { objects.getIconFiles(parent = resDir, iconName = it) } ?: emptyList()
val iconFiles = (foregroundFiles + monochromeFiles).toSet()

iconFiles.forEach { iconFile ->
val outputFile = iconFile.getOutputFile()
if (iconFile.extension == "xml") {
iconFile.transformXml(outputFile, minSdkVersion.get(), filters.get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ internal fun File.tryParseXmlFile(): IconFile? {
val iconXml = XmlSlurper().parse(this)
val foreground = iconXml.getProperty("foreground") as GPathResult
val background = iconXml.getProperty("background") as GPathResult
val monochrome = iconXml.getProperty("monochrome") as GPathResult
val backgroundDrawable = background.property("@android:drawable")
val foregroundDrawable = foreground.property("@android:drawable")
val monochromeDrawable = monochrome.property("@android:drawable")

return if (backgroundDrawable != null && foregroundDrawable != null) {
IconFile.Adaptive(
file = this,
background = backgroundDrawable,
foreground = foregroundDrawable,
monochrome = monochromeDrawable,
)
} else {
IconFile.XmlDrawableResource(file = this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ 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 Adaptive(
val file: File,
val background: String,
val foreground: String,
val monochrome: String?,
) : IconFile()

data class XmlDrawableResource(val file: File) : IconFile()
}
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,29 @@ internal class XmlReaderTest {
assertThat(icon.file.path).isEqualTo(adaptiveIcon.path)
}

@Test
fun `parses adaptive icon with monochrome option`() {
val adaptiveIcon = tempDir.resolve("ic_launcher.xml")
adaptiveIcon.writeText(
"""
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@mipmap/ic_launcher_foreground" />
<monochrome android:drawable="@mipmap/ic_launcher_foreground_monochrome" />
</adaptive-icon>
""".trimIndent(),
)

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.monochrome).isEqualTo("@mipmap/ic_launcher_foreground_monochrome")
assertThat(icon.file.path).isEqualTo(adaptiveIcon.path)
}

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

0 comments on commit db9cf3d

Please sign in to comment.