com.mastercard.masterpasswallet.adapters.CardPagerAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.mastercard.masterpasswallet.adapters.CardPagerAdapter.java

Source

/*
 *  ****************************************************************************
 *  Copyright (c) 2015, MasterCard International Incorporated and/or its
 *  affiliates. All rights reserved.
 *  <p/>
 *  The contents of this file may only be used subject to the MasterCard
 *  Mobile Payment SDK for MCBP and/or MasterCard Mobile MPP UI SDK
 *  Materials License.
 *  <p/>
 *  Please refer to the file LICENSE.TXT for full details.
 *  <p/>
 *  TO THE EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
 *  WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 *  WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NON INFRINGEMENT. TO THE EXTENT PERMITTED BY LAW, IN NO EVENT SHALL
 *  MASTERCARD OR ITS AFFILIATES BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 *  IN THE SOFTWARE.
 *  *****************************************************************************
 */

package com.mastercard.masterpasswallet.adapters;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.mastercard.masterpasswallet.R;
import com.mastercard.masterpasswallet.models.Card;
import com.mastercard.masterpasswallet.models.PaymentContext;

import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * View Pager for cards on CardsActivity screen. Lets user swipe through cards
 */
public class CardPagerAdapter extends PagerAdapter {

    private final Context mContext;
    private final ArrayList<Card> mCards;
    private CardClickedListener mListener;

    /**
     * Exposed actions
     */
    public interface CardClickedListener {
        /**
         * Invoked when user clicks "i" icon to show card information
         *
         * @param card clicked card
         */
        void showCardInfo(Card card);

        /**
         * Invoked when user click "heart" icon to make card default for contactless payments
         *
         * @param card clicked card
         */
        void makeDefaultcard(Card card);
    }

    private CardPagerAdapter(Context context, ArrayList<Card> cards) {
        mContext = context;
        mCards = cards;
    }

    public CardPagerAdapter(Context context, ArrayList<Card> cards, CardClickedListener listener) {
        this(context, cards);
        mListener = listener;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final Card card = mCards.get(position);

        View view = LayoutInflater.from(mContext).inflate(R.layout.partial_payment_card, null);
        ImageView cardImage = (ImageView) view.findViewById(R.id.img_card);
        cardImage.setImageResource(card.getCardImageFull());
        TextView cardPan = (TextView) view.findViewById(R.id.txt_card_pan);
        cardPan.setText(mContext.getString(R.string.masked_pan, " " + card.getPANEnding()));

        TextView cardName = (TextView) view.findViewById(R.id.txt_card_name);
        cardName.setText(card.getCardName());
        cardName.setVisibility(View.VISIBLE);

        // Show/hide overlay to indicate if card is suspended or not
        View inactiveOverlay = view.findViewById(R.id.img_overlay);
        if (card.IsActive()) {
            inactiveOverlay.setVisibility(View.GONE);
        } else {
            inactiveOverlay.setVisibility(View.VISIBLE);
        }

        // Set actions
        if (mListener != null) {
            // Configure card info action
            ImageView imgInfo = (ImageView) view.findViewById(R.id.img_info);
            imgInfo.setVisibility(View.VISIBLE);
            imgInfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.showCardInfo(card);
                }
            });

            // Configure making card default
            ImageView imgDefaultCard = (ImageView) view.findViewById(R.id.img_default_card);
            imgDefaultCard.setVisibility(View.VISIBLE);
            if (card.isDefault(PaymentContext.CONTACTLESS)) {
                imgDefaultCard.setImageResource(R.drawable.ic_favourite_selected);
            } else {
                imgDefaultCard.setImageResource(R.drawable.ic_favourite_deselected);
            }
            imgDefaultCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.makeDefaultcard(card);
                }
            });

        }

        // Configure card balance
        View layBalance = view.findViewById(R.id.lay_balance);
        layBalance.setVisibility(View.VISIBLE);

        TextView txtBalanceType = (TextView) view.findViewById(R.id.tvBalanceType);
        TextView txtBalance = (TextView) view.findViewById(R.id.tvBalance);
        if (card.isCreditCard()) {
            txtBalanceType.setText("Current Balance:");
        } else {
            txtBalanceType.setText("Available Balance:");
        }
        DecimalFormat decimalFormat = new DecimalFormat("$##,##0.00");
        String amount = decimalFormat.format(card.getCardBalance() / 100f);
        txtBalance.setText(amount);

        // Add the layout to the container
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return mCards.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}