SHARE
TWEET

Untitled

a guest Apr 20th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package sample
  2.  
  3. import android.arch.lifecycle.MutableLiveData
  4. import android.os.Bundle
  5. import android.support.v7.app.AppCompatActivity
  6. import android.view.View
  7. import android.widget.Toast
  8. import com.devindi.wallpaper.R
  9.  
  10. interface CounterView {
  11.     fun setCounterValue(value: Int)
  12. }
  13.  
  14. class CounterActivityMvp : AppCompatActivity(), CounterView {
  15.  
  16.     val presenter = CounterPresenter()
  17.  
  18.     override fun onCreate(savedInstanceState: Bundle?) {
  19.         super.onCreate(savedInstanceState)
  20.  
  21.         presenter.view = this
  22.  
  23.         val view = View(this)
  24.         view.id = R.id.increment
  25.  
  26.         setContentView(view)
  27.  
  28.         view.setOnClickListener{ presenter.increment() }
  29.     }
  30.  
  31.     override fun setCounterValue(value: Int) {
  32.         Toast.makeText(this, "Counter: $value", Toast.LENGTH_SHORT).show()
  33.     }
  34.  
  35.     override fun onDestroy() {
  36.         super.onDestroy()
  37.         presenter.view = null
  38.     }
  39. }
  40.  
  41. class CounterPresenter {
  42.     val model = CounterRepository()
  43.  
  44.     var view: CounterView? = null
  45.  
  46.     init {
  47.         model.counter.observeForever { t -> view?.setCounterValue(t!!) }
  48.     }
  49.  
  50.     fun increment() {
  51.         model.increment()
  52.     }
  53.  
  54. }
  55.  
  56. class CounterRepository {
  57.  
  58.     val counter = MutableLiveData<Int>()
  59.  
  60.     init {
  61.         counter.value = 0
  62.     }
  63.  
  64.     fun increment() {
  65.         counter.value = counter.value!! + 1
  66.     }
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top