2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.adapters;
|
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2020-04-23 22:40:13 +02:00
|
|
|
import android.view.LayoutInflater;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.view.View;
|
2020-04-23 22:40:13 +02:00
|
|
|
import android.view.ViewGroup;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2020-04-14 22:55:04 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2021-01-26 16:10:31 +01:00
|
|
|
import androidx.core.content.res.ResourcesCompat;
|
|
|
|
import androidx.core.text.HtmlCompat;
|
2020-04-14 22:55:04 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2021-01-26 18:53:59 +01:00
|
|
|
import com.vdurmont.emoji.EmojiParser;
|
2021-02-11 11:23:35 +01:00
|
|
|
import org.gitnex.tea4j.models.Issues;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.activities.IssueDetailActivity;
|
2020-04-01 13:26:32 +02:00
|
|
|
import org.mian.gitnex.clients.PicassoService;
|
2021-03-05 06:24:48 +01:00
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.helpers.ClickListener;
|
|
|
|
import org.mian.gitnex.helpers.RoundedTransformation;
|
|
|
|
import org.mian.gitnex.helpers.TimeHelper;
|
2020-07-07 19:15:01 +02:00
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.ocpsoft.prettytime.PrettyTime;
|
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
public class IssuesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-01-26 18:53:59 +01:00
|
|
|
private final Context context;
|
2020-04-23 22:40:13 +02:00
|
|
|
private final int TYPE_LOAD = 0;
|
|
|
|
private List<Issues> issuesList;
|
|
|
|
private OnLoadMoreListener loadMoreListener;
|
|
|
|
private boolean isLoading = false, isMoreDataAvailable = true;
|
2020-04-14 22:55:04 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
public IssuesAdapter(Context ctx, List<Issues> issuesListMain) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
this.context = ctx;
|
2020-04-23 22:40:13 +02:00
|
|
|
this.issuesList = issuesListMain;
|
2020-04-14 22:55:04 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
if(viewType == TYPE_LOAD) {
|
|
|
|
return new IssuesHolder(inflater.inflate(R.layout.list_issues, parent, false));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return new LoadHolder(inflater.inflate(R.layout.row_load, parent, false));
|
|
|
|
}
|
2020-04-14 22:55:04 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
if(position >= getItemCount() - 1 && isMoreDataAvailable && !isLoading && loadMoreListener != null) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
isLoading = true;
|
|
|
|
loadMoreListener.onLoadMore();
|
|
|
|
}
|
2020-04-14 22:55:04 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
if(getItemViewType(position) == TYPE_LOAD) {
|
2020-04-14 22:55:04 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
((IssuesHolder) holder).bindData(issuesList.get(position));
|
|
|
|
}
|
2020-04-14 22:55:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-23 22:40:13 +02:00
|
|
|
public int getItemViewType(int position) {
|
2020-04-14 22:55:04 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
if(issuesList.get(position).getTitle() != null) {
|
|
|
|
return TYPE_LOAD;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-04-14 22:55:04 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-14 22:55:04 +02:00
|
|
|
@Override
|
2020-04-23 22:40:13 +02:00
|
|
|
public int getItemCount() {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
return issuesList.size();
|
|
|
|
}
|
2019-11-24 13:42:57 +01:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
class IssuesHolder extends RecyclerView.ViewHolder {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
private Issues issue;
|
2021-03-05 06:24:48 +01:00
|
|
|
|
2021-01-26 18:53:59 +01:00
|
|
|
private final ImageView issueAssigneeAvatar;
|
|
|
|
private final TextView issueTitle;
|
|
|
|
private final TextView issueCreatedTime;
|
|
|
|
private final TextView issueCommentsCount;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
IssuesHolder(View itemView) {
|
2019-09-30 18:12:45 +02:00
|
|
|
|
2020-04-14 22:55:04 +02:00
|
|
|
super(itemView);
|
|
|
|
issueAssigneeAvatar = itemView.findViewById(R.id.assigneeAvatar);
|
|
|
|
issueTitle = itemView.findViewById(R.id.issueTitle);
|
|
|
|
issueCommentsCount = itemView.findViewById(R.id.issueCommentsCount);
|
|
|
|
issueCreatedTime = itemView.findViewById(R.id.issueCreatedTime);
|
2020-04-07 23:17:12 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
itemView.setOnClickListener(layoutView -> {
|
2020-04-23 22:40:13 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
Context context = layoutView.getContext();
|
2020-04-23 22:40:13 +02:00
|
|
|
|
|
|
|
Intent intent = new Intent(context, IssueDetailActivity.class);
|
2021-03-21 16:56:54 +01:00
|
|
|
intent.putExtra("issueNumber", issue.getNumber());
|
2020-04-23 22:40:13 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
TinyDB tinyDb = TinyDB.getInstance(context);
|
2021-03-21 16:56:54 +01:00
|
|
|
tinyDb.putString("issueNumber", String.valueOf(issue.getNumber()));
|
2020-07-22 21:32:42 +02:00
|
|
|
tinyDb.putString("issueType", "Issue");
|
2020-04-23 22:40:13 +02:00
|
|
|
context.startActivity(intent);
|
|
|
|
});
|
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
issueAssigneeAvatar.setOnClickListener(v -> {
|
|
|
|
Context context = v.getContext();
|
|
|
|
String userLoginId = issue.getUser().getLogin();
|
2021-03-05 06:24:48 +01:00
|
|
|
|
|
|
|
AppUtil.copyToClipboard(context, userLoginId, context.getString(R.string.copyLoginIdToClipBoard, userLoginId));
|
|
|
|
});
|
|
|
|
|
2020-04-14 22:55:04 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
@SuppressLint("SetTextI18n")
|
2021-03-21 16:56:54 +01:00
|
|
|
void bindData(Issues issue) {
|
2019-06-03 23:25:05 +02: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-03-21 16:56:54 +01:00
|
|
|
String timeFormat = tinyDb.getString("dateFormat");
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
int imgRadius = AppUtil.getPixelsFromDensity(context, 3);
|
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
PicassoService.getInstance(context).get()
|
|
|
|
.load(issue.getUser().getAvatar_url())
|
|
|
|
.placeholder(R.drawable.loader_animated)
|
2021-04-13 16:19:42 +02:00
|
|
|
.transform(new RoundedTransformation(imgRadius, 0))
|
2021-03-21 16:56:54 +01:00
|
|
|
.resize(120, 120)
|
|
|
|
.centerCrop()
|
|
|
|
.into(issueAssigneeAvatar);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
String issueNumber_ = "<font color='" + ResourcesCompat.getColor(context.getResources(), R.color.lightGray, null) + "'>" + context.getResources().getString(R.string.hash) + issue.getNumber() + "</font>";
|
|
|
|
issueTitle.setText(HtmlCompat.fromHtml(issueNumber_ + " " + EmojiParser.parseToUnicode(issue.getTitle()), HtmlCompat.FROM_HTML_MODE_LEGACY));
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
this.issue = issue;
|
|
|
|
this.issueCommentsCount.setText(String.valueOf(issue.getComments()));
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
switch(timeFormat) {
|
2020-04-14 22:55:04 +02:00
|
|
|
case "pretty": {
|
2021-05-30 19:28:14 +02:00
|
|
|
PrettyTime prettyTime = new PrettyTime(locale);
|
2021-03-21 16:56:54 +01:00
|
|
|
String createdTime = prettyTime.format(issue.getCreated_at());
|
|
|
|
this.issueCreatedTime.setText(createdTime);
|
|
|
|
this.issueCreatedTime.setOnClickListener(new ClickListener(TimeHelper.customDateFormatForToastDateFormat(issue.getCreated_at()), context));
|
2020-04-14 22:55:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "normal": {
|
2021-05-30 19:28:14 +02:00
|
|
|
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd '" + context.getResources().getString(R.string.timeAtText) + "' HH:mm", locale);
|
2021-03-21 16:56:54 +01:00
|
|
|
String createdTime = formatter.format(issue.getCreated_at());
|
|
|
|
this.issueCreatedTime.setText(createdTime);
|
2020-04-14 22:55:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "normal1": {
|
2021-05-30 19:28:14 +02:00
|
|
|
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy '" + context.getResources().getString(R.string.timeAtText) + "' HH:mm", locale);
|
2021-03-21 16:56:54 +01:00
|
|
|
String createdTime = formatter.format(issue.getCreated_at());
|
|
|
|
this.issueCreatedTime.setText(createdTime);
|
2020-04-14 22:55:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-14 22:55:04 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static class LoadHolder extends RecyclerView.ViewHolder {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
LoadHolder(View itemView) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
super(itemView);
|
2020-04-14 22:55:04 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
public void setMoreDataAvailable(boolean moreDataAvailable) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
isMoreDataAvailable = moreDataAvailable;
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
public void notifyDataChanged() {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
notifyDataSetChanged();
|
|
|
|
isLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnLoadMoreListener {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
void onLoadMore();
|
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
this.loadMoreListener = loadMoreListener;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateList(List<Issues> list) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2020-04-23 22:40:13 +02:00
|
|
|
issuesList = list;
|
|
|
|
notifyDataSetChanged();
|
2020-04-14 22:55:04 +02:00
|
|
|
}
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
}
|