202 lines
9.4 KiB
Java
202 lines
9.4 KiB
Java
package it.unisannio.ding.ids.wedroid.app.view.adapter;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.widget.ArrayAdapter;
|
|
import android.widget.Button;
|
|
import android.widget.EditText;
|
|
import android.widget.ListView;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.appcompat.app.AlertDialog;
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import it.unisannio.ding.ids.wedroid.app.R;
|
|
import it.unisannio.ding.ids.wedroid.app.data.entity.WList;
|
|
import it.unisannio.ding.ids.wedroid.app.util.ServicesFactory;
|
|
import it.unisannio.ding.ids.wedroid.app.util.SharedPreferenceHelper;
|
|
import it.unisannio.ding.ids.wedroid.wrapper.entity.Card;
|
|
import it.unisannio.ding.ids.wedroid.wrapper.entity.Swimlane;
|
|
import retrofit2.Call;
|
|
import retrofit2.Callback;
|
|
import retrofit2.Response;
|
|
|
|
public class WListsAdapter extends RecyclerView.Adapter<WListsAdapter.WListViewHolder> {
|
|
|
|
private final LayoutInflater mInflater;
|
|
private List<WList> mWLists; // Cached copy of lists
|
|
private Context mContext;
|
|
private ServicesFactory service;
|
|
private SharedPreferenceHelper sp;
|
|
|
|
public WListsAdapter(Context context) {
|
|
mInflater = LayoutInflater.from(context);
|
|
mContext = context;
|
|
sp = new SharedPreferenceHelper(mContext);
|
|
service = new ServicesFactory(sp);
|
|
}
|
|
|
|
class WListViewHolder extends RecyclerView.ViewHolder {
|
|
private final TextView wListItemView;
|
|
ListView listView;
|
|
Button button;
|
|
|
|
private WListViewHolder(View itemView) {
|
|
super(itemView);
|
|
wListItemView = itemView.findViewById(R.id.wListTitle);
|
|
listView = itemView.findViewById(R.id.listViewCard);
|
|
button = itemView.findViewById(R.id.buttonAddCard);
|
|
}
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public WListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
View itemView = mInflater.inflate(R.layout.wlist_recyclerview_item, parent, false);
|
|
return new WListViewHolder(itemView);
|
|
}
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
@Override
|
|
public void onBindViewHolder(@NonNull final WListViewHolder holder, int position) {
|
|
if (mWLists != null) {
|
|
final WList current = mWLists.get(position);
|
|
final List<String> cardTitle = new ArrayList<>();
|
|
service.getCardService().getAllCards(sp.getBoardId(), current.getId()).enqueue(
|
|
new Callback<List<Card>>() {
|
|
@Override
|
|
public void onResponse(@NotNull Call<List<Card>> call, @NotNull Response<List<Card>> response) {
|
|
assert response.body() != null;
|
|
for (int i = 0; i < response.body().size(); i++) {
|
|
cardTitle.add(response.body().get(i).getTitle());
|
|
}
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext,
|
|
android.R.layout.simple_list_item_1, cardTitle);
|
|
holder.listView.setAdapter(adapter);
|
|
holder.wListItemView.setText(current.getTitle());
|
|
holder.button.setText("Add Card to " + current.getTitle());
|
|
holder.button.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View view) {
|
|
showInputDialog(current.getId());
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(@NotNull Call<List<Card>> call, @NotNull Throwable t) {
|
|
}
|
|
});
|
|
|
|
/*position of the old content holder**/
|
|
|
|
} else {
|
|
// Covers the case of data not being ready yet.
|
|
holder.wListItemView.setText("No wList");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int getItemCount() {
|
|
if (mWLists != null)
|
|
return mWLists.size();
|
|
else return 0;
|
|
}
|
|
|
|
public void setWLists(List<WList> wList) {
|
|
mWLists = wList;
|
|
notifyDataSetChanged();
|
|
}
|
|
|
|
private void showInputDialog(final String current) {
|
|
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
|
|
@SuppressLint("InflateParams") View promptView = layoutInflater.inflate(
|
|
R.layout.alert_new_card, null);
|
|
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
|
|
alertDialogBuilder.setView(promptView);
|
|
|
|
final EditText editText = promptView.findViewById(R.id.edittext);
|
|
// setup a dialog window
|
|
alertDialogBuilder.setCancelable(false)
|
|
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
|
|
|
|
public void onClick(DialogInterface dialog, int id) {
|
|
if (!editText.getText().toString().matches("")) {
|
|
service.getSwimlanesService().getAllSwimlanes(sp.getBoardId())
|
|
.enqueue(new Callback<List<Swimlane>>() {
|
|
@Override
|
|
public void onResponse(@NotNull Call<List<Swimlane>> call,
|
|
@NotNull Response<List<Swimlane>> response) {
|
|
String idDefaultSwimlane = null;
|
|
assert response.body() != null;
|
|
for (Swimlane swim : response.body()) {
|
|
if (swim.getTitle().equalsIgnoreCase("default"))
|
|
idDefaultSwimlane = swim.getTitle();
|
|
}
|
|
final Card card = new Card();
|
|
card.setTitle(editText.getText().toString());
|
|
card.setAuthorId(sp.getUserId());
|
|
card.setSwimlaneId(idDefaultSwimlane);
|
|
//card.setDescription("new card from app");
|
|
service.getListService().getList(sp.getBoardId(), current).enqueue(new Callback<it.unisannio.ding.ids.wedroid.wrapper.entity.WList>() {
|
|
@Override
|
|
public void onResponse(@NotNull Call<it.unisannio.ding.ids.wedroid.wrapper.entity.WList> call, @NotNull Response<it.unisannio.ding.ids.wedroid.wrapper.entity.WList> response) {
|
|
|
|
service.getCardService().newCard(sp.getBoardId(), current, card).enqueue(new Callback<Card>() {
|
|
@Override
|
|
public void onResponse(@NotNull Call<Card> call, @NotNull Response<Card> response) {
|
|
if (response.isSuccessful())
|
|
Toast.makeText(mContext, "card posted",
|
|
Toast.LENGTH_LONG).show();
|
|
else
|
|
Toast.makeText(mContext, "card doesn't posted",
|
|
Toast.LENGTH_LONG).show();
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(@NotNull Call<Card> call, @NotNull Throwable t) {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(@NotNull Call<it.unisannio.ding.ids.wedroid.wrapper.entity.WList> call, @NotNull Throwable t) {
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onFailure(@NotNull Call<List<Swimlane>> call, @NotNull Throwable t) {
|
|
|
|
}
|
|
});
|
|
} else
|
|
Toast.makeText(mContext, "cancel", Toast.LENGTH_LONG).show();
|
|
}
|
|
})
|
|
.setNegativeButton("Cancel",
|
|
new DialogInterface.OnClickListener() {
|
|
public void onClick(DialogInterface dialog, int id) {
|
|
dialog.cancel();
|
|
}
|
|
});
|
|
|
|
// create an alert dialog
|
|
AlertDialog alert = alertDialogBuilder.create();
|
|
alert.show();
|
|
}
|
|
}
|