166 lines
6.0 KiB
Java
166 lines
6.0 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.annotation.StringRes;
|
|
import androidx.fragment.app.DialogFragment;
|
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
|
import com.thebrokenrail.mtudining.R;
|
|
import com.thebrokenrail.mtudining.api.method.PeriodDetail;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Dialog for a food item.
|
|
*/
|
|
public class ItemDialog {
|
|
private static final String ALLERGEN_FILTER = "allergen";
|
|
private static final String LABEL_FILTER = "label";
|
|
|
|
/**
|
|
* 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() : "");
|
|
writeNewline(message, 2);
|
|
for (String filterType : new String[]{LABEL_FILTER, ALLERGEN_FILTER}) {
|
|
List<String> filters = getFilters(item, filterType);
|
|
if (filters.size() > 0) {
|
|
writeNewline(message, 2);
|
|
@StringRes int filterName = filterType.equals(LABEL_FILTER) ? R.string.labels : R.string.allergens;
|
|
message.append(context.getString(filterName), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
for (String filter : filters) {
|
|
writeBullet(message, null, filter);
|
|
}
|
|
}
|
|
}
|
|
writeNewline(message, 2);
|
|
if (item.portion != null) {
|
|
message.append(context.getString(R.string.portion), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
message.append(item.portion);
|
|
}
|
|
writeNewline(message, 2);
|
|
if (item.ingredients != null) {
|
|
message.append(context.getString(R.string.ingredients), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
message.append(item.ingredients);
|
|
}
|
|
writeNewline(message, 2);
|
|
if (item.nutrients.size() > 0) {
|
|
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) {
|
|
writeBullet(message, nutrient.name, nutrient.value);
|
|
}
|
|
}
|
|
|
|
// Remove Newlines From End
|
|
while (message.length() > 0 && message.charAt(message.length() - 1) == '\n') {
|
|
message.delete(message.length() - 1, message.length());
|
|
}
|
|
|
|
// Show
|
|
Fragment fragment = new Fragment(item.name, message);
|
|
fragment.show(((MenuActivity) context).getSupportFragmentManager(), "item_" + item.name.hashCode());
|
|
}
|
|
|
|
/**
|
|
* Get filters of a specific type in food item.
|
|
* @param item The item
|
|
* @param type The filter type
|
|
* @return List of filters
|
|
*/
|
|
private static List<String> getFilters(PeriodDetail.Response.Menu.PeriodData.MenuCategory.MenuItem item, String type) {
|
|
List<String> out = new ArrayList<>();
|
|
if (item.filters != null) {
|
|
for (PeriodDetail.Response.Menu.PeriodData.MenuCategory.MenuItem.Filter filter : item.filters) {
|
|
if (filter.type.equals(type)) {
|
|
out.add(filter.name);
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Write bullet point to string.
|
|
* @param message The string to modify
|
|
* @param name The first part
|
|
* @param value The second part
|
|
*/
|
|
private static void writeBullet(SpannableStringBuilder message, String name, String value) {
|
|
writeNewline(message, 1);
|
|
message.append("• ", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
if (name != null) {
|
|
message.append(name + ": ", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
}
|
|
if (value != null) {
|
|
message.append(value);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Write newline to string.
|
|
* @param message The string to modify
|
|
* @param count The amount of newlines to write
|
|
*/
|
|
private static void writeNewline(SpannableStringBuilder message, int count) {
|
|
if (message.length() > 0) {
|
|
int existingCount = 0;
|
|
for (int i = message.length() - 1; i >= 0; i--) {
|
|
if (message.charAt(i) == '\n') {
|
|
existingCount++;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
count -= existingCount;
|
|
if (count > 0) {
|
|
for (int i = 0; i < count; i++) {
|
|
message.append('\n');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|