Skip to content

Commit 97face9

Browse files
authored
Merge pull request #77 from zsmb13/compose
Convert UI to Jetpack Compose
2 parents f6bfd2d + 391ba8d commit 97face9

File tree

5 files changed

+75
-92
lines changed

5 files changed

+75
-92
lines changed

androidApp/build.gradle.kts

+12
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,23 @@ android {
2525
jvmTarget = "1.8"
2626
}
2727
namespace = "com.jetbrains.androidApp"
28+
buildFeatures {
29+
compose = true
30+
}
31+
composeOptions {
32+
kotlinCompilerExtensionVersion = "1.5.8"
33+
}
2834
}
2935

3036
dependencies {
3137
implementation(project(":shared"))
3238
implementation("com.google.android.material:material:1.10.0")
3339
implementation("androidx.appcompat:appcompat:1.6.1")
3440
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
41+
42+
implementation(platform("androidx.compose:compose-bom:2023.10.00"))
43+
implementation("androidx.activity:activity-compose")
44+
implementation("androidx.compose.ui:ui")
45+
implementation("androidx.compose.ui:ui-graphics")
46+
implementation("androidx.compose.material3:material3")
3547
}
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,78 @@
11
package com.jetbrains.kmm.androidApp
22

3-
import androidx.appcompat.app.AppCompatActivity
43
import android.os.Bundle
5-
import android.text.Editable
6-
import android.text.TextWatcher
7-
import android.widget.EditText
8-
import com.jetbrains.kmm.shared.Greeting
4+
import androidx.activity.ComponentActivity
5+
import androidx.activity.compose.setContent
6+
import androidx.compose.foundation.layout.Box
7+
import androidx.compose.foundation.layout.Column
8+
import androidx.compose.foundation.layout.Row
9+
import androidx.compose.foundation.layout.fillMaxSize
10+
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.foundation.layout.width
12+
import androidx.compose.foundation.text.KeyboardOptions
13+
import androidx.compose.material3.Text
14+
import androidx.compose.material3.TextField
15+
import androidx.compose.runtime.getValue
16+
import androidx.compose.runtime.mutableStateOf
17+
import androidx.compose.runtime.saveable.rememberSaveable
18+
import androidx.compose.runtime.setValue
19+
import androidx.compose.ui.Alignment
20+
import androidx.compose.ui.Modifier
21+
import androidx.compose.ui.text.input.KeyboardType
22+
import androidx.compose.ui.unit.dp
923
import com.jetbrains.kmm.shared.Calculator
10-
import android.widget.TextView
11-
import com.jetbrains.androidApp.R
24+
import com.jetbrains.kmm.shared.Greeting
1225

1326
fun greet(): String {
1427
return Greeting().greeting()
1528
}
1629

17-
class MainActivity : AppCompatActivity() {
30+
class MainActivity : ComponentActivity() {
1831
override fun onCreate(savedInstanceState: Bundle?) {
1932
super.onCreate(savedInstanceState)
20-
setContentView(R.layout.activity_main)
33+
setContent {
34+
Box(
35+
modifier = Modifier.fillMaxSize(),
36+
contentAlignment = Alignment.Center
37+
) {
38+
Column(
39+
horizontalAlignment = Alignment.Start,
40+
) {
41+
Text(greet(), Modifier.padding(8.dp))
2142

22-
val tv: TextView = findViewById(R.id.textView)
23-
tv.text = greet()
43+
var firstNumber by rememberSaveable { mutableStateOf("") }
44+
var secondNumber by rememberSaveable { mutableStateOf("") }
2445

25-
val numATV: EditText = findViewById(R.id.editTextNumberDecimalA)
26-
val numBTV: EditText = findViewById(R.id.editTextNumberDecimalB)
46+
Row(verticalAlignment = Alignment.CenterVertically) {
47+
TextField(
48+
value = firstNumber,
49+
onValueChange = { firstNumber = it },
50+
modifier = Modifier.width(100.dp),
51+
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
52+
)
53+
Text(text = "+", modifier = Modifier.padding(4.dp))
54+
TextField(
55+
value = secondNumber,
56+
onValueChange = { secondNumber = it },
57+
modifier = Modifier.width(100.dp),
58+
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
59+
)
2760

28-
val sumTV: TextView = findViewById(R.id.textViewSum)
29-
30-
val textWatcher = object: TextWatcher {
31-
override fun afterTextChanged(s: Editable?) {
32-
try {
33-
val numA = Integer.parseInt(numATV.text.toString())
34-
val numB = Integer.parseInt(numBTV.text.toString())
35-
sumTV.text = "= " + Calculator.sum(numA, numB).toString()
36-
} catch(e: NumberFormatException) {
37-
sumTV.text = "= 🤔"
61+
val first = firstNumber.toIntOrNull()
62+
val second = secondNumber.toIntOrNull()
63+
Text(
64+
text = if (first != null && second != null) {
65+
"= ${Calculator.sum(first, second)}"
66+
} else {
67+
"= \uD83E\uDD14"
68+
},
69+
modifier = Modifier
70+
.width(100.dp)
71+
.padding(4.dp)
72+
)
73+
}
3874
}
3975
}
40-
41-
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
42-
43-
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
4476
}
45-
46-
numATV.addTextChangedListener(textWatcher)
47-
numBTV.addTextChangedListener(textWatcher)
48-
4977
}
5078
}

androidApp/src/main/res/layout/activity_main.xml

-59
This file was deleted.

build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ plugins {
22
//trick: for the same plugin versions in all sub-modules
33
id("com.android.application").version("8.1.2").apply(false)
44
id("com.android.library").version("8.1.2").apply(false)
5-
kotlin("android").version("1.9.20-RC").apply(false)
6-
kotlin("multiplatform").version("1.9.20-RC").apply(false)
5+
kotlin("android").version("1.9.22").apply(false)
6+
kotlin("multiplatform").version("1.9.22").apply(false)
77
}

settings.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ pluginManagement {
33
google()
44
gradlePluginPortal()
55
mavenCentral()
6+
maven("https://androidx.dev/storage/compose-compiler/repository/")
67
}
78
}
89

910
dependencyResolutionManagement {
1011
repositories {
1112
google()
1213
mavenCentral()
14+
maven("https://androidx.dev/storage/compose-compiler/repository/")
1315
}
1416
}
1517

0 commit comments

Comments
 (0)