Jump to:
[Introduction](#Introduction) |
[Usage](#Usage) |
[Samples](#Samples) |
[FAQ](#FAQ) |
[License](#License)
## Introduction
The entrypoint to Mosaic is the `runMosaic` function.
The lambda passed to this function is responsible for both output and performing work.
Output (for now) happens through the `setContent` function.
You can call `setContent` multiple times, but as you'll see you probably won't need to.
```kotlin
fun main() = runMosaic {
setContent {
Text("The count is: 0")
}
}
```
To change the output dynamically we can use local properties to hold state.
Let's update our counter to actually count to 20.
```kotlin
fun main() = runMosaic {
var count = 0
setContent {
Text("The count is: $count")
}
for (i in 1..20) {
delay(250)
count = i
}
}
```
**This will not work!** Our count stays at 0 for 5 seconds instead of incrementing until 20.
Instead, we have to use Compose's `State` objects to hold state.
```diff
-var count = 0
+var count by mutableStateOf(0)
```
Now, when the `count` value is updated, Compose will know that it needs to re-render the string.
```kotlin
fun main() = runMosaic {
var count by mutableStateOf(0)
setContent {
Text("The count is: $count")
}
for (i in 1..20) {
delay(250)
count = i
}
}
```
(Note: You may need to add imports for `androidx.compose.runtime.getValue` and `import androidx.compose.runtime.setValue` manually.)
## Usage
In order to use Mosaic you must write your code in Kotlin and must apply the Compose Kotlin
compiler plugin.
For Gradle users, the Mosaic Gradle plugin will take care of applying the compiler plugin.
```groovy
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.21'
classpath 'com.jakewharton.mosaic:mosaic-gradle-plugin:0.7.0'
}
}
apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'com.jakewharton.mosaic'
```
The runtime APIs will be made available automatically by applying the plugin.
Documentation is available at [jakewharton.github.io/mosaic/docs/0.x/](https://jakewharton.github.io/mosaic/docs/0.x/).
**Note**: Any module which contains a `@Composable`-annotated function or lambda must apply the
Mosaic plugin. While the runtime dependency will be available to downstream modules as a
transitive dependency, the compiler plugin is not inherited and must be applied to every module.
Since Kotlin compiler plugins are an unstable API, certain versions of Mosaic only work with
certain versions of Kotlin.
| Kotlin | Mosaic |
|--------|---------------|
| 1.8.21 | 0.7.0 |
| 1.8.20 | 0.6.0 |
| 1.8.10 | 0.5.0 |
| 1.8.0 | 0.3.0 - 0.4.0 |
| 1.7.10 | 0.2.0 |
| 1.5.10 | 0.1.0 |
Versions newer than those listed may be supported but are untested.
```groovy buildscript { repository { mavenCentral() maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies { classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.21' classpath 'com.jakewharton.mosaic:mosaic-gradle-plugin:0.8.0-SNAPSHOT' } } apply plugin: 'org.jetbrains.kotlin.jvm' apply plugin: 'com.jakewharton.mosaic' ``` Snapshot documentation is available at [jakewharton.github.io/mosaic/docs/latest/](https://jakewharton.github.io/mosaic/docs/latest/).