Skip to content

Instantly share code, notes, and snippets.

View 1jGabriel's full-sized avatar
🏠
Working from home

João Gabriel 1jGabriel

🏠
Working from home
View GitHub Profile
@1jGabriel
1jGabriel / PersonageRepository.kt
Last active January 23, 2021 17:30
PersonageRepository
class PersonageRepository(
private val api: RickNMortyApi
) {
override fun getPersonages(): Flow<PagingData<CharacterUi>> {
return Pager(
config = PagingConfig(pageSize = 20, maxSize = 500),
pagingSourceFactory = { PersonageDataSource(api) }
).flow
}
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)
@1jGabriel
1jGabriel / RickNMortyApi.kt
Created January 23, 2021 17:24
RickNMortyApi
interface RickNMortyApi {
@GET("api/character/")
suspend fun getCharacters(@Query("page") page: Int): CharacterResult
}
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
#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 January 23, 2021 17:25
Paging dependencies
def paging_version = "3.0.0-alpha07"
implementation "androidx.paging: paging-runtime:$paging_version"
class FragmentB : Fragment() {
fun onViewCreated(...) {
...
...
view.setOnClickListener {
val uri = Uri.parse("myApp://fragmentD")
findNavController().navigate(uri)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_main"
app:startDestination="@id/nav_feature_one">
<include app:graph="@navigation/nav_feature_one" />
<include app:graph="@navigation/nav_feature_two" />
<include app:graph="@navigation/nav_feature_three" />
</navigation>
@1jGabriel
1jGabriel / build.gradle
Created October 19, 2020 13:19
App Module build.gradle
implementation project(":featureone")
implementation project(":featuretwo")
implementation project(":featurethree")