• 7
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

I'm trying to implement a chat app with Paging Library, but my DataSource never triggers loadBefore and loadAfter, but triggers loadInitial and I can show the initial data in my RecyclerView. I'm using Room too. I have searched for some tutorials, but none has helped me solve this problem.

Can anybody help me?

Thank you!

ChatDataSourceFactory.kt

class ChatDataSourceFactory(private val userId : Long, private val partnerId : Long): DataSource.Factory<Long, MessageAttachments>() {

    val source = MutableLiveData<ChatDataSource>()

    private lateinit var latestSource: ChatDataSource

    override fun create(): DataSource<Long, MessageAttachments> {
        latestSource = ChatDataSource(userId, partnerId)
        source.postValue(latestSource)
        return latestSource
    }

}

ChatDataSource.kt

class ChatDataSource (private val userId : Long, private val partnerId : Long): ItemKeyedDataSource<Long, MessageAttachments>() {

    private val compositeDisposable = CompositeDisposable()

    override fun loadInitial(params: LoadInitialParams<Long>, callback: LoadInitialCallback<MessageAttachments>) {
        compositeDisposable += MessageRepository.getMessageAttachmentsWith(userId, partnerId, params.requestedInitialKey ?: Long.MAX_VALUE, params.requestedLoadSize.toLong())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    {
                        callback.onResult(it, 0, it.size)
                    },
                    {
                        Log.e("ChatDataSource", it.toString())
                    })
    }

    override fun loadAfter(params: LoadParams<Long>, callback: LoadCallback<MessageAttachments>) {
        Log.d("ChatDataSource", "loadAfter")
    }

    override fun loadBefore(params: LoadParams<Long>, callback: LoadCallback<MessageAttachments>) {
        Log.d("ChatDataSource", "loadBefore")
    }

    override fun getKey(item: MessageAttachments): Long = item.message?.localMessageId ?: -1

    override fun invalidate() {
        super.invalidate()
        if(!compositeDisposable.isDisposed)
            compositeDisposable.dispose()
    }
}

ChatViewModel.kt

class ChatViewModel(application: Application, partner : Employee) : BaseAndroidViewModel(application){

    private val chatDataSourceFactory = ChatDataSourceFactory(UserConfig.userId, partner.employeeId ?: -1)

    val chat = LivePagedListBuilder(chatDataSourceFactory, pagedListConfig())
        .setBoundaryCallback(object : PagedList.BoundaryCallback<MessageAttachments>(){
            override fun onItemAtEndLoaded(itemAtEnd: MessageAttachments) {
                super.onItemAtEndLoaded(itemAtEnd)
            }

            override fun onZeroItemsLoaded() {
                super.onZeroItemsLoaded()
            }

            override fun onItemAtFrontLoaded(itemAtFront: MessageAttachments) {
                super.onItemAtFrontLoaded(itemAtFront)
            }
        }) .build()

    private fun pagedListConfig() = PagedList.Config.Builder()
        .setInitialLoadSizeHint(20)
        .setEnablePlaceholders(false)
        .setPageSize(20 * 2)
        .build()
}

chat_fragment.xml

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".fragments.communication.ChatPagedPartnerFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewChat"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:scrollbars="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</layout>