/** * Adds the given amount of items to the storage, and if notify is true * posts the change in the balance to the event bus. * * @param item the required virtual item * @param amount the amount of items to add * @param notify if true posts balance change event * @return new balance */ public int add(VirtualItem item, int amount, bool notify) { SoomlaUtils.LogDebug(mTag, "adding " + amount + " " + item.getName()); String itemId = item.getItemId(); int balance = getBalance(item); if (balance < 0) { /* in case the user "adds" a negative value */ balance = 0; amount = 0; } String balanceStr = (balance + amount).ToString(); String key = keyBalance(itemId); KeyValueStorage.SetValue(key, balanceStr); if (notify) { postBalanceChangeEvent(item, balance+amount, amount); } return balance + amount; }
/** * Sets the balance of the given virtual item to be the given balance, and if notify is true * posts the change in the balance to the event bus. * * @param item the required virtual item * @param balance the new balance to be set * @param notify if notify is true post balance change event * @return the balance of the required virtual item */ public int setBalance(VirtualItem item, int balance, bool notify) { SoomlaUtils.LogDebug(mTag, "setting balance " + balance + " to " + item.getName() + "."); int oldBalance = getBalance(item); if (oldBalance == balance) { return balance; } String itemId = item.getItemId(); String balanceStr = balance.ToString(); String key = keyBalance(itemId); KeyValueStorage.SetValue(key, balanceStr); if (notify) { postBalanceChangeEvent(item, balance, 0); } return balance; }
/** * Removes the given amount from the given virtual item's balance, and if notify is true * posts the change in the balance to the event bus. * * @param item is the virtual item to remove the given amount from * @param amount is the amount to remove * @param notify if notify is true post balance change event * @return new balance */ public int remove(VirtualItem item, int amount, bool notify){ SoomlaUtils.LogDebug(mTag, "Removing " + amount + " " + item.getName() + "."); String itemId = item.getItemId(); int balance = getBalance(item) - amount; if (balance < 0) { balance = 0; amount = 0; } String balanceStr = balance.ToString(); String key = keyBalance(itemId); KeyValueStorage.SetValue(key, balanceStr); if (notify) { postBalanceChangeEvent(item, balance, -1*amount); } return balance; }