-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
493 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
app/src/androidTest/java/fr/corenting/convertisseureurofranc/ApplicationTest.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
app/src/main/java/fr/corenting/convertisseureurofranc/ButtonsStuff.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package fr.corenting.convertisseureurofranc; | ||
|
||
import android.app.Activity; | ||
import android.view.KeyEvent; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.Spinner; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
public class ButtonsStuff { | ||
|
||
static void ButtonsInit(Activity activity) { | ||
//Convert when the button is clicked | ||
final Button convertButton = (Button) activity.findViewById(R.id.convertButton); | ||
convertButton.setImeActionLabel(activity.getString(R.string.convertButton), KeyEvent.KEYCODE_ENTER); | ||
final EditText resultEditText = (EditText) activity.findViewById(R.id.resultEditText); | ||
resultEditText.setKeyListener(null); //Make the EditText widget read only | ||
} | ||
|
||
public static void amountEditTextOnKeyInit(final Activity activity) { | ||
final EditText amountEditText = (EditText) activity.findViewById(R.id.amountEditText); | ||
amountEditText.setOnKeyListener(new View.OnKeyListener() { | ||
|
||
@Override | ||
public boolean onKey(View v, int keyCode, KeyEvent event) { | ||
if (event.getAction() == KeyEvent.ACTION_DOWN | ||
&& event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { | ||
activity.findViewById(R.id.convertButton).performClick(); | ||
return false; | ||
} | ||
|
||
return false; | ||
} | ||
}); | ||
} | ||
|
||
public static void convertButtonOnClick(final Activity activity, final ConvertCalc convertCalc) { | ||
final Spinner originSpinner = (Spinner) activity.findViewById(R.id.yearOfOriginSpinner); | ||
final Spinner resultSpinner = (Spinner) activity.findViewById(R.id.yearOfResultSpinner); | ||
final TextView amountEditText = (TextView) activity.findViewById(R.id.amountEditText); | ||
final TextView resultEditText = (TextView) activity.findViewById(R.id.resultEditText); | ||
Button convertButton = (Button) activity.findViewById(R.id.convertButton); | ||
|
||
convertButton.setOnClickListener(new View.OnClickListener() { | ||
public void onClick(View v) { | ||
try { | ||
Utils.hideSoftKeyboard(v); | ||
int yearOfOrigin = Integer.parseInt(originSpinner.getSelectedItem().toString()); | ||
int yearOfResult = Integer.parseInt(resultSpinner.getSelectedItem().toString()); | ||
float amount = Float.parseFloat(amountEditText.getText().toString()); | ||
resultEditText.setText(String.valueOf(convertCalc.convertFunction(yearOfOrigin, yearOfResult, amount))); | ||
} catch (Exception e) { | ||
Toast errorToast = Toast.makeText(activity.getApplicationContext(), activity.getString(R.string.errorToast), Toast.LENGTH_SHORT); | ||
errorToast.show(); | ||
} | ||
} | ||
}); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/fr/corenting/convertisseureurofranc/ConvertCalc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package fr.corenting.convertisseureurofranc; | ||
|
||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
|
||
public class ConvertCalc { | ||
|
||
HashMap<Integer, Float> values = new LinkedHashMap<Integer, Float>(115); | ||
|
||
ConvertCalc(Context context) { | ||
//Load the values from the csv file | ||
InputStream inputStream = context.getResources().openRawResource(R.raw.converter_values); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); | ||
String line; | ||
try { | ||
while ((line = reader.readLine()) != null) { | ||
String[] separatedLine = line.split(";"); | ||
values.put(Integer.parseInt(separatedLine[0]), Float.parseFloat(separatedLine[1])); | ||
} | ||
reader.close(); | ||
} catch (IOException e) { | ||
Log.d("ConvertCalc", e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
public float convertFunction(int yearOfOrigin, int yearOfResult, float amount) { | ||
float amountInEuroToday = amount * values.get(yearOfOrigin); | ||
return amountInEuroToday / values.get(yearOfResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/java/fr/corenting/convertisseureurofranc/SpinnersStuff.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package fr.corenting.convertisseureurofranc; | ||
|
||
import android.app.Activity; | ||
import android.view.View; | ||
import android.widget.AdapterView; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.Spinner; | ||
import android.widget.TextView; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class SpinnersStuff { | ||
|
||
static void Init(Activity activity) { | ||
Spinner originSpinner = (Spinner) activity.findViewById(R.id.yearOfOriginSpinner); | ||
Spinner resultSpinner = (Spinner) activity.findViewById(R.id.yearOfResultSpinner); | ||
TextView currencyOriginTextView = (TextView) activity.findViewById(R.id.currencyOriginTextView); | ||
TextView currencyResultTextView = (TextView) activity.findViewById(R.id.currencyResultTextView); | ||
|
||
//Initialize the spinners | ||
fillSpinnerWithYears(activity, originSpinner, 2013); | ||
fillSpinnerWithYears(activity, resultSpinner, 2013); | ||
|
||
//Add an onItemSelected listener to change the currency text according to the year | ||
setListeners(originSpinner, currencyOriginTextView); | ||
setListeners(resultSpinner, currencyResultTextView); | ||
} | ||
|
||
static void setListeners(Spinner spinner, final TextView textView) { | ||
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | ||
@Override | ||
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { | ||
int year = Integer.parseInt(parent.getItemAtPosition(pos).toString()); | ||
textView.setText(Utils.getCurrencyFromYear(year)); | ||
} | ||
|
||
@Override | ||
public void onNothingSelected(AdapterView<?> adapterView) { | ||
} | ||
}); | ||
} | ||
|
||
static void fillSpinnerWithYears(Activity activity, Spinner spinner, int maxYear) { | ||
//Populate the spinners with a list of years | ||
List<Integer> yearsList = new LinkedList<Integer>(); | ||
for (int i = maxYear; i >= 1901; i--) { | ||
yearsList.add(i); | ||
} | ||
ArrayAdapter<Integer> spinnerArray = new ArrayAdapter<Integer>(activity, android.R.layout.simple_spinner_item, yearsList); | ||
spinnerArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | ||
spinner.setAdapter(spinnerArray); | ||
} | ||
} |
Oops, something went wrong.