Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
How to inject data source factory from android pagination library?

I'm using pagination library and i want to inject datasource into my viewmodel. My factory looks like:

class ArticleDataSourceFactory @Inject constructor(
    val articleRepository: ArticleRepository
) : DataSource.Factory<Long, Article>() {

    override fun create(): DataSource<Long, Article> {
        return ArticleDateKeyedDataSource(articleRepository)
    }
}

My DataSource:

class ArticleDateKeyedDataSource(
    private val repository: ArticleRepository
) : ItemKeyedDataSource<Long, Article>() {
    override fun loadInitial(params: LoadInitialParams<Long>, callback: LoadInitialCallback<Article>) {
        val articles = repository.getInitial(params.requestedInitialKey!!, params.requestedLoadSize)
        callback.onResult(articles)
    }

    override fun loadAfter(params: LoadParams<Long>, callback: LoadCallback<Article>) {
        val articles = repository.getAfter(params.key, params.requestedLoadSize)
        callback.onResult(articles)
    }

    override fun loadBefore(params: LoadParams<Long>, callback: LoadCallback<Article>) {
        val articles = repository.getBefore(params.key, params.requestedLoadSize)
        callback.onResult(articles)
    }

    override fun getKey(item: Article): Long {
        return item.createdAt
    }
}

And my ViewModel:

class ArticleFragmentViewModel @Inject constructor(
    private val dataSourceFactory: ArticleDataSourceFactory
) : BaseViewModel() {

    var initialArticlePosition = 0L

    val navigateToArticleDetails: MutableLiveData<SingleEvent<Long>> = MutableLiveData()

    val articlesLiveList: LiveData<PagedList<Article>>
        get() {
            val config = PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPageSize(5)
                .build()

            return LivePagedListBuilder(dataSourceFactory, config)
                .setInitialLoadKey(initialArticlePosition)
                .setFetchExecutor(Executors.newSingleThreadExecutor())
                .build()
        }


    fun onArticleSelected(createdAt: Long) {
        navigateToArticleDetails.value = SingleEvent(createdAt)
    }
}

After rebuild, i get an error:

error: cannot access DataSource
  class file for androidx.paging.DataSource not found
  Consult the following stack trace for details.    

What does it mean? I have no idea, what i do wrong. For example, i have no problem to inject repository.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Did you use Android Module? You may need to use api dependency in the base module like

api "androidx.paging:paging-runtime-ktx:$paging_version"

And there is similar issue https://stackoverflow.com/a/47128596/5934119

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!