Skip to content

Commit

Permalink
Merge pull request #77 from zsmb13/compose
Browse files Browse the repository at this point in the history
Convert UI to Jetpack Compose
  • Loading branch information
zsmb13 authored Jan 25, 2024
2 parents f6bfd2d + 391ba8d commit 97face9
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 92 deletions.
12 changes: 12 additions & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ android {
jvmTarget = "1.8"
}
namespace = "com.jetbrains.androidApp"
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.8"
}
}

dependencies {
implementation(project(":shared"))
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")
}
Original file line number Diff line number Diff line change
@@ -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)

}
}
59 changes: 0 additions & 59 deletions androidApp/src/main/res/layout/activity_main.xml

This file was deleted.

4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ pluginManagement {
google()
gradlePluginPortal()
mavenCentral()
maven("https://androidx.dev/storage/compose-compiler/repository/")
}
}

dependencyResolutionManagement {
repositories {
google()
mavenCentral()
maven("https://androidx.dev/storage/compose-compiler/repository/")
}
}

Expand Down

0 comments on commit 97face9

Please sign in to comment.