MTU-Dining/app/src/main/java/com/thebrokenrail/mtudining/activity/list/ListAdapter.java
2024-02-17 00:57:49 -05:00

60 lines
2.1 KiB
Java

package com.thebrokenrail.mtudining.activity.list;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.thebrokenrail.mtudining.activity.menu.MenuActivity;
import com.thebrokenrail.mtudining.activity.task.Task;
import com.thebrokenrail.mtudining.activity.task.TaskAdapter;
import com.thebrokenrail.mtudining.widget.CategoryView;
/**
* Adapter for listing dining halls.
*/
class ListAdapter extends TaskAdapter<ListData> {
ListAdapter(Task<ListData> task) {
super(task);
}
@Override
protected View createItemView(@NonNull ViewGroup parent) {
// Create View
CategoryView category = new CategoryView(parent.getContext(), null);
RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT);
category.setLayoutParams(layoutParams);
return category;
}
@Override
protected void bindItemView(View view, int position) {
ListData.Category data = getResult().categories.get(position - getFirstElementPosition());
// Setup View
CategoryView category = (CategoryView) view;
category.setup(data.isOpen, data.name, () -> {
// Open/Close Category
data.isOpen = !data.isOpen;
notifyItemChanged(getResult().categories.indexOf(data) + getFirstElementPosition());
});
// Add Locations
category.clearItems();
for (ListData.Category.Element location : data.locations) {
category.addItem(location.name, () -> {
// Open Menu
Intent intent = new Intent(category.getContext(), MenuActivity.class);
intent.putExtra(MenuActivity.ID_EXTRA, location.id);
intent.putExtra(MenuActivity.NAME_EXTRA, location.name);
category.getContext().startActivity(intent);
});
}
}
@Override
protected int getDataSize() {
return getResult().categories.size();
}
}