75.12 Adding LiveData Observers
The user interface now needs to add observers to remain synchronized with the searchResults and allProducts live data objects within the ViewModel. Remaining in the Mainfragment.kt file, implement the observer setup method as follows:
private fun observerSetup() {
viewModel.getAllProducts()?.observe(this, Observer { products ->
products?.let {
adapter?.setProductList(it)
}
})
viewModel.getSearchResults().observe(this, Observer { products ->
products?.let {
if (it.isNotEmpty()) {
...