aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/Paging3LoadingStateAdapter.java
blob: e2617c6193be90bace8778dc7d3cc09f616c4550 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package ml.docilealligator.infinityforreddit.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.paging.LoadState;
import androidx.paging.LoadStateAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.button.MaterialButton;

import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;

public class Paging3LoadingStateAdapter extends LoadStateAdapter<Paging3LoadingStateAdapter.LoadStateViewHolder> {
    private final BaseActivity activity;
    private final CustomThemeWrapper mCustomThemeWrapper;
    private final int mErrorStringId;
    private final View.OnClickListener mRetryCallback;

    public Paging3LoadingStateAdapter(BaseActivity activity, CustomThemeWrapper customThemeWrapper, int errorStringId, View.OnClickListener retryCallback) {
        this.activity = activity;
        this.mCustomThemeWrapper = customThemeWrapper;
        this.mErrorStringId = errorStringId;
        this.mRetryCallback = retryCallback;
    }

    @Override
    public void onBindViewHolder(@NonNull LoadStateViewHolder loadStateViewHolder, @NonNull LoadState loadState) {
        loadStateViewHolder.bind(loadState);
    }

    @NonNull
    @Override
    public LoadStateViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, @NonNull LoadState loadState) {
        return new LoadStateViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_paging_3_load_state, viewGroup, false), mRetryCallback);
    }

    class LoadStateViewHolder extends RecyclerView.ViewHolder {
        private final ProgressBar mProgressBar;
        private final RelativeLayout mErrorView;
        private final TextView mErrorMsg;
        private final MaterialButton mRetry;

        LoadStateViewHolder(@NonNull View itemView, @NonNull View.OnClickListener retryCallback) {
            super(itemView);

            mProgressBar = itemView.findViewById(R.id.progress_bar_item_paging_3_load_state);
            mErrorView = itemView.findViewById(R.id.error_view_relative_layout_item_paging_3_load_state);
            mErrorMsg = itemView.findViewById(R.id.error_text_view_item_paging_3_load_state);
            mRetry = itemView.findViewById(R.id.retry_button_item_paging_3_load_state);

            mErrorMsg.setText(mErrorStringId);
            mErrorMsg.setTextColor(mCustomThemeWrapper.getPrimaryTextColor());
            mRetry.setBackgroundColor(mCustomThemeWrapper.getColorPrimaryLightTheme());
            mRetry.setTextColor(mCustomThemeWrapper.getButtonTextColor());
            mRetry.setOnClickListener(retryCallback);
            mErrorView.setOnClickListener(retryCallback);

            if (activity.typeface != null) {
                mErrorMsg.setTypeface(activity.typeface);
                mRetry.setTypeface(activity.typeface);
            }
        }

        public void bind(LoadState loadState) {
            mProgressBar.setVisibility(loadState instanceof LoadState.Loading
                    ? View.VISIBLE : View.GONE);
            mErrorView.setVisibility(loadState instanceof LoadState.Error
                    ? View.VISIBLE : View.GONE);
        }
    }
}