From 2da59312c5ed730ea50e4374098e103d989cad3d Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sat, 17 Feb 2024 21:25:31 -0500 Subject: [PATCH] Make Dialogs Persist --- .../mtudining/activity/menu/ItemDialog.java | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/thebrokenrail/mtudining/activity/menu/ItemDialog.java b/app/src/main/java/com/thebrokenrail/mtudining/activity/menu/ItemDialog.java index 493b87f..0f59dfa 100644 --- a/app/src/main/java/com/thebrokenrail/mtudining/activity/menu/ItemDialog.java +++ b/app/src/main/java/com/thebrokenrail/mtudining/activity/menu/ItemDialog.java @@ -1,11 +1,17 @@ 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; @@ -14,6 +20,35 @@ 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 @@ -29,20 +64,21 @@ public class ItemDialog { message.append(item.portion); message.append('\n'); message.append(context.getString(R.string.ingredients), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); - message.append(item.ingredients); + 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); - message.append(nutrient.value); + if (nutrient.value != null) { + message.append(nutrient.value); + } } // Show - new MaterialAlertDialogBuilder(context) - .setTitle(item.name) - .setMessage(message) - .setPositiveButton(R.string.ok, (dialog, which) -> {}) - .show(); + Fragment fragment = new Fragment(item.name, message); + fragment.show(((MenuActivity) context).getSupportFragmentManager(), "item_" + item.name.hashCode()); } }