|
|
|
* Deklarative Definition
|
|
|
|
* baut ein Modell auf für die Views
|
|
|
|
|
|
|
|
## Kotlin Grundlagen für Jetpack Compose
|
|
|
|
|
|
|
|
*(Receiver) Lambdas*
|
|
|
|
```kotlin
|
|
|
|
val receiverLambda: Number.() -> Unit = {
|
|
|
|
sqrt(toFloat())
|
|
|
|
}
|
|
|
|
|
|
|
|
val number = 2
|
|
|
|
number.receiverLambda()
|
|
|
|
```
|
|
|
|
|
|
|
|
*Higher Order Functions*
|
|
|
|
```kotlin
|
|
|
|
fun myFunction(param: (Int) -> Int) = param(3)
|
|
|
|
|
|
|
|
fun square(input: Int): Int = input.toDouble().pow(2.0).toInt()
|
|
|
|
|
|
|
|
println(myFunction {
|
|
|
|
it.toDouble().pow(2.0).toInt()
|
|
|
|
})
|
|
|
|
|
|
|
|
println(myFunction(::square))
|
|
|
|
```
|
|
|
|
|
|
|
|
*Extension Functions*
|
|
|
|
```kotlin
|
|
|
|
fun String?.parseDate() = if (this == null) {
|
|
|
|
Date()
|
|
|
|
} else {
|
|
|
|
SimpleDateFormat("yyyy-MM-dd")
|
|
|
|
.parse(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
val String?.date: Date?
|
|
|
|
get() = if (this == null) {
|
|
|
|
Date()
|
|
|
|
} else {
|
|
|
|
SimpleDateFormat("yyyy-MM-dd")
|
|
|
|
.parse(this)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Compose Grundlagen
|
|
|
|
|
|
|
|
*Gradle Setup*
|
|
|
|
|
|
|
|
```groovy
|
|
|
|
android {
|
|
|
|
buildFeatures {
|
|
|
|
compose true
|
|
|
|
}
|
|
|
|
composeOptions {
|
|
|
|
kotlinCompilerExtensionVersion = "1.3.2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dependencies {
|
|
|
|
|
|
|
|
implementation 'androidx.core:core-ktx:1.9.0'
|
|
|
|
implementation 'androidx.appcompat:appcompat:1.6.0'
|
|
|
|
def composeBom = platform('androidx.compose:compose-bom:2022.12.00')
|
|
|
|
implementation composeBom
|
|
|
|
|
|
|
|
// or Material Design 2
|
|
|
|
// implementation 'androidx.compose.material3:material3'
|
|
|
|
implementation 'androidx.compose.ui:ui'
|
|
|
|
|
|
|
|
// Android Studio Preview support
|
|
|
|
implementation 'androidx.compose.ui:ui-tooling-preview'
|
|
|
|
debugImplementation 'androidx.compose.ui:ui-tooling'
|
|
|
|
|
|
|
|
// Optional - Included automatically by material, only add when you need
|
|
|
|
// the icons but not the material library (e.g. when using Material3 or a
|
|
|
|
// custom design system based on Foundation)
|
|
|
|
implementation 'androidx.compose.material:material-icons-core'
|
|
|
|
// Optional - Add full set of material icons
|
|
|
|
implementation 'androidx.compose.material:material-icons-extended'
|
|
|
|
|
|
|
|
// Optional - Integration with activities
|
|
|
|
implementation 'androidx.activity:activity-compose:1.6.1'
|
|
|
|
// Optional - Integration with ViewModels
|
|
|
|
implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1'
|
|
|
|
// Optional - Integration with LiveData
|
|
|
|
// implementation 'androidx.compose.runtime:runtime-livedata'
|
|
|
|
|
|
|
|
|
|
|
|
// testImplementation 'junit:junit:4.13.2'
|
|
|
|
// androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
|
|
|
// androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
|
|
|
// androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
|
|
|
|
// debugImplementation 'androidx.compose.ui:ui-test-manifest'
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
* kotlin Version und Compose Compiler müssen zusammen passen! ( Bsp: kotlin 1.7.20 <-> Compose 1.3.2)
|
|
|
|
* Später nähere Erklärung der Material Bibliotheken.
|
|
|
|
|
|
|
|
*Compose in einer Activity*
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
class SampleActivity : AppCompatActivity() {
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
|
|
|
setContent {
|
|
|
|
/* Compose Content */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
*Definition eines Composables*
|
|
|
|
|
|
|
|
```kotlin
|
|
|
|
@Composable
|
|
|
|
fun MyComposable() {
|
|
|
|
/* Compose Content */
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
* Kann nur innerhalb von Composable Code aufgerufen werden. |