Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Convert bindingAdapters to Java #90

Merged
merged 12 commits into from
Apr 14, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.goooler.demoapp.adapter.rv.core;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Collections;
import java.util.List;

public interface BindingAdapters {

@BindingAdapter("binding_rv_dataList")
static <M extends IVhModelType> void bindingSetList(
@NonNull RecyclerView recyclerView,
@Nullable List<M> list
) {
if (recyclerView.getAdapter() instanceof IMutableRvAdapter) {
if (list == null) {
list = Collections.emptyList();
}
//noinspection unchecked
((IMutableRvAdapter<M>) recyclerView.getAdapter()).setList(list);
}
}

@BindingAdapter("binding_rv_refreshItems")
static <M extends IVhModelType> void bindingRefreshItems(
@NonNull RecyclerView recyclerView,
@Nullable List<M> list
) {
if (recyclerView.getAdapter() instanceof IMutableRvAdapter) {
if (list == null) {
list = Collections.emptyList();
}
//noinspection unchecked
((IMutableRvAdapter<M>) recyclerView.getAdapter()).refreshItems(list);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.IntRange
import androidx.annotation.LayoutRes
import androidx.databinding.BindingAdapter
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -67,15 +66,3 @@ internal interface IMutableRvAdapter<M : IVhModelType> : IRvAdapter<M> {

fun removeItem(item: M)
}

@BindingAdapter(value = ["binding_rv_dataList"], requireAll = true)
fun <M : IVhModelType> RecyclerView.setList(list: List<M>?) {
@Suppress("UNCHECKED_CAST")
(adapter as? IMutableRvAdapter<M>)?.list = list.orEmpty()
}

@BindingAdapter(value = ["binding_rv_refreshItems"], requireAll = true)
fun <M : IVhModelType> RecyclerView.refreshItems(items: List<M>?) {
@Suppress("UNCHECKED_CAST")
(adapter as? IMutableRvAdapter<M>)?.refreshItems(items.orEmpty())
}
Loading