Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- subscribeUi()
- }
- private fun subscribeUi() {
- /**
- * 이 과정은 DI(dependency injection)으로 대체가 가능합니다.
- */
- val dao = AppDatabase.getInstance(this).inputMsgDao()
- val repository = InputMsgRepository.getInstance(dao)
- val factory = MailViewModelFactory(repository)
- var viewModel = ViewModelProviders.of(this, factory).get(MainViewModel::class.java)
- viewModel.inputMsgs.observe(this, Observer {
- if (it == null || it.isEmpty())
- return@Observer
- var sb = StringBuffer()
- for (data in it) {
- sb.append(data.msg).append("\n")
- }
- tv_result.text = sb.toString()
- })
- btn_input.setOnClickListener {
- var input = et_input.text.toString()
- if (input == null || input.isEmpty())
- return@setOnClickListener
- et_input.setText("")
- viewModel.insertMsg(InputMsg(msg = input))
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement