From 55c62f5574e65e43c7e5b6d651a62378cce8dbb9 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 28 Oct 2020 19:20:37 +0100 Subject: [PATCH] added a copy function --- .../currencies/view/main/MainActivity.kt | 40 ++++++++++++++++++- app/src/main/res/layout/main_display.xml | 4 ++ app/src/main/res/values/changelog.xml | 6 ++- app/src/main/res/values/strings.xml | 1 + doc/TODO.md | 5 +-- 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/de/salomax/currencies/view/main/MainActivity.kt b/app/src/main/java/de/salomax/currencies/view/main/MainActivity.kt index 8553dea7..fb8e194f 100644 --- a/app/src/main/java/de/salomax/currencies/view/main/MainActivity.kt +++ b/app/src/main/java/de/salomax/currencies/view/main/MainActivity.kt @@ -1,5 +1,8 @@ package de.salomax.currencies.view.main +import android.content.ClipData +import android.content.ClipboardManager +import android.content.Context import android.content.Intent import android.os.Bundle import android.view.Menu @@ -8,12 +11,15 @@ import android.view.MenuItem import android.view.View import android.widget.* import androidx.appcompat.app.AppCompatActivity +import androidx.core.text.HtmlCompat import androidx.lifecycle.ViewModelProvider +import com.google.android.material.snackbar.Snackbar import de.salomax.currencies.R import de.salomax.currencies.view.preference.PreferenceActivity import de.salomax.currencies.viewmodel.main.CurrentInputViewModel import de.salomax.currencies.viewmodel.main.ExchangeRatesViewModel + class MainActivity : AppCompatActivity() { private lateinit var ratesModel: ExchangeRatesViewModel @@ -80,8 +86,21 @@ class MainActivity : AppCompatActivity() { true } + // long click on input "from" + findViewById(R.id.clickFrom).setOnLongClickListener { + val copyText = "${it.findViewById(R.id.currencyFrom).text} ${it.findViewById(R.id.textFrom).text}" + copyToClipboard(copyText) + true + } + // long click on input "to" + findViewById(R.id.clickTo).setOnLongClickListener { + val copyText = "${it.findViewById(R.id.currencyTo).text} ${it.findViewById(R.id.textTo).text}" + copyToClipboard(copyText) + true + } + // spinners: listen for changes - spinnerFrom.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{ + spinnerFrom.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onNothingSelected(parent: AdapterView<*>?) {} override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { inputModel.setCurrencyFrom( @@ -89,7 +108,7 @@ class MainActivity : AppCompatActivity() { ) } } - spinnerTo.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{ + spinnerTo.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onNothingSelected(parent: AdapterView<*>?) {} override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { inputModel.setCurrencyTo( @@ -99,6 +118,23 @@ class MainActivity : AppCompatActivity() { } } + private fun copyToClipboard(copyText: String) { + // copy + val clipboard: ClipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + clipboard.setPrimaryClip(ClipData.newPlainText(null, copyText)) + // notify + Snackbar.make( + tvCalculations, + HtmlCompat.fromHtml( + getString(R.string.copied_to_clipboard, copyText), + HtmlCompat.FROM_HTML_MODE_LEGACY + ), + Snackbar.LENGTH_SHORT + ) + .setBackgroundTint(getColor(R.color.colorAccent)) + .show() + } + private fun observe() { //exchange rates changed ratesModel.getExchangeRate().observe(this, { diff --git a/app/src/main/res/layout/main_display.xml b/app/src/main/res/layout/main_display.xml index 14200631..98dab04a 100644 --- a/app/src/main/res/layout/main_display.xml +++ b/app/src/main/res/layout/main_display.xml @@ -75,9 +75,11 @@ app:layout_constraintTop_toTopOf="parent"> Changelog - 0.1.0 + 0.2.0 + \n + \n• Added a copy function: Long click on the value you want to copy + \n + \n0.1.0 \n \n• More fail-proof updating of the currency rates \n• Added a disclaimer diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 85d5edad..3c17d7e4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -5,6 +5,7 @@ toggle currencies Exchange rate age: %s + Copied <b>%s</b> to clipboard Settings @string/menu_settings diff --git a/doc/TODO.md b/doc/TODO.md index 535aad22..6805c952 100644 --- a/doc/TODO.md +++ b/doc/TODO.md @@ -1,7 +1,4 @@ # TODO -* [add] copy function: -Allows for copying the current values. Probably best solution: Longclick on a value copies this specific value. So basically a "hidden" feature. Make a Toast/Snackbar notify the user about what just happened. - -* [add] "fee"-calculator: +* [add] "fee"-calculator: Some credit card companies charge a percentual fee when paying in a foreign currency. As this app is inteded to be used on vacation, it could be quite useful to inclue an option to add this fee to the calculation.