2020-09-30 13:09:24 +02:00
|
|
|
package org.mian.gitnex.adapters;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
2021-01-26 16:10:31 +01:00
|
|
|
import androidx.core.content.res.ResourcesCompat;
|
|
|
|
import androidx.core.text.HtmlCompat;
|
2020-09-30 13:09:24 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2021-02-11 11:23:35 +01:00
|
|
|
import org.gitnex.tea4j.models.Issues;
|
2020-09-30 13:09:24 +02:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.activities.IssueDetailActivity;
|
|
|
|
import org.mian.gitnex.clients.PicassoService;
|
2021-04-13 20:56:50 +02:00
|
|
|
import org.mian.gitnex.database.api.BaseApi;
|
2020-09-30 13:09:24 +02:00
|
|
|
import org.mian.gitnex.database.api.RepositoriesApi;
|
|
|
|
import org.mian.gitnex.database.models.Repository;
|
2021-03-05 06:24:48 +01:00
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
2020-09-30 13:09:24 +02:00
|
|
|
import org.mian.gitnex.helpers.ClickListener;
|
|
|
|
import org.mian.gitnex.helpers.RoundedTransformation;
|
|
|
|
import org.mian.gitnex.helpers.TimeHelper;
|
|
|
|
import org.mian.gitnex.helpers.TinyDB;
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class SearchIssuesAdapter extends RecyclerView.Adapter<SearchIssuesAdapter.SearchViewHolder> {
|
|
|
|
|
2021-03-05 06:24:48 +01:00
|
|
|
private final List<Issues> searchedList;
|
2021-04-13 16:19:42 +02:00
|
|
|
private final Context context;
|
2021-03-05 06:24:48 +01:00
|
|
|
private final TinyDB tinyDb;
|
2020-09-30 13:09:24 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
public SearchIssuesAdapter(List<Issues> dataList, Context ctx) {
|
2020-09-30 13:09:24 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
this.context = ctx;
|
2020-09-30 13:09:24 +02:00
|
|
|
this.searchedList = dataList;
|
2021-04-13 16:19:42 +02:00
|
|
|
this.tinyDb = TinyDB.getInstance(context);
|
2020-09-30 13:09:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class SearchViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
private Issues issue;
|
2021-03-05 06:24:48 +01:00
|
|
|
|
|
|
|
private final ImageView issueAssigneeAvatar;
|
|
|
|
private final TextView issueTitle;
|
|
|
|
private final TextView issueCreatedTime;
|
|
|
|
private final TextView issueCommentsCount;
|
2020-09-30 13:09:24 +02:00
|
|
|
|
|
|
|
private SearchViewHolder(View itemView) {
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
itemView.setOnClickListener(v -> {
|
2020-09-30 13:09:24 +02:00
|
|
|
|
|
|
|
Context context = v.getContext();
|
|
|
|
|
|
|
|
Intent intent = new Intent(context, IssueDetailActivity.class);
|
2021-03-21 16:56:54 +01:00
|
|
|
intent.putExtra("issueNumber", issue.getNumber());
|
2020-09-30 13:09:24 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
tinyDb.putString("issueNumber", String.valueOf(issue.getNumber()));
|
2020-09-30 13:09:24 +02:00
|
|
|
tinyDb.putString("issueType", "Issue");
|
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
tinyDb.putString("repoFullName", issue.getRepository().getFull_name());
|
2020-09-30 13:09:24 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
String[] parts = issue.getRepository().getFull_name().split("/");
|
2020-09-30 13:09:24 +02:00
|
|
|
final String repoOwner = parts[0];
|
|
|
|
final String repoName = parts[1];
|
|
|
|
|
|
|
|
int currentActiveAccountId = tinyDb.getInt("currentActiveAccountId");
|
2021-04-13 20:56:50 +02:00
|
|
|
RepositoriesApi repositoryData = BaseApi.getInstance(context, RepositoriesApi.class);
|
2020-09-30 13:09:24 +02:00
|
|
|
|
2021-04-15 17:05:56 +02:00
|
|
|
assert repositoryData != null;
|
2020-09-30 13:09:24 +02:00
|
|
|
Integer count = repositoryData.checkRepository(currentActiveAccountId, repoOwner, repoName);
|
|
|
|
|
|
|
|
if(count == 0) {
|
|
|
|
|
|
|
|
long id = repositoryData.insertRepository(currentActiveAccountId, repoOwner, repoName);
|
|
|
|
tinyDb.putLong("repositoryId", id);
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
Repository data = repositoryData.getRepository(currentActiveAccountId, repoOwner, repoName);
|
|
|
|
tinyDb.putLong("repositoryId", data.getRepositoryId());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
context.startActivity(intent);
|
|
|
|
});
|
2021-03-05 06:24:48 +01:00
|
|
|
|
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-09-30 13:09:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public SearchIssuesAdapter.SearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
|
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_issues, parent, false);
|
|
|
|
return new SearchIssuesAdapter.SearchViewHolder(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull final SearchIssuesAdapter.SearchViewHolder holder, int position) {
|
|
|
|
|
|
|
|
Issues currentItem = searchedList.get(position);
|
2021-04-13 16:19:42 +02:00
|
|
|
int imgRadius = AppUtil.getPixelsFromDensity(context, 3);
|
2020-09-30 13:09:24 +02:00
|
|
|
|
|
|
|
String locale = tinyDb.getString("locale");
|
|
|
|
String timeFormat = tinyDb.getString("dateFormat");
|
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
PicassoService.getInstance(context).get()
|
2021-03-21 16:56:54 +01:00
|
|
|
.load(currentItem.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(holder.issueAssigneeAvatar);
|
2020-09-30 13:09:24 +02:00
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
String issueNumber_ = "<font color='" + ResourcesCompat.getColor(context.getResources(), R.color.lightGray, null) + "'>" + currentItem.getRepository().getFull_name() + context.getResources().getString(R.string.hash) + currentItem.getNumber() + "</font>";
|
2020-09-30 13:09:24 +02:00
|
|
|
|
2021-03-21 16:56:54 +01:00
|
|
|
holder.issue = currentItem;
|
|
|
|
holder.issueTitle.setText(HtmlCompat.fromHtml(issueNumber_ + " " + currentItem.getTitle(), HtmlCompat.FROM_HTML_MODE_LEGACY));
|
2020-09-30 13:09:24 +02:00
|
|
|
holder.issueCommentsCount.setText(String.valueOf(currentItem.getComments()));
|
|
|
|
|
|
|
|
switch(timeFormat) {
|
|
|
|
case "pretty": {
|
|
|
|
PrettyTime prettyTime = new PrettyTime(new Locale(locale));
|
|
|
|
String createdTime = prettyTime.format(currentItem.getCreated_at());
|
|
|
|
holder.issueCreatedTime.setText(createdTime);
|
2021-04-13 16:19:42 +02:00
|
|
|
holder.issueCreatedTime.setOnClickListener(new ClickListener(TimeHelper.customDateFormatForToastDateFormat(currentItem.getCreated_at()), context));
|
2020-09-30 13:09:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "normal": {
|
2021-04-13 16:19:42 +02:00
|
|
|
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd '" + context.getResources().getString(R.string.timeAtText) + "' HH:mm", new Locale(locale));
|
2020-09-30 13:09:24 +02:00
|
|
|
String createdTime = formatter.format(currentItem.getCreated_at());
|
|
|
|
holder.issueCreatedTime.setText(createdTime);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "normal1": {
|
2021-04-13 16:19:42 +02:00
|
|
|
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy '" + context.getResources().getString(R.string.timeAtText) + "' HH:mm", new Locale(locale));
|
2020-09-30 13:09:24 +02:00
|
|
|
String createdTime = formatter.format(currentItem.getCreated_at());
|
|
|
|
holder.issueCreatedTime.setText(createdTime);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
|
|
|
|
return searchedList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void notifyDataChanged() {
|
|
|
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|