Skip to content

Commit

Permalink
Version 2.0 : new look, new icon, converted code to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
corenting committed Feb 12, 2019
1 parent a2beb2d commit 2b98a3f
Show file tree
Hide file tree
Showing 30 changed files with 222 additions and 228 deletions.
14 changes: 11 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
Expand All @@ -8,8 +10,8 @@ android {
applicationId "fr.corenting.convertisseureurofranc"
minSdkVersion 14
targetSdkVersion 28
versionCode 8
versionName "1.7"
versionCode 9
versionName "2.0"
}

signingConfigs {
Expand Down Expand Up @@ -38,5 +40,11 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.1.0-alpha03'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

repositories {
mavenCentral()
}
Binary file modified app/src/main/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
package fr.corenting.convertisseureurofranc

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.preference.PreferenceManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import android.view.KeyEvent
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.Spinner
import android.widget.TextView
import android.widget.Toast

import java.util.Arrays
import java.util.LinkedList

import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import fr.corenting.convertisseureurofranc.convert.ConvertAbstract
import fr.corenting.convertisseureurofranc.convert.France
import fr.corenting.convertisseureurofranc.convert.USA
import fr.corenting.convertisseureurofranc.utils.Utils
import kotlinx.android.synthetic.main.activity_converter.*
import java.util.*

class ConverterActivity : AppCompatActivity() {

var converter: ConvertAbstract
internal var prefs: SharedPreferences

private var originSpinner: Spinner? = null
private var resultSpinner: Spinner? = null
private var currencyOriginTextView: TextView? = null
private var currencyResultTextView: TextView? = null
private var convertButton: Button? = null
private var resultEditText: EditText? = null
private var amountEditText: EditText? = null

lateinit var converter: ConvertAbstract
private lateinit var prefs: SharedPreferences

override fun onCreate(savedInstanceState: Bundle?) {
// Dark theme
prefs = PreferenceManager.getDefaultSharedPreferences(this)
if (prefs.getBoolean(getString(R.string.preferenceDarkThemeKey), false)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
Expand All @@ -50,63 +33,72 @@ class ConverterActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_converter)

//Initialize the converter
converter = France(this)

// Binding
originSpinner = findViewById(R.id.yearOfOriginSpinner)
resultSpinner = findViewById(R.id.yearOfResultSpinner)
val currencySpinner = findViewById<Spinner>(R.id.currencySpinner)
currencyOriginTextView = findViewById(R.id.currencyOriginTextView)
currencyResultTextView = findViewById(R.id.currencyResultTextView)
// Default converter
converter = France(applicationContext)

//Initialize the years spinners and the buttons
initSpinners()
initButtons()

//Set currency spinner content
val currenciesList = Arrays.asList("France (euros, francs, anciens francs)", "USA (dollars)")
val currenciesList = Arrays
.asList(getString(R.string.france_currencies), getString(R.string.usa_currencies))
setSpinnerAdapter(currencySpinner, currenciesList)
currencySpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
if (pos == 0) {
converter = France(parent.context)
initSpinners()
} else {
converter = USA(parent.context)
initSpinners()
override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) {
when (pos) {
0 -> converter = France(applicationContext)
else -> converter = USA(applicationContext)
}
initSpinners()
}

override fun onNothingSelected(adapterView: AdapterView<*>) {}
}
}

private fun initSpinners() {
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.converter, menu)
if (prefs.getBoolean(getString(R.string.preferenceDarkThemeKey), false)) {
menu.getItem(0).isChecked = true
}
return true
}

//Populate the spinners with a list of years
val yearsList = LinkedList<String>()
for (i in converter.latestYear downTo converter.firstYear) {
yearsList.add(i.toString())
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_about -> Utils.showCredits(this)
R.id.action_dark_theme -> {
val editor = prefs.edit()
editor.putBoolean(getString(R.string.preferenceDarkThemeKey), !item.isChecked)
item.isChecked = !item.isChecked
editor.apply()
finish()
startActivity(intent)
}
}
setSpinnerAdapter(originSpinner!!, yearsList)
setSpinnerAdapter(resultSpinner!!, yearsList)
return super.onOptionsItemSelected(item)
}

private fun initSpinners() {
//Populate the spinners with a list of years
val yearsList = (converter.latestYear downTo converter.firstYear)
.mapTo(LinkedList()) { it.toString() }
setSpinnerAdapter(yearOfOriginSpinner!!, yearsList)
setSpinnerAdapter(yearOfResultSpinner!!, yearsList)

//Add an onItemSelected listener to change the currency text according to the year
setSpinnerListener(originSpinner!!, currencyOriginTextView)
setSpinnerListener(resultSpinner!!, currencyResultTextView)
setSpinnerListener(yearOfOriginSpinner!!, currencyOriginTextView)
setSpinnerListener(yearOfResultSpinner!!, currencyResultTextView)
}

private fun initButtons() {
//Convert when the button is clicked
convertButton = findViewById(R.id.convertButton)
convertButton!!.setImeActionLabel(getString(R.string.convertButton), KeyEvent.KEYCODE_ENTER)
resultEditText = findViewById(R.id.resultEditText)
resultEditText!!.keyListener = null //Make the EditText widget read only
convertButton.setImeActionLabel(getString(R.string.convertButton), KeyEvent.KEYCODE_ENTER)
resultEditText.keyListener = null //Make the EditText widget read only

//Click button when using enter on the keyboard
amountEditText = findViewById(R.id.amountEditText)
amountEditText!!.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
amountEditText.setOnKeyListener(View.OnKeyListener { _, _, event ->
if (event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) {
convertButton!!.performClick()
return@OnKeyListener false
Expand All @@ -115,61 +107,40 @@ class ConverterActivity : AppCompatActivity() {
})

//Setup listeners
val c = this
convertButton!!.setOnClickListener { v ->
convertButton.setOnClickListener { v ->
try {
Utils.hideSoftKeyboard(v)
val yearOfOrigin = Integer.parseInt(originSpinner!!.selectedItem.toString())
val yearOfResult = Integer.parseInt(resultSpinner!!.selectedItem.toString())
val amount = java.lang.Float.parseFloat(amountEditText!!.text.toString())
resultEditText!!.setText(Utils.formatNumber(c, converter.convertFunction(yearOfOrigin, yearOfResult, amount)))
val yearOfOrigin = Integer.parseInt(yearOfOriginSpinner.selectedItem.toString())
val yearOfResult = Integer.parseInt(yearOfResultSpinner.selectedItem.toString())
val amount = java.lang.Float.parseFloat(amountEditText.text.toString())
resultEditText.setText(Utils.formatNumber(this,
converter.convertFunction(yearOfOrigin, yearOfResult, amount)))
} catch (e: Exception) {
val errorToast = Toast.makeText(c, getString(R.string.errorToast), Toast.LENGTH_SHORT)
val errorToast = Toast.makeText(this, getString(R.string.errorToast),
Toast.LENGTH_SHORT)
errorToast.show()
}
}
}

private fun setSpinnerAdapter(s: Spinner, items: List<String>) {
s.adapter = null
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, items)
val adapter = ArrayAdapter(this,
android.R.layout.simple_spinner_item, items)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
s.adapter = adapter
}

private fun setSpinnerListener(spinner: Spinner, textView: TextView?) {
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, pos: Int, id: Long) {
val year = Integer.parseInt(parent.getItemAtPosition(pos).toString())
textView!!.text = converter.getCurrencyFromYear(year)
override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) {
if (parent != null) {
val year = Integer.parseInt(parent.getItemAtPosition(pos).toString())
textView!!.text = converter.getCurrencyFromYear(year)
}
}

override fun onNothingSelected(adapterView: AdapterView<*>) {}
}
}


override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.converter, menu)
if (prefs.getBoolean(getString(R.string.preferenceDarkThemeKey), false)) {
menu.getItem(0).isChecked = true
}
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
when (id) {
R.id.action_about -> Utils.showCredits(this)
R.id.action_dark_theme -> {
val editor = prefs.edit()
editor.putBoolean(getString(R.string.preferenceDarkThemeKey), !item.isChecked)
item.isChecked = !item.isChecked
editor.apply()
finish()
startActivity(intent)
}
}
return super.onOptionsItemSelected(item)
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
package fr.corenting.convertisseureurofranc.convert

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
import java.util.*

abstract class ConvertAbstract {
abstract class ConvertAbstract(protected val context: Context, private val fileId: Int) {

var latestYear: Int = 0
var firstYear: Int = 0
internal var context: Context? = null
internal var values: HashMap<Int, Float> = LinkedHashMap()
protected var values: HashMap<Int, Float> = LinkedHashMap()

init {
loadValuesFromCSV()
}

abstract fun convertFunction(yearOfOrigin: Int, yearOfResult: Int, amount: Float): Double
abstract fun convertFunction(yearOfOrigin: Int, yearOfResult: Int, amount: Float): Float

abstract fun getCurrencyFromYear(year: Int): String

internal fun loadValuesFromCSV(fileID: Int) {
private fun loadValuesFromCSV() {
//Load the values from the csv file
val inputStream = context!!.resources.openRawResource(fileID)
val reader = BufferedReader(InputStreamReader(inputStream))
var line: String
val inputStream = context.resources.openRawResource(fileId)
val iterator = BufferedReader(InputStreamReader(inputStream)).lineSequence().iterator()

latestYear = Integer.MIN_VALUE
firstYear = Integer.MAX_VALUE
try {
while ((line = reader.readLine()) != null) {
val separatedLine = line.split(";".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
val year = Integer.parseInt(separatedLine[0])
if (year > latestYear)
latestYear = year
if (year < firstYear)
firstYear = year
val value = java.lang.Float.parseFloat(separatedLine[1])
values[year] = value
}
reader.close()
} catch (e: IOException) {
Log.d("CSV parsing error", e.message)
e.printStackTrace()
}

while (iterator.hasNext()) {
val line = iterator.next()

val separatedLine = line
.split(";".toRegex())
.dropLastWhile { it.isEmpty() }
.toTypedArray()

val year = Integer.parseInt(separatedLine[0])
if (year > latestYear)
latestYear = year
if (year < firstYear)
firstYear = year
val value = java.lang.Float.parseFloat(separatedLine[1])
values[year] = value
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,35 @@ import android.content.Context

import fr.corenting.convertisseureurofranc.R

class France(context: Context) : ConvertAbstract() {
class France(context: Context) : ConvertAbstract(context, R.raw.fr_values) {

init {
this.setContext(context)
loadValuesFromCSV(R.raw.fr_values)
}
override fun convertFunction(yearOfOrigin: Int, yearOfResult: Int, amount: Float): Float {
var newAmount = amount
if (yearOfOrigin == yearOfResult) return amount

override fun convertFunction(yearOfOrigin: Int, yearOfResult: Int, amount: Float): Double {
var amount = amount
if (yearOfOrigin == yearOfResult) return amount.toDouble()
val multiplier = getValues().get(yearOfResult) / getValues().get(yearOfOrigin)
val multiplier = values[yearOfResult]!! / values[yearOfOrigin]!!
//Convert values if currency is different
if (yearOfResult < 1960) {
amount *= 100f
newAmount *= 100f
}
if (yearOfOrigin < 1960) {
amount /= 100f
newAmount /= 100f
}
if (yearOfResult < 2002) {
amount *= 6.55957f
newAmount *= 6.55957f
}
if (yearOfOrigin < 2002) {
amount *= 0.15244f
newAmount *= 0.15244f
}

return amount * multiplier
return newAmount * multiplier
}

override fun getCurrencyFromYear(year: Int): String {
if (year >= 2002) return getContext().getString(R.string.euros)
return if (year >= 1960) getContext().getString(R.string.francs) else getContext().getString(R.string.oldFrancs)
return when {
year >= 2002 -> context.getString(R.string.euros)
year >= 1960 -> context.getString(R.string.francs)
else -> context.getString(R.string.oldFrancs)
}
}
}
Loading

0 comments on commit 2b98a3f

Please sign in to comment.