Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

João Gabriel 1jGabriel

🏠
Working from home
View GitHub Profile
View CharacterItem.kt
@Composable
fun CharacterItem(character: CharacterUi) {
Row(
modifier = Modifier
.padding(start = 16.dp, top = 16.dp, end = 16.dp)
.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
) {
CharacterImage(
@1jGabriel
1jGabriel / PersonageList.kt
Last active Jan 23, 2021
PersonageListComposable
View PersonageList.kt
@Composable
fun CharacterList(characters: Flow<PagingData<CharacterUi>>) {
val lazyCharacters: LazyPagingItems<CharacterUi> = characters.collectAsLazyPagingItems()
LazyColumn {
items(lazyPagingItems = lazyCharacters) { character ->
character?.let {
CharacterItem(character = character)
View PersonageViewModel.kt
class PersonageViewModel(
private val repository: PersonageRepository
) : ViewModel() {
fun getCharacters(): Flow<PagingData<CharacterUi>> {
return repository.getPersonages().cachedIn(viewModelScope)
}
}
@1jGabriel
1jGabriel / PersonageRepository.kt
Last active Jan 23, 2021
PersonageRepository
View PersonageRepository.kt
class PersonageRepository(
private val api: RickNMortyApi
) {
override fun getPersonages(): Flow<PagingData<CharacterUi>> {
return Pager(
config = PagingConfig(pageSize = 20, maxSize = 500),
pagingSourceFactory = { PersonageDataSource(api) }
).flow
}
View PersonageDataSource.kt
class PersonageDataSource(
private val api: RickNMortyApi
) : PagingSource<Int, CharacterUi>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CharacterUi> {
return try {
val result = api.getCharacters(params.key ?: STARTING_PAGE_INDEX).toCharacters()
Page(
data = result,
prevKey = params.key,
nextKey = params.key?.plus(1) ?: STARTING_PAGE_INDEX.plus(1)
View RickNMortyApi.kt
interface RickNMortyApi {
@GET("api/character/")
suspend fun getCharacters(@Query("page") page: Int): CharacterResult
}
View MyAdapter.kt
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.View.OnClickListener
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
class MyAdapter(
private val click: () -> Unit
View MyAdapter.kt
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}#end
import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.View.OnClickListener
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
@1jGabriel
1jGabriel / build.gradle
Last active Jan 23, 2021
Paging dependencies
View build.gradle
def paging_version = "3.0.0-alpha07"
implementation "androidx.paging: paging-runtime:$paging_version"
View FragmentB.kt
class FragmentB : Fragment() {
fun onViewCreated(...) {
...
...
view.setOnClickListener {
val uri = Uri.parse("myApp://fragmentD")
findNavController().navigate(uri)
}
}
}