Skip to content

Instantly share code, notes, and snippets.

@aartikov
Created October 17, 2019 03:51
Show Gist options
  • Save aartikov/98b7a52b4e34d58a452154ffd7a28a94 to your computer and use it in GitHub Desktop.
Save aartikov/98b7a52b4e34d58a452154ffd7a28a94 to your computer and use it in GitHub Desktop.
MobX Sample
package com.example.mobxtest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_counter.*
import magneton.observable.ReactionDisposer
import magneton.observable.computed
import magneton.observable.observable
import magneton.observable.reaction
class CounterActivity : AppCompatActivity() {
private var count by observable(0)
private val formattedCount by computed { "Count: $count" }
private lateinit var disposer: ReactionDisposer
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_counter)
incrementButton.setOnClickListener { increment() }
decrementButton.setOnClickListener { decrement() }
disposer = reaction {
countTextView.text = formattedCount
}
}
private fun increment() {
count++
}
private fun decrement() {
count--
}
override fun onDestroy() {
disposer.dispose()
super.onDestroy()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment