Make Dialogs Persist

This commit is contained in:
TheBrokenRail 2024-02-17 21:25:31 -05:00
parent ba486ab5d6
commit 2da59312c5
1 changed files with 43 additions and 7 deletions

View File

@ -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());
}
}