TP-69452 | Moved boilterplate code into common base and expose necess… (#11193)

This commit is contained in:
Kishan Kumar
2024-06-07 15:07:25 +05:30
committed by GitHub
parent 3aac39a28e
commit 4bf0f8b73d
7 changed files with 160 additions and 161 deletions

View File

@@ -7,14 +7,13 @@
package com.navi.coin.repo.pagingsource
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.navi.base.utils.orFalse
import com.navi.coin.models.model.TransactionHistoryCardData
import com.navi.coin.models.model.TransactionHistoryResponse
import com.navi.coin.repo.repository.CoinHistoryScreenRepo
import com.navi.coin.utils.constant.Constants.SOMETHING_WENT_WRONG
import com.navi.coin.vm.CoinHistoryCardScreenState
import com.navi.common.utils.log
import com.navi.common.network.models.RepoResult
import com.navi.rr.utils.pagingsource.BasePagingSource
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
@@ -22,54 +21,33 @@ import kotlinx.coroutines.flow.update
class CashHistoryListSource
@Inject
constructor(private val coinHistoryRepo: CoinHistoryScreenRepo) :
PagingSource<Int, TransactionHistoryCardData>() {
constructor(
private val coinHistoryRepo: CoinHistoryScreenRepo,
) : BasePagingSource<TransactionHistoryCardData, TransactionHistoryResponse>() {
private val _cashHistoryData =
MutableStateFlow<CoinHistoryCardScreenState>(CoinHistoryCardScreenState.Loading)
val cashHistoryData = _cashHistoryData.asStateFlow()
override fun getRefreshKey(state: PagingState<Int, TransactionHistoryCardData>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val page = state.closestPageToPosition(anchorPosition)
page?.prevKey?.minus(1) ?: page?.nextKey?.plus(1)
}
override val DEFAULT_PAGE_SIZE: Int = 10
override val DEFAULT_START_KEY: Int = 0
override suspend fun fetchData(
page: Int,
pageSize: Int,
): RepoResult<TransactionHistoryResponse> {
return coinHistoryRepo.fetchCashListData(page, pageSize)
}
override suspend fun load(
params: LoadParams<Int>
): LoadResult<Int, TransactionHistoryCardData> {
try {
val currentKey = params.key ?: PAGINATION_START_KEY_VALUE
val response =
coinHistoryRepo.fetchCashListData(
pageNo = currentKey,
pageSize = CASH_HISTORY_ITEM_LIMIT
)
if (
response.error != null ||
response.errors.isNullOrEmpty().not() ||
response.data == null
) {
_cashHistoryData.update { CoinHistoryCardScreenState.Error }
return LoadResult.Error(Throwable(SOMETHING_WENT_WRONG))
}
_cashHistoryData.update { CoinHistoryCardScreenState.Success }
return LoadResult.Page(
data = response.data!!.coinHistoryList as List<TransactionHistoryCardData>,
prevKey = null,
nextKey =
if (response.data!!.isNextPageAvailable.orFalse()) currentKey + 1 else null
)
} catch (exception: Exception) {
exception.log()
_cashHistoryData.update { CoinHistoryCardScreenState.Error }
return LoadResult.Error(throwable = exception)
}
}
override fun mapToList(data: TransactionHistoryResponse?): List<TransactionHistoryCardData> =
data?.coinHistoryList.orEmpty()
companion object {
const val CASH_HISTORY_ITEM_LIMIT = 10
const val PAGINATION_START_KEY_VALUE = 0
}
override fun handleSuccessState(data: TransactionHistoryResponse?) =
_cashHistoryData.update { CoinHistoryCardScreenState.Success }
override fun isNextPageAvailable(data: TransactionHistoryResponse?): Boolean =
data?.isNextPageAvailable.orFalse()
override fun handleFailureState() = _cashHistoryData.update { CoinHistoryCardScreenState.Error }
}

View File

@@ -7,14 +7,13 @@
package com.navi.coin.repo.pagingsource
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.navi.base.utils.orFalse
import com.navi.coin.models.model.TransactionHistoryCardData
import com.navi.coin.models.model.TransactionHistoryResponse
import com.navi.coin.repo.repository.CoinHistoryScreenRepo
import com.navi.coin.utils.constant.Constants.SOMETHING_WENT_WRONG
import com.navi.coin.vm.CoinHistoryCardScreenState
import com.navi.common.utils.log
import com.navi.common.network.models.RepoResult
import com.navi.rr.utils.pagingsource.BasePagingSource
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
@@ -22,54 +21,31 @@ import kotlinx.coroutines.flow.update
class CoinHistoryListSource
@Inject
constructor(private val coinHistoryRepo: CoinHistoryScreenRepo) :
PagingSource<Int, TransactionHistoryCardData>() {
constructor(
private val coinHistoryRepo: CoinHistoryScreenRepo,
) : BasePagingSource<TransactionHistoryCardData, TransactionHistoryResponse>() {
private val _coinHistoryData =
MutableStateFlow<CoinHistoryCardScreenState>(CoinHistoryCardScreenState.Loading)
val coinHistoryData = _coinHistoryData.asStateFlow()
companion object {
const val COIN_HISTORY_ITEM_LIMIT = 10
const val PAGINATION_START_KEY_VALUE = 0
}
override val DEFAULT_PAGE_SIZE: Int = 10
override fun getRefreshKey(state: PagingState<Int, TransactionHistoryCardData>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val page = state.closestPageToPosition(anchorPosition)
page?.prevKey?.minus(1) ?: page?.nextKey?.plus(1)
}
}
override val DEFAULT_START_KEY: Int = 0
override suspend fun load(
params: LoadParams<Int>
): LoadResult<Int, TransactionHistoryCardData> {
try {
val currentKey = params.key ?: PAGINATION_START_KEY_VALUE
val response =
coinHistoryRepo.fetchCoinsListData(
pageNo = currentKey,
pageSize = COIN_HISTORY_ITEM_LIMIT
)
if (
response.error != null ||
response.errors.isNullOrEmpty().not() ||
response.data == null
) {
_coinHistoryData.update { CoinHistoryCardScreenState.Error }
return LoadResult.Error(Throwable(SOMETHING_WENT_WRONG))
}
_coinHistoryData.update { CoinHistoryCardScreenState.Success }
return LoadResult.Page(
data = response.data!!.coinHistoryList as List<TransactionHistoryCardData>,
prevKey = null,
nextKey =
if (response.data!!.isNextPageAvailable.orFalse()) currentKey + 1 else null
)
} catch (exception: Exception) {
exception.log()
_coinHistoryData.update { CoinHistoryCardScreenState.Error }
return LoadResult.Error(throwable = exception)
}
}
override suspend fun fetchData(
page: Int,
pageSize: Int,
): RepoResult<TransactionHistoryResponse> = coinHistoryRepo.fetchCoinsListData(page, pageSize)
override fun mapToList(data: TransactionHistoryResponse?): List<TransactionHistoryCardData> =
data?.coinHistoryList.orEmpty()
override fun isNextPageAvailable(data: TransactionHistoryResponse?): Boolean =
data?.isNextPageAvailable.orFalse()
override fun handleSuccessState(data: TransactionHistoryResponse?) =
_coinHistoryData.update { CoinHistoryCardScreenState.Success }
override fun handleFailureState() = _coinHistoryData.update { CoinHistoryCardScreenState.Error }
}

View File

@@ -7,13 +7,13 @@
package com.navi.coin.repo.pagingsource
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.navi.base.utils.orFalse
import com.navi.coin.models.model.ScratchCard
import com.navi.coin.models.model.ScratchCardHistoryResponse
import com.navi.coin.models.states.ScratchCardPaginatedHistoryScreenState
import com.navi.coin.repo.repository.ScratchCardHistoryScreenRepo
import com.navi.coin.utils.constant.Constants.SOMETHING_WENT_WRONG
import com.navi.common.network.models.RepoResult
import com.navi.rr.utils.pagingsource.BasePagingSource
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
@@ -21,8 +21,9 @@ import kotlinx.coroutines.flow.update
class ScratchCardHistoryListSource
@Inject
constructor(private val scratchCardHistoryScreenRepo: ScratchCardHistoryScreenRepo) :
PagingSource<Int, ScratchCard>() {
constructor(
private val scratchCardHistoryScreenRepo: ScratchCardHistoryScreenRepo,
) : BasePagingSource<ScratchCard, ScratchCardHistoryResponse>() {
private val _scratchCardHistoryListData =
MutableStateFlow<ScratchCardPaginatedHistoryScreenState>(
@@ -30,60 +31,29 @@ constructor(private val scratchCardHistoryScreenRepo: ScratchCardHistoryScreenRe
)
val scratchCardHistoryListData = _scratchCardHistoryListData.asStateFlow()
override fun getRefreshKey(state: PagingState<Int, ScratchCard>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val page = state.closestPageToPosition(anchorPosition)
page?.prevKey?.minus(1) ?: page?.nextKey?.plus(1)
override val DEFAULT_PAGE_SIZE: Int = 10
override val DEFAULT_START_KEY: Int = 0
override suspend fun fetchData(
page: Int,
pageSize: Int,
): RepoResult<ScratchCardHistoryResponse> =
scratchCardHistoryScreenRepo.fetchScratchCardListData(page, pageSize)
override fun mapToList(data: ScratchCardHistoryResponse?): List<ScratchCard> =
data?.scratchCardHistoryList.orEmpty()
override fun isNextPageAvailable(data: ScratchCardHistoryResponse?): Boolean =
data?.isNextPageAvailable.orFalse()
override fun handleSuccessState(data: ScratchCardHistoryResponse?) =
if (data?.scratchCardHistoryList.isNullOrEmpty()) {
_scratchCardHistoryListData.update { ScratchCardPaginatedHistoryScreenState.Empty }
} else {
_scratchCardHistoryListData.update { ScratchCardPaginatedHistoryScreenState.Success }
}
}
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ScratchCard> {
try {
val currentKey =
if (params is LoadParams.Refresh) {
PAGINATION_START_KEY_VALUE
} else {
params.key ?: PAGINATION_START_KEY_VALUE
}
val response =
scratchCardHistoryScreenRepo.fetchScratchCardListData(
pageNo = currentKey,
pageSize = SCRATCH_CARD_HISTORY_ITEM_LIMIT
)
if (
response.error != null ||
response.errors.isNullOrEmpty().not() ||
response.data == null
) {
_scratchCardHistoryListData.update { ScratchCardPaginatedHistoryScreenState.Error }
return LoadResult.Error(Throwable(SOMETHING_WENT_WRONG))
}
if (
currentKey == PAGINATION_START_KEY_VALUE &&
response.data?.scratchCardHistoryList.isNullOrEmpty()
)
_scratchCardHistoryListData.update { ScratchCardPaginatedHistoryScreenState.Empty }
else
_scratchCardHistoryListData.update {
ScratchCardPaginatedHistoryScreenState.Success
}
return LoadResult.Page(
data = response.data?.scratchCardHistoryList as List<ScratchCard>,
prevKey = null,
nextKey = if (response.data?.isNextPageAvailable.orFalse()) currentKey + 1 else null
)
} catch (exception: Exception) {
_scratchCardHistoryListData.update { ScratchCardPaginatedHistoryScreenState.Error }
return LoadResult.Error(throwable = exception)
}
}
suspend fun refresh() {
invalidate()
}
companion object {
const val SCRATCH_CARD_HISTORY_ITEM_LIMIT = 10
const val PAGINATION_START_KEY_VALUE = 0
}
override fun handleFailureState() =
_scratchCardHistoryListData.update { ScratchCardPaginatedHistoryScreenState.Error }
}

View File

@@ -70,8 +70,8 @@ constructor(
Pager(
config =
PagingConfig(
pageSize = CoinHistoryListSource.COIN_HISTORY_ITEM_LIMIT,
prefetchDistance = CoinHistoryListSource.COIN_HISTORY_ITEM_LIMIT
pageSize = coinHistoryListSource.DEFAULT_PAGE_SIZE,
prefetchDistance = cashHistoryListSource.DEFAULT_PAGE_SIZE
)
) {
coinHistoryListSource
@@ -83,8 +83,8 @@ constructor(
Pager(
config =
PagingConfig(
pageSize = CashHistoryListSource.CASH_HISTORY_ITEM_LIMIT,
prefetchDistance = CashHistoryListSource.CASH_HISTORY_ITEM_LIMIT
pageSize = cashHistoryListSource.DEFAULT_PAGE_SIZE,
prefetchDistance = cashHistoryListSource.DEFAULT_PAGE_SIZE
)
) {
cashHistoryListSource

View File

@@ -84,9 +84,8 @@ constructor(
Pager(
config =
PagingConfig(
pageSize = ScratchCardHistoryListSource.SCRATCH_CARD_HISTORY_ITEM_LIMIT,
prefetchDistance =
ScratchCardHistoryListSource.SCRATCH_CARD_HISTORY_ITEM_LIMIT
pageSize = scratchCardHistoryListSource.DEFAULT_PAGE_SIZE,
prefetchDistance = scratchCardHistoryListSource.DEFAULT_PAGE_SIZE
),
pagingSourceFactory = {
ScratchCardHistoryListSource(

View File

@@ -49,7 +49,6 @@ import com.navi.design.font.FontWeightEnum
import com.navi.design.theme.getFontWeight
import com.navi.design.theme.ttComposeFontFamily
import com.navi.rr.R
import com.navi.rr.common.activity.RRBaseActivity
import com.navi.rr.common.models.RRErrorData
import com.navi.rr.common.vm.RRErrorVM
import com.navi.rr.milestones.ui.themes.defaultPurple
@@ -77,7 +76,7 @@ fun RRErrorScreen(
var subtitle by remember { mutableStateOf(Constants.WE_ARE_FACING_TECHNICAL_ISSUE) }
val buttonText = ReferralHomeConstants.TRY_AGAIN
val errorImage = viewModel.getErrorImageAsPerStatusCode(error.value.statusCode)
val context = LocalContext.current as RRBaseActivity
val context = LocalContext.current as Activity
BackHandler {
viewModel.hideError()

View File

@@ -0,0 +1,77 @@
/*
*
* * Copyright © 2024 by Navi Technologies Limited
* * All rights reserved. Strictly confidential
*
*/
package com.navi.rr.utils.pagingsource
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.navi.common.network.models.RepoResult
import com.navi.common.utils.isValidResponse
import com.navi.common.utils.log
abstract class BasePagingSource<IndividualItemResponseType : Any, NetworkResponseType : Any> :
PagingSource<Int, IndividualItemResponseType>() {
abstract val DEFAULT_PAGE_SIZE: Int
abstract val DEFAULT_START_KEY: Int
override fun getRefreshKey(state: PagingState<Int, IndividualItemResponseType>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val page = state.closestPageToPosition(anchorPosition)
page?.prevKey?.minus(1) ?: page?.nextKey?.plus(1)
}
}
override suspend fun load(
params: LoadParams<Int>,
): LoadResult<Int, IndividualItemResponseType> {
val currentKey =
if (params is LoadParams.Refresh) {
DEFAULT_START_KEY
} else {
params.key ?: DEFAULT_START_KEY
}
return try {
val response = fetchData(currentKey, DEFAULT_PAGE_SIZE)
if (response.isValidResponse()) {
handleFailureState()
LoadResult.Error(Exception("Something went wrong"))
} else {
val data = response.data
handleSuccessState(data)
LoadResult.Page(
data = mapToList(data),
prevKey = null,
nextKey = if (isNextPageAvailable(data)) currentKey + 1 else null
)
}
} catch (exception: Exception) {
exception.log()
handleFailureState()
LoadResult.Error(exception)
}
}
fun refresh() {
invalidate()
}
protected abstract suspend fun fetchData(
page: Int,
pageSize: Int,
): RepoResult<NetworkResponseType>
protected abstract fun mapToList(data: NetworkResponseType?): List<IndividualItemResponseType>
protected abstract fun isNextPageAvailable(data: NetworkResponseType?): Boolean
abstract fun handleSuccessState(data: NetworkResponseType?)
abstract fun handleFailureState()
}