Skip to content

Commit

Permalink
Use existing image parameter infrastructure instead of additional fol…
Browse files Browse the repository at this point in the history
…der.
  • Loading branch information
Sven Obser committed Jul 15, 2023
1 parent ec69d31 commit 23aa788
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 44 deletions.
25 changes: 10 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ Multiplatform:
│ ├── images
│ │ ├── vector_image.svg
│ │ └── raster_image.png
│ ├── images-night
│ │ ├── vector_image.svg
│ │ └── raster_image.png
│ └── strings
│ ├── strings_en.xml
│ └── strings_ru.xml
Expand All @@ -77,9 +74,6 @@ Android or JVM:
│ ├── images
│ │ ├── vector_image.svg
│ │ └── raster_image.png
│ ├── images-night
│ │ ├── vector_image.svg
│ │ └── raster_image.png
│ └── strings
│ ├── strings_en.xml
│ └── strings_ru.xml
Expand Down Expand Up @@ -147,17 +141,27 @@ Recommended for multicolor images.
> - For iOS images are generated from 1x to 3x (where 1x is 1:1 in pixels to the specified size)
> - For JVM and JS a single image of the specified size is generated.
> **night**
>
> Night/Dark Mode images are supported for Android and iOS by adding the `(night)` modifier. The filename and type of
> the image must match the corresponding day/light version without the `(night)` modifier.
>
> - For Android this creates night images in `drawable-night-nodpi`.
> - For iOS this creates a `"appearances" : [ { "appearance" : "luminosity", "value" : "dark" } ]` entry in the `imageset`.
Filename examples:
```
some_hd_image_(100).jpg
app_logo_(orig).svg
my_colorful_bitmap_(orig)_(150).png
image_with_night_support_(night).png
```
Kotlin:
```kotlin
MainRes.image.some_hd_image
MainRes.image.app_logo
MainRes.image.my_colorful_bitmap
MainRes.image.image_with_night_support
```
Swift:
```swift
Expand All @@ -177,15 +181,6 @@ Sample:
Image size in Figma is **240x89**. Final image name is **pic_(orig)_(240).png**
</details>

#### Night/Dark Mode Images
Night/Dark Mode images are supported for Android and iOS by placing a night version of the image into the
`libres/images-night/` folder.

The filename and type of the image must match the corresponding day/light version in `libres/images/`.

- For Android this creates night images in `drawable-night-nodpi`.
- For iOS this creates a `"appearances" : [ { "appearance" : "luminosity", "value" : "dark" } ]` entry in the `imageset`.

## Jetpack Compose

```kotlin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import io.github.skeptick.libres.plugin.images.declarations.ImagesObjectFile
import io.github.skeptick.libres.plugin.images.models.ImageProps
import io.github.skeptick.libres.plugin.images.models.ImageSet
import io.github.skeptick.libres.plugin.images.processing.removeImage
import io.github.skeptick.libres.plugin.images.processing.saveImageSet
import io.github.skeptick.libres.plugin.images.processing.saveImage
import io.github.skeptick.libres.plugin.images.processing.saveImageSet
import org.gradle.api.DefaultTask
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.*
Expand All @@ -29,11 +29,6 @@ abstract class LibresImagesGenerationTask : DefaultTask() {
@get:PathSensitive(PathSensitivity.RELATIVE)
internal abstract var inputDirectory: FileCollection

@get:Incremental
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
internal abstract var nightInputDirectory: FileCollection

@get:OutputDirectories
internal abstract var outputSourcesDirectories: Map<KotlinPlatform, File>

Expand All @@ -43,10 +38,7 @@ abstract class LibresImagesGenerationTask : DefaultTask() {
@TaskAction
fun apply(inputChanges: InputChanges) {
// Update images for changed files
sequenceOf(
inputChanges.getFileChanges(inputDirectory),
inputChanges.getFileChanges(nightInputDirectory),
).flatten()
inputChanges.getFileChanges(inputDirectory)
.forEach { change ->
val image = ImageProps(change.file)

Expand All @@ -57,11 +49,8 @@ abstract class LibresImagesGenerationTask : DefaultTask() {
}
}

// Generate image catalog
sequenceOf(
inputDirectory.files,
nightInputDirectory.files,
).flatten()
// Generate image sets
inputDirectory.files
.map(::ImageProps)
.groupBy(ImageProps::name)
.map { (name, files) -> ImageSet(name, files) }
Expand All @@ -70,13 +59,10 @@ abstract class LibresImagesGenerationTask : DefaultTask() {
}

// Generate code
sequenceOf(
inputDirectory.files,
nightInputDirectory.files,
).flatten().toSet()
inputDirectory.files
.takeIf { files -> files.isNotEmpty() }
?.distinctBy(File::nameWithoutExtension)
?.map(::ImageProps)
?.distinctBy(ImageProps::name)
?.let(::buildImages)
?: buildEmptyImages()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ class ResourcesPlugin : Plugin<Project> {
private fun Project.registerGeneratorsTasks() {
val stringsInputDirectory = File(inputDirectory, "strings")
val imagesInputDirectory = File(inputDirectory, "images")
val nightImagesInputDirectory = File(inputDirectory, "images-night")
val stringsOutputPackageName = listOfNotNull(outputPackageName, "strings").joinToString(".")
val imagesOutputPackageName = listOfNotNull(outputPackageName, "images").joinToString(".")

Expand Down Expand Up @@ -152,11 +151,6 @@ class ResourcesPlugin : Plugin<Project> {
!element.isDirectory && element.file.extension.lowercase() in IMAGES_EXTENSIONS
}
}
task.nightInputDirectory = fileTree(nightImagesInputDirectory) { config ->
config.include { element ->
!element.isDirectory && element.file.extension.lowercase() in IMAGES_EXTENSIONS
}
}
task.outputSourcesDirectories = allSourceSets.associateBy(PluginSourceSet::platform) {
it.sourcesDir.toOutputDirectory(imagesOutputPackageName)
}
Expand Down Expand Up @@ -254,4 +248,4 @@ class ResourcesPlugin : Plugin<Project> {
File(absolutePath, packageName.replace('.', File.separatorChar))

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ internal class ImageProps(val file: File) {
val isNightMode: Boolean

init {
val directoryName = file.parentFile.name
val nameWithoutExtension = file.nameWithoutExtension
val parameters = ParametersRegex.findAll(nameWithoutExtension).toList()

this.name = nameWithoutExtension.substringBefore("_(").lowercase()
this.extension = file.extension.lowercase()
this.targetSize = if (!isVector) parameters.firstNotNullOfOrNull { it.groupValues[1].toIntOrNull() } else null
this.isTintable = parameters.none { it.groupValues[1].startsWith("orig") }
this.isNightMode = directoryName.endsWith("-night")
this.isNightMode = parameters.any { it.groupValues[1] == "night" }
}

companion object {
Expand Down

0 comments on commit 23aa788

Please sign in to comment.