<commit_before>package fr.free.nrw.commons.contributions;

import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.ContributionsListAdapter.Callback;
import fr.free.nrw.commons.media.MediaClient;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class ContributionViewHolder extends RecyclerView.ViewHolder {

    private final Callback callback;
    @BindView(R.id.contributionImage)
    SimpleDraweeView imageView;
    @BindView(R.id.contributionTitle)
    TextView titleView;
    @BindView(R.id.authorView)
    TextView authorView;
    @BindView(R.id.contributionState)
    TextView stateView;
    @BindView(R.id.contributionSequenceNumber)
    TextView seqNumView;
    @BindView(R.id.contributionProgress)
    ProgressBar progressView;
    @BindView(R.id.image_options)
    RelativeLayout imageOptions;
    @BindView(R.id.wikipediaButton)
    ImageButton addToWikipediaButton;
    @BindView(R.id.retryButton)
    ImageButton retryButton;
    @BindView(R.id.cancelButton)
    ImageButton cancelButton;
    @BindView(R.id.pauseResumeButton)
    ImageButton pauseResumeButton;


    private int position;
    private Contribution contribution;
    private final CompositeDisposable compositeDisposable = new CompositeDisposable();
    private final MediaClient mediaClient;
    private boolean isWikipediaButtonDisplayed;

    ContributionViewHolder(final View parent, final Callback callback,
        final MediaClient mediaClient) {
        super(parent);
        this.mediaClient = mediaClient;
        ButterKnife.bind(this, parent);
        this.callback = callback;
    }

    public void init(final int position, final Contribution contribution) {

        //handling crashes when the contribution is null.
        if (null == contribution) {
            return;
        }

        this.contribution = contribution;
        this.position = position;
        titleView.setText(contribution.getMedia().getMostRelevantCaption());
        authorView.setText(contribution.getMedia().getAuthor());

        //Removes flicker of loading image.
        imageView.getHierarchy().setFadeDuration(0);

        imageView.getHierarchy().setPlaceholderImage(R.drawable.image_placeholder);
        imageView.getHierarchy().setFailureImage(R.drawable.image_placeholder);

        final String imageSource = chooseImageSource(contribution.getMedia().getThumbUrl(),
            contribution.getLocalUri());
        if (!TextUtils.isEmpty(imageSource)) {
            final ImageRequest imageRequest =
                ImageRequestBuilder.newBuilderWithSource(Uri.parse(imageSource))
                    .setProgressiveRenderingEnabled(true)
                    .build();
            imageView.setImageRequest(imageRequest);
        }

        seqNumView.setText(String.valueOf(position + 1));
        seqNumView.setVisibility(View.VISIBLE);

        addToWikipediaButton.setVisibility(View.GONE);
        switch (contribution.getState()) {
            case Contribution.STATE_COMPLETED:
                stateView.setVisibility(View.GONE);
                progressView.setVisibility(View.GONE);
                imageOptions.setVisibility(View.GONE);
                stateView.setText("");
                checkIfMediaExistsOnWikipediaPage(contribution);
                break;
            case Contribution.STATE_QUEUED:
            case Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE:
                stateView.setVisibility(View.VISIBLE);
                progressView.setVisibility(View.GONE);
                stateView.setText(R.string.contribution_state_queued);
                imageOptions.setVisibility(View.GONE);
                break;
            case Contribution.STATE_IN_PROGRESS:
                stateView.setVisibility(View.GONE);
                progressView.setVisibility(View.VISIBLE);
                addToWikipediaButton.setVisibility(View.GONE);
                pauseResumeButton.setVisibility(View.VISIBLE);
                cancelButton.setVisibility(View.GONE);
                retryButton.setVisibility(View.GONE);
                imageOptions.setVisibility(View.VISIBLE);
                final long total = contribution.getDataLength();
                final long transferred = contribution.getTransferred();
                if (transferred == 0 || transferred >= total) {
                    progressView.setIndeterminate(true);
                } else {
                    progressView.setProgress((int) (((double) transferred / (double) total) * 100));
                }
                break;
            case Contribution.STATE_PAUSED:
                stateView.setVisibility(View.VISIBLE);
                stateView.setText(R.string.paused);
                setResume();
                progressView.setVisibility(View.GONE);
                cancelButton.setVisibility(View.GONE);
                retryButton.setVisibility(View.GONE);
                pauseResumeButton.setVisibility(View.VISIBLE);
                imageOptions.setVisibility(View.VISIBLE);
                break;
            case Contribution.STATE_FAILED:
                stateView.setVisibility(View.VISIBLE);
                stateView.setText(R.string.contribution_state_failed);
                progressView.setVisibility(View.GONE);
                cancelButton.setVisibility(View.VISIBLE);
                retryButton.setVisibility(View.VISIBLE);<commit_msg>Show progress when uploading image (#4831)<commit_after>package fr.free.nrw.commons.contributions;

import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.contributions.ContributionsListAdapter.Callback;
import fr.free.nrw.commons.media.MediaClient;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;

public class ContributionViewHolder extends RecyclerView.ViewHolder {

    private final Callback callback;
    @BindView(R.id.contributionImage)
    SimpleDraweeView imageView;
    @BindView(R.id.contributionTitle)
    TextView titleView;
    @BindView(R.id.authorView)
    TextView authorView;
    @BindView(R.id.contributionState)
    TextView stateView;
    @BindView(R.id.contributionSequenceNumber)
    TextView seqNumView;
    @BindView(R.id.contributionProgress)
    ProgressBar progressView;
    @BindView(R.id.image_options)
    RelativeLayout imageOptions;
    @BindView(R.id.wikipediaButton)
    ImageButton addToWikipediaButton;
    @BindView(R.id.retryButton)
    ImageButton retryButton;
    @BindView(R.id.cancelButton)
    ImageButton cancelButton;
    @BindView(R.id.pauseResumeButton)
    ImageButton pauseResumeButton;


    private int position;
    private Contribution contribution;
    private final CompositeDisposable compositeDisposable = new CompositeDisposable();
    private final MediaClient mediaClient;
    private boolean isWikipediaButtonDisplayed;

    ContributionViewHolder(final View parent, final Callback callback,
        final MediaClient mediaClient) {
        super(parent);
        this.mediaClient = mediaClient;
        ButterKnife.bind(this, parent);
        this.callback = callback;
    }

    public void init(final int position, final Contribution contribution) {

        //handling crashes when the contribution is null.
        if (null == contribution) {
            return;
        }

        this.contribution = contribution;
        this.position = position;
        titleView.setText(contribution.getMedia().getMostRelevantCaption());
        authorView.setText(contribution.getMedia().getAuthor());

        //Removes flicker of loading image.
        imageView.getHierarchy().setFadeDuration(0);

        imageView.getHierarchy().setPlaceholderImage(R.drawable.image_placeholder);
        imageView.getHierarchy().setFailureImage(R.drawable.image_placeholder);

        final String imageSource = chooseImageSource(contribution.getMedia().getThumbUrl(),
            contribution.getLocalUri());
        if (!TextUtils.isEmpty(imageSource)) {
            final ImageRequest imageRequest =
                ImageRequestBuilder.newBuilderWithSource(Uri.parse(imageSource))
                    .setProgressiveRenderingEnabled(true)
                    .build();
            imageView.setImageRequest(imageRequest);
        }

        seqNumView.setText(String.valueOf(position + 1));
        seqNumView.setVisibility(View.VISIBLE);

        addToWikipediaButton.setVisibility(View.GONE);
        switch (contribution.getState()) {
            case Contribution.STATE_COMPLETED:
                stateView.setVisibility(View.GONE);
                progressView.setVisibility(View.GONE);
                imageOptions.setVisibility(View.GONE);
                stateView.setText("");
                checkIfMediaExistsOnWikipediaPage(contribution);
                break;
            case Contribution.STATE_QUEUED:
            case Contribution.STATE_QUEUED_LIMITED_CONNECTION_MODE:
                stateView.setVisibility(View.VISIBLE);
                progressView.setVisibility(View.GONE);
                stateView.setText(R.string.contribution_state_queued);
                imageOptions.setVisibility(View.GONE);
                break;
            case Contribution.STATE_IN_PROGRESS:
                stateView.setVisibility(View.GONE);
                progressView.setVisibility(View.VISIBLE);
                addToWikipediaButton.setVisibility(View.GONE);
                pauseResumeButton.setVisibility(View.VISIBLE);
                cancelButton.setVisibility(View.GONE);
                retryButton.setVisibility(View.GONE);
                imageOptions.setVisibility(View.VISIBLE);
                final long total = contribution.getDataLength();
                final long transferred = contribution.getTransferred();
                if (transferred == 0 || transferred >= total) {
                    progressView.setIndeterminate(true);
                } else {
                    progressView.setIndeterminate(false);
                    progressView.setProgress((int) (((double) transferred / (double) total) * 100));
                }
                break;
            case Contribution.STATE_PAUSED:
                stateView.setVisibility(View.VISIBLE);
                stateView.setText(R.string.paused);
                setResume();
                progressView.setVisibility(View.GONE);
                cancelButton.setVisibility(View.GONE);
                retryButton.setVisibility(View.GONE);
                pauseResumeButton.setVisibility(View.VISIBLE);
                imageOptions.setVisibility(View.VISIBLE);
                break;
            case Contribution.STATE_FAILED:
                stateView.setVisibility(View.VISIBLE);
                stateView.setText(R.string.contribution_state_failed);
                progressView.setVisibility(View.GONE);
                cancelButton.setVisibility(View.VISIBLE);
                retryButton.setVisibility(View.VISIBLE);
{'fraction_non_alphanumeric': 0.0859958932238193, 'fraction_numerical': 0.0013141683778234085, 'mean_word_length': 2.748768472906404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '536658', 'n_tokens_mistral': 3343, 'n_tokens_neox': 3129, 'n_words': 485}