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() { | |
private lateinit var view: HelloView | |
private lateinit var presenter: HelloPresenter | |
private lateinit var model: HelloViewModel | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
model = of(this).get(HelloViewModel::class.java) |
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
fun HelloPresenter.configure(lifecycle: Lifecycle, vm: HelloViewModel, view: HelloView) { | |
// action flows | |
view.onShowNewGreetingButtonPressed(::onShowNewGreetingButtonPressed) | |
// data flows | |
vm.greeting.observe({ lifecycle }, ::onGreetingLoaded) | |
} |
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
internal class HelloViewModel : ViewModel(), HelloModel { | |
val greeting: MutableLiveData<String> = MutableLiveData() | |
override fun loadGreeting() { | |
greeting.value = greetings.random() | |
} | |
} | |
private val greetings = listOf( | |
"Hello", "Yo!", "Big up!", "Greetings!", "Hiya!" |
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() } |
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
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
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
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
@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) } | |
observe(getHello()) { | |
onResult { | |
if(hello.state != result) hello.state = result |
NewerOlder