|
| 1 | +package io.snabble.sdk.ui.scanner; |
| 2 | + |
| 3 | +import android.app.AlertDialog; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.DialogInterface; |
| 6 | +import android.view.Gravity; |
| 7 | +import android.view.View; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.view.Window; |
| 10 | +import android.widget.TextView; |
| 11 | + |
| 12 | +import io.snabble.sdk.Product; |
| 13 | +import io.snabble.sdk.ui.R; |
| 14 | +import io.snabble.sdk.ui.utils.OneShotClickListener; |
| 15 | +import io.snabble.sdk.utils.Logger; |
| 16 | + |
| 17 | +class SelectBundleDialog { |
| 18 | + public static void show(Context context, Product product, final Callback callback) { |
| 19 | + if(callback == null) { |
| 20 | + Logger.e("No callback provided"); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + View view = View.inflate(context, R.layout.dialog_bundle_select, null); |
| 25 | + |
| 26 | + final AlertDialog alertDialog = new AlertDialog.Builder(context) |
| 27 | + .setView(view) |
| 28 | + .setCancelable(true) |
| 29 | + .create(); |
| 30 | + |
| 31 | + alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { |
| 32 | + @Override |
| 33 | + public void onDismiss(DialogInterface dialogInterface) { |
| 34 | + callback.onDismissed(); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + View close = view.findViewById(R.id.close); |
| 39 | + close.setOnClickListener(new OneShotClickListener() { |
| 40 | + @Override |
| 41 | + public void click() { |
| 42 | + alertDialog.dismiss(); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + ViewGroup container = view.findViewById(R.id.container); |
| 47 | + |
| 48 | + Product[] bundles = product.getBundleProducts(); |
| 49 | + final Product[] products = new Product[bundles.length + 1]; |
| 50 | + products[0] = product; |
| 51 | + int i=1; |
| 52 | + for(Product p : bundles) { |
| 53 | + products[i] = p; |
| 54 | + i++; |
| 55 | + } |
| 56 | + |
| 57 | + for(final Product p : products) { |
| 58 | + View itemView = View.inflate(context, R.layout.item_bundle_select, null); |
| 59 | + TextView name = itemView.findViewById(R.id.name); |
| 60 | + name.setText(p.getName()); |
| 61 | + itemView.setOnClickListener(new OneShotClickListener() { |
| 62 | + @Override |
| 63 | + public void click() { |
| 64 | + alertDialog.dismiss(); |
| 65 | + callback.onProductSelected(p); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + itemView.setLayoutParams(new ViewGroup.LayoutParams( |
| 70 | + ViewGroup.LayoutParams.MATCH_PARENT, |
| 71 | + ViewGroup.LayoutParams.WRAP_CONTENT)); |
| 72 | + |
| 73 | + container.addView(itemView); |
| 74 | + } |
| 75 | + |
| 76 | + Window window = alertDialog.getWindow(); |
| 77 | + if (window == null) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + window.setGravity(Gravity.BOTTOM); |
| 82 | + alertDialog.show(); |
| 83 | + } |
| 84 | + |
| 85 | + public interface Callback { |
| 86 | + void onProductSelected(Product product); |
| 87 | + void onDismissed(); |
| 88 | + } |
| 89 | +} |
0 commit comments