This file contains hidden or 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
class MyViewModel : BaseViewModel() { | |
val _data = MutableLiveData<String>() | |
val data: LiveData<String> | |
get() = _data | |
fun load() { | |
_data.value = "Hello, World" | |
} | |
} |
This file contains hidden or 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
class MyViewModel : BaseViewModel() { | |
val data: LiveData<String> = MutableLiveData() | |
fun load() { | |
data.forceValue("Hello, World") | |
} | |
private fun <T> LiveData<T>.forceValue(value: T) { | |
(this as MutableLiveData<T>).value = value | |
} |
This file contains hidden or 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
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
onActive { | |
// TODO something special | |
onDispose { | |
// TODO clean up the special stuff |
This file contains hidden or 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
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
val hello = remember { HelloModel(HelloResult.Pending) } | |
observe(getHello()) { | |
onResult { | |
if(hello.state != result) hello.state = result |
This file contains hidden or 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
@Composable | |
fun <T> observe(@Pivotal data: LiveData<T>, block: ObserveScope<T>.() -> Unit) { | |
onActive { | |
val context = ObserveScope<T>() | |
block(context) | |
context.onStartBlock() | |
val observer = object : Observer<T> { | |
override fun onChanged(t: T) { | |
val resultScope = ObserveScope.OnResultScope(this, data, t) |
This file contains hidden or 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
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
val hello = remember { HelloModel(HelloResult.Pending) } | |
Recompose { recompose -> | |
observe(getHello()) { | |
onResult { |
This file contains hidden or 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
interface HelloView { | |
fun updateGreetingText(greetingText: String) | |
fun onShowNewGreetingButtonPressed(onPressed: () -> Unit) | |
} |
This file contains hidden or 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
interface HelloModel { | |
fun loadGreeting() | |
} |
This file contains hidden or 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
class HelloPresenter(val model: HelloModel, val view: HelloView) { | |
fun onCreated(init: Boolean) { | |
// If this is the first time we loaded, | |
// then load the first greeting | |
if (init) { | |
model.loadGreeting() | |
} | |
} |
This file contains hidden or 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
class HelloViewImpl(activity: AppCompatActivity) : HelloView { | |
private val greetingTextView: TextView = activity.findViewById(R.id.greeting_text) | |
private val newGreetingButton: Button = activity.findViewById(R.id.new_greeting_button) | |
override fun updateGreetingText(greetingText: String) { | |
greetingTextView.text = greetingText | |
} | |
override fun onShowNewGreetingButtonPressed(onPressed: () -> Unit) = | |
newGreetingButton.setOnClickListener { onPressed() } |