/** Update currency on activity */ public void updateCurrency() { DecimalFormat decimalFormat = new DecimalFormat("#.00"); // update currency items. double d; if (editTexts.get(0).getText().toString().length() > 0) { d = Double.parseDouble(0 + editTexts.get(0).getText().toString()); } else { d = 0.0; } int pos1 = (int) spinners.get(0).getSelectedItemId(); // get the position of the first spinner int pos2 = (int) spinners.get(1).getSelectedItemId(); // get the position of the first spinner // update the converted value // pos1 will be the number in the currencyItems list id. Double convertedValue = currencyModel.updateCurrency(d, pos1, pos2); String outputString = decimalFormat.format(convertedValue); if (convertedValue < 1) { outputString = "0" + outputString; } editTexts.get(1).setText(outputString); // update the rate for pos1 textViews.get(0).setText(currencyModel.getRate(pos1).toString()); // update the rate for pos2 textViews.get(1).setText(currencyModel.getRate(pos2).toString()); }
@Override protected void onPause() { super.onPause(); if (!currencyModel.downloadFinished()) { // if pause occured and the download is not finished, cancel it. currencyModel.cancelDownload(); } // write to file, the program might be killed currencyModel.fileWrite(); }
@Override protected void onStart() { super.onStart(); // update the textview with update info!! // inform user that the File is up to date or not. dateValid(); // UpdateEnabled must be true first // if file does not exist or the file is old - download a newer version if ((!currencyModel.fileExists() || !currencyModel.dateValidFile()) && currencyModel.isUpdateEnabled()) { currencyModel.download(); } else { // if the file is valid, update the spinners with those items. currencyModel.updateSpinners(); } }
/** dateValid - prints out if the file is valid in a textview */ public void dateValid() { // update the textfield if the File is valid or not. if (currencyModel.dateValidFile()) { textViews.get(2).setText("File is up to date."); } else { textViews.get(2).setText("File is not updated."); } }
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { // if the settings menuitem was chosen // create a settings intent and pass some values to it. Intent intent = new Intent(MainActivity.this, SettingsActivity.class); intent.putExtra("INTERVALHOUR", currencyModel.lastUpdateToHour()); intent.putExtra("INTERVALMINUTE", currencyModel.lastUpdateToMinute()); intent.putExtra("ENABLED", currencyModel.isUpdateEnabled()); startActivityForResult(intent, SETTINGS_RETURN_CODE); return true; } return super.onOptionsItemSelected(item); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // when the settings intent has ended we need to pick up some information regarding settings. if (requestCode == SETTINGS_RETURN_CODE) { if (resultCode == RESULT_OK) { Bundle extras = data.getExtras(); if (extras != null) { // update interval currencyModel.updateNextUpdate( extras.getInt("INTERVALHOUR"), extras.getInt("INTERVALMINUTE")); // update if update is going to be enabled currencyModel.setUpdateEnabled(extras.getBoolean("ENABLED")); System.out.println(extras.getBoolean("UPDATE")); if (extras.getBoolean("UPDATE")) { // if forced update was pressed, redownload. currencyModel.download(); } } } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // init variables setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); spinners = new ArrayList<>(); textViews = new ArrayList<>(); editTexts = new ArrayList<>(); createBindings(); currencyModel = new CurrencyModel(this); // read from file currencyModel.fileRead(); }