Skip to content

Instantly share code, notes, and snippets.

@markchristopherng
Last active July 6, 2018 06:57
Show Gist options
  • Save markchristopherng/a975a0b53323cc9aad4e896073a09b0c to your computer and use it in GitHub Desktop.
Save markchristopherng/a975a0b53323cc9aad4e896073a09b0c to your computer and use it in GitHub Desktop.
FindPostcodeActivity
class FindPostcodeActivity : KAusPostActivity() {
@Inject
lateinit var mgr: BranchLocationManager
lateinit var searchText: EditText
lateinit var listAdapter: BranchLocationPageAdapter
public override fun doOnCreate(savedInstanceState: Bundle?) {
setContentView(R.layout.activity_find_postcode)
Toothpick.inject(this, appScope)
setupList()
setupSearchView()
}
private fun setupSearchView() {
searchText.textChanges()
.debounce(300, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
.subscribe { text: CharSequence ->
loadList(text.toString())
}
}
private fun setupList() {
text_no_results.visibility = View.GONE
loadList("")
}
private fun setupListAdapter() {
listAdapter = BranchLocationPageAdapter(this)
with(list_postcode) {
adapter = listAdapter
visibility = View.GONE
setHasFixedSize(true)
layoutManager = LinearLayoutManager(this@FindPostcodeActivity)
}
}
private fun loadList(filterKeyword: String) {
setupListAdapter()
val postcodeList = RxPagedListBuilder(mgr.pageByKeyword(filterKeyword), PagedList.Config
.Builder()
.setPageSize(1500)
.setEnablePlaceholders(true)
.build())
.buildFlowable(BackpressureStrategy.LATEST)
postcodeList.compose(defaultOptions())
.autoDisposable()
.subscribe {
val hasResult = it?.isNotEmpty() ?: false
displayList(hasResult)
listAdapter.submitList(it)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment