Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove CustomerSessionProxy from PaymentMethodsActivity #861

Merged
merged 2 commits into from Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -44,13 +44,14 @@ public class PaymentMethodsActivity extends AppCompatActivity {
static final int REQUEST_CODE_ADD_CARD = 700;
private boolean mCommunicating;
private Customer mCustomer;
private CustomerSessionProxy mCustomerSessionProxy;
private MaskedCardAdapter mMaskedCardAdapter;
private ProgressBar mProgressBar;
private RecyclerView mRecyclerView;
private boolean mRecyclerViewUpdated;
private boolean mStartedFromPaymentSession;

private CustomerSession mCustomerSession;

/**
* @deprecated use {@link PaymentMethodsActivityStarter#newIntent()}
*/
Expand All @@ -67,24 +68,26 @@ protected void onCreate(Bundle savedInstanceState) {

mProgressBar = findViewById(R.id.payment_methods_progress_bar);
mRecyclerView = findViewById(R.id.payment_methods_recycler);
View addCardView = findViewById(R.id.payment_methods_add_payment_container);
final View addCardView = findViewById(R.id.payment_methods_add_payment_container);

addCardView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent addSourceIntent = AddSourceActivity.newIntent(
PaymentMethodsActivity.this,
false,
true);
if (mStartedFromPaymentSession) {
addSourceIntent.putExtra(EXTRA_PAYMENT_SESSION_ACTIVE, true);
}
startActivityForResult(addSourceIntent, REQUEST_CODE_ADD_CARD);
}
});
mCustomerSession = CustomerSession.getInstance();
mStartedFromPaymentSession = getIntent().hasExtra(EXTRA_PAYMENT_SESSION_ACTIVE);

addCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(@NonNull View view) {
final Intent addSourceIntent = AddSourceActivity.newIntent(
PaymentMethodsActivity.this,
false,
true);
if (mStartedFromPaymentSession) {
addSourceIntent.putExtra(EXTRA_PAYMENT_SESSION_ACTIVE, true);
}
startActivityForResult(addSourceIntent, REQUEST_CODE_ADD_CARD);
}
});

Toolbar toolbar = findViewById(R.id.payment_methods_toolbar);
final Toolbar toolbar = findViewById(R.id.payment_methods_toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Expand All @@ -96,7 +99,6 @@ public void onClick(View view) {
}
// This prevents the first click from being eaten by the focus.
addCardView.requestFocusFromTouch();
mStartedFromPaymentSession = getIntent().hasExtra(EXTRA_PAYMENT_SESSION_ACTIVE);
}

@Override
Expand All @@ -122,23 +124,18 @@ public void onError(int httpCode, @Nullable String errorMessage,
setCommunicatingProgress(false);
}
};
if (mCustomerSessionProxy == null) {
CustomerSession.getInstance().updateCurrentCustomer(listener);
} else {
mCustomerSessionProxy.updateCurrentCustomer(listener);
}
mCustomerSession.updateCurrentCustomer(listener);
}
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
final MenuItem saveItem = menu.findItem(R.id.action_save);
final Drawable compatIcon =
ViewUtils.getTintedIconWithAttribute(
this,
getTheme(),
R.attr.titleTextColor,
R.drawable.ic_checkmark);
final Drawable compatIcon = ViewUtils.getTintedIconWithAttribute(
this,
getTheme(),
R.attr.titleTextColor,
R.drawable.ic_checkmark);
saveItem.setIcon(compatIcon);
return super.onPrepareOptionsMenu(menu);
}
Expand All @@ -157,7 +154,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
setSelectionAndFinish();
return true;
} else {
boolean handled = super.onOptionsItemSelected(item);
final boolean handled = super.onOptionsItemSelected(item);
if (!handled) {
onBackPressed();
}
Expand All @@ -167,9 +164,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

@VisibleForTesting
void initializeCustomerSourceData() {
final Customer cachedCustomer = mCustomerSessionProxy == null
? CustomerSession.getInstance().getCachedCustomer()
: mCustomerSessionProxy.getCachedCustomer();
final Customer cachedCustomer = mCustomerSession.getCachedCustomer();

if (cachedCustomer != null) {
mCustomer = cachedCustomer;
Expand All @@ -179,23 +174,11 @@ void initializeCustomerSourceData() {
}
}

@VisibleForTesting
void setCustomerSessionProxy(CustomerSessionProxy proxy) {
mCustomerSessionProxy = proxy;
}

private void initLoggingTokens() {
if (mCustomerSessionProxy == null) {
if (mStartedFromPaymentSession) {
CustomerSession.getInstance().addProductUsageTokenIfValid(TOKEN_PAYMENT_SESSION);
}
CustomerSession.getInstance().addProductUsageTokenIfValid(PAYMENT_METHODS_ACTIVITY);
} else {
if (mStartedFromPaymentSession) {
mCustomerSessionProxy.addProductUsageTokenIfValid(TOKEN_PAYMENT_SESSION);
}
mCustomerSessionProxy.addProductUsageTokenIfValid(PAYMENT_METHODS_ACTIVITY);
if (mStartedFromPaymentSession) {
mCustomerSession.addProductUsageTokenIfValid(TOKEN_PAYMENT_SESSION);
}
mCustomerSession.addProductUsageTokenIfValid(PAYMENT_METHODS_ACTIVITY);
}

/**
Expand All @@ -214,7 +197,7 @@ private void updateCustomerAndSetDefaultSourceIfNecessary(@NonNull Customer cust
return;
}

CustomerSession.CustomerRetrievalListener listener =
final CustomerSession.CustomerRetrievalListener listener =
new CustomerSession.CustomerRetrievalListener() {
@Override
public void onCustomerRetrieved(@NonNull Customer customer) {
Expand All @@ -236,7 +219,7 @@ public void onError(int httpCode, @Nullable String errorMessage,
};

// We only activate this if there is a single source in the list
CustomerSource customerSource = customer.getSources().get(0);
final CustomerSource customerSource = customer.getSources().get(0);
if (customerSource == null || customerSource.getId() == null) {
// If the source ID is null for the only source we have, then there is nothing
// we can do but update the display. This should not happen. It is only possible
Expand All @@ -246,18 +229,11 @@ public void onError(int httpCode, @Nullable String errorMessage,
return;
}

if (mCustomerSessionProxy == null) {
CustomerSession.getInstance().setCustomerDefaultSource(
this,
customerSource.getId(),
customerSource.getSourceType(),
listener);
} else {
mCustomerSessionProxy.setCustomerDefaultSource(
customerSource.getId(),
customerSource.getSourceType(),
listener);
}
mCustomerSession.setCustomerDefaultSource(
this,
customerSource.getId(),
customerSource.getSourceType(),
listener);
}

private void createListFromCustomerSources() {
Expand Down Expand Up @@ -320,11 +296,7 @@ public void onError(int httpCode, @Nullable String errorMessage,
};

setCommunicatingProgress(true);
if (mCustomerSessionProxy == null) {
CustomerSession.getInstance().retrieveCurrentCustomer(listener);
} else {
mCustomerSessionProxy.retrieveCurrentCustomer(listener);
}
mCustomerSession.retrieveCurrentCustomer(listener);
}

private void setCommunicatingProgress(boolean communicating) {
Expand All @@ -343,8 +315,8 @@ private void setSelectionAndFinish() {
return;
}

CustomerSource selectedSource = mMaskedCardAdapter.getSelectedSource();
CustomerSession.CustomerRetrievalListener listener =
final CustomerSource selectedSource = mMaskedCardAdapter.getSelectedSource();
final CustomerSession.CustomerRetrievalListener listener =
new CustomerSession.CustomerRetrievalListener() {
@Override
public void onCustomerRetrieved(@NonNull Customer customer) {
Expand All @@ -365,15 +337,8 @@ public void onError(int httpCode, @Nullable String errorMessage,
if (selectedSource == null || selectedSource.getId() == null) {
return;
}
if (mCustomerSessionProxy == null) {
CustomerSession.getInstance().setCustomerDefaultSource(
this, selectedSource.getId(), selectedSource.getSourceType(), listener);
} else {
mCustomerSessionProxy.setCustomerDefaultSource(
selectedSource.getId(),
selectedSource.getSourceType(),
listener);
}
mCustomerSession.setCustomerDefaultSource(
this, selectedSource.getId(), selectedSource.getSourceType(), listener);
setCommunicatingProgress(true);
}

Expand Down Expand Up @@ -401,19 +366,4 @@ private void updateAdapterWithCustomer(@NonNull Customer customer) {
mMaskedCardAdapter.updateCustomer(customer);
setCommunicatingProgress(false);
}

interface CustomerSessionProxy {
void addProductUsageTokenIfValid(@NonNull String token);

@Nullable
Customer getCachedCustomer();

void retrieveCurrentCustomer(@NonNull CustomerSession.CustomerRetrievalListener listener);

void setCustomerDefaultSource(@NonNull String sourceId,
@NonNull String sourceType,
@Nullable CustomerSession.CustomerRetrievalListener listener);

void updateCurrentCustomer(@NonNull CustomerSession.CustomerRetrievalListener listener);
}
}
Expand Up @@ -24,7 +24,6 @@
import com.stripe.android.view.PaymentFlowActivity;

import org.json.JSONException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -241,13 +240,6 @@ public Object answer(InvocationOnMock invocation) {
}).when(mThreadPoolExecutor).execute(any(Runnable.class));
}

@After
public void teardown() {
LocalBroadcastManager.getInstance(mContext)
.unregisterReceiver(mBroadcastReceiver);
CustomerSession.endCustomerSession();
}

@Test(expected = IllegalStateException.class)
public void getInstance_withoutInitializing_throwsException() {
CustomerSession.clearInstance();
Expand Down
@@ -0,0 +1,12 @@
package com.stripe.android;

import android.support.annotation.NonNull;

public class CustomerSessionTestHelper {
private CustomerSessionTestHelper() {
}

public static void setInstance(@NonNull CustomerSession customerSession) {
CustomerSession.setInstance(customerSession);
}
}