131 lines
5.1 KiB
Java
131 lines
5.1 KiB
Java
package com.thebrokenrail.mtudining.widget;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.Typeface;
|
|
import android.util.AttributeSet;
|
|
import android.util.TypedValue;
|
|
import android.widget.FrameLayout;
|
|
import android.widget.LinearLayout;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.appcompat.widget.AppCompatTextView;
|
|
|
|
import com.google.android.material.card.MaterialCardView;
|
|
import com.thebrokenrail.mtudining.R;
|
|
import com.thebrokenrail.mtudining.util.Category;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
/**
|
|
* Widget that shows a category of items.
|
|
*/
|
|
public class CategoryView extends FrameLayout {
|
|
private final MaterialCardView card;
|
|
private final LinearLayout children;
|
|
private final AppCompatTextView title;
|
|
|
|
public CategoryView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
|
super(context, attrs);
|
|
LinearLayout inner = new LinearLayout(context);
|
|
inner.setOrientation(LinearLayout.VERTICAL);
|
|
addView(inner);
|
|
|
|
// Set Margin
|
|
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
|
|
int margin = getResources().getDimensionPixelSize(R.dimen.margin);
|
|
layoutParams.setMargins(margin, 0, margin, margin);
|
|
inner.setLayoutParams(layoutParams);
|
|
|
|
// Add Title
|
|
title = new AppCompatTextView(context);
|
|
TypedValue value = new TypedValue();
|
|
context.getTheme().resolveAttribute(androidx.appcompat.R.attr.colorPrimary, value, true);
|
|
title.setTextColor(value.data);
|
|
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.category_font_size));
|
|
title.setTypeface(null, Typeface.BOLD);
|
|
title.setClickable(true);
|
|
title.setFocusable(true);
|
|
inner.addView(title);
|
|
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
title.setLayoutParams(innerLayoutParams);
|
|
|
|
// Setup Card
|
|
card = new MaterialCardView(context, null, com.google.android.material.R.attr.materialCardViewElevatedStyle);
|
|
inner.addView(card);
|
|
innerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
innerLayoutParams.setMargins(0, margin, 0, 0);
|
|
card.setLayoutParams(innerLayoutParams);
|
|
|
|
// Setup Children
|
|
children = new LinearLayout(context);
|
|
children.setOrientation(LinearLayout.VERTICAL);
|
|
card.addView(children);
|
|
MaterialCardView.LayoutParams cardLayoutParams = new MaterialCardView.LayoutParams(MaterialCardView.LayoutParams.MATCH_PARENT, MaterialCardView.LayoutParams.WRAP_CONTENT);
|
|
children.setLayoutParams(cardLayoutParams);
|
|
}
|
|
|
|
/**
|
|
* Setup widget.
|
|
* @param category The category
|
|
* @param onClickTitle Callback when clicking on title
|
|
* @param onClickItem Callback when clicking on an item
|
|
*/
|
|
public <T extends Category.Named> void setup(Category<T> category, Runnable onClickTitle, Consumer<T> onClickItem) {
|
|
String icon = category.isOpen ? "▼" : "▶";
|
|
String titleText = category.name;
|
|
if (getResources().getConfiguration().getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
|
|
// RTL
|
|
titleText += " " + icon;
|
|
} else {
|
|
// LTR
|
|
titleText = icon + " " + titleText;
|
|
}
|
|
title.setText(titleText);
|
|
card.setVisibility(category.isOpen ? VISIBLE : GONE);
|
|
title.setOnClickListener(v -> onClickTitle.run());
|
|
// Add Items
|
|
clearItems();
|
|
for (T item : category.items) {
|
|
addItem(item.getName(), () -> onClickItem.accept(item));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Clear category.
|
|
*/
|
|
private void clearItems() {
|
|
children.removeAllViews();
|
|
}
|
|
|
|
/**
|
|
* Add item.
|
|
* @param name Item name
|
|
* @param onClick Click handler
|
|
*/
|
|
private void addItem(String name, Runnable onClick) {
|
|
AppCompatTextView item = new AppCompatTextView(getContext());
|
|
// Text
|
|
item.setText(name);
|
|
// Text Size
|
|
item.setTextSize(TypedValue.COMPLEX_UNIT_PX, item.getResources().getDimension(R.dimen.item_font_size));
|
|
// Padding
|
|
int margin = getResources().getDimensionPixelSize(R.dimen.margin);
|
|
item.setPadding(margin, margin, margin, margin);
|
|
// Make Clickable
|
|
item.setClickable(true);
|
|
item.setFocusable(true);
|
|
item.setOnClickListener(v -> onClick.run());
|
|
// Ripple Effect
|
|
TypedValue outValue = new TypedValue();
|
|
getContext().getTheme().resolveAttribute(androidx.appcompat.R.attr.selectableItemBackground, outValue, true);
|
|
item.setBackgroundResource(outValue.resourceId);
|
|
// Layout
|
|
item.setTextAlignment(TEXT_ALIGNMENT_VIEW_START);
|
|
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
item.setLayoutParams(layoutParams);
|
|
// Add To View
|
|
children.addView(item);
|
|
}
|
|
}
|