Advertisement
Guest User

rx binding sample

a guest
Apr 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1.  
  2. -- rx dependencis --
  3. // RxJava + RxAndroid
  4. implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
  5. implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
  6.  
  7. // RxBinding
  8. implementation 'com.jakewharton.rxbinding2:rxbinding:2.2.0'
  9.  
  10.  
  11. -- logic binding edit text --
  12. -- SearchActivity.kt --
  13.  
  14. class SearchActivity : AppCompatActivity() {
  15.  
  16. private val TAG = this::class.java.simpleName + TSMLOG
  17. private val searchAdapter = SearchAppAdapter()
  18. private val disposable = CompositeDisposable()
  19. private lateinit var searchViewModel: SearchViewModel
  20.  
  21.  
  22. override fun onCreate(savedInstanceState: Bundle?) {
  23. super.onCreate(savedInstanceState)
  24. setContentView(R.layout.activity_search)
  25. ViewUtil.customStatusAndNav(this)
  26.  
  27. val type = intent.getIntExtra("type", TYPE_APP)
  28. when (type) {
  29. TYPE_APP -> initSearchApps(resources.getColor(R.color.appsAccent))
  30. TYPE_GAMES -> initSearchApps(resources.getColor(R.color.gamesAccent))
  31. }
  32.  
  33. list_result.layoutManager = LinearLayoutManager(this)
  34. list_result.addItemDecoration(DividerItemDecoration(this, OrientationHelper.VERTICAL))
  35.  
  36. search_view.typeface = Typeface.createFromAsset(assets, "Roboto-Regular.ttf")
  37. progress_circular.visibility = View.GONE
  38.  
  39. btn_back.setOnClickListener {
  40. super.onBackPressed()
  41. }
  42. }
  43.  
  44. private fun initSearchApps(color: Int) {
  45. list_result.adapter = searchAdapter
  46. search_view.imeOptions = EditorInfo.IME_ACTION_SEARCH
  47. search_view.requestFocus()
  48.  
  49.  
  50. appbar_main.setBackgroundColor(color)
  51. disposable.add(
  52. RxTextView.textChanges(search_view)
  53. .subscribeOn(Schedulers.io())
  54. .observeOn(Schedulers.computation())
  55. .filter { it.length > 1 }
  56. .map { it.toString() }
  57. .debounce(500, TimeUnit.MILLISECONDS)
  58. .observeOn(AndroidSchedulers.mainThread())
  59. .subscribe({ query ->
  60. searchViewModel = ViewModelProviders.of(this).get(SearchViewModel::class.java)
  61. searchViewModel.getApps(query).observe(this, Observer {
  62. progress_circular.visibility = View.VISIBLE
  63. searchAdapter.submitList(it)
  64. })
  65.  
  66. searchViewModel.getLoader().observe(this, Observer {
  67. progressConfig(progress_circular, it)
  68. })
  69.  
  70. }, { e ->
  71. Log.i(TAG, e.message)
  72. })
  73. )
  74. }
  75.  
  76. override fun onDestroy() {
  77. super.onDestroy()
  78. disposable.dispose()
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement