Make Dialogs Persist

This commit is contained in:
TheBrokenRail 2024-02-17 21:25:31 -05:00
parent ba486ab5d6
commit 2da59312c5

View File

@ -1,11 +1,17 @@
package com.thebrokenrail.mtudining.activity.menu; package com.thebrokenrail.mtudining.activity.menu;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
import android.text.style.StyleSpan; 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.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.thebrokenrail.mtudining.R; import com.thebrokenrail.mtudining.R;
import com.thebrokenrail.mtudining.api.method.PeriodDetail; import com.thebrokenrail.mtudining.api.method.PeriodDetail;
@ -14,6 +20,35 @@ import com.thebrokenrail.mtudining.api.method.PeriodDetail;
* Dialog for a food item. * Dialog for a food item.
*/ */
public class ItemDialog { 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. * Show dialog.
* @param context The context * @param context The context
@ -29,20 +64,21 @@ public class ItemDialog {
message.append(item.portion); message.append(item.portion);
message.append('\n'); message.append('\n');
message.append(context.getString(R.string.ingredients), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 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(item.ingredients);
}
message.append('\n'); message.append('\n');
message.append(context.getString(R.string.nutrients), new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 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) { for (PeriodDetail.Response.Menu.PeriodData.MenuCategory.MenuItem.Nutrient nutrient : item.nutrients) {
message.append('\n'); message.append('\n');
message.append("" + nutrient.name + ": ", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); message.append("" + nutrient.name + ": ", new StyleSpan(Typeface.BOLD), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (nutrient.value != null) {
message.append(nutrient.value); message.append(nutrient.value);
} }
}
// Show // Show
new MaterialAlertDialogBuilder(context) Fragment fragment = new Fragment(item.name, message);
.setTitle(item.name) fragment.show(((MenuActivity) context).getSupportFragmentManager(), "item_" + item.name.hashCode());
.setMessage(message)
.setPositiveButton(R.string.ok, (dialog, which) -> {})
.show();
} }
} }