1

I'm making an app with NASA APOD api with paging library. I use boundarycallback class for caching. My problem is that when there is already cached apod items in the database from yesterday, the new pictures won't show in the recycelrview unless I use onItemAtFrontLoaded() and scroll up. The only way I found was to nuke the database and insert all items again.

class ApodBoundaryCallback(
    private var api: ApodApi,
    private val cache: ApodDao,
    private val disposable: CompositeDisposable
)
    : PagedList.BoundaryCallback<ApodEntity>() {

    val networkState = MutableLiveData<NetworkState>()
    private val TAG = ApodBoundaryCallback::class.java.simpleName
    private var formatter = SimpleDateFormat("yyyy-MM-dd", Locale.US)


    override fun onZeroItemsLoaded() {
        Log.d(TAG, "onZeroItemsLoaded")
        if (networkState.value?.status == Status.RUNNING)
            return

        networkState.postValue(NetworkState.LOADING)

         getApodByDate(addOrSubFromDate(formatter, -20), "")

    }

    override fun onItemAtEndLoaded(itemAtEnd: ApodEntity) {
        Log.d(TAG, "onItemAtEndLoaded")

        if (networkState.value?.status == Status.RUNNING)
            return
        networkState.postValue(NetworkState.LOADING)

        val endDate = addOrSubFromStringDate(formatter, -1, itemAtEnd.date)
        val startDate = addOrSubFromStringDate(formatter, -21, itemAtEnd.date)

        getApodByDate(startDate, endDate)

    }

    override fun onItemAtFrontLoaded(itemAtFront: ApodEntity) {
        if (networkState.value?.status == Status.RUNNING)
            return
        networkState.postValue(NetworkState.LOADING)

        getApodByDate(addOrSubFromStringDate(formatter, +1, itemAtFront.date), "")
    }

    private fun getApodByDate(startDate:String, endDate:String){

        disposable.add(
            api
                .getPictures(
                    startDate,
                    endDate,
                    true,
                    Constants.API_KEY)
                .subscribeOn(Schedulers.io())

                .map { it.filter {apodEntity ->  apodEntity.mediaType == "image" } }
                .subscribe(
                    {
                        networkState.postValue(NetworkState.LOADED)
                        cache.insertApodList(it)
                    },
                    {
                        Log.e("ApodBoundary", it.message)
                        networkState.postValue(NetworkState.error(it.message))
                    }

                )

        )
    }


    private fun addOrSubFromStringDate(formatter: SimpleDateFormat, days: Int, Date: String): String{
        val cal = Calendar.getInstance()
        cal.time = formatter.parse(Date);
        cal.add(Calendar.DATE, days)
        return formatter.format(cal.time)
    }

    private fun addOrSubFromDate(formatter: SimpleDateFormat, days: Int): String{
        val cal = Calendar.getInstance()
        cal.add(Calendar.DATE, days)
        return formatter.format(cal.time)
    }


}
  • 2
    Could you please provide more info on your implementation please? How is your BoundaryCallback implemented? Are you using Room or any other approach for caching? Are you using a custom DataSource (and how is it implemented)? – Sarquella Apr 06 '20 at 21:59
  • 2
    More code from your boundaryCallback will be helpful in finding the solution. – Nataraj KR Apr 07 '20 at 09:43
  • 2
    Please post what you have tried so far - share your current code/solution. – Boken Apr 07 '20 at 21:37
  • 1
    I'm using room. I feel like the problem is with the library. I read google samples. –  Apr 07 '20 at 23:48

0 Answers0