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;
|
2022-03-28 19:57:47 +02:00
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.Typeface;
|
2022-08-01 09:24:23 +02:00
|
|
|
import android.os.Handler;
|
2022-03-28 19:57:47 +02:00
|
|
|
import android.view.Gravity;
|
2019-11-24 13:42:57 +01:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2022-03-28 19:57:47 +02:00
|
|
|
import android.widget.HorizontalScrollView;
|
2019-11-24 13:42:57 +01:00
|
|
|
import android.widget.ImageView;
|
2022-03-28 19:57:47 +02:00
|
|
|
import android.widget.LinearLayout;
|
2019-11-24 13:42:57 +01:00
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
2022-09-14 20:40:44 +02:00
|
|
|
import androidx.core.content.ContextCompat;
|
2021-01-26 16:10:31 +01:00
|
|
|
import androidx.core.content.res.ResourcesCompat;
|
|
|
|
import androidx.core.text.HtmlCompat;
|
2019-11-24 13:42:57 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2022-03-28 19:57:47 +02:00
|
|
|
import com.amulyakhare.textdrawable.TextDrawable;
|
2021-01-26 18:53:59 +01:00
|
|
|
import com.vdurmont.emoji.EmojiParser;
|
2022-09-21 07:43:00 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2022-04-18 09:10:54 +02:00
|
|
|
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;
|
2021-08-02 20:04:33 +02:00
|
|
|
import org.mian.gitnex.activities.ProfileActivity;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.mian.gitnex.activities.RepoDetailActivity;
|
2020-04-01 13:26:32 +02:00
|
|
|
import org.mian.gitnex.clients.PicassoService;
|
2022-09-14 20:40:44 +02:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
2022-03-28 19:57:47 +02:00
|
|
|
* @author M M Arif
|
2019-11-24 13:42:57 +01:00
|
|
|
*/
|
2020-04-23 22:40:13 +02:00
|
|
|
public class PullRequestsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2021-01-26 18:53:59 +01:00
|
|
|
private final Context context;
|
2022-04-18 09:10:54 +02:00
|
|
|
private List<PullRequest> prList;
|
2022-02-25 23:35:51 +01:00
|
|
|
private Runnable loadMoreListener;
|
2020-04-23 22:40:13 +02:00
|
|
|
private boolean isLoading = false, isMoreDataAvailable = true;
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public PullRequestsAdapter(Context context, List<PullRequest> prListMain) {
|
2020-04-23 22:40:13 +02:00
|
|
|
this.context = context;
|
|
|
|
this.prList = prListMain;
|
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
@NonNull @Override
|
2020-04-23 22:40:13 +02:00
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
2022-09-21 07:43:00 +02:00
|
|
|
return new PullRequestsAdapter.PullRequestsHolder(
|
|
|
|
inflater.inflate(R.layout.list_pr, parent, false));
|
2020-04-23 22:40:13 +02:00
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (position >= getItemCount() - 1
|
|
|
|
&& isMoreDataAvailable
|
|
|
|
&& !isLoading
|
|
|
|
&& loadMoreListener != null) {
|
2020-04-23 22:40:13 +02:00
|
|
|
isLoading = true;
|
2022-02-25 23:35:51 +01:00
|
|
|
loadMoreListener.run();
|
2020-04-23 22:40:13 +02:00
|
|
|
}
|
2022-03-28 19:57:47 +02:00
|
|
|
((PullRequestsAdapter.PullRequestsHolder) holder).bindData(prList.get(position));
|
2020-04-23 22:40:13 +02:00
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
2022-03-28 19:57:47 +02:00
|
|
|
return position;
|
2020-04-23 22:40:13 +02:00
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return prList.size();
|
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2022-08-15 16:26:02 +02:00
|
|
|
public void setMoreDataAvailable(boolean moreDataAvailable) {
|
|
|
|
isMoreDataAvailable = moreDataAvailable;
|
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2022-08-15 16:26:02 +02: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 {
|
2021-03-05 06:24:48 +01:00
|
|
|
|
2021-01-26 18:53:59 +01:00
|
|
|
private final ImageView assigneeAvatar;
|
|
|
|
private final TextView prTitle;
|
|
|
|
private final TextView prCreatedTime;
|
|
|
|
private final TextView prCommentsCount;
|
2022-03-28 19:57:47 +02:00
|
|
|
private final HorizontalScrollView labelsScrollViewWithText;
|
|
|
|
private final LinearLayout frameLabels;
|
|
|
|
private final HorizontalScrollView labelsScrollViewDots;
|
|
|
|
private final LinearLayout frameLabelsDots;
|
2022-09-14 20:40:44 +02:00
|
|
|
private final ImageView commentIcon;
|
2022-08-15 16:26:02 +02:00
|
|
|
private PullRequest pullRequestObject;
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
PullRequestsHolder(View itemView) {
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2020-04-23 22:40:13 +02: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);
|
2022-03-28 19:57:47 +02:00
|
|
|
labelsScrollViewWithText = itemView.findViewById(R.id.labelsScrollViewWithText);
|
|
|
|
frameLabels = itemView.findViewById(R.id.frameLabels);
|
|
|
|
labelsScrollViewDots = itemView.findViewById(R.id.labelsScrollViewDots);
|
|
|
|
frameLabelsDots = itemView.findViewById(R.id.frameLabelsDots);
|
2022-09-14 20:40:44 +02:00
|
|
|
commentIcon = itemView.findViewById(R.id.comment_icon);
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2022-09-21 07:43:00 +02: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
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
itemView.setOnClickListener(openPr);
|
|
|
|
frameLabels.setOnClickListener(openPr);
|
|
|
|
frameLabelsDots.setOnClickListener(openPr);
|
2021-03-05 06:24:48 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
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);
|
2020-04-23 22:40:13 +02:00
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
void bindData(PullRequest pullRequest) {
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
TinyDB tinyDb = TinyDB.getInstance(context);
|
2021-05-30 19:28:14 +02:00
|
|
|
Locale locale = context.getResources().getConfiguration().locale;
|
2021-04-13 16:19:42 +02:00
|
|
|
int imgRadius = AppUtil.getPixelsFromDensity(context, 3);
|
2021-03-05 06:24:48 +01:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
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
|
|
|
|
2022-03-28 19:57:47 +02:00
|
|
|
this.pullRequestObject = pullRequest;
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
LinearLayout.LayoutParams params =
|
|
|
|
new LinearLayout.LayoutParams(
|
|
|
|
LinearLayout.LayoutParams.WRAP_CONTENT,
|
|
|
|
LinearLayout.LayoutParams.WRAP_CONTENT);
|
2022-03-28 19:57:47 +02:00
|
|
|
params.setMargins(0, 0, 15, 0);
|
|
|
|
|
2023-06-13 07:23:27 +02:00
|
|
|
Typeface typeface = AppUtil.getTypeface(context);
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (pullRequest.getLabels() != null) {
|
2022-03-28 19:57:47 +02:00
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
if (!tinyDb.getBoolean("showLabelsInList", false)) { // default
|
2022-03-28 19:57:47 +02:00
|
|
|
|
|
|
|
labelsScrollViewWithText.setVisibility(View.GONE);
|
|
|
|
labelsScrollViewDots.setVisibility(View.VISIBLE);
|
|
|
|
frameLabelsDots.removeAllViews();
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
for (int i = 0; i < pullRequest.getLabels().size(); i++) {
|
2022-03-28 19:57:47 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
TextDrawable drawable =
|
|
|
|
TextDrawable.builder()
|
|
|
|
.beginConfig()
|
2023-06-13 07:23:27 +02:00
|
|
|
.useFont(typeface)
|
2022-09-21 07:43:00 +02:00
|
|
|
.width(54)
|
|
|
|
.height(54)
|
|
|
|
.endConfig()
|
|
|
|
.buildRound("", color);
|
2022-03-28 19:57:47 +02:00
|
|
|
|
|
|
|
labelsView.setImageDrawable(drawable);
|
|
|
|
frameLabelsDots.addView(labelsView);
|
|
|
|
}
|
2022-09-21 07:43:00 +02:00
|
|
|
} else {
|
2022-03-28 19:57:47 +02:00
|
|
|
|
|
|
|
labelsScrollViewDots.setVisibility(View.GONE);
|
|
|
|
labelsScrollViewWithText.setVisibility(View.VISIBLE);
|
|
|
|
frameLabels.removeAllViews();
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
for (int i = 0; i < pullRequest.getLabels().size(); i++) {
|
2022-03-28 19:57:47 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-09-21 07:43:00 +02:00
|
|
|
TextDrawable drawable =
|
|
|
|
TextDrawable.builder()
|
|
|
|
.beginConfig()
|
2023-06-13 07:23:27 +02:00
|
|
|
.useFont(typeface)
|
2022-09-21 07:43:00 +02:00
|
|
|
.textColor(new ColorInverter().getContrastColor(color))
|
|
|
|
.fontSize(textSize)
|
|
|
|
.width(
|
|
|
|
LabelWidthCalculator.calculateLabelWidth(
|
|
|
|
labelName,
|
2023-06-13 07:23:27 +02:00
|
|
|
typeface,
|
2022-09-21 07:43:00 +02:00
|
|
|
textSize,
|
|
|
|
AppUtil.getPixelsFromDensity(context, 8)))
|
|
|
|
.height(height)
|
|
|
|
.endConfig()
|
|
|
|
.buildRoundRect(
|
|
|
|
labelName,
|
|
|
|
color,
|
|
|
|
AppUtil.getPixelsFromDensity(context, 18));
|
2022-03-28 19:57:47 +02:00
|
|
|
|
|
|
|
labelsView.setImageDrawable(drawable);
|
|
|
|
frameLabels.addView(labelsView);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2022-09-21 07:43:00 +02: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));
|
2021-03-21 16:56:54 +01:00
|
|
|
this.prCommentsCount.setText(String.valueOf(pullRequest.getComments()));
|
2022-10-01 06:16:19 +02:00
|
|
|
this.prCreatedTime.setText(TimeHelper.formatTime(pullRequest.getCreatedAt(), locale));
|
2022-09-21 07:43:00 +02:00
|
|
|
|
|
|
|
if (pullRequest.getComments() > 15) {
|
|
|
|
commentIcon.setImageDrawable(
|
|
|
|
ContextCompat.getDrawable(context, R.drawable.ic_flame));
|
|
|
|
commentIcon.setColorFilter(
|
|
|
|
context.getResources().getColor(R.color.releasePre, null));
|
2022-09-14 20:40:44 +02:00
|
|
|
}
|
|
|
|
|
2022-10-01 06:16:19 +02:00
|
|
|
this.prCreatedTime.setOnClickListener(
|
|
|
|
new ClickListener(
|
|
|
|
TimeHelper.customDateFormatForToastDateFormat(
|
|
|
|
pullRequest.getCreatedAt()),
|
|
|
|
context));
|
2020-04-23 22:40:13 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
}
|