Библиотека подкачки Android с привязкой данных - PullRequest
0 голосов
/ 03 июля 2019

Я попытался реализовать библиотеку подкачки Android с привязкой данных.Я не могу реализовать это с общедоступными источниками.Можно ли использовать библиотеку подкачки с адаптером привязки данных и привязки?

Проект на Github: https://github.com/DoomMortal/paging

Может кто-нибудь разобраться в этом?Любая помощь будет оценена.

Мой адаптер:

class OverviewAdapter(val onClickListener: OnClickListener) : PagedListAdapter<VideoProperty, OverviewAdapter.ViewHolder>(DiffCallback) {

    class ViewHolder private constructor(private var binding: OverviewItemBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bind(clickListener: OnClickListener, videoProperty: VideoProperty){
            binding.property = videoProperty
            binding.clickListener = clickListener
            binding.executePendingBindings()
        }

        companion object {
            fun from(parent: ViewGroup): ViewHolder {
                val layoutInflater = LayoutInflater.from(parent.context)
                val binding = OverviewItemBinding.inflate(layoutInflater, parent, false)

                return ViewHolder(binding)
            }
        }

    }

    companion object DiffCallback : DiffUtil.ItemCallback<VideoProperty>() {
        override fun areItemsTheSame(oldItem: VideoProperty, newItem: VideoProperty): Boolean {
            return oldItem === newItem
        }

        override fun areContentsTheSame(oldItem: VideoProperty, newItem: VideoProperty): Boolean {
            return oldItem.videoTableId == newItem.videoTableId
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder.from(parent)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        when (holder) {
            is ViewHolder -> {
                val nightItem = getItem(position) as VideoProperty
                holder.bind(onClickListener, nightItem)
            }
        }

    }

    class OnClickListener(val clicklistener: (videoProperty: VideoProperty) -> Unit) {
        fun onClick(videoProperty: VideoProperty) = clicklistener(videoProperty)
    }

}

ViewModel:

enum class MarsApiStatus { LOADING, ERROR, DONE }

class OverviewViewModel : ViewModel() {

    private val TAG = "OverviewViewModel"

    private var executor: Executor? = null

    // The internal MutableLiveData String that stores the status of the most recent request
    private val _status = MutableLiveData<MarsApiStatus>()

    // The external immutable LiveData for the request status String
    val status: LiveData<MarsApiStatus>
        get() = _status

    private val _properties = MutableLiveData<VideosOverview>()

    val properties: LiveData<VideosOverview>
        get() = _properties

    private val _navigateToSelectedProperty = MutableLiveData<VideoProperty>()
    val navigateToSelectedProperty: LiveData<VideoProperty>
        get() = _navigateToSelectedProperty

    private var viewModelJob = Job()
    private val coroutineScope =  CoroutineScope(viewModelJob + Dispatchers.Main)

    //val itemPagedList: LiveData<PagedList<VideosOverview>>

    var itemPagedList: LiveData<PagedList<VideosOverview>>
    var liveDataSource: LiveData<PageKeyedDataSource<Int, VideosOverview>>? = null

    /**
     * Call getMarsRealEstateProperties() on init so we can display status immediately.
     */
    init {
        //getMarsRealEstateProperties(VideosOverviewApiFilter.SHOW_POOP)

        executor = Executors.newFixedThreadPool(5)

        val feedDataFactory = ItemDataSourceFactory()

        val pagedListConfig = PagedList.Config.Builder()
                .setEnablePlaceholders(false)
                .setPageSize(ItemVideosDataSource.PAGE_SIZE)
                .build()

        /*_properties.value = LivePagedListBuilder(feedDataFactory, pagedListConfig)
                .build()*/


        liveDataSource = feedDataFactory.getItemLiveDataSource()
        itemPagedList = LivePagedListBuilder(feedDataFactory, pagedListConfig).build()

    }

    /**
     * Sets the value of the status LiveData to the VideosOverview API status.
     */
    private fun getMarsRealEstateProperties(filter: VideosOverviewApiFilter, page: Long) {

        coroutineScope.launch {
            //val getPropertiesDeferred =
                try{
                    _status.value = MarsApiStatus.LOADING
                    val listResult = VideosOverviewApi.retrofitService.getProperties(filter.value, page)

                    Log.d("CONTENT: ", filter.value.toString())
                    Log.d("CONTENT: ", page.toString())
                    Log.d("CONTENT: ", listResult.items[0].toString())

                    _status.value = MarsApiStatus.DONE
                    if (listResult.items.size > 0) {
                        _properties.value = listResult
                    }
                } catch (e: Throwable){
                    Log.d(TAG, e.toString())
                    _status.value = MarsApiStatus.ERROR
                    //_properties.value = ArrayList() OLD
                }

        }

    }

    override fun onCleared() {
        super.onCleared()
        viewModelJob.cancel()
    }

    fun displayPropertyDetails(marsProperty: VideoProperty) {
        _navigateToSelectedProperty.value = marsProperty
    }

    fun displayPropertyDetailsComplete() {
        _navigateToSelectedProperty.value = null
    }

    fun updateFilter(filter: VideosOverviewApiFilter, page: Long){
        getMarsRealEstateProperties(filter, page)
    }
}

Источник данных:

class ItemVideosDataSource : PageKeyedDataSource<Long, VideosOverview>() {

    companion object {
        val FIRST_PAGE: Long = 1
        val PAGE_SIZE: Int = 50
    }

    private val TAG = "ItemVideosDataSource"

    private var viewModelJob = Job()
    private val coroutineScope =  CoroutineScope(viewModelJob + Dispatchers.Main)

    override fun loadInitial(params: LoadInitialParams<Long>, callback: LoadInitialCallback<Long, VideosOverview>) {
        //videos.retrofitService.getProperties(FIRST_PAGE)

        coroutineScope.launch {
            //val getPropertiesDeferred =

            try{
                val listResult = VideosOverviewApi.retrofitService.getProperties(1, FIRST_PAGE)

                val lest : MutableList<VideosOverview>? = null
                lest!!.add(listResult)


                if (listResult.items.size > 0) {
                    callback.onResult(lest, null, FIRST_PAGE + 1)
                    //_properties.value = listResult
                }
            } catch (e: Throwable){
                Log.d(TAG, e.toString())
            }

        }

    }

    override fun loadBefore(params: LoadParams<Long>, callback: LoadCallback<Long, VideosOverview>) {

        coroutineScope.launch {
            //val getPropertiesDeferred =

            try{
                val listResult = VideosOverviewApi.retrofitService.getProperties(1, FIRST_PAGE)

                val lest : MutableList<VideosOverview>? = null
                lest!!.add(listResult)


                if (listResult.items.size > 0) {

                    val key: Long? = if(params.key > 1) params.key - 1 else null

                    callback.onResult(lest, key)
                    //_properties.value = listResult
                }
            } catch (e: Throwable){
                Log.d(TAG, e.toString())
            }

        }
    }

    override fun loadAfter(params: LoadParams<Long>, callback: LoadCallback<Long, VideosOverview>) {

        coroutineScope.launch {
            //val getPropertiesDeferred =

            try{
                val listResult = VideosOverviewApi.retrofitService.getProperties(1, FIRST_PAGE)

                val lest : MutableList<VideosOverview>? = null
                lest!!.add(listResult)


                if (listResult.items.size > 0) {

                    val key: Long? = if(params.key > 1) params.key + 1 else null

                    callback.onResult(lest, key)
                    //_properties.value = listResult
                }
            } catch (e: Throwable){
                Log.d(TAG, e.toString())
            }

        }

    }

}

Метод фабрики:

class ItemDataSourceFactory : DataSource.Factory<Long, VideosOverview>() {

    //val sourceLiveData = MutableLiveData<ItemVideosDataSource>()
    private val itemLiveDataSource = MutableLiveData<PageKeyedDataSource<Int, VideosOverview>>()

    override fun create(): DataSource<Long, VideosOverview> {

        //getting our data source object
        val itemDataSource = ItemVideosDataSource()

        //posting the datasource to get the values
        //itemLiveDataSource.postValue(itemDataSource)

        //returning the datasource
        return itemDataSource

    }

    fun getItemLiveDataSource(): MutableLiveData<PageKeyedDataSource<Int, VideosOverview>> {
        return itemLiveDataSource
    }

}

Компоновка:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_overview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:clipToPadding="false"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:listData="@{viewModel.itemPagedList}"
    tools:listitem="@layout/overview_item" />

Связующий адаптер

@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: PagedList<VideoProperty>?){
//fun bindRecyclerView(recyclerView: RecyclerView, data: List<VideoProperty>?){
    val adapter = recyclerView.adapter as OverviewAdapter
    adapter.submitList(data)
}

1 Ответ

0 голосов
/ 04 июля 2019

На всякий случай другого вида решения я использовал этот пример и работаю

https://github.com/sharmadhiraj/AndroidPagingLibraryStepByStepImplementationGuide

...