Skip to content

Commit

Permalink
added a copy function
Browse files Browse the repository at this point in the history
  • Loading branch information
sal0max committed Oct 28, 2020
1 parent acdc1a7 commit 55c62f5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
40 changes: 38 additions & 2 deletions app/src/main/java/de/salomax/currencies/view/main/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -80,16 +86,29 @@ class MainActivity : AppCompatActivity() {
true
}

// long click on input "from"
findViewById<LinearLayout>(R.id.clickFrom).setOnLongClickListener {
val copyText = "${it.findViewById<TextView>(R.id.currencyFrom).text} ${it.findViewById<TextView>(R.id.textFrom).text}"
copyToClipboard(copyText)
true
}
// long click on input "to"
findViewById<LinearLayout>(R.id.clickTo).setOnLongClickListener {
val copyText = "${it.findViewById<TextView>(R.id.currencyTo).text} ${it.findViewById<TextView>(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(
(parent?.adapter as SpinnerAdapter).getItem(position)
)
}
}
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(
Expand All @@ -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, {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/layout/main_display.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:id="@+id/clickFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:longClickable="true"
android:padding="@dimen/margin2x">

<TextView
Expand Down Expand Up @@ -121,9 +123,11 @@
app:layout_constraintTop_toTopOf="@+id/guideline">

<LinearLayout
android:id="@+id/clickTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:longClickable="true"
android:padding="@dimen/margin2x">

<TextView
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/values/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<string name="title_changelog">Changelog</string>

<string name="changelog">
<b><big>0.1.0</big></b>
<b><big>0.2.0</big></b>
\n
\n• Added a copy function: Long click on the value you want to copy
\n
\n<b><big>0.1.0</big></b>
\n
\n• More fail-proof updating of the currency rates
\n• Added a disclaimer
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

<string name="desc_toggle_currencies">toggle currencies</string>
<string name="last_updated">Exchange rate age: %s</string>
<string name="copied_to_clipboard">Copied &lt;b>%s&lt;/b> to clipboard</string>

<string name="menu_settings">Settings</string>
<string name="title_preferences">@string/menu_settings</string>
Expand Down
5 changes: 1 addition & 4 deletions doc/TODO.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 55c62f5

Please sign in to comment.