X-Git-Url: https://git.mdrn.pl/wl-app.git/blobdiff_plain/48b2fe9f7c2dc3d9aeaaa6dbfb27c7da4f3235ff..269195b3729c1bdc22e9053ee4ebca667ea8549d:/Android/app/src/main/java/com/moiseum/wolnelektury/components/ProgressRecyclerView.java diff --git a/Android/app/src/main/java/com/moiseum/wolnelektury/components/ProgressRecyclerView.java b/Android/app/src/main/java/com/moiseum/wolnelektury/components/ProgressRecyclerView.java new file mode 100644 index 0000000..cccb42a --- /dev/null +++ b/Android/app/src/main/java/com/moiseum/wolnelektury/components/ProgressRecyclerView.java @@ -0,0 +1,141 @@ +package com.moiseum.wolnelektury.components; + +import android.content.Context; +import android.content.res.TypedArray; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.annotation.StringRes; +import android.support.v7.widget.RecyclerView; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.FrameLayout; +import android.widget.ImageButton; +import android.widget.ProgressBar; +import android.widget.TextView; + +import com.moiseum.wolnelektury.R; +import com.moiseum.wolnelektury.components.recycler.RecyclerAdapter; + +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; +import butterknife.OnClick; + +/** + * @author golonkos + */ + +public class ProgressRecyclerView extends FrameLayout { + + public interface ProgressRecycleViewRetryListener { + void onRetryClicked(); + } + + @BindView(R.id.rvList) + RecyclerView rvList; + @BindView(R.id.tvEmpty) + TextView tvEmpty; + @BindView(R.id.pbLoading) + ProgressBar pbLoading; + @BindView(R.id.ibRetry) + ImageButton ibRetry; + + private RecyclerAdapter adapter; + private ProgressRecycleViewRetryListener listener; + + public ProgressRecyclerView(@NonNull Context context) { + this(context, null); + } + + public ProgressRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) { + this(context, attrs, 0); + } + + public ProgressRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(attrs); + } + + private void init(AttributeSet attrs) { + View view = LayoutInflater.from(getContext()).inflate(R.layout.progress_recyclerview, this, true); + ButterKnife.bind(this, view); + + TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ProgressRecyclerView); + try { + if (a.hasValue(R.styleable.ProgressRecyclerView_emptyText)) { + tvEmpty.setText(a.getString(R.styleable.ProgressRecyclerView_emptyText)); + } + } finally { + a.recycle(); + } + } + + public void setAdapter(RecyclerAdapter adapter) { + this.adapter = adapter; + rvList.setAdapter(adapter); + } + + public void setItems(List items) { + if (adapter == null) { + throw new UnsupportedOperationException("Adapter not set"); + } + adapter.setItems(items); + tvEmpty.setVisibility(items.isEmpty() ? VISIBLE : GONE); + } + + public void addItems(List items) { + if (adapter == null) { + throw new UnsupportedOperationException("Adapter not set"); + } + if (items.size() > 0) { + adapter.addItems(items); + } + } + + public void setProgressVisible(boolean visible) { + pbLoading.setVisibility(visible ? VISIBLE : GONE); + if (visible) { + tvEmpty.setVisibility(GONE); + } + } + + public void showRetryButton(ProgressRecycleViewRetryListener listener) { + this.listener = listener; + tvEmpty.setVisibility(GONE); + ibRetry.setVisibility(VISIBLE); + } + + public void setEmptyText(@StringRes int stringResId) { + tvEmpty.setText(stringResId); + } + + public void setLayoutManager(RecyclerView.LayoutManager layoutManager) { + rvList.setLayoutManager(layoutManager); + } + + public void setHasFixedSize(boolean fixedSize) { + rvList.setHasFixedSize(fixedSize); + } + + public void addOnScrollListener(RecyclerView.OnScrollListener listener) { + rvList.addOnScrollListener(listener); + } + + public void removeOnScrollListener(RecyclerView.OnScrollListener listener) { + rvList.removeOnScrollListener(listener); + } + + @OnClick(R.id.ibRetry) + public void retryButtonClick() { + if (listener != null) { + listener.onRetryClicked(); + } + ibRetry.setVisibility(GONE); + } + + public void updateEmptyViewVisibility() { + tvEmpty.setVisibility(ibRetry.getVisibility() == GONE && adapter.getItems().isEmpty() ? VISIBLE : GONE); + } +}