Skip to content

Commit 9d19f2d

Browse files
committed
ref: jetpack compose working with live data on android sample
1 parent b4e158f commit 9d19f2d

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

android-sample/app/build.gradle.kts

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ dependencies {
8989
implementation(composeBom)
9090
androidTestImplementation(composeBom)
9191

92+
// Livedata
93+
implementation(libs.androidx.compose.runtime.livedata)
94+
9295
// Material Design 3
9396
implementation(libs.androidx.compose.material3)
9497
implementation(libs.androidx.compose.material3.window.size)

android-sample/app/src/main/kotlin/com/fernandocejas/sample/MainActivity.kt

+23-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import androidx.compose.material3.TextField
3636
import androidx.compose.material3.TopAppBarDefaults
3737
import androidx.compose.runtime.Composable
3838
import androidx.compose.runtime.getValue
39+
import androidx.compose.runtime.livedata.observeAsState
3940
import androidx.compose.runtime.mutableStateOf
4041
import androidx.compose.runtime.remember
4142
import androidx.compose.runtime.setValue
@@ -44,6 +45,7 @@ import androidx.compose.ui.Modifier
4445
import androidx.compose.ui.res.stringResource
4546
import androidx.compose.ui.tooling.preview.Preview
4647
import androidx.compose.ui.unit.dp
48+
import androidx.core.view.WindowCompat
4749

4850
@OptIn(ExperimentalMaterial3Api::class)
4951
class MainActivity : AppCompatActivity() {
@@ -53,6 +55,10 @@ class MainActivity : AppCompatActivity() {
5355
override fun onCreate(savedInstanceState: Bundle?) {
5456
super.onCreate(savedInstanceState)
5557

58+
// Turn off the decor fitting system windows, which allows
59+
// us to handle insets, including IME animations.
60+
WindowCompat.setDecorFitsSystemWindows(window, false)
61+
5662
setContent {
5763
MaterialTheme {
5864
Surface(
@@ -111,9 +117,24 @@ class MainActivity : AppCompatActivity() {
111117
textFieldLabel: String = stringResource(id = R.string.txt_encrypt_label_hint)
112118
) {
113119
TextFieldComponent(textFieldLabel)
120+
// //////////////////
121+
val name = viewModel.encryptedString.observeAsState().value
122+
// if (name.isNotEmpty()) {
123+
Text(
124+
text = "Hello, $name!",
125+
modifier = Modifier.padding(bottom = 8.dp),
126+
style = MaterialTheme.typography.bodyMedium
127+
)
128+
// }
129+
// //////////////////
114130
Spacer(modifier = Modifier.height(height = 5.dp))
115131
Row {
116-
ButtonComponent(buttonText)
132+
Button(
133+
onClick = { viewModel.encryptString("Fernando Cejas") },
134+
) {
135+
Text(text = buttonText)
136+
}
137+
// ButtonComponent(buttonText)
117138
}
118139
}
119140

@@ -133,7 +154,7 @@ class MainActivity : AppCompatActivity() {
133154
@Composable
134155
fun ButtonComponent(buttonText: String) {
135156
Button(
136-
onClick = { },
157+
onClick = { viewModel.encryptString("Fernando") },
137158
) {
138159
Text(text = buttonText)
139160
}
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package com.fernandocejas.sample
22

3+
import androidx.lifecycle.LiveData
4+
import androidx.lifecycle.MutableLiveData
35
import androidx.lifecycle.ViewModel
46

57
/**
68
* Used to communicate between screens.
79
*/
810
class MainViewModel : ViewModel() {
911

10-
// private val _drawerShouldBeOpened = MutableStateFlow(false)
11-
// val drawerShouldBeOpened = _drawerShouldBeOpened.asStateFlow()
12+
private val cryptor = Cryptor()
1213

13-
fun openDrawer() {
14-
// _drawerShouldBeOpened.value = true
15-
}
14+
private val _encryptedString = MutableLiveData<String>()
15+
val encryptedString: LiveData<String> = _encryptedString
1616

17-
fun resetOpenDrawerAction() {
18-
// _drawerShouldBeOpened.value = false
17+
fun encryptString(strToEncrypt: String) {
18+
// _encryptedString.value = cryptor.encrypt(strToEncrypt)
19+
_encryptedString.value = strToEncrypt
1920
}
2021
}

android-sample/gradle/libs.versions.toml

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ appCompat = "1.6.1"
1212
materialDesign = "1.1.1"
1313
composeBom = "2023.05.01"
1414
activityCompose = "1.7.2"
15+
liveData = "1.4.3"
1516
# plugings -------
1617
androidGradlePlugin = "8.0.2"
1718

@@ -24,9 +25,11 @@ kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutine
2425
android-core-ktx = { module = "androidx.core:core-ktx", version.ref = "ktx" }
2526
android-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appCompat" }
2627
androidx-compose-bom = { module = "androidx.compose:compose-bom", version.ref = "composeBom" }
28+
androidx-compose-runtime-livedata = { module = "androidx.compose.runtime:runtime-livedata", version.ref = "liveData" }
2729
androidx-compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "materialDesign" }
2830
androidx-compose-material3-window-size = { module = "androidx.compose.material3:material3-window-size-class", version.ref = "materialDesign" }
2931
androidx-activty-compose = { module = "androidx.activity:activity-compose", version.ref = "activityCompose" }
32+
3033
# tooling --------
3134
androidx-compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
3235
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }

0 commit comments

Comments
 (0)