From 2c22d9140bde535f9c2f8aabf5ab8f1900868c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Fri, 20 Oct 2023 14:44:58 +0200 Subject: [PATCH 1/2] Convert UI to Jetpack Compose --- androidApp/build.gradle.kts | 12 +++ .../jetbrains/kmm/androidApp/MainActivity.kt | 90 ++++++++++++------- .../src/main/res/layout/activity_main.xml | 59 ------------ settings.gradle.kts | 2 + 4 files changed, 73 insertions(+), 90 deletions(-) delete mode 100644 androidApp/src/main/res/layout/activity_main.xml diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 7853800..515004c 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -25,6 +25,12 @@ android { jvmTarget = "1.8" } namespace = "com.jetbrains.androidApp" + buildFeatures { + compose = true + } + composeOptions { + kotlinCompilerExtensionVersion = "1.5.4-dev-k1.9.20-RC-1edce5fd625" + } } dependencies { @@ -32,4 +38,10 @@ dependencies { implementation("com.google.android.material:material:1.10.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("androidx.constraintlayout:constraintlayout:2.1.4") + + implementation(platform("androidx.compose:compose-bom:2023.10.00")) + implementation("androidx.activity:activity-compose") + implementation("androidx.compose.ui:ui") + implementation("androidx.compose.ui:ui-graphics") + implementation("androidx.compose.material3:material3") } \ No newline at end of file diff --git a/androidApp/src/main/java/com/jetbrains/kmm/androidApp/MainActivity.kt b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/MainActivity.kt index b8621cb..e51972d 100644 --- a/androidApp/src/main/java/com/jetbrains/kmm/androidApp/MainActivity.kt +++ b/androidApp/src/main/java/com/jetbrains/kmm/androidApp/MainActivity.kt @@ -1,50 +1,78 @@ package com.jetbrains.kmm.androidApp -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle -import android.text.Editable -import android.text.TextWatcher -import android.widget.EditText -import com.jetbrains.kmm.shared.Greeting +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.text.KeyboardOptions +import androidx.compose.material3.Text +import androidx.compose.material3.TextField +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.text.input.KeyboardType +import androidx.compose.ui.unit.dp import com.jetbrains.kmm.shared.Calculator -import android.widget.TextView -import com.jetbrains.androidApp.R +import com.jetbrains.kmm.shared.Greeting fun greet(): String { return Greeting().greeting() } -class MainActivity : AppCompatActivity() { +class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) + setContent { + Box( + modifier = Modifier.fillMaxSize(), + contentAlignment = Alignment.Center + ) { + Column( + horizontalAlignment = Alignment.Start, + ) { + Text(greet(), Modifier.padding(8.dp)) - val tv: TextView = findViewById(R.id.textView) - tv.text = greet() + var firstNumber by rememberSaveable { mutableStateOf("") } + var secondNumber by rememberSaveable { mutableStateOf("") } - val numATV: EditText = findViewById(R.id.editTextNumberDecimalA) - val numBTV: EditText = findViewById(R.id.editTextNumberDecimalB) + Row(verticalAlignment = Alignment.CenterVertically) { + TextField( + value = firstNumber, + onValueChange = { firstNumber = it }, + modifier = Modifier.width(100.dp), + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), + ) + Text(text = "+", modifier = Modifier.padding(4.dp)) + TextField( + value = secondNumber, + onValueChange = { secondNumber = it }, + modifier = Modifier.width(100.dp), + keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number), + ) - val sumTV: TextView = findViewById(R.id.textViewSum) - - val textWatcher = object: TextWatcher { - override fun afterTextChanged(s: Editable?) { - try { - val numA = Integer.parseInt(numATV.text.toString()) - val numB = Integer.parseInt(numBTV.text.toString()) - sumTV.text = "= " + Calculator.sum(numA, numB).toString() - } catch(e: NumberFormatException) { - sumTV.text = "= 🤔" + val first = firstNumber.toIntOrNull() + val second = secondNumber.toIntOrNull() + Text( + text = if (first != null && second != null) { + "= ${Calculator.sum(first, second)}" + } else { + "= \uD83E\uDD14" + }, + modifier = Modifier + .width(100.dp) + .padding(4.dp) + ) + } } } - - override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} - - override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {} } - - numATV.addTextChangedListener(textWatcher) - numBTV.addTextChangedListener(textWatcher) - } } diff --git a/androidApp/src/main/res/layout/activity_main.xml b/androidApp/src/main/res/layout/activity_main.xml deleted file mode 100644 index 4c4af60..0000000 --- a/androidApp/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,59 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index 8eed67d..8e8a6f3 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -3,6 +3,7 @@ pluginManagement { google() gradlePluginPortal() mavenCentral() + maven("https://androidx.dev/storage/compose-compiler/repository/") } } @@ -10,6 +11,7 @@ dependencyResolutionManagement { repositories { google() mavenCentral() + maven("https://androidx.dev/storage/compose-compiler/repository/") } } From 391ba8dd478dfc5c51935b8aec063d1ed1e97fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Wed, 24 Jan 2024 14:59:27 +0100 Subject: [PATCH 2/2] Update Kotlin and Compose to stable versions --- androidApp/build.gradle.kts | 2 +- build.gradle.kts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 515004c..c96c735 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -29,7 +29,7 @@ android { compose = true } composeOptions { - kotlinCompilerExtensionVersion = "1.5.4-dev-k1.9.20-RC-1edce5fd625" + kotlinCompilerExtensionVersion = "1.5.8" } } diff --git a/build.gradle.kts b/build.gradle.kts index 279bb84..38d4322 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,6 +2,6 @@ plugins { //trick: for the same plugin versions in all sub-modules id("com.android.application").version("8.1.2").apply(false) id("com.android.library").version("8.1.2").apply(false) - kotlin("android").version("1.9.20-RC").apply(false) - kotlin("multiplatform").version("1.9.20-RC").apply(false) + kotlin("android").version("1.9.22").apply(false) + kotlin("multiplatform").version("1.9.22").apply(false) } \ No newline at end of file