Created
October 17, 2019 03:51
-
-
Save aartikov/98b7a52b4e34d58a452154ffd7a28a94 to your computer and use it in GitHub Desktop.
MobX Sample
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.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