Android Open Source - makler Wallet Item Calculator






From Project

Back to project page makler.

License

The source code is released under:

GNU General Public License

If you think the Android project makler listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.net.newton.Makler.ui;
//w  ww  .j  a  v a2 s .  com
import java.math.BigDecimal;

import pl.net.newton.Makler.db.quote.Quote;
import pl.net.newton.Makler.db.quote.QuoteField;
import pl.net.newton.Makler.db.quote.QuotesDao;
import pl.net.newton.Makler.db.symbol.SymbolsDb;
import pl.net.newton.Makler.db.wallet.WalletDb;
import pl.net.newton.Makler.db.wallet.WalletItem;
import pl.net.newton.Makler.history.service.HistoryService;
import pl.net.newton.Makler.ui.adapter.QuotesAdapter;
import pl.net.newton.Makler.R;
import pl.net.newton.Makler.common.Configuration;
import pl.net.newton.Makler.common.NumberFormatUtils;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * @author Igor Andruszkiewicz
 * 
 */
public class WalletItemCalculator extends AbstractActivity implements TextWatcher, OnTouchListener {
  private static final int COUNTER_TIME_INTERVAL = 150; // ms

  private Quote quote;

  private WalletItem walletItem;

  private TextView walletItemZmiana;

  private TextView walletItemKurs;

  private TextView walletItemZysk;

  private TextView textViewZysk;

  BigDecimal commision = null;

  BigDecimal minCommision = null;

  private EditText editTextIlosc;

  private int quantity;

  private double kupno;

  private double sprzedaz;

  private TextView walletItemAvg;

  private TextView walletItemQuantity;

  private EditText editTextKupno;

  private EditText editTextSprzedaz;

  private Handler handler = new Handler();

  private String quoteSymbol;

  private WalletDb walletDb;

  private SymbolsDb symbolsDb;

  /**
   * speed counter used to increase or decrease value step
   */
  private int speedCounter = 0;

  private double mValueStep = 0.01;

  Runnable buttonPressedTask = new Runnable() {

    public void run() {
      buttonPressed();
      handler.postDelayed(this, COUNTER_TIME_INTERVAL);
      speedCounter += 1;

      if (speedCounter == 10) {
        mValueStep = 0.2;
      } else if (speedCounter == 10) {
        mValueStep = 0.5;
      } else if (speedCounter == 10) {
        mValueStep = 1.0;
      }
    }
  };

  private Button currentButtonPressed;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.wallet_item_calculator);

    quoteSymbol = getIntent().getStringExtra("symbol");

    Button buttonReset = (Button) findViewById(R.id.buttonReset);
    buttonReset.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        reset();
      }
    });

    Configuration config = new Configuration(this);
    commision = config.getCommision();
    minCommision = config.getMinCommision();

  }

  private void reset() {
    walletItemQuantity.setText(NumberFormatUtils.formatNumber(walletItem.getQuantity()));
    walletItemKurs.setText(NumberFormatUtils.formatNumber(quote.chooseKurs()));
    walletItemZmiana.setText(NumberFormatUtils.formatNumber(quote.chooseZmiana()));
    walletItemAvg.setText(NumberFormatUtils.formatNumber(walletItem.getAvgBuy()));
    walletItemZysk.setText(NumberFormatUtils.formatNumber(walletItem.gain()));

    quantity = walletItem.getQuantity();
    kupno = walletItem.getAvgBuy().doubleValue();

    if (quote.get(QuoteField.QUOTE) == null) {
      sprzedaz = kupno;
    } else {
      sprzedaz = quote.getAsDecimal(QuoteField.QUOTE).doubleValue();
    }
    updateFields();
  }

  private void setView() {
    String symbolName = getIntent().getStringExtra("symbol");
    walletItem = walletDb.getWalletItem(symbolsDb.getSymbolBySymbol(symbolName));

    if (quote.get(QuoteField.SYMBOL).length() > 5) {
      TextView symbolField = (TextView) findViewById(R.id.quoteDetailSymbol);
      symbolField.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
      symbolField.setPadding(0, QuotesAdapter.dpToPx(this, 14), 0, 0);
    }

    TextView walletItemSymbol = (TextView) findViewById(R.id.walletItemSymbol);
    TextView walletItemName = (TextView) findViewById(R.id.walletItemName);

    walletItemKurs = (TextView) findViewById(R.id.walletItemKurs); // aktualny
                                    // kurs
                                    // (ewentualnej
                                    // sprzeda?y)

    walletItemZmiana = (TextView) findViewById(R.id.walletItemZmiana);
    walletItemZysk = (TextView) findViewById(R.id.walletItemGain);
    walletItemAvg = (TextView) findViewById(R.id.walletItemAvg);
    walletItemQuantity = (TextView) findViewById(R.id.walletItemQuantity);

    textViewZysk = (TextView) findViewById(R.id.textViewZysk);

    walletItemSymbol.setText(quote.get(QuoteField.SYMBOL));
    walletItemName.setText(quote.get(QuoteField.NAME));
    if (quote.get(QuoteField.QUOTE) == null) {
      walletItemKurs.setText(R.string.zero);
    } else {
      walletItemKurs.setText(quote.chooseKurs().toString());
    }
    walletItemZmiana.setText(String.valueOf(0));
    walletItemAvg.setText(NumberFormatUtils.formatNumber(walletItem.getAvgBuy()));
    walletItemZysk.setText(NumberFormatUtils.formatNumber(walletItem.gain()));
    walletItemQuantity.setText(String.valueOf(walletItem.getQuantity()));
    updateZmianaBackground(quote.chooseZmiana());

    editTextIlosc = (EditText) findViewById(R.id.editTextIlosc);
    editTextKupno = (EditText) findViewById(R.id.editTextKupno);
    editTextSprzedaz = (EditText) findViewById(R.id.editTextSprzedaz);

    Button button = (Button) findViewById(R.id.iloscPlus);
    button.setOnTouchListener(this);
    button = (Button) findViewById(R.id.iloscMinus);
    button.setOnTouchListener(this);

    button = (Button) findViewById(R.id.kupnoMinus);
    button.setOnTouchListener(this);
    button = (Button) findViewById(R.id.kupnoPlus);
    button.setOnTouchListener(this);

    button = (Button) findViewById(R.id.sprzedazMinus);
    button.setOnTouchListener(this);
    button = (Button) findViewById(R.id.sprzedazPlus);
    button.setOnTouchListener(this);

  }

  /**
   * @param quote
   */
  @SuppressWarnings("deprecation")
  public void updateZmianaBackground(BigDecimal value) {
    Resources res = getResources();
    View zmiana = findViewById(R.id.quoteDetailZmiana);

    if (value != null) {
      switch (quote.chooseZmiana().compareTo(BigDecimal.ZERO)) {
        case 0:
          zmiana.setBackgroundDrawable(res.getDrawable(R.drawable.bluebox));
          break;
        case -1:
          zmiana.setBackgroundDrawable(res.getDrawable(R.drawable.redbox));
          break;
        case 1:
          zmiana.setBackgroundDrawable(res.getDrawable(R.drawable.greenbox));
          break;
      }
    }
  }

  private void updateZysk() {

    // Koszt KUPNA
    BigDecimal costKupno = new BigDecimal(kupno).multiply(new BigDecimal(quantity));
    BigDecimal commisionKupno = costKupno.multiply(commision).divide(BigDecimal.valueOf(100));
    if (commisionKupno.compareTo(minCommision) == -1) {
      commisionKupno = minCommision;
    }

    // Koszt SPRZEDAZY
    BigDecimal costSprzedaz = new BigDecimal(sprzedaz).multiply(new BigDecimal(quantity));
    BigDecimal commisionSprzedaz = costSprzedaz.multiply(commision).divide(BigDecimal.valueOf(100));
    if (commisionSprzedaz.compareTo(minCommision) == -1) {
      commisionSprzedaz = minCommision;
    }

    BigDecimal z = costSprzedaz.subtract(costKupno).subtract(commisionSprzedaz).subtract(commisionKupno);

    int compare = z.compareTo(BigDecimal.ZERO);
    if (compare > 0) {
      textViewZysk.setTextColor(Color.parseColor("#ff229c22"));
    } else if (compare < 0) {
      textViewZysk.setTextColor(Color.parseColor("#ffff0303"));
    } else {
      textViewZysk.setTextColor(Color.GRAY);
    }

    textViewZysk.setText(String.format("%.02f", z.doubleValue()));

  }

  private void updateFields() {
    editTextIlosc.removeTextChangedListener(this);
    editTextKupno.removeTextChangedListener(this);
    editTextSprzedaz.removeTextChangedListener(this);

    editTextIlosc.setText(NumberFormatUtils.formatNumber(quantity));
    editTextKupno.setText(NumberFormatUtils.formatNumber(kupno));
    editTextSprzedaz.setText(NumberFormatUtils.formatNumber(sprzedaz));

    editTextIlosc.addTextChangedListener(this);
    editTextKupno.addTextChangedListener(this);
    editTextSprzedaz.addTextChangedListener(this);

    updateZysk();
  }

  public void buttonPressed() {
    switch (currentButtonPressed.getId()) {
      case R.id.iloscPlus:
        quantity += 1;
        break;
      case R.id.iloscMinus:
        quantity -= 1;
        break;
      case R.id.kupnoPlus:
        kupno += mValueStep;
        break;
      case R.id.kupnoMinus:
        kupno -= mValueStep;
        break;
      case R.id.sprzedazPlus:
        sprzedaz += mValueStep;
        break;
      case R.id.sprzedazMinus:
        sprzedaz -= mValueStep;
        break;
    }
    updateFields();
  }

  public boolean onTouch(View view, MotionEvent event) {

    boolean v = false;
    if ((event.getActionMasked() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
      currentButtonPressed = (Button) view;
      currentButtonPressed.setPressed(true);
      handler.postDelayed(buttonPressedTask, 100);
      v = true;
    } else if ((event.getActionMasked() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
      handler.removeCallbacks(buttonPressedTask);
      if (currentButtonPressed != null) {
        currentButtonPressed.setPressed(false);
      }
      currentButtonPressed = null;
      speedCounter = 0;
      mValueStep = 0.01;
      v = true;
    }
    return v;
  }

  public void afterTextChanged(Editable s) {
    if (s == editTextIlosc.getEditableText()) {
      quantity = NumberFormatUtils.parseIntOrZero(editTextIlosc.getText().toString());
    }

    if (s == editTextKupno.getEditableText()) {
      kupno = NumberFormatUtils.parseDoubleOrZero(editTextKupno.getText().toString());
    }

    if (s == editTextSprzedaz.getEditableText()) {
      sprzedaz = NumberFormatUtils.parseDoubleOrZero(editTextSprzedaz.getText().toString());
    }

    updateZysk();
  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // do nothing
  }

  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count) {
    // do nothing
  }

  @Override
  protected void initUi(SQLiteDatabase sqlDb, HistoryService historyService) {
    this.walletDb = new WalletDb(sqlDb, this);
    this.symbolsDb = new SymbolsDb(sqlDb, this);
    QuotesDao quotesDb = new QuotesDao(sqlDb, this);
    quote = quotesDb.getQuoteBySymbol(quoteSymbol);
    setView();
    reset();
  }
}




Java Source Code List

pl.net.newton.Makler.common.Configuration.java
pl.net.newton.Makler.common.DateFormatUtils.java
pl.net.newton.Makler.common.GpwUtils.java
pl.net.newton.Makler.common.LocaleUtils.java
pl.net.newton.Makler.common.NumberFormatUtils.java
pl.net.newton.Makler.db.Constants.java
pl.net.newton.Makler.db.DbHelper.java
pl.net.newton.Makler.db.SqlConnection.java
pl.net.newton.Makler.db.alert.AlertBuilder.java
pl.net.newton.Makler.db.alert.AlertChecker.java
pl.net.newton.Makler.db.alert.AlertValue.java
pl.net.newton.Makler.db.alert.Alert.java
pl.net.newton.Makler.db.alert.AlertsDao.java
pl.net.newton.Makler.db.alert.Event.java
pl.net.newton.Makler.db.alert.Subject.java
pl.net.newton.Makler.db.quote.QuoteField.java
pl.net.newton.Makler.db.quote.Quote.java
pl.net.newton.Makler.db.quote.QuotesDao.java
pl.net.newton.Makler.db.service.SqlProvider.java
pl.net.newton.Makler.db.symbol.SymbolBuilder.java
pl.net.newton.Makler.db.symbol.Symbol.java
pl.net.newton.Makler.db.symbol.SymbolsDb.java
pl.net.newton.Makler.db.wallet.WalletDb.java
pl.net.newton.Makler.db.wallet.WalletItemBuilder.java
pl.net.newton.Makler.db.wallet.WalletItem.java
pl.net.newton.Makler.gpw.DefaultQuotesReceiver.java
pl.net.newton.Makler.gpw.QuotesReceiver.java
pl.net.newton.Makler.gpw.ex.GpwException.java
pl.net.newton.Makler.gpw.service.QuotesListener.java
pl.net.newton.Makler.gpw.service.QuotesService.java
pl.net.newton.Makler.gpw.service.UpdatingThread.java
pl.net.newton.Makler.history.BossaProvider.java
pl.net.newton.Makler.history.ByteArrayUtils.java
pl.net.newton.Makler.history.Cache.java
pl.net.newton.Makler.history.ChannelTools.java
pl.net.newton.Makler.history.EntryListWithIndexes.java
pl.net.newton.Makler.history.EntryList.java
pl.net.newton.Makler.history.HistoryFilter.java
pl.net.newton.Makler.history.HistoryProvider.java
pl.net.newton.Makler.history.service.HistoryListener.java
pl.net.newton.Makler.history.service.HistoryService.java
pl.net.newton.Makler.httpClient.Connector.java
pl.net.newton.Makler.receivers.QuotesAlarmReceiver.java
pl.net.newton.Makler.receivers.StartupReceiver.java
pl.net.newton.Makler.service.ServiceManager.java
pl.net.newton.Makler.ui.About.java
pl.net.newton.Makler.ui.AbstractActivity.java
pl.net.newton.Makler.ui.Alerts.java
pl.net.newton.Makler.ui.FullScreenGraph.java
pl.net.newton.Makler.ui.Preferences.java
pl.net.newton.Makler.ui.QuoteDetails.java
pl.net.newton.Makler.ui.Quotes.java
pl.net.newton.Makler.ui.Symbols.java
pl.net.newton.Makler.ui.WalletForm.java
pl.net.newton.Makler.ui.WalletItemCalculator.java
pl.net.newton.Makler.ui.Wallet.java
pl.net.newton.Makler.ui.adapter.AlertsAdapter.java
pl.net.newton.Makler.ui.adapter.QuotesAdapter.java
pl.net.newton.Makler.ui.adapter.SymbolsAdapter.java
pl.net.newton.Makler.ui.adapter.WalletAdapter.java
pl.net.newton.Makler.ui.graph.GraphView.java
pl.net.newton.Makler.ui.graph.MaklerGraphicalView.java