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 MainViewModel(val view: MainView) { | |
| private var currentItem: Item by Delegates.observable(Item.ITEM_A, | |
| { _: KProperty<*>, oldItem: Item, newItem: Item -> | |
| view.onUpdatedItem(oldItem, newItem) | |
| }) | |
| fun onClicked(item: Item) { | |
| currentItem = item | |
| } |
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 MainPresenter(val view: MainView) { | |
| private var currentItem: Item = Item.ITEM_A | |
| fun onClicked(item: Item) { | |
| view.onUpdatedItem(currentItem, item) | |
| currentItem = item | |
| } | |
| } |
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
| enum class Item(val titleId: Int, val colorId: Int) { | |
| ITEM_A(R.string.a, R.color.red), | |
| ITEM_B(R.string.b, R.color.green), | |
| ITEM_C(R.string.c, R.color.blue), | |
| ITEM_D(R.string.d, R.color.black) | |
| } | |
| bt_d.setOnClickListener { | |
| mainPresenter.onClicked(Item.ITEM_D) | |
| } |
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(), MainView { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| val mainPresenter = MainPresenter(this) | |
| bt_a.setOnClickListener { | |
| mainPresenter.onClicked(Item.ITEM_A) | |
| } |
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(), MainView { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| val mainPresenter = MainPresenter(this) | |
| bt_a.setOnClickListener { | |
| mainPresenter.onClicked(ITEM_A) | |
| } |
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
| enum class Item(val titleId: Int, val colorId: Int) { | |
| ITEM_A(R.string.a, R.color.red), | |
| ITEM_B(R.string.b, R.color.green), | |
| ITEM_C(R.string.c, R.color.blue) | |
| } |
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
| enum class Item { | |
| ITEM_A, ITEM_B, ITEM_C, ITEM_D | |
| } |
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
| override fun onUpdatedItem(oldItem: Item, newItem: Item) { | |
| tv_item.text = "${getItemStr(oldItem)} -> ${getItemStr(newItem)}" | |
| iv_item.setBackgroundColor(ContextCompat.getColor(this, when (newItem) { | |
| ITEM_A -> R.color.red | |
| ITEM_B -> R.color.green | |
| ITEM_C -> R.color.blue | |
| })) | |
| } | |
| private fun getItemStr(item: Item): String = getString(when (item) { |
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
| override fun onUpdatedItem(oldItem: Int, newItem: Int) { | |
| tv_item.text = "${getItemStr(oldItem)} -> ${getItemStr(newItem)}" | |
| iv_item.setBackgroundColor(ContextCompat.getColor(this, when (newItem) { | |
| ITEM_A -> R.color.red | |
| ITEM_B -> R.color.green | |
| ITEM_C -> R.color.blue | |
| else -> throw IllegalArgumentException("invalid item") | |
| })) | |
| } |
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
| enum class Item { | |
| ITEM_A, ITEM_B, ITEM_C | |
| } |