void updateCustomer(@NonNull Customer customer) { mCustomerSourceList.clear(); CustomerSource[] customerSources = new CustomerSource[customer.getSources().size()]; addCustomerSourceIfSupported(customer.getSources().toArray(customerSources)); String sourceId = customer.getDefaultSource(); if (sourceId == null) { updateSelectedIndex(NO_SELECTION); } else { setSelectedSource(sourceId); } notifyDataSetChanged(); }
MaskedCardAdapter(@NonNull List<CustomerSource> startingSources) { mCustomerSourceList = new ArrayList<>(); setCustomerSourceList(startingSources); }
void addCustomerSourceIfSupported(CustomerSource... customerSources) { if (customerSources == null) { return; } for (CustomerSource customerSource : customerSources) { if (customerSource.asCard() != null || canDisplaySource(customerSource.asSource())) { // If it's a card, we can display it. mCustomerSourceList.add(customerSource); } } notifyDataSetChanged(); }
private void createListFromCustomerSources() { setCommunicatingProgress(false); if (mCustomer == null) { return; } List<CustomerSource> customerSourceList = mCustomer.getSources(); if (!mRecyclerViewUpdated) { mMaskedCardAdapter = new MaskedCardAdapter(customerSourceList); // init the RecyclerView RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this); mRecyclerView.setHasFixedSize(false); mRecyclerView.setLayoutManager(linearLayoutManager); mRecyclerView.setAdapter(mMaskedCardAdapter); mRecyclerViewUpdated = true; } else { mMaskedCardAdapter.setCustomerSourceList(customerSourceList); } String defaultSource = mCustomer.getDefaultSource(); if (!TextUtils.isEmpty(defaultSource)) { mMaskedCardAdapter.setSelectedSource(defaultSource); } mMaskedCardAdapter.notifyDataSetChanged(); }
@Test public void updateCustomer_removesExistingSourcesAndAddsAllCustomerSources() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE_SECOND); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource); assertEquals(1, mMaskedCardAdapter.getItemCount()); assertNull(mMaskedCardAdapter.getSelectedSource()); Customer customer = Customer.fromString(PaymentMethodsActivityTest.TEST_CUSTOMER_OBJECT_WITH_SOURCES); assertNotNull(customer); mMaskedCardAdapter.updateCustomer(customer); assertEquals(2, mMaskedCardAdapter.getItemCount()); assertNotNull(mMaskedCardAdapter.getSelectedSource()); assertEquals(customer.getDefaultSource(), mMaskedCardAdapter.getSelectedSource().getId()); verify(mAdapterDataObserver, times(4)).onChanged(); }
@Test public void setSelection_changesSelection() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE); CustomerSource secondCardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE_SECOND); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource, secondCardSource); assertEquals(2, mMaskedCardAdapter.getItemCount()); verify(mAdapterDataObserver, times(1)).onChanged(); assertNull(mMaskedCardAdapter.getSelectedSource()); assertNotNull(secondCardSource); assertNotNull(secondCardSource.getId()); mMaskedCardAdapter.setSelectedSource(secondCardSource.getId()); verify(mAdapterDataObserver, times(2)).onChanged(); assertNotNull(mMaskedCardAdapter.getSelectedSource()); assertEquals(secondCardSource.getId(), mMaskedCardAdapter.getSelectedSource().getId()); }
@Before public void setup() { MockitoAnnotations.initMocks(this); mMaskedCardAdapter = new MaskedCardAdapter(new ArrayList<CustomerSource>()); mMaskedCardAdapter.registerAdapterDataObserver(mAdapterDataObserver); }
@Test public void setCustomerSourceList_filtersOutNonCardSources() { List<CustomerSource> customerSourceList = new ArrayList<>(); CustomerSource cardCustomerSource = Mockito.mock(CustomerSource.class); Card card = Mockito.mock(Card.class); Mockito.when(cardCustomerSource.asCard()).thenReturn(card); CustomerSource nonCardCustomerSource = Mockito.mock(CustomerSource.class); customerSourceList.add(cardCustomerSource); customerSourceList.add(nonCardCustomerSource); mMaskedCardAdapter.setCustomerSourceList(customerSourceList); assertEquals(1, mMaskedCardAdapter.getItemCount()); } }
@Test public void updateCustomer_filtersOutNonCardSources() { List<CustomerSource> customerSourceList = new ArrayList<>(); CustomerSource cardCustomerSource = Mockito.mock(CustomerSource.class); Card card = Mockito.mock(Card.class); Mockito.when(cardCustomerSource.asCard()).thenReturn(card); CustomerSource nonCardCustomerSource = Mockito.mock(CustomerSource.class); customerSourceList.add(cardCustomerSource); customerSourceList.add(nonCardCustomerSource); Customer customer = Mockito.mock(Customer.class); Mockito.when(customer.getSources()).thenReturn(customerSourceList); mMaskedCardAdapter.updateCustomer(customer); assertEquals(1, mMaskedCardAdapter.getItemCount()); }
@Test public void addSources_onlyDisplaysCards() { CustomerSource cardSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_CARD_SOURCE); CustomerSource bitcoinSource = CustomerSource.fromString(CardInputTestActivity.EXAMPLE_JSON_SOURCE_BITCOIN); mMaskedCardAdapter.addCustomerSourceIfSupported(cardSource, bitcoinSource); assertEquals(1, mMaskedCardAdapter.getItemCount()); verify(mAdapterDataObserver).onChanged(); }
void setCustomerSourceList(@NonNull List<CustomerSource> sourceList) { mCustomerSourceList.clear(); CustomerSource[] customerSources = new CustomerSource[sourceList.size()]; addCustomerSourceIfSupported(sourceList.toArray(customerSources)); }
private void updateAdapterWithCustomer(@NonNull Customer customer) { if (mMaskedCardAdapter == null) { createListFromCustomerSources(); if (mCustomer == null) { return; } } mMaskedCardAdapter.updateCustomer(customer); setCommunicatingProgress(false); }
private void setSelectionAndFinish() { if (mMaskedCardAdapter == null || mMaskedCardAdapter.getSelectedSource() == null) { cancelAndFinish(); return; CustomerSource selectedSource = mMaskedCardAdapter.getSelectedSource(); CustomerSession.CustomerRetrievalListener listener = new CustomerSession.CustomerRetrievalListener() {
void updateSelectedIndex(int selectedIndex) { mSelectedIndex = selectedIndex; notifyDataSetChanged(); }