From dc1c392a94a186f4095924e793ad7fc560905967 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 20 Feb 2016 12:51:52 +0100 Subject: [PATCH] Purchase dialog fixed --- app/src/main/AndroidManifest.xml | 2 +- .../android/calculator/FragmentTab.java | 9 +- .../preferences/PurchaseDialogActivity.java | 125 ++++++++++-------- .../layout/cpp_purchase_dialog_fragment.xml | 48 ------- 4 files changed, 72 insertions(+), 112 deletions(-) delete mode 100644 app/src/main/res/layout/cpp_purchase_dialog_fragment.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 82a03149..f03f5dde 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -105,7 +105,7 @@ + android:theme="@style/Cpp.Theme.Translucent" /> purchaseListener = new RequestListener() { - @Override - public void onSuccess(@Nonnull Purchase purchase) { - finish(); - } - - @Override - public void onError(int i, @Nonnull Exception e) { - finish(); - } - }; +public class PurchaseDialogActivity extends AppCompatActivity implements RequestListener { @Inject Billing billing; @@ -63,25 +50,19 @@ public class PurchaseDialogActivity extends BaseActivity { Products products; ActivityCheckout checkout; - public PurchaseDialogActivity() { - super(R.layout.cpp_dialog); - } - @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - FragmentUtils.createFragment(this, PurchaseDialogFragment.class, R.id.dialog_layout, "purchase-dialog"); + cast(getApplication()).getComponent().inject(this); + + if (savedInstanceState == null) { + App.showDialog(new PurchaseDialogFragment(), PurchaseDialogFragment.FRAGMENT_TAG, getSupportFragmentManager()); + } checkout = Checkout.forActivity(this, billing, products); checkout.start(); - checkout.createPurchaseFlow(purchaseListener); - } - - @Override - protected void inject(@Nonnull AppComponent component) { - super.inject(component); - component.inject(this); + checkout.createPurchaseFlow(this); } @Override @@ -118,34 +99,66 @@ public class PurchaseDialogActivity extends BaseActivity { super.onDestroy(); } - public static class PurchaseDialogFragment extends BaseFragment { + public void onDialogClosed() { + final Fragment fragment = getSupportFragmentManager().findFragmentByTag(PurchaseDialogFragment.FRAGMENT_TAG); + if (fragment == null) { + // activity is closing + return; + } + finish(); + } - @Bind(R.id.cpp_purchase_text) - TextView purchaseText; - @Bind(R.id.cpp_continue_button) - View continueButton; + @Override + public void onSuccess(@Nonnull Purchase result) { + finish(); + } + + @Override + public void onError(int response, @Nonnull Exception e) { + finish(); + } + + public static class PurchaseDialogFragment extends BaseDialogFragment { + + public static final String FRAGMENT_TAG = "purchase-dialog"; + @Nullable + private PurchaseDialogActivity activity; - @Nonnull @Override - protected FragmentUi createUi() { - return createUi(purchase_dialog); + public void onAttach(Activity activity) { + super.onAttach(activity); + this.activity = (PurchaseDialogActivity) activity; } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View view = super.onCreateView(inflater, container, savedInstanceState); - ButterKnife.bind(this, view); - purchaseText.setMovementMethod(ScrollingMovementMethod.getInstance()); - continueButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - final Activity activity = getActivity(); + protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) { + super.onPrepareDialog(builder); + builder.setTitle(R.string.cpp_purchase_title); + builder.setMessage(R.string.cpp_purchase_text); + builder.setPositiveButton(R.string.cpp_continue, null); + } + + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case DialogInterface.BUTTON_POSITIVE: if (activity != null) { - ((PurchaseDialogActivity) activity).purchase(); + activity.purchase(); } - } - }); - return view; + break; + default: + super.onClick(dialog, which); + break; + } + } + + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + if (activity != null) { + activity.onDialogClosed(); + activity = null; + } } } } diff --git a/app/src/main/res/layout/cpp_purchase_dialog_fragment.xml b/app/src/main/res/layout/cpp_purchase_dialog_fragment.xml deleted file mode 100644 index ea23e98d..00000000 --- a/app/src/main/res/layout/cpp_purchase_dialog_fragment.xml +++ /dev/null @@ -1,48 +0,0 @@ - - - - - - - - -