package it.integry.integrywmsnative.core.expansion;

import android.widget.BaseAdapter;

import androidx.databinding.ObservableList;

import java.lang.ref.WeakReference;

public class WeakReferenceOnListChangedCallback extends ObservableList.OnListChangedCallback {

    private final WeakReference<BaseAdapter> adapterReference;

    public WeakReferenceOnListChangedCallback(BaseAdapter baseAdapter) {
        this.adapterReference = new WeakReference<>(baseAdapter);
    }

    @Override
    public void onChanged(ObservableList sender) {
        BaseAdapter adapter = adapterReference.get();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemRangeChanged(ObservableList sender, int positionStart, int itemCount) {
        BaseAdapter adapter = adapterReference.get();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemRangeInserted(ObservableList sender, int positionStart, int itemCount) {
        BaseAdapter adapter = adapterReference.get();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemRangeMoved(ObservableList sender, int fromPosition, int toPosition, int itemCount) {
        BaseAdapter adapter = adapterReference.get();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemRangeRemoved(ObservableList sender, int positionStart, int itemCount) {
        BaseAdapter adapter = adapterReference.get();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }
}
