Search in sources :

Example 1 with InsufficientFundsException

use of com.soomla.store.exceptions.InsufficientFundsException in project android-store by soomla.

the class StorePacksActivity method onCreate.

/**
     * Called when the activity starts.
     *
     * @param savedInstanceState if the activity should be re-initialized after previously being
     *                           shut down then this <code>Bundle</code> will contain the most
     *                           recent data, otherwise it will be null.
     */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    LinearLayout getMore = (LinearLayout) findViewById(R.id.getMore);
    TextView title = (TextView) findViewById(R.id.title);
    getMore.setVisibility(View.INVISIBLE);
    title.setText("Virtual Currency Packs");
    mImages = generateImagesHash();
    mStoreAdapter = new StoreAdapter();
    /* configuring the list with an adapter */
    final Activity activity = this;
    ListView list = (ListView) findViewById(R.id.list);
    list.setAdapter(mStoreAdapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            /*
                * The user decided to make an actual purchase of virtual goods. We try to buy() the
                * user's desired good and SoomlaStore tells us if the user has enough funds to
                * make the purchase. If he/she doesn't have enough then an
                * InsufficientFundsException will be thrown.
                */
            PurchaseWithMarket pwm = null;
            VirtualCurrencyPack pack = StoreInfo.getCurrencyPacks().get(i);
            pwm = (PurchaseWithMarket) pack.getPurchaseType();
            try {
                pwm.buy("this is just a payload");
            } catch (InsufficientFundsException e) {
                AlertDialog ad = new AlertDialog.Builder(activity).create();
                // This blocks the 'BACK' button
                ad.setCancelable(false);
                ad.setMessage("Can't continue with purchase (You don't have enough muffins !)");
                ad.setButton(DialogInterface.BUTTON_NEGATIVE, "OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                ad.show();
            }
            /* fetching the currency balance and placing it in the balance label */
            TextView muffinsBalance = (TextView) activity.findViewById(R.id.balance);
            muffinsBalance.setText("" + StorageManager.getVirtualCurrencyStorage().getBalance(StoreInfo.getCurrencies().get(0).getItemId()));
        }
    });
}
Also used : AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Activity(android.app.Activity) VirtualCurrencyPack(com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) ListView(android.widget.ListView) InsufficientFundsException(com.soomla.store.exceptions.InsufficientFundsException) TextView(android.widget.TextView) AdapterView(android.widget.AdapterView) PurchaseWithMarket(com.soomla.store.purchaseTypes.PurchaseWithMarket) LinearLayout(android.widget.LinearLayout)

Example 2 with InsufficientFundsException

use of com.soomla.store.exceptions.InsufficientFundsException in project android-store by soomla.

the class PurchaseWithVirtualItem method buy.

/**
     * Buys the virtual item with other virtual items.
     *
     * @throws InsufficientFundsException
     */
@Override
public void buy(String payload) throws InsufficientFundsException {
    SoomlaUtils.LogDebug(TAG, "Trying to buy a " + getAssociatedItem().getName() + " with " + mAmount + " pieces of " + mTargetItemId);
    VirtualItem item = null;
    try {
        item = StoreInfo.getVirtualItem(mTargetItemId);
    } catch (VirtualItemNotFoundException e) {
        SoomlaUtils.LogError(TAG, "Target virtual item doesn't exist !");
        return;
    }
    BusProvider.getInstance().post(new ItemPurchaseStartedEvent(getAssociatedItem().getItemId()));
    VirtualItemStorage storage = StorageManager.getVirtualItemStorage(item);
    assert storage != null;
    int balance = storage.getBalance(item.getItemId());
    if (balance < mAmount) {
        throw new InsufficientFundsException(mTargetItemId);
    }
    storage.remove(item.getItemId(), mAmount);
    getAssociatedItem().give(1);
    BusProvider.getInstance().post(new ItemPurchasedEvent(getAssociatedItem().getItemId(), payload));
}
Also used : ItemPurchaseStartedEvent(com.soomla.store.events.ItemPurchaseStartedEvent) VirtualItemNotFoundException(com.soomla.store.exceptions.VirtualItemNotFoundException) VirtualItem(com.soomla.store.domain.VirtualItem) VirtualItemStorage(com.soomla.store.data.VirtualItemStorage) InsufficientFundsException(com.soomla.store.exceptions.InsufficientFundsException) ItemPurchasedEvent(com.soomla.store.events.ItemPurchasedEvent)

Example 3 with InsufficientFundsException

use of com.soomla.store.exceptions.InsufficientFundsException in project android-store by soomla.

the class StoreGoodsActivity method onCreate.

/**
     * Called when the activity starts.
     * Displays the list view of the game, where users can see the available goods for purchase.
     *
     * @param savedInstanceState if the activity should be re-initialized after previously being
     *                           shut down then this <code>Bundle</code> will contain the most
     *                           recent data, otherwise it will be null.
     */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    SoomlaStore.getInstance().startIabServiceInBg();
    TextView title = (TextView) findViewById(R.id.title);
    title.setText("Virtual Goods");
    mImages = generateImagesHash();
    mStoreAdapter = new StoreAdapter();
    /* configuring the list with an adapter */
    final Activity activity = this;
    ListView list = (ListView) findViewById(R.id.list);
    list.setAdapter(mStoreAdapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            /*
                The user decided to make an actual purchase of virtual goods. We try to buy() the
                user's desired good and SoomlaStore tells us if the user has enough funds to
                make the purchase. If he/she doesn't have enough then an InsufficientFundsException
                will be thrown.
                */
            VirtualGood good = StoreInfo.getGoods().get(i);
            try {
                good.buy("this is just a payload");
            } catch (InsufficientFundsException e) {
                AlertDialog ad = new AlertDialog.Builder(activity).create();
                ad.setCancelable(false);
                ad.setMessage("You don't have enough muffins.");
                ad.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                ad.show();
            }
        }
    });
}
Also used : VirtualGood(com.soomla.store.domain.virtualGoods.VirtualGood) AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) Activity(android.app.Activity) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ListView(android.widget.ListView) InsufficientFundsException(com.soomla.store.exceptions.InsufficientFundsException) TextView(android.widget.TextView) AdapterView(android.widget.AdapterView)

Aggregations

InsufficientFundsException (com.soomla.store.exceptions.InsufficientFundsException)3 Activity (android.app.Activity)2 AlertDialog (android.app.AlertDialog)2 DialogInterface (android.content.DialogInterface)2 View (android.view.View)2 AdapterView (android.widget.AdapterView)2 ImageView (android.widget.ImageView)2 ListView (android.widget.ListView)2 TextView (android.widget.TextView)2 LinearLayout (android.widget.LinearLayout)1 VirtualItemStorage (com.soomla.store.data.VirtualItemStorage)1 VirtualItem (com.soomla.store.domain.VirtualItem)1 VirtualCurrencyPack (com.soomla.store.domain.virtualCurrencies.VirtualCurrencyPack)1 VirtualGood (com.soomla.store.domain.virtualGoods.VirtualGood)1 ItemPurchaseStartedEvent (com.soomla.store.events.ItemPurchaseStartedEvent)1 ItemPurchasedEvent (com.soomla.store.events.ItemPurchasedEvent)1 VirtualItemNotFoundException (com.soomla.store.exceptions.VirtualItemNotFoundException)1 PurchaseWithMarket (com.soomla.store.purchaseTypes.PurchaseWithMarket)1