Skip to content

Commit abbecea

Browse files
authored
Merge pull request #15 from icerockdev/develop
release 0.6.0
2 parents d918d2f + a1d843b commit abbecea

28 files changed

Lines changed: 354 additions & 110 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: KMP library compilation check
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- master
7+
8+
jobs:
9+
build:
10+
runs-on: macOS-latest
11+
12+
steps:
13+
- uses: actions/checkout@v1
14+
- name: Set up JDK 1.8
15+
uses: actions/setup-java@v1
16+
with:
17+
java-version: 1.8
18+
- name: Build and publish local
19+
run: ./gradlew -PlibraryPublish :gradle-plugin:publishPluginPublicationToMavenLocal :resources:publishToMavenLocal

.github/workflows/publish.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: KMP library publish
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
build:
9+
runs-on: macOS-latest
10+
11+
steps:
12+
- uses: actions/checkout@v1
13+
- name: Set up JDK 1.8
14+
uses: actions/setup-java@v1
15+
with:
16+
java-version: 1.8
17+
- name: Build and publish to Bintray
18+
run: ./gradlew -PlibraryPublish :gradle-plugin:publishPluginPublicationToBintrayRepository :resources:publishAllPublicationsToBintrayRepository -DBINTRAY_USER=${{ secrets.BINTRAY_USER }} -DBINTRAY_KEY=${{ secrets.BINTRAY_KEY }}

MultiPlatformLibraryResources.podspec

Lines changed: 0 additions & 20 deletions
This file was deleted.

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
![moko-resources](img/logo.png)
2-
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0) [![Download](https://api.bintray.com/packages/icerockdev/moko/moko-resources/images/download.svg) ](https://bintray.com/icerockdev/moko/moko-resources/_latestVersion) ![kotlin-version](https://img.shields.io/badge/kotlin-1.3.60-orange)
2+
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0) [![Download](https://api.bintray.com/packages/icerockdev/moko/moko-resources/images/download.svg) ](https://bintray.com/icerockdev/moko/moko-resources/_latestVersion) ![kotlin-version](https://img.shields.io/badge/kotlin-1.3.61-orange)
33

44
# Mobile Kotlin resources
55
This is a Kotlin MultiPlatform library that provides access to the resources on iOS & Android with the support of the default system localization.
@@ -16,7 +16,7 @@ This is a Kotlin MultiPlatform library that provides access to the resources on
1616
- [License](#license)
1717

1818
## Features
19-
- **Strings, Plurals, Drawables** to access the corresponding resources from common code;
19+
- **Strings, Plurals, Images** to access the corresponding resources from common code;
2020
- **StringDesc** for lifecycle-aware access to resources and unified localization on both platforms.
2121

2222
## Requirements
@@ -32,6 +32,8 @@ This is a Kotlin MultiPlatform library that provides access to the resources on
3232
- 0.4.0
3333
- kotlin 1.3.60
3434
- 0.5.0
35+
- kotlin 1.3.61
36+
- 0.6.0
3537

3638
## Installation
3739
root build.gradle
@@ -42,7 +44,7 @@ buildscript {
4244
}
4345
4446
dependencies {
45-
classpath "dev.icerock.moko:resources-generator:0.5.0"
47+
classpath "dev.icerock.moko:resources-generator:0.6.0"
4648
}
4749
}
4850
@@ -59,7 +61,7 @@ project build.gradle
5961
apply plugin: "dev.icerock.mobile.multiplatform-resources"
6062
6163
dependencies {
62-
commonMainApi("dev.icerock.moko:resources:0.5.0")
64+
commonMainApi("dev.icerock.moko:resources:0.6.0")
6365
}
6466
6567
multiplatformResources {
@@ -218,6 +220,25 @@ let string1 = getUserName(user: user).localized() // we got name from User model
218220
let string2 = getUserName(user: null).localized() // we got name_placeholder from resources
219221
```
220222

223+
### Example 6 - pass image
224+
Image resources directory is `commonMain/resources/MR/images` with support of nested directories.
225+
Image name should be end with one of:
226+
- `@0.75x` - android ldpi;
227+
- `@1x` - android mdpi, ios 1x;
228+
- `@1.5x` - android hdpi;
229+
- `@2x` - android xhdpi, ios 2x;
230+
- `@3x` - android xxhdpi, ios 3x;
231+
- `@4x` - android xxxhdpi.
232+
Supported `png` and `jpg` resources for now.
233+
234+
If we add to `commonMain/resources/MR/images` files:
235+
- `home_black_18@1x.png`
236+
- `home_black_18@2x.png`
237+
238+
We got autogenerated `MR.images.home_black_18` `ImageResource` in code, that we can use:
239+
- Android: `imageView.setImageResource(image.drawableResId)`
240+
- iOS: `imageView.image = image.toUIImage()`
241+
221242
## Samples
222243
Please see more examples in the [sample directory](sample).
223244

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ object Versions {
99
const val minSdk = 16
1010
}
1111

12-
const val kotlin = "1.3.60"
12+
const val kotlin = "1.3.61"
1313

14-
private const val mokoResources = "0.5.0"
14+
private const val mokoResources = "0.6.0"
1515

1616
object Plugins {
1717
const val android = "3.5.2"

gradle-plugin/src/main/kotlin/dev/icerock/gradle/MultiplatformResourcesPlugin.kt

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import dev.icerock.gradle.generator.AndroidMRGenerator
99
import dev.icerock.gradle.generator.CommonMRGenerator
1010
import dev.icerock.gradle.generator.IosMRGenerator
1111
import dev.icerock.gradle.generator.MRGenerator
12+
import dev.icerock.gradle.generator.image.AndroidImagesGenerator
13+
import dev.icerock.gradle.generator.image.CommonImagesGenerator
14+
import dev.icerock.gradle.generator.image.IosImagesGenerator
1215
import dev.icerock.gradle.generator.plurals.AndroidPluralsGenerator
1316
import dev.icerock.gradle.generator.plurals.CommonPluralsGenerator
1417
import dev.icerock.gradle.generator.plurals.IosPluralsGenerator
@@ -60,6 +63,9 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
6063
val plurals = commonResources.matching {
6164
include("MR/**/plurals.xml")
6265
}
66+
val images = commonResources.matching {
67+
include("MR/images/**/*.png", "MR/images/**/*.jpg")
68+
}
6369

6470
val androidExtension = target.extensions.getByType(LibraryExtension::class)
6571
val mainAndroidSet = androidExtension.sourceSets.getByName("main")
@@ -71,6 +77,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
7177
project = target,
7278
stringsFileTree = strings,
7379
pluralsFileTree = plurals,
80+
imagesFileTree = images,
7481
sourceSets = sourceSets.filter { it.name.endsWith("Main") },
7582
extension = mrExtension,
7683
multiplatformExtension = multiplatformExtension,
@@ -94,13 +101,13 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
94101
project: Project,
95102
stringsFileTree: FileTree,
96103
pluralsFileTree: FileTree,
104+
imagesFileTree: FileTree,
97105
sourceSets: List<KotlinSourceSet>,
98106
extension: MultiplatformResourcesPluginExtension,
99107
multiplatformExtension: KotlinMultiplatformExtension,
100108
androidPackage: String
101109
) {
102110
val generatedDir = File(project.buildDir, "generated/moko")
103-
generatedDir.deleteRecursively()
104111

105112
sourceSets.forEach { sourceSet ->
106113
val generator = createGenerator(
@@ -109,6 +116,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
109116
sourceSet = sourceSet,
110117
stringsFileTree = stringsFileTree,
111118
pluralsFileTree = pluralsFileTree,
119+
imagesFileTree = imagesFileTree,
112120
mrClassPackage = extension.multiplatformResourcesPackage!!,
113121
androidRClassPackage = androidPackage
114122
) ?: return@forEach
@@ -123,6 +131,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
123131
sourceSet: KotlinSourceSet,
124132
stringsFileTree: FileTree,
125133
pluralsFileTree: FileTree,
134+
imagesFileTree: FileTree,
126135
mrClassPackage: String,
127136
androidRClassPackage: String
128137
): MRGenerator? {
@@ -132,6 +141,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
132141
sourceSet = sourceSet,
133142
stringsFileTree = stringsFileTree,
134143
pluralsFileTree = pluralsFileTree,
144+
imagesFileTree = imagesFileTree,
135145
mrClassPackage = mrClassPackage
136146
)
137147
}
@@ -148,6 +158,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
148158
sourceSet = sourceSet,
149159
stringsFileTree = stringsFileTree,
150160
pluralsFileTree = pluralsFileTree,
161+
imagesFileTree = imagesFileTree,
151162
mrClassPackage = mrClassPackage,
152163
androidRClassPackage = androidRClassPackage
153164
)
@@ -160,6 +171,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
160171
sourceSet = sourceSet,
161172
stringsFileTree = stringsFileTree,
162173
pluralsFileTree = pluralsFileTree,
174+
imagesFileTree = imagesFileTree,
163175
mrClassPackage = mrClassPackage
164176
)
165177
} else {
@@ -178,8 +190,9 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
178190
generatedDir: File,
179191
sourceSet: KotlinSourceSet,
180192
stringsFileTree: FileTree,
181-
mrClassPackage: String,
182-
pluralsFileTree: FileTree
193+
imagesFileTree: FileTree,
194+
pluralsFileTree: FileTree,
195+
mrClassPackage: String
183196
): MRGenerator {
184197
return CommonMRGenerator(
185198
generatedDir = generatedDir,
@@ -193,6 +206,10 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
193206
CommonPluralsGenerator(
194207
sourceSet = sourceSet,
195208
pluralsFileTree = pluralsFileTree
209+
),
210+
CommonImagesGenerator(
211+
sourceSet = sourceSet,
212+
inputFileTree = imagesFileTree
196213
)
197214
)
198215
)
@@ -202,9 +219,10 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
202219
generatedDir: File,
203220
sourceSet: KotlinSourceSet,
204221
stringsFileTree: FileTree,
222+
pluralsFileTree: FileTree,
223+
imagesFileTree: FileTree,
205224
mrClassPackage: String,
206-
androidRClassPackage: String,
207-
pluralsFileTree: FileTree
225+
androidRClassPackage: String
208226
): MRGenerator {
209227
return AndroidMRGenerator(
210228
generatedDir = generatedDir,
@@ -220,6 +238,11 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
220238
sourceSet = sourceSet,
221239
pluralsFileTree = pluralsFileTree,
222240
androidRClassPackage = androidRClassPackage
241+
),
242+
AndroidImagesGenerator(
243+
sourceSet = sourceSet,
244+
inputFileTree = imagesFileTree,
245+
androidRClassPackage = androidRClassPackage
223246
)
224247
)
225248
)
@@ -229,8 +252,9 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
229252
generatedDir: File,
230253
sourceSet: KotlinSourceSet,
231254
stringsFileTree: FileTree,
232-
mrClassPackage: String,
233-
pluralsFileTree: FileTree
255+
pluralsFileTree: FileTree,
256+
imagesFileTree: FileTree,
257+
mrClassPackage: String
234258
): MRGenerator {
235259
return IosMRGenerator(
236260
generatedDir = generatedDir,
@@ -244,6 +268,10 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
244268
IosPluralsGenerator(
245269
sourceSet = sourceSet,
246270
pluralsFileTree = pluralsFileTree
271+
),
272+
IosImagesGenerator(
273+
sourceSet = sourceSet,
274+
inputFileTree = imagesFileTree
247275
)
248276
)
249277
)

gradle-plugin/src/main/kotlin/dev/icerock/gradle/generator/MRGenerator.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ abstract class MRGenerator(
3131
}
3232

3333
private fun generate() {
34+
sourcesGenerationDir.deleteRecursively()
35+
resourcesGenerationDir.deleteRecursively()
36+
3437
val mrClassSpec = TypeSpec.objectBuilder(mrClassName)
3538
.addModifiers(*getMRClassModifiers())
3639

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.icerock.gradle.generator.image
6+
7+
import com.squareup.kotlinpoet.ClassName
8+
import com.squareup.kotlinpoet.CodeBlock
9+
import com.squareup.kotlinpoet.KModifier
10+
import org.gradle.api.file.FileTree
11+
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
12+
import java.io.File
13+
14+
class AndroidImagesGenerator(
15+
sourceSet: KotlinSourceSet,
16+
inputFileTree: FileTree,
17+
private val androidRClassPackage: String
18+
) : ImagesGenerator(
19+
sourceSet = sourceSet,
20+
inputFileTree = inputFileTree
21+
) {
22+
override fun getClassModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)
23+
24+
override fun getPropertyModifiers(): Array<KModifier> = arrayOf(KModifier.ACTUAL)
25+
26+
override fun getPropertyInitializer(key: String): CodeBlock? {
27+
return CodeBlock.of("ImageResource(R.drawable.%L)", key)
28+
}
29+
30+
override fun getImports(): List<ClassName> = listOf(
31+
ClassName(androidRClassPackage, "R")
32+
)
33+
34+
override fun generateResources(
35+
resourcesGenerationDir: File,
36+
keyFileMap: Map<String, List<File>>
37+
) {
38+
keyFileMap.flatMap { (key, files) ->
39+
files.map { key to it }
40+
}.forEach { (key, file) ->
41+
val scale = file.nameWithoutExtension.substringAfter("@").substringBefore("x")
42+
val drawableDirName = "drawable-" + when (scale) {
43+
"0.75" -> "ldpi"
44+
"1" -> "mdpi"
45+
"1.5" -> "hdpi"
46+
"2" -> "xhdpi"
47+
"3" -> "xxhdpi"
48+
"4" -> "xxxhdpi"
49+
else -> {
50+
println("ignore $file - unknown scale ($scale)")
51+
return@forEach
52+
}
53+
}
54+
55+
val drawableDir = File(resourcesGenerationDir, drawableDirName)
56+
file.copyTo(File(drawableDir, "$key.${file.extension}"))
57+
}
58+
}
59+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2019 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.icerock.gradle.generator.image
6+
7+
import com.squareup.kotlinpoet.CodeBlock
8+
import com.squareup.kotlinpoet.KModifier
9+
import org.gradle.api.file.FileTree
10+
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
11+
12+
class CommonImagesGenerator(
13+
sourceSet: KotlinSourceSet,
14+
inputFileTree: FileTree
15+
) : ImagesGenerator(
16+
sourceSet = sourceSet,
17+
inputFileTree = inputFileTree
18+
) {
19+
override fun getClassModifiers(): Array<KModifier> = emptyArray()
20+
21+
override fun getPropertyModifiers(): Array<KModifier> = emptyArray()
22+
23+
override fun getPropertyInitializer(key: String): CodeBlock? = null
24+
}

0 commit comments

Comments
 (0)