2019-06-03 23:25:05 +02:00
|
|
|
package org.mian.gitnex.adapters;
|
|
|
|
|
2020-03-17 11:32:03 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.Filter;
|
|
|
|
import android.widget.Filterable;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
2020-07-07 19:15:01 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2022-04-18 09:10:54 +02:00
|
|
|
import org.gitnex.tea4j.v2.models.Organization;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.R;
|
2020-03-17 11:32:03 +01:00
|
|
|
import org.mian.gitnex.activities.OrganizationDetailActivity;
|
2020-04-01 13:26:32 +02:00
|
|
|
import org.mian.gitnex.clients.PicassoService;
|
2021-04-13 16:19:42 +02:00
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.helpers.RoundedTransformation;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
2022-04-04 10:00:54 +02:00
|
|
|
* @author M M Arif
|
2019-06-03 23:25:05 +02:00
|
|
|
*/
|
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
public class OrganizationsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable {
|
|
|
|
|
|
|
|
private final Context context;
|
2022-04-18 09:10:54 +02:00
|
|
|
private List<Organization> orgList;
|
|
|
|
private final List<Organization> orgListFull;
|
2022-04-04 10:00:54 +02:00
|
|
|
private OnLoadMoreListener loadMoreListener;
|
|
|
|
private boolean isLoading = false, isMoreDataAvailable = true;
|
|
|
|
|
2022-04-25 07:42:52 +02:00
|
|
|
public OrganizationsListAdapter(Context ctx, List<Organization> orgListMain) {
|
2022-04-04 10:00:54 +02:00
|
|
|
this.context = ctx;
|
|
|
|
this.orgList = orgListMain;
|
|
|
|
orgListFull = new ArrayList<>(orgList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
|
|
|
return new OrganizationsListAdapter.OrgHolder(inflater.inflate(R.layout.list_organizations, parent, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
|
|
|
if(position >= getItemCount() - 1 && isMoreDataAvailable && !isLoading && loadMoreListener != null) {
|
|
|
|
isLoading = true;
|
|
|
|
loadMoreListener.onLoadMore();
|
|
|
|
}
|
|
|
|
|
|
|
|
((OrganizationsListAdapter.OrgHolder) holder).bindData(orgList.get(position));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return orgList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
class OrgHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
private Organization userOrganizations;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
private final ImageView image;
|
|
|
|
private final TextView orgName;
|
|
|
|
private final TextView orgDescription;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
OrgHolder(View itemView) {
|
|
|
|
|
|
|
|
super(itemView);
|
|
|
|
orgName = itemView.findViewById(R.id.orgName);
|
|
|
|
orgDescription = itemView.findViewById(R.id.orgDescription);
|
|
|
|
image = itemView.findViewById(R.id.imageAvatar);
|
|
|
|
|
|
|
|
itemView.setOnClickListener(v -> {
|
|
|
|
Context context = v.getContext();
|
|
|
|
Intent intent = new Intent(context, OrganizationDetailActivity.class);
|
|
|
|
intent.putExtra("orgName", userOrganizations.getUsername());
|
|
|
|
context.startActivity(intent);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
void bindData(Organization org) {
|
2022-04-04 10:00:54 +02:00
|
|
|
|
2022-08-07 21:34:02 +02:00
|
|
|
int imgRadius = AppUtil.getPixelsFromDensity(context, 60);
|
2022-04-04 10:00:54 +02:00
|
|
|
|
|
|
|
this.userOrganizations = org;
|
|
|
|
orgName.setText(org.getUsername());
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
PicassoService.getInstance(context).get().load(org.getAvatarUrl()).placeholder(R.drawable.loader_animated).transform(new RoundedTransformation(imgRadius, 0)).resize(120, 120).centerCrop().into(image);
|
2022-04-04 10:00:54 +02:00
|
|
|
|
|
|
|
if(!org.getDescription().equals("")) {
|
|
|
|
orgDescription.setVisibility(View.VISIBLE);
|
|
|
|
orgDescription.setText(org.getDescription());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
orgDescription.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMoreDataAvailable(boolean moreDataAvailable) {
|
|
|
|
isMoreDataAvailable = moreDataAvailable;
|
|
|
|
if(!isMoreDataAvailable) {
|
|
|
|
loadMoreListener.onLoadFinished();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
|
|
public void notifyDataChanged() {
|
|
|
|
notifyDataSetChanged();
|
|
|
|
isLoading = false;
|
|
|
|
loadMoreListener.onLoadFinished();
|
|
|
|
}
|
|
|
|
|
2022-04-25 07:42:52 +02:00
|
|
|
public abstract static class OnLoadMoreListener {
|
|
|
|
protected abstract void onLoadMore();
|
|
|
|
public void onLoadFinished() {}
|
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setLoadMoreListener(OnLoadMoreListener loadMoreListener) {
|
|
|
|
this.loadMoreListener = loadMoreListener;
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
public void updateList(List<Organization> list) {
|
2022-04-04 10:00:54 +02:00
|
|
|
orgList = list;
|
|
|
|
notifyDataChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Filter getFilter() {
|
|
|
|
return orgFilter;
|
|
|
|
}
|
|
|
|
|
|
|
|
private final Filter orgFilter = new Filter() {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
@Override
|
|
|
|
protected FilterResults performFiltering(CharSequence constraint) {
|
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
List<Organization> filteredList = new ArrayList<>();
|
2021-04-13 16:19:42 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
if(constraint == null || constraint.length() == 0) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
filteredList.addAll(orgListFull);
|
|
|
|
}
|
|
|
|
else {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
String filterPattern = constraint.toString().toLowerCase().trim();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-18 09:10:54 +02:00
|
|
|
for(Organization item : orgListFull) {
|
2022-04-04 10:00:54 +02:00
|
|
|
if(item.getUsername().toLowerCase().contains(filterPattern) || item.getDescription().toLowerCase().contains(filterPattern)) {
|
|
|
|
filteredList.add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 23:20:14 +01:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
FilterResults results = new FilterResults();
|
|
|
|
results.values = filteredList;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
return results;
|
|
|
|
}
|
2020-11-30 23:20:14 +01:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
@Override
|
|
|
|
protected void publishResults(CharSequence constraint, FilterResults results) {
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-04-04 10:00:54 +02:00
|
|
|
orgList.clear();
|
|
|
|
orgList.addAll((List) results.values);
|
|
|
|
notifyDataChanged();
|
|
|
|
}
|
|
|
|
};
|
2019-06-03 23:25:05 +02:00
|
|
|
}
|