[android] Fixed crash during show alert dialog on payment failure

[android] Refactore AlertDialog class to use builder to reduce input parameters
This commit is contained in:
Alexander Zatsepin 2018-09-26 16:26:14 +03:00 committed by Aleksandr Zatsepin
parent 778d0781bc
commit c0368c5a45
2 changed files with 127 additions and 39 deletions

View file

@ -7,8 +7,9 @@ import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.mapswithme.maps.base.BaseMwmDialogFragment;
@ -26,19 +27,15 @@ public class AlertDialog extends BaseMwmDialogFragment
@Nullable
private AlertDialogCallback mTargetCallback;
public static void show(@StringRes int titleId, @StringRes int messageId,
@StringRes int positiveBtnId, @NonNull Fragment parent,
int requestCode)
public void show(@NonNull Fragment parent, @NonNull String tag)
{
Bundle args = new Bundle();
args.putInt(ARG_TITLE_ID, titleId);
args.putInt(ARG_MESSAGE_ID, messageId);
args.putInt(ARG_POSITIVE_BUTTON_ID, positiveBtnId);
args.putInt(ARG_REQ_CODE, requestCode);
DialogFragment fragment = (DialogFragment) Fragment.instantiate(parent.getActivity(),
AlertDialog.class.getName());
fragment.setArguments(args);
fragment.show(parent.getChildFragmentManager(), AlertDialog.class.getName());
FragmentManager fm = parent.getChildFragmentManager();
if (fm.findFragmentByTag(tag) != null)
return;
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(this, tag);
transaction.commitAllowingStateLoss();
}
private void onClick(int which)
@ -92,4 +89,85 @@ public class AlertDialog extends BaseMwmDialogFragment
if (mTargetCallback != null)
mTargetCallback.onAlertDialogCancel(getArguments().getInt(ARG_REQ_CODE));
}
@NonNull
private static AlertDialog createDialog(@NonNull Builder builder)
{
Bundle args = new Bundle();
args.putInt(ARG_TITLE_ID, builder.getTitleId());
args.putInt(ARG_MESSAGE_ID, builder.getMessageId());
args.putInt(ARG_POSITIVE_BUTTON_ID, builder.getPositiveBtnId());
args.putInt(ARG_REQ_CODE, builder.getReqCode());
AlertDialog dialog = new AlertDialog();
dialog.setArguments(args);
return dialog;
}
public static class Builder
{
private int mReqCode;
@StringRes
private int mTitleId;
@StringRes
private int mMessageId;
@StringRes
private int mPositiveBtn;
@NonNull
public Builder setReqCode(int reqCode)
{
mReqCode = reqCode;
return this;
}
int getReqCode()
{
return mReqCode;
}
@NonNull
public Builder setTitleId(@StringRes int titleId)
{
mTitleId = titleId;
return this;
}
@StringRes
int getTitleId()
{
return mTitleId;
}
@NonNull
public Builder setMessageId(@StringRes int messageId)
{
mMessageId = messageId;
return this;
}
@StringRes
int getMessageId()
{
return mMessageId;
}
@NonNull
public Builder setPositiveBtnId(@StringRes int positiveBtnId)
{
mPositiveBtn = positiveBtnId;
return this;
}
@StringRes
int getPositiveBtnId()
{
return mPositiveBtn;
}
@NonNull
public AlertDialog build()
{
return createDialog(this);
}
}
}

View file

@ -79,33 +79,43 @@ enum AdsRemovalPaymentState
UiUtils.show(progressLayout);
}
},
PAYMENT_FAILURE
{
@Override
void activate(@NonNull AdsRemovalPurchaseDialog dialog)
{
AlertDialog.show(R.string.bookmarks_convert_error_title, R.string.purchase_error_subtitle,
R.string.back, dialog, AdsRemovalPurchaseDialog.REQ_CODE_PAYMENT_FAILURE);
}
},
PRODUCT_DETAILS_FAILURE
{
@Override
void activate(@NonNull AdsRemovalPurchaseDialog dialog)
{
AlertDialog.show(R.string.bookmarks_convert_error_title,
R.string.discovery_button_other_error_message, R.string.ok,
dialog, AdsRemovalPurchaseDialog.REQ_CODE_PRODUCT_DETAILS_FAILURE);
}
},
VALIDATION_FINISH
PAYMENT_FAILURE
{
@Override
void activate(@NonNull AdsRemovalPurchaseDialog dialog)
{
@Override
void activate(@NonNull AdsRemovalPurchaseDialog dialog)
{
dialog.dismissAllowingStateLoss();
}
};
AlertDialog alertDialog = new AlertDialog.Builder()
.setReqCode(AdsRemovalPurchaseDialog.REQ_CODE_PAYMENT_FAILURE)
.setTitleId(R.string.bookmarks_convert_error_title)
.setMessageId(R.string.purchase_error_subtitle)
.setPositiveBtnId(R.string.back)
.build();
alertDialog.show(dialog, name());
}
},
PRODUCT_DETAILS_FAILURE
{
@Override
void activate(@NonNull AdsRemovalPurchaseDialog dialog)
{
AlertDialog alertDialog = new AlertDialog.Builder()
.setReqCode(AdsRemovalPurchaseDialog.REQ_CODE_PAYMENT_FAILURE)
.setTitleId(R.string.bookmarks_convert_error_title)
.setMessageId(R.string.discovery_button_other_error_message)
.setPositiveBtnId(R.string.ok)
.build();
alertDialog.show(dialog, name());
}
},
VALIDATION_FINISH
{
@Override
void activate(@NonNull AdsRemovalPurchaseDialog dialog)
{
dialog.dismissAllowingStateLoss();
}
};
private static void alignPayButtonBelow(@NonNull View view, @IdRes int anchor)
{