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

60 lines
2.3 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.api.method.AllLocations;
import com.thebrokenrail.mtudining.util.Category;
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) {
Category<AllLocations.Response.Location> data = getResult().categories.get(position - getFirstElementPosition());
// Setup View
CategoryView category = (CategoryView) view;
category.setup(data, () -> {
// Open/Close Category
data.isOpen = !data.isOpen;
notifyItemChanged(getResult().categories.indexOf(data) + getFirstElementPosition());
}, location -> {
// Open Menu
Intent intent = new Intent(category.getContext(), MenuActivity.class);
intent.putExtra(MenuActivity.ID_EXTRA, location.id);
intent.putExtra(MenuActivity.NAME_EXTRA, location.name);
intent.putExtra(MenuActivity.LATITUDE_EXTRA, location.address.lat);
intent.putExtra(MenuActivity.LONGITUDE_EXTRA, location.address.lon);
intent.putExtra(MenuActivity.STREET_EXTRA, location.address.street);
category.getContext().startActivity(intent);
});
}
@Override
protected int getDataSize() {
return getResult().categories.size();
}
}