Exemplo n.º 1
0
  /**
   * Read in the wishlist from the file, and pack it into an ArrayList of CompressedWishlistInfo for
   * display in the ListView. This data structure stores one copy of the card itself, and a list of
   * set-specific attributes like set name, rarity, and price.
   *
   * @param changedCardName If the wishlist was changed by a dialog, this is the card which we
   *     should look at for changes
   */
  private void readAndCompressWishlist(String changedCardName) {
    /* Read the wishlist */
    ArrayList<MtgCard> wishlist = WishlistHelpers.ReadWishlist(getActivity());
    SQLiteDatabase database = DatabaseManager.getInstance(getActivity(), false).openDatabase(false);
    try {
      /* Translate the set code to tcg name, of course it's not saved */
      for (MtgCard card : wishlist) {
        card.setName = CardDbAdapter.getSetNameFromCode(card.setCode, database);
      }

      /* Clear the wishlist, or just the card that changed */
      if (changedCardName == null) {
        mCompressedWishlist.clear();
      } else {
        for (CompressedWishlistInfo cwi : mCompressedWishlist) {
          if (cwi.mCard.name.equals(changedCardName)) {
            cwi.clearCompressedInfo();
          }
        }
      }

      /* Compress the whole wishlist, or just the card that changed */
      for (MtgCard card : wishlist) {
        if (changedCardName == null || changedCardName.equals(card.name)) {
          /* This works because both MtgCard's and CompressedWishlistInfo's .equals() can compare each
           * other */
          if (!mCompressedWishlist.contains(card)) {
            mCompressedWishlist.add(new CompressedWishlistInfo(card));
          } else {
            mCompressedWishlist.get(mCompressedWishlist.indexOf(card)).add(card);
          }
          /* Look up the new price */
          if (mShowIndividualPrices || mShowTotalWishlistPrice) {
            loadPrice(card.name, card.setCode, card.number);
          }
        }
      }

      /* Check for wholly removed cards if one card was modified */
      if (changedCardName != null) {
        for (int i = 0; i < mCompressedWishlist.size(); i++) {
          if (mCompressedWishlist.get(i).mInfo.size() == 0) {
            mCompressedWishlist.remove(i);
            i--;
          }
        }
      }

      /* Fill extra card data from the database, for displaying full card info */
      CardDbAdapter.fillExtraWishlistData(mCompressedWishlist, database);

    } catch (FamiliarDbException e) {
      handleFamiliarDbException(false);
    }
    DatabaseManager.getInstance(getActivity(), false).closeDatabase(false);
  }
Exemplo n.º 2
0
  /**
   * This function takes care of adding a card to the wishlist from this fragment. It makes sure
   * that fields are not null or have bad information.
   */
  private void addCardToWishlist() {
    /* Do not allow empty fields */
    String name = String.valueOf(mNameField.getText());
    String numberOf = (String.valueOf(mNumberField.getText()));
    if (name == null || name.equals("")) {
      return;
    }
    if (numberOf == null || numberOf.equals("")) {
      return;
    }

    SQLiteDatabase database = DatabaseManager.getInstance(getActivity(), false).openDatabase(false);
    try {
      /* Make the new card */
      MtgCard card = new MtgCard();
      card.name = name;
      card.foil = mFoilCheckBox.isChecked();
      card.numberOf = Integer.parseInt(numberOf);
      card.message = getString(R.string.wishlist_loading);

      /* Get some extra information from the database */
      Cursor cardCursor = CardDbAdapter.fetchCardByName(card.name, CardDbAdapter.allData, database);
      if (cardCursor.getCount() == 0) {
        ToastWrapper.makeText(
                WishlistFragment.this.getActivity(),
                getString(R.string.toast_no_card),
                ToastWrapper.LENGTH_LONG)
            .show();
        DatabaseManager.getInstance(getActivity(), false).closeDatabase(false);
        return;
      }
      card.type = cardCursor.getString(cardCursor.getColumnIndex(CardDbAdapter.KEY_TYPE));
      card.rarity = (char) cardCursor.getInt(cardCursor.getColumnIndex(CardDbAdapter.KEY_RARITY));
      card.manaCost = cardCursor.getString(cardCursor.getColumnIndex(CardDbAdapter.KEY_MANACOST));
      card.power = cardCursor.getInt(cardCursor.getColumnIndex(CardDbAdapter.KEY_POWER));
      card.toughness = cardCursor.getInt(cardCursor.getColumnIndex(CardDbAdapter.KEY_TOUGHNESS));
      card.loyalty = cardCursor.getInt(cardCursor.getColumnIndex(CardDbAdapter.KEY_LOYALTY));
      card.ability = cardCursor.getString(cardCursor.getColumnIndex(CardDbAdapter.KEY_ABILITY));
      card.flavor = cardCursor.getString(cardCursor.getColumnIndex(CardDbAdapter.KEY_FLAVOR));
      card.number = cardCursor.getString(cardCursor.getColumnIndex(CardDbAdapter.KEY_NUMBER));
      card.setCode = cardCursor.getString(cardCursor.getColumnIndex(CardDbAdapter.KEY_SET));
      card.setName = CardDbAdapter.getSetNameFromCode(card.setCode, database);
      card.cmc = cardCursor.getInt((cardCursor.getColumnIndex(CardDbAdapter.KEY_CMC)));
      card.color = cardCursor.getString(cardCursor.getColumnIndex(CardDbAdapter.KEY_COLOR));
      /* Override choice if the card can't be foil */
      if (!CardDbAdapter.canBeFoil(card.setCode, database)) {
        card.foil = false;
      }
      /* Clean up */
      cardCursor.close();

      /* Add it to the wishlist, either as a new CompressedWishlistInfo, or to an existing one */
      if (mCompressedWishlist.contains(card)) {
        CompressedWishlistInfo cwi = mCompressedWishlist.get(mCompressedWishlist.indexOf(card));
        boolean added = false;
        for (IndividualSetInfo isi : cwi.mInfo) {
          if (isi.mSetCode.equals(card.setCode) && isi.mIsFoil.equals(card.foil)) {
            added = true;
            isi.mNumberOf++;
          }
        }
        if (!added) {
          cwi.add(card);
        }
      } else {
        mCompressedWishlist.add(new CompressedWishlistInfo(card));
      }

      /* load the price */
      loadPrice(card.name, card.setCode, card.number);

      /* Sort the wishlist */
      sortWishlist();

      /* Save the wishlist */
      WishlistHelpers.WriteCompressedWishlist(getActivity(), mCompressedWishlist);

      /* Clean up for the next add */
      mNumberField.setText("1");
      mNameField.setText("");
      mFoilCheckBox.setChecked(false);

      /* Redraw the new wishlist with the new card */
      mWishlistAdapter.notifyDataSetChanged();

    } catch (FamiliarDbException e) {
      handleFamiliarDbException(false);
    } catch (NumberFormatException e) {
      /* eat it */
    }
    DatabaseManager.getInstance(getActivity(), false).closeDatabase(false);
  }