85 lines
3.1 KiB
Java
85 lines
3.1 KiB
Java
package com.thebrokenrail.mtudining.activity.menu;
|
|
|
|
import android.app.Dialog;
|
|
import android.content.Context;
|
|
import android.graphics.Typeface;
|
|
import android.os.Bundle;
|
|
import android.text.SpannableStringBuilder;
|
|
import android.text.Spanned;
|
|
import android.text.style.StyleSpan;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.fragment.app.DialogFragment;
|
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
|
import com.thebrokenrail.mtudining.R;
|
|
import com.thebrokenrail.mtudining.api.method.PeriodDetail;
|
|
|
|
/**
|
|
* Dialog for a food item.
|
|
*/
|
|
public class ItemDialog {
|
|
/**
|
|
* Fragment for dialog.
|
|
*/
|
|
public static class Fragment extends DialogFragment {
|
|
// Required Public Default Constructor
|
|
public Fragment() {
|
|
}
|
|
|
|
private Fragment(CharSequence title, CharSequence message) {
|
|
Bundle arguments = new Bundle();
|
|
arguments.putCharSequence("title", title);
|
|
arguments.putCharSequence("message", message);
|
|
setArguments(arguments);
|
|
}
|
|
|
|
@NonNull
|
|
@Override
|
|
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
|
|
// Create Material Dialog
|
|
Bundle arguments = getArguments();
|
|
assert arguments != null;
|
|
return new MaterialAlertDialogBuilder(requireActivity())
|
|
.setTitle(arguments.getCharSequence("title"))
|
|
.setMessage(arguments.getCharSequence("message"))
|
|
.setPositiveButton(R.string.ok, null)
|
|
.create();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show dialog.
|
|
* @param context The context
|
|
* @param item The food item to display
|
|
*/
|
|
public static void show(Context context, PeriodDetail.Response.Menu.PeriodData.MenuCategory.MenuItem item) {
|
|
// Build Message
|
|
SpannableStringBuilder message = new SpannableStringBuilder(item.desc != null ? item.desc.trim() : "");
|
|
if (message.length() > 0) {
|
|
message.append("\n\n");
|
|
}
|
|
message.append(context.getString(R.string.portion), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
message.append(item.portion);
|
|
message.append('\n');
|
|
message.append(context.getString(R.string.ingredients), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
if (item.ingredients != null) {
|
|
message.append(item.ingredients);
|
|
}
|
|
message.append('\n');
|
|
message.append(context.getString(R.string.nutrients), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
for (PeriodDetail.Response.Menu.PeriodData.MenuCategory.MenuItem.Nutrient nutrient : item.nutrients) {
|
|
message.append('\n');
|
|
message.append("• " + nutrient.name + ": ", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
if (nutrient.value != null) {
|
|
message.append(nutrient.value);
|
|
}
|
|
}
|
|
|
|
// Show
|
|
Fragment fragment = new Fragment(item.name, message);
|
|
fragment.show(((MenuActivity) context).getSupportFragmentManager(), "item_" + item.name.hashCode());
|
|
}
|
|
}
|