GitNex-Android-App/app/src/main/java/org/mian/gitnex/adapters/PullRequestsAdapter.java

312 lines
9.8 KiB
Java
Raw Normal View History

2019-11-24 13:42:57 +01:00
package org.mian.gitnex.adapters;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Handler;
import android.view.Gravity;
2019-11-24 13:42:57 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
2019-11-24 13:42:57 +01:00
import android.widget.ImageView;
import android.widget.LinearLayout;
2019-11-24 13:42:57 +01:00
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.content.res.ResourcesCompat;
import androidx.core.text.HtmlCompat;
2019-11-24 13:42:57 +01:00
import androidx.recyclerview.widget.RecyclerView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.vdurmont.emoji.EmojiParser;
import java.util.List;
import java.util.Locale;
import org.gitnex.tea4j.v2.models.PullRequest;
2019-11-24 13:42:57 +01:00
import org.mian.gitnex.R;
import org.mian.gitnex.activities.IssueDetailActivity;
import org.mian.gitnex.activities.ProfileActivity;
import org.mian.gitnex.activities.RepoDetailActivity;
import org.mian.gitnex.clients.PicassoService;
import org.mian.gitnex.helpers.AppUtil;
import org.mian.gitnex.helpers.ClickListener;
import org.mian.gitnex.helpers.ColorInverter;
import org.mian.gitnex.helpers.LabelWidthCalculator;
import org.mian.gitnex.helpers.RoundedTransformation;
import org.mian.gitnex.helpers.TimeHelper;
import org.mian.gitnex.helpers.TinyDB;
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
import org.mian.gitnex.helpers.contexts.IssueContext;
2019-11-24 13:42:57 +01:00
/**
* @author M M Arif
2019-11-24 13:42:57 +01:00
*/
public class PullRequestsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
2019-11-24 13:42:57 +01:00
private final Context context;
private List<PullRequest> prList;
private Runnable loadMoreListener;
private boolean isLoading = false, isMoreDataAvailable = true;
2019-11-24 13:42:57 +01:00
public PullRequestsAdapter(Context context, List<PullRequest> prListMain) {
this.context = context;
this.prList = prListMain;
}
2019-11-24 13:42:57 +01:00
@NonNull @Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
return new PullRequestsAdapter.PullRequestsHolder(
inflater.inflate(R.layout.list_pr, parent, false));
}
2019-11-24 13:42:57 +01:00
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
2019-11-24 13:42:57 +01:00
if (position >= getItemCount() - 1
&& isMoreDataAvailable
&& !isLoading
&& loadMoreListener != null) {
isLoading = true;
loadMoreListener.run();
}
((PullRequestsAdapter.PullRequestsHolder) holder).bindData(prList.get(position));
}
2019-11-24 13:42:57 +01:00
@Override
public int getItemViewType(int position) {
return position;
}
2019-11-24 13:42:57 +01:00
@Override
public int getItemCount() {
return prList.size();
}
2019-11-24 13:42:57 +01:00
public void setMoreDataAvailable(boolean moreDataAvailable) {
isMoreDataAvailable = moreDataAvailable;
}
2019-11-24 13:42:57 +01:00
@SuppressLint("NotifyDataSetChanged")
public void notifyDataChanged() {
notifyDataSetChanged();
isLoading = false;
}
public void setLoadMoreListener(Runnable loadMoreListener) {
this.loadMoreListener = loadMoreListener;
}
public void updateList(List<PullRequest> list) {
prList = list;
notifyDataChanged();
}
class PullRequestsHolder extends RecyclerView.ViewHolder {
private final ImageView assigneeAvatar;
private final TextView prTitle;
private final TextView prCreatedTime;
private final TextView prCommentsCount;
private final HorizontalScrollView labelsScrollViewWithText;
private final LinearLayout frameLabels;
private final HorizontalScrollView labelsScrollViewDots;
private final LinearLayout frameLabelsDots;
private final ImageView commentIcon;
private PullRequest pullRequestObject;
2019-11-24 13:42:57 +01:00
PullRequestsHolder(View itemView) {
2019-11-24 13:42:57 +01:00
super(itemView);
assigneeAvatar = itemView.findViewById(R.id.assigneeAvatar);
prTitle = itemView.findViewById(R.id.prTitle);
prCommentsCount = itemView.findViewById(R.id.prCommentsCount);
prCreatedTime = itemView.findViewById(R.id.prCreatedTime);
labelsScrollViewWithText = itemView.findViewById(R.id.labelsScrollViewWithText);
frameLabels = itemView.findViewById(R.id.frameLabels);
labelsScrollViewDots = itemView.findViewById(R.id.labelsScrollViewDots);
frameLabelsDots = itemView.findViewById(R.id.frameLabelsDots);
commentIcon = itemView.findViewById(R.id.comment_icon);
2019-11-24 13:42:57 +01:00
View.OnClickListener openPr =
v -> {
Intent intentPrDetail =
new IssueContext(
pullRequestObject,
((RepoDetailActivity) context).repository)
.getIntent(context, IssueDetailActivity.class);
context.startActivity(intentPrDetail);
};
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
itemView.setOnClickListener(openPr);
frameLabels.setOnClickListener(openPr);
frameLabelsDots.setOnClickListener(openPr);
new Handler()
.postDelayed(
() -> {
if (!AppUtil.checkGhostUsers(
pullRequestObject.getUser().getLogin())) {
assigneeAvatar.setOnClickListener(
v -> {
Intent intent =
new Intent(context, ProfileActivity.class);
intent.putExtra(
"username",
pullRequestObject.getUser().getLogin());
context.startActivity(intent);
});
assigneeAvatar.setOnLongClickListener(
loginId -> {
AppUtil.copyToClipboard(
context,
pullRequestObject.getUser().getLogin(),
context.getString(
R.string.copyLoginIdToClipBoard,
pullRequestObject
.getUser()
.getLogin()));
return true;
});
}
},
500);
}
2019-11-24 13:42:57 +01:00
@SuppressLint("SetTextI18n")
void bindData(PullRequest pullRequest) {
2019-11-24 13:42:57 +01:00
TinyDB tinyDb = TinyDB.getInstance(context);
Locale locale = context.getResources().getConfiguration().locale;
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
String timeFormat = tinyDb.getString("dateFormat", "pretty");
int imgRadius = AppUtil.getPixelsFromDensity(context, 3);
PicassoService.getInstance(context)
.get()
.load(pullRequest.getUser().getAvatarUrl())
.placeholder(R.drawable.loader_animated)
.transform(new RoundedTransformation(imgRadius, 0))
.resize(120, 120)
.centerCrop()
.into(this.assigneeAvatar);
2019-11-24 13:42:57 +01:00
this.pullRequestObject = pullRequest;
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 15, 0);
if (pullRequest.getLabels() != null) {
if (!tinyDb.getBoolean("showLabelsInList", false)) { // default
labelsScrollViewWithText.setVisibility(View.GONE);
labelsScrollViewDots.setVisibility(View.VISIBLE);
frameLabelsDots.removeAllViews();
for (int i = 0; i < pullRequest.getLabels().size(); i++) {
String labelColor = pullRequest.getLabels().get(i).getColor();
int color = Color.parseColor("#" + labelColor);
ImageView labelsView = new ImageView(context);
frameLabelsDots.setOrientation(LinearLayout.HORIZONTAL);
frameLabelsDots.setGravity(Gravity.START | Gravity.TOP);
labelsView.setLayoutParams(params);
TextDrawable drawable =
TextDrawable.builder()
.beginConfig()
.useFont(Typeface.DEFAULT)
.width(54)
.height(54)
.endConfig()
.buildRound("", color);
labelsView.setImageDrawable(drawable);
frameLabelsDots.addView(labelsView);
}
} else {
labelsScrollViewDots.setVisibility(View.GONE);
labelsScrollViewWithText.setVisibility(View.VISIBLE);
frameLabels.removeAllViews();
for (int i = 0; i < pullRequest.getLabels().size(); i++) {
String labelColor = pullRequest.getLabels().get(i).getColor();
String labelName = pullRequest.getLabels().get(i).getName();
int color = Color.parseColor("#" + labelColor);
ImageView labelsView = new ImageView(context);
frameLabels.setOrientation(LinearLayout.HORIZONTAL);
frameLabels.setGravity(Gravity.START | Gravity.TOP);
labelsView.setLayoutParams(params);
int height = AppUtil.getPixelsFromDensity(context, 20);
int textSize = AppUtil.getPixelsFromScaledDensity(context, 12);
TextDrawable drawable =
TextDrawable.builder()
.beginConfig()
.useFont(Typeface.DEFAULT)
.textColor(new ColorInverter().getContrastColor(color))
.fontSize(textSize)
.width(
LabelWidthCalculator.calculateLabelWidth(
labelName,
Typeface.DEFAULT,
textSize,
AppUtil.getPixelsFromDensity(context, 8)))
.height(height)
.endConfig()
.buildRoundRect(
labelName,
color,
AppUtil.getPixelsFromDensity(context, 18));
labelsView.setImageDrawable(drawable);
frameLabels.addView(labelsView);
}
}
}
2019-11-24 13:42:57 +01:00
String prNumber_ =
"<font color='"
+ ResourcesCompat.getColor(
context.getResources(), R.color.lightGray, null)
+ "'>"
+ context.getResources().getString(R.string.hash)
+ pullRequest.getNumber()
+ "</font>";
this.prTitle.setText(
HtmlCompat.fromHtml(
prNumber_ + " " + EmojiParser.parseToUnicode(pullRequest.getTitle()),
HtmlCompat.FROM_HTML_MODE_LEGACY));
this.prCommentsCount.setText(String.valueOf(pullRequest.getComments()));
this.prCreatedTime.setText(
TimeHelper.formatTime(pullRequest.getCreatedAt(), locale, timeFormat, context));
if (pullRequest.getComments() > 15) {
commentIcon.setImageDrawable(
ContextCompat.getDrawable(context, R.drawable.ic_flame));
commentIcon.setColorFilter(
context.getResources().getColor(R.color.releasePre, null));
}
if (timeFormat.equals("pretty")) {
this.prCreatedTime.setOnClickListener(
new ClickListener(
TimeHelper.customDateFormatForToastDateFormat(
pullRequest.getCreatedAt()),
context));
}
}
}
2019-11-24 13:42:57 +01:00
}