Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. class MainActivity : AppCompatActivity() {
  2.  
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. setContentView(R.layout.activity_main)
  6.  
  7. subscribeUi()
  8. }
  9.  
  10. private fun subscribeUi() {
  11. /**
  12. * 이 과정은 DI(dependency injection)으로 대체가 가능합니다.
  13. */
  14. val dao = AppDatabase.getInstance(this).inputMsgDao()
  15. val repository = InputMsgRepository.getInstance(dao)
  16. val factory = MailViewModelFactory(repository)
  17. var viewModel = ViewModelProviders.of(this, factory).get(MainViewModel::class.java)
  18.  
  19. viewModel.inputMsgs.observe(this, Observer {
  20. if (it == null || it.isEmpty())
  21. return@Observer
  22.  
  23. var sb = StringBuffer()
  24. for (data in it) {
  25. sb.append(data.msg).append("\n")
  26. }
  27.  
  28. tv_result.text = sb.toString()
  29. })
  30.  
  31. btn_input.setOnClickListener {
  32. var input = et_input.text.toString()
  33. if (input == null || input.isEmpty())
  34. return@setOnClickListener
  35.  
  36. et_input.setText("")
  37. viewModel.insertMsg(InputMsg(msg = input))
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement