This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ExperimentalPagingApi | |
class CharacterRemoteMediador ( | |
private val name: String, | |
private val dao: CharacterDao, | |
private val service: CharacterService | |
) : RemoteMediator<Int, java.lang.Character>() { | |
override suspend fun load( | |
loadType: LoadType, | |
state: PagingState<Int, java.lang.Character> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getCharacters(query: String): Flow<PagingData<java.lang.Character>> = Pager( | |
config = PagingConfig( | |
pageSize = 20, | |
enablePlaceholders = false | |
), | |
pagingSourceFactory = { dao.getPagingSource("%$query%") }, | |
remoteMediator = CharacterRemoteMediador(query, dao, service) | |
).flow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun characters(query: String): LiveData<PagingData<Character>> = | |
characterRepository.getCharacters(query).cachedIn(viewModelScope).asLiveData() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.samuelnunes.rickandmortyapp.ui.characters | |
import android.os.Bundle | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.fragment.app.Fragment | |
import androidx.fragment.app.activityViewModels | |
import androidx.fragment.app.viewModels | |
import androidx.paging.ExperimentalPagingApi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainViewModel : ViewModel() { | |
private val _query = MutableLiveData<String>() | |
val query: LiveData<String> = _query | |
val onQueryTextListener = object : SearchView.OnQueryTextListener { | |
override fun onQueryTextSubmit(query: String?): Boolean { | |
if (query != null) { | |
_query.postValue(query.lowercase()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val viewModel by viewModels<MainViewModel>() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreateOptionsMenu(menu: Menu?): Boolean { | |
menuInflater.inflate(R.menu.menu_main_activity, menu) | |
val item = menu?.findItem(R.id.search) | |
val searchView = item?.actionView as SearchView | |
searchView.setOnQueryTextListener(viewModel.onQueryTextListener) | |
return super.onCreateOptionsMenu(menu) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
viewModel.characters().observe(viewLifecycleOwner) { | |
adapter.submitData(lifecycle, it) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CharacterAdapter : PagingDataAdapter<Character, CharacterAdapter.CharacterViewHolder>(CharacterAdapter) |