2019-06-03 23:25:05 +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.Filter;
|
|
|
|
import android.widget.Filterable;
|
2022-02-22 07:23:46 +01:00
|
|
|
import android.widget.LinearLayout;
|
2019-06-03 23:25:05 +02:00
|
|
|
import android.widget.TextView;
|
2021-02-11 11:23:35 +01:00
|
|
|
import androidx.annotation.NonNull;
|
2022-02-22 07:23:46 +01:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
2021-02-11 11:23:35 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2022-02-11 15:27:31 +01:00
|
|
|
import org.gitnex.tea4j.models.OrgPermissions;
|
2021-02-11 11:23:35 +01:00
|
|
|
import org.gitnex.tea4j.models.Teams;
|
2022-02-22 07:23:46 +01:00
|
|
|
import org.gitnex.tea4j.models.UserInfo;
|
2019-06-03 23:25:05 +02:00
|
|
|
import org.mian.gitnex.R;
|
2022-02-22 07:23:46 +01:00
|
|
|
import org.mian.gitnex.activities.OrganizationTeamInfoActivity;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
|
|
|
import org.mian.gitnex.helpers.Authorization;
|
2019-06-03 23:25:05 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2022-02-22 07:23:46 +01:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class TeamsByOrgAdapter extends RecyclerView.Adapter<TeamsByOrgAdapter.OrgTeamsViewHolder> implements Filterable {
|
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
private final List<Teams> teamList;
|
|
|
|
private final Context context;
|
|
|
|
private final List<Teams> teamListFull;
|
2022-02-11 15:27:31 +01:00
|
|
|
private final OrgPermissions permissions;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
|
|
|
static class OrgTeamsViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
2022-02-22 07:23:46 +01:00
|
|
|
private Teams team;
|
2021-04-13 16:19:42 +02:00
|
|
|
|
2022-02-11 15:27:31 +01:00
|
|
|
private OrgPermissions permissions;
|
2021-04-13 16:19:42 +02:00
|
|
|
private final TextView teamTitle;
|
|
|
|
private final TextView teamDescription;
|
2022-02-22 07:23:46 +01:00
|
|
|
private final LinearLayout membersPreviewFrame;
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-02-22 07:23:46 +01:00
|
|
|
private final List<UserInfo> userInfos;
|
|
|
|
private final TeamMembersByOrgPreviewAdapter adapter;
|
2021-04-13 16:19:42 +02:00
|
|
|
|
2022-02-22 07:23:46 +01:00
|
|
|
private OrgTeamsViewHolder(View itemView) {
|
2019-06-03 23:25:05 +02:00
|
|
|
super(itemView);
|
2022-02-22 07:23:46 +01:00
|
|
|
|
2019-06-03 23:25:05 +02:00
|
|
|
teamTitle = itemView.findViewById(R.id.teamTitle);
|
|
|
|
teamDescription = itemView.findViewById(R.id.teamDescription);
|
2022-02-22 07:23:46 +01:00
|
|
|
membersPreviewFrame = itemView.findViewById(R.id.membersPreviewFrame);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-02-22 07:23:46 +01:00
|
|
|
RecyclerView membersPreview = itemView.findViewById(R.id.membersPreview);
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-02-22 07:23:46 +01:00
|
|
|
userInfos = new ArrayList<>();
|
|
|
|
adapter = new TeamMembersByOrgPreviewAdapter(itemView.getContext(), userInfos);
|
|
|
|
|
|
|
|
membersPreview.setLayoutManager(new LinearLayoutManager(itemView.getContext(), RecyclerView.HORIZONTAL, false));
|
|
|
|
membersPreview.setAdapter(adapter);
|
|
|
|
|
|
|
|
itemView.setOnClickListener(v -> {
|
2021-04-13 16:19:42 +02:00
|
|
|
Context context = v.getContext();
|
2019-06-03 23:25:05 +02:00
|
|
|
|
2022-02-22 07:23:46 +01:00
|
|
|
Intent intent = new Intent(context, OrganizationTeamInfoActivity.class);
|
|
|
|
intent.putExtra("team", team);
|
2022-02-11 15:27:31 +01:00
|
|
|
intent.putExtra("permissions", permissions);
|
2021-04-13 16:19:42 +02:00
|
|
|
context.startActivity(intent);
|
2019-06-03 23:25:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-11 15:27:31 +01:00
|
|
|
public TeamsByOrgAdapter(Context ctx, List<Teams> teamListMain, OrgPermissions permissions) {
|
2021-04-13 16:19:42 +02:00
|
|
|
this.context = ctx;
|
2019-06-03 23:25:05 +02:00
|
|
|
this.teamList = teamListMain;
|
2022-02-11 15:27:31 +01:00
|
|
|
this.permissions = permissions;
|
2019-06-03 23:25:05 +02:00
|
|
|
teamListFull = new ArrayList<>(teamList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public TeamsByOrgAdapter.OrgTeamsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
2020-03-28 11:52:09 +01:00
|
|
|
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_teams_by_org, parent, false);
|
2019-06-03 23:25:05 +02:00
|
|
|
return new TeamsByOrgAdapter.OrgTeamsViewHolder(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBindViewHolder(@NonNull TeamsByOrgAdapter.OrgTeamsViewHolder holder, int position) {
|
|
|
|
|
|
|
|
Teams currentItem = teamList.get(position);
|
2021-04-13 16:19:42 +02:00
|
|
|
|
2022-02-22 07:23:46 +01:00
|
|
|
holder.team = currentItem;
|
2019-06-03 23:25:05 +02:00
|
|
|
holder.teamTitle.setText(currentItem.getName());
|
2022-02-11 15:27:31 +01:00
|
|
|
holder.permissions = permissions;
|
2021-04-13 16:19:42 +02:00
|
|
|
|
2022-02-22 07:23:46 +01:00
|
|
|
holder.membersPreviewFrame.setVisibility(View.GONE);
|
|
|
|
holder.userInfos.clear();
|
|
|
|
holder.adapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
RetrofitClient.getApiInterface(context)
|
|
|
|
.getTeamMembersByOrg(Authorization.get(context), currentItem.getId())
|
|
|
|
.enqueue(new Callback<List<UserInfo>>() {
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<UserInfo>> call, @NonNull Response<List<UserInfo>> response) {
|
|
|
|
if(response.isSuccessful() &&
|
|
|
|
response.body() != null &&
|
|
|
|
response.body().size() > 0) {
|
|
|
|
|
|
|
|
holder.membersPreviewFrame.setVisibility(View.VISIBLE);
|
|
|
|
holder.userInfos.addAll(response.body().stream()
|
|
|
|
.limit(Math.min(response.body().size(), 6))
|
|
|
|
.collect(Collectors.toList()));
|
|
|
|
|
|
|
|
holder.adapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public void onFailure(@NonNull Call<List<UserInfo>> call, @NonNull Throwable t) {}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (currentItem.getDescription() != null && !currentItem.getDescription().isEmpty()) {
|
2019-06-03 23:25:05 +02:00
|
|
|
holder.teamDescription.setVisibility(View.VISIBLE);
|
|
|
|
holder.teamDescription.setText(currentItem.getDescription());
|
2022-02-22 07:23:46 +01:00
|
|
|
} else {
|
2019-06-03 23:25:05 +02:00
|
|
|
holder.teamDescription.setVisibility(View.GONE);
|
|
|
|
holder.teamDescription.setText("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return teamList.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Filter getFilter() {
|
|
|
|
return orgTeamsFilter;
|
|
|
|
}
|
|
|
|
|
2021-04-13 16:19:42 +02:00
|
|
|
private final Filter orgTeamsFilter = new Filter() {
|
2019-06-03 23:25:05 +02:00
|
|
|
@Override
|
|
|
|
protected FilterResults performFiltering(CharSequence constraint) {
|
|
|
|
List<Teams> filteredList = new ArrayList<>();
|
|
|
|
|
|
|
|
if (constraint == null || constraint.length() == 0) {
|
|
|
|
filteredList.addAll(teamListFull);
|
|
|
|
} else {
|
|
|
|
String filterPattern = constraint.toString().toLowerCase().trim();
|
|
|
|
|
|
|
|
for (Teams item : teamListFull) {
|
|
|
|
if (item.getName().toLowerCase().contains(filterPattern) || item.getDescription().toLowerCase().contains(filterPattern)) {
|
|
|
|
filteredList.add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FilterResults results = new FilterResults();
|
|
|
|
results.values = filteredList;
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void publishResults(CharSequence constraint, FilterResults results) {
|
|
|
|
teamList.clear();
|
|
|
|
teamList.addAll((List) results.values);
|
|
|
|
notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|