Last active
October 4, 2020 06:02
-
-
Save okhobb/ba7791af4562ea672d0c52769a7cd8ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sandbox | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.compose.foundation.Text | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.platform.setContent | |
import androidx.compose.ui.viewinterop.AndroidView | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.ViewModel | |
import android.widget.TextView | |
import androidx.compose.material.Button | |
import androidx.compose.runtime.getValue | |
// Add this to gradle.build for the observeAsState function: | |
// implementation "androidx.compose.runtime:runtime-livedata:$compose_version" | |
import androidx.compose.runtime.livedata.observeAsState | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.ui.platform.ContextAmbient | |
import androidx.compose.ui.text.TextStyle | |
import androidx.compose.ui.unit.em | |
sealed class AppView | |
data class ShowSomeText(val text: String) : AppView() | |
data class SomeOtherState(val data: Any?) : AppView() | |
data class ShowSomeText2(val text: String) : AppView() | |
class AppViewModel : ViewModel() { | |
var currentView = MutableLiveData<AppView>(ShowSomeText("original text")) | |
var currentViewWorkaround = MutableLiveData<AppView>(ShowSomeText("original text")) | |
private val textRing = arrayOf("one", "two", "three") | |
private var textRingPosition = 0 | |
fun incrementTextState() { | |
val nextState = ShowSomeText(textRing[textRingPosition]) | |
currentView.postValue(nextState) | |
val nextStateWorkaround = when(currentViewWorkaround.value) { | |
is ShowSomeText -> ShowSomeText2(textRing[textRingPosition]) | |
else -> ShowSomeText(textRing[textRingPosition]) | |
} | |
currentViewWorkaround.postValue(nextStateWorkaround) | |
textRingPosition = (textRingPosition + 1) % textRing.size | |
} | |
} | |
class MainActivity : AppCompatActivity() { | |
private val viewModel = AppViewModel() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
ViewContainer(viewModel) | |
} | |
} | |
} | |
@Composable | |
fun ViewContainer(viewModel: AppViewModel) { | |
// Add this to gradle.build for the observeAsState function: | |
// implementation "androidx.compose.runtime:runtime-livedata:$compose_version" | |
val currentView: AppView by viewModel.currentView.observeAsState(ShowSomeText("starting text")) | |
val currentViewWorkaround: AppView by viewModel.currentViewWorkaround.observeAsState(ShowSomeText("starting text")) | |
Column { | |
Button(onClick = viewModel::incrementTextState) { | |
Text( | |
text = "tap to change", | |
style = TextStyle(fontSize = 12.em) | |
) | |
} | |
Text("Compose Text") | |
when (currentView) { | |
is ShowSomeText -> createComposeTextView((currentView as ShowSomeText).text) | |
is SomeOtherState -> Text("the other state") | |
} | |
Text("AndroidView wrapping TextView") | |
when (currentView) { | |
is ShowSomeText -> createAndroidViewForTextView((currentView as ShowSomeText).text) | |
is SomeOtherState -> Text("the other state") | |
} | |
Text("AndroidView wrapping TextView with 2-state workaround") | |
when (currentViewWorkaround) { | |
is ShowSomeText -> createAndroidViewForTextView((currentViewWorkaround as ShowSomeText).text) | |
is ShowSomeText2 -> createAndroidViewForTextView((currentViewWorkaround as ShowSomeText2).text) | |
is SomeOtherState -> Text("the other state") | |
} | |
} | |
} | |
@Composable | |
fun createAndroidViewForTextView(text: String) { | |
val context = ContextAmbient.current | |
val tv = remember(text, context) { | |
val x = TextView(context) | |
x.text = text | |
x.textSize = 48.0f | |
x | |
} | |
AndroidView({ tv }) | |
} | |
@Composable | |
fun createComposeTextView(text: String) { | |
Text(text, style = TextStyle(fontSize = 12.em)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment