|
| 1 | +@file:OptIn(ExperimentalMaterial3Api::class) |
| 2 | + |
| 3 | +package com.example.compose.snippets.interop |
| 4 | + |
| 5 | +import android.content.Context |
| 6 | +import android.os.Bundle |
| 7 | +import android.util.AttributeSet |
| 8 | +import android.widget.LinearLayout |
| 9 | +import android.widget.TextView |
| 10 | +import androidx.activity.ComponentActivity |
| 11 | +import androidx.activity.compose.setContent |
| 12 | +import androidx.compose.foundation.layout.Column |
| 13 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 14 | +import androidx.compose.material3.MaterialTheme |
| 15 | +import androidx.compose.material3.Text |
| 16 | +import androidx.compose.material3.TextField |
| 17 | +import androidx.compose.runtime.Composable |
| 18 | +import androidx.compose.runtime.getValue |
| 19 | +import androidx.compose.runtime.livedata.observeAsState |
| 20 | +import androidx.compose.runtime.mutableStateOf |
| 21 | +import androidx.compose.runtime.setValue |
| 22 | +import androidx.compose.ui.platform.ComposeView |
| 23 | +import androidx.lifecycle.LiveData |
| 24 | +import androidx.lifecycle.MutableLiveData |
| 25 | +import androidx.lifecycle.ViewModel |
| 26 | +import androidx.lifecycle.ViewModelProvider |
| 27 | +import androidx.lifecycle.viewmodel.compose.viewModel |
| 28 | +import androidx.navigation.compose.NavHost |
| 29 | +import androidx.navigation.compose.composable |
| 30 | +import androidx.navigation.compose.rememberNavController |
| 31 | + |
| 32 | +// [START android_compose_interop_existing_arch_viewmodels_in_compose] |
| 33 | +class GreetingActivity : ComponentActivity() { |
| 34 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 35 | + super.onCreate(savedInstanceState) |
| 36 | + |
| 37 | + setContent { |
| 38 | + MaterialTheme { |
| 39 | + Column { |
| 40 | + GreetingScreen("user1") |
| 41 | + GreetingScreen("user2") |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +@Composable |
| 49 | +fun GreetingScreen( |
| 50 | + userId: String, |
| 51 | + viewModel: GreetingViewModel = viewModel( |
| 52 | + factory = GreetingViewModelFactory(userId) |
| 53 | + ) |
| 54 | +) { |
| 55 | + val messageUser by viewModel.message.observeAsState("") |
| 56 | + |
| 57 | + Text(messageUser) |
| 58 | +} |
| 59 | + |
| 60 | +class GreetingViewModel(private val userId: String) : ViewModel() { |
| 61 | + private val _message = MutableLiveData("Hi $userId") |
| 62 | + val message: LiveData<String> = _message |
| 63 | +} |
| 64 | + |
| 65 | +class GreetingViewModelFactory(private val userId: String): ViewModelProvider.Factory { |
| 66 | + @Suppress("UNCHECKED_CAST") |
| 67 | + override fun <T : ViewModel> create(modelClass: Class<T>): T { |
| 68 | + return GreetingViewModel(userId) as T |
| 69 | + } |
| 70 | +} |
| 71 | +// [END android_compose_interop_existing_arch_viewmodels_in_compose] |
| 72 | + |
| 73 | +// [START android_compose_interop_existing_arch_viewmodels_in_compose_nav_graph] |
| 74 | +@Composable |
| 75 | +fun MyApp() { |
| 76 | + NavHost(rememberNavController(), startDestination = "profile/{userId}") { |
| 77 | + /* ... */ |
| 78 | + composable("profile/{userId}") { backStackEntry -> |
| 79 | + GreetingScreen(backStackEntry.arguments?.getString("userId") ?: "") |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +// [END android_compose_interop_existing_arch_viewmodels_in_compose_nav_graph] |
| 84 | + |
| 85 | +// [START android_compose_interop_existing_arch_views_sot] |
| 86 | +class CustomViewGroup @JvmOverloads constructor( |
| 87 | + context: Context, |
| 88 | + attrs: AttributeSet? = null, |
| 89 | + defStyle: Int = 0 |
| 90 | +) : LinearLayout(context, attrs, defStyle) { |
| 91 | + |
| 92 | + // Source of truth in the View system as mutableStateOf |
| 93 | + // to make it thread-safe for Compose |
| 94 | + private var text by mutableStateOf("") |
| 95 | + |
| 96 | + private val textView: TextView |
| 97 | + |
| 98 | + init { |
| 99 | + orientation = VERTICAL |
| 100 | + |
| 101 | + textView = TextView(context) |
| 102 | + val composeView = ComposeView(context).apply { |
| 103 | + setContent { |
| 104 | + MaterialTheme { |
| 105 | + TextField(value = text, onValueChange = { updateState(it) }) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + addView(textView) |
| 111 | + addView(composeView) |
| 112 | + } |
| 113 | + |
| 114 | + // Update both the source of truth and the TextView |
| 115 | + private fun updateState(newValue: String) { |
| 116 | + text = newValue |
| 117 | + textView.text = newValue |
| 118 | + } |
| 119 | +} |
| 120 | +// [END android_compose_interop_existing_arch_views_sot] |
0 commit comments