Created
October 23, 2018 19:46
-
-
Save jtrindade/7f83f15cd205bb2ce09698b3bd592067 to your computer and use it in GitHub Desktop.
TryingViewModel
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
package pt.isel.pdm.jht.tryviewmodel | |
import android.content.Intent | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import android.view.View | |
import androidx.lifecycle.MutableLiveData | |
import androidx.lifecycle.Observer | |
import androidx.lifecycle.ViewModel | |
import androidx.lifecycle.ViewModelProviders | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
val counterModel by lazy { ViewModelProviders.of(this).get(CounterModel::class.java) } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
counterModel.getData().observe(this, Observer<Int> { | |
update(it) | |
}) | |
} | |
private fun update(c : Int) { | |
countView.text = c.toString() | |
} | |
fun doDec(view: View) { | |
counterModel.dec() | |
} | |
fun doInc(view: View) { | |
counterModel.inc() | |
} | |
fun doNew(view: View) { | |
startActivity(Intent(this, MainActivity::class.java)) | |
} | |
} | |
class CounterModel : ViewModel() { | |
private var count = 0 | |
private val countData = MutableLiveData<Int>() | |
init { | |
countData.value = count | |
} | |
fun dec() { countData.value = --count } | |
fun inc() { countData.value = ++count } | |
fun get() = count | |
fun getData() = countData | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment