2020-04-14 22:55:04 +02:00
|
|
|
package org.mian.gitnex.adapters;
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2020-03-27 19:59:24 +01:00
|
|
|
import android.content.Context;
|
2020-04-27 00:59:09 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.view.LayoutInflater;
|
2020-03-27 19:59:24 +01:00
|
|
|
import android.view.View;
|
2020-04-27 00:59:09 +02:00
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.Button;
|
2020-03-27 19:59:24 +01:00
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.annotation.NonNull;
|
2020-04-27 00:59:09 +02:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2020-03-27 19:59:24 +01:00
|
|
|
import org.mian.gitnex.R;
|
|
|
|
import org.mian.gitnex.helpers.ClickListener;
|
|
|
|
import org.mian.gitnex.helpers.TimeHelper;
|
2020-04-27 00:59:09 +02:00
|
|
|
import org.mian.gitnex.models.Commits;
|
2020-03-27 19:59:24 +01:00
|
|
|
import org.mian.gitnex.util.TinyDB;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
public class CommitsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
private Context ctx;
|
|
|
|
private final int TYPE_LOAD = 0;
|
|
|
|
private List<Commits> commitsList;
|
|
|
|
private CommitsAdapter.OnLoadMoreListener loadMoreListener;
|
|
|
|
private boolean isLoading = false;
|
|
|
|
private boolean isMoreDataAvailable = true;
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
public CommitsAdapter(Context ctx, List<Commits> commitsListMain) {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
|
|
|
this.ctx = ctx;
|
2020-04-27 00:59:09 +02:00
|
|
|
this.commitsList = commitsListMain;
|
2020-03-27 19:59:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
LayoutInflater inflater = LayoutInflater.from(ctx);
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
if(viewType == TYPE_LOAD) {
|
|
|
|
return new CommitsHolder(inflater.inflate(R.layout.list_commits, parent, false));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return new LoadHolder(inflater.inflate(R.layout.row_load, parent, false));
|
|
|
|
}
|
2020-03-27 19:59:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
if(position >= getItemCount() - 1 && isMoreDataAvailable && !isLoading && loadMoreListener != null) {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
isLoading = true;
|
|
|
|
loadMoreListener.onLoadMore();
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
}
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
if(getItemViewType(position) == TYPE_LOAD) {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
((CommitsHolder) holder).bindData(commitsList.get(position));
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
}
|
2020-03-27 19:59:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-27 00:59:09 +02:00
|
|
|
public int getItemViewType(int position) {
|
|
|
|
|
|
|
|
if(commitsList.get(position).getSha() != null) {
|
|
|
|
return TYPE_LOAD;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-27 19:59:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-04-27 00:59:09 +02:00
|
|
|
public int getItemCount() {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
return commitsList.size();
|
2020-03-27 19:59:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
class CommitsHolder extends RecyclerView.ViewHolder {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
TextView commitTitle;
|
|
|
|
TextView commitCommitter;
|
|
|
|
TextView commitDate;
|
|
|
|
Button commitHtmlUrl;
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
CommitsHolder(View itemView) {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
|
|
|
super(itemView);
|
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
commitTitle = itemView.findViewById(R.id.commitTitleVw);
|
|
|
|
commitCommitter = itemView.findViewById(R.id.commitCommitterVw);
|
|
|
|
commitDate = itemView.findViewById(R.id.commitDateVw);
|
|
|
|
commitHtmlUrl = itemView.findViewById(R.id.commitHtmlUrlVw);
|
2020-03-27 19:59:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
|
void bindData(Commits commitsModel) {
|
|
|
|
|
|
|
|
final TinyDB tinyDb = new TinyDB(ctx);
|
|
|
|
final String locale = tinyDb.getString("locale");
|
|
|
|
final String timeFormat = tinyDb.getString("dateFormat");
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
commitTitle.setText(commitsModel.getCommit().getMessage());
|
|
|
|
commitCommitter.setText(ctx.getString(R.string.commitCommittedBy, commitsModel.getCommit().getCommitter().getName()));
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
commitDate.setText(TimeHelper.formatTime(commitsModel.getCommit().getCommitter().getDate(), new Locale(locale), timeFormat, ctx));
|
2020-04-07 23:17:12 +02:00
|
|
|
|
|
|
|
if(timeFormat.equals("pretty")) {
|
2020-04-27 00:59:09 +02:00
|
|
|
commitDate.setOnClickListener(new ClickListener(TimeHelper.customDateFormatForToastDateFormat(commitsModel.getCommit().getCommitter().getDate()), ctx));
|
2020-03-27 19:59:24 +01:00
|
|
|
}
|
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
commitHtmlUrl.setOnClickListener(v -> ctx.startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse(commitsModel.getHtml_url()))));
|
2020-03-27 19:59:24 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static class LoadHolder extends RecyclerView.ViewHolder {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
LoadHolder(View itemView) {
|
2020-03-27 19:59:24 +01:00
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
super(itemView);
|
2020-03-27 19:59:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-27 00:59:09 +02:00
|
|
|
public void setMoreDataAvailable(boolean moreDataAvailable) {
|
|
|
|
|
|
|
|
isMoreDataAvailable = moreDataAvailable;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void notifyDataChanged() {
|
|
|
|
|
|
|
|
notifyDataSetChanged();
|
|
|
|
isLoading = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface OnLoadMoreListener {
|
|
|
|
|
|
|
|
void onLoadMore();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLoadMoreListener(CommitsAdapter.OnLoadMoreListener loadMoreListener) {
|
|
|
|
|
|
|
|
this.loadMoreListener = loadMoreListener;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateList(List<Commits> list) {
|
|
|
|
|
|
|
|
commitsList = list;
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:59:24 +01:00
|
|
|
}
|