Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Paging V3 #4

Merged
merged 10 commits into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ dependencies {
implementation "androidx.multidex:multidex:$multidexVersion"
implementation "androidx.fragment:fragment-ktx:$fragmentExtVersion"
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion"
implementation "androidx.paging:paging-runtime-ktx:$pagingVersion"
implementation "androidx.paging:paging-rxjava2:$pagingVersion"
//dependency injection
implementation "com.google.dagger:hilt-android:$hiltVersion"
kapt "com.google.dagger:hilt-android-compiler:$hiltVersion"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package app.web.drjackycv.mvvmtemplate.di.module

import app.web.drjackycv.data.products.datasource.ProductsRemoteDataSource
import app.web.drjackycv.data.products.repository.ProductsRepositoryImpl
import app.web.drjackycv.domain.products.repository.ProductsRepository
import app.web.drjackycv.data.products.datasource.ProductsPagingSource
import app.web.drjackycv.data.products.repository.ProductsListRepositoryImpl
import app.web.drjackycv.domain.products.repository.ProductsListRepository
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ApplicationComponent
import java.util.concurrent.Executors

@InstallIn(ApplicationComponent::class)
@Module
class RepositoryModule {

@Suppress("PrivatePropertyName")
private val NETWORK_IO = Executors.newFixedThreadPool(5)

@Provides
fun products(productsRemoteDataSource: ProductsRemoteDataSource): ProductsRepository =
ProductsRepositoryImpl(productsRemoteDataSource, NETWORK_IO)
fun productsList(pagingSource: ProductsPagingSource): ProductsListRepository =
ProductsListRepositoryImpl(pagingSource)

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package app.web.drjackycv.data.products.datasource

import androidx.paging.rxjava2.RxPagingSource
import app.web.drjackycv.data.products.remote.ProductsApi
import app.web.drjackycv.domain.base.RecyclerItem
import io.reactivex.Single
import io.reactivex.annotations.NonNull
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject
import javax.inject.Singleton


private const val STARTING_PAGE_INDEX = 1

@Singleton
class ProductsPagingSource @Inject constructor(
private val productsApi: ProductsApi
//private val query: String
) : RxPagingSource<Int, RecyclerItem>() {

override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, RecyclerItem>> {
val position = params.key ?: STARTING_PAGE_INDEX
//val apiQuery = query

return productsApi.getBeersList(position, params.loadSize)
.subscribeOn(Schedulers.io())
.map { listBeerResponse ->
listBeerResponse.map { beerResponse ->
beerResponse.toDomain()
}
}
.map { toLoadResult(it, position) }
.onErrorReturn { LoadResult.Error(it) }
}

private fun toLoadResult(
@NonNull response: List<RecyclerItem>,
position: Int
): LoadResult<Int, RecyclerItem> {
return LoadResult.Page(
data = response,
prevKey = if (position == STARTING_PAGE_INDEX) null else position - 1,
nextKey = if (response.isEmpty()) null else position + 1,
itemsBefore = LoadResult.Page.COUNT_UNDEFINED,
itemsAfter = LoadResult.Page.COUNT_UNDEFINED
)
}

}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package app.web.drjackycv.data.products.remote

import app.web.drjackycv.data.products.entity.BeerResponse
import retrofit2.Call
import io.reactivex.Single
import retrofit2.http.GET
import retrofit2.http.Query

interface ProductsApi {

@GET("beers")
fun getBeers(
fun getBeersList(
/*@Query("ids") ids: String,*/
@Query("page") page: Int = 1,
@Query("per_page") perPage: Int = 40
): Call<List<BeerResponse>>
): Single<List<BeerResponse>>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package app.web.drjackycv.data.products.repository

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.rxjava2.flowable
import app.web.drjackycv.data.products.datasource.ProductsPagingSource
import app.web.drjackycv.domain.base.RecyclerItem
import app.web.drjackycv.domain.products.repository.ProductsListRepository
import io.reactivex.Flowable
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class ProductsListRepositoryImpl @Inject constructor(
private val pagingSource: ProductsPagingSource
) : ProductsListRepository {

override fun getBeersList(ids: String): Flowable<PagingData<RecyclerItem>> = Pager(
config = PagingConfig(
pageSize = 10,
enablePlaceholders = false,
maxSize = 30,
prefetchDistance = 5,
initialLoadSize = 10
),
pagingSourceFactory = { pagingSource }
).flowable

}

This file was deleted.

2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ext {
designSupportVersion = '28.0.0'
coreKtxVersion = '1.3.1'
navigationVersion = '1.0.0'
pagingVersion = '2.1.2'
pagingVersion = '3.0.0-alpha04'//'2.1.2'
multidexVersion = '2.0.1'
fragmentExtVersion = '1.2.5'
//dependency injection
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading