Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinaStepanova committed Apr 6, 2020
1 parent 67fe5a8 commit 1055c19
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/avs/rates/currency/BaseCurrency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.avs.rates.currency
import com.avs.rates.DEFAULT_RATE_VALUE
import com.avs.rates.R

/**
* Base class for every currency. Holds common data and methods.
*/
abstract class BaseCurrency {

var rate: Double = DEFAULT_RATE_VALUE
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/avs/rates/ui/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.avs.rates.di.ViewModelFactory

/**
* A class which serve as a parent to all activities classes in the app.
* Contains common data and methods
* Contains common data and methods.
*/
abstract class BaseActivity : AppCompatActivity() {

Expand Down
40 changes: 39 additions & 1 deletion app/src/main/java/com/avs/rates/ui/main/CurrenciesAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.avs.rates.R
import com.avs.rates.currency.BaseCurrency
import com.avs.rates.databinding.RateItemListBinding
import com.avs.rates.utils.CircleTransform
import com.avs.rates.utils.formatNumber
import com.avs.rates.utils.getSelectionIndex
import com.squareup.picasso.Picasso
import java.util.*

interface RatesListener {
fun onListItemClick(newBaseCurrency: BaseCurrency)
fun onEditTextChanged(text: String)
fun onEditTextChanged(stringValue: String)
}

class CurrenciesAdapter(
Expand All @@ -29,6 +31,10 @@ class CurrenciesAdapter(
private var textWatcher = getTextWatcher()
private var currencies: LinkedList<BaseCurrency>? = currencyList

/**
* Updates currencies list
* @param currencies - updated list of currencies
*/
fun setCurrencies(currencies: LinkedList<BaseCurrency>?) {
this.currencies = currencies
notifyItemRangeChanged(1, (this.currencies?.size?.minus(1) ?: 0))
Expand All @@ -50,6 +56,9 @@ class CurrenciesAdapter(
}
}

/**
* Update first two top items after base currency has changed
*/
fun updateTopItems() {
if (itemCount != 0) notifyItemChanged(0)
if (itemCount > 0) notifyItemChanged(1)
Expand All @@ -58,9 +67,12 @@ class CurrenciesAdapter(
inner class RatesViewHolder(private val binding: RateItemListBinding) :
RecyclerView.ViewHolder(binding.root), View.OnClickListener {

private val editTextMaxLength : Int

init {
binding.root.setOnClickListener(this)
binding.editText.setOnClickListener(this)
editTextMaxLength = context.resources.getInteger(R.integer.edit_text_max_length)
}

fun bind(item: BaseCurrency, context: Context) {
Expand All @@ -70,14 +82,27 @@ class CurrenciesAdapter(
bindFlagImage(item.getImagePath())
}

/**
* Sets short currency name to a corresponding text view
* @param name - short name of currency
*/
private fun bindCurrencyShortName(name: String?) {
if (name != null) binding.tvCurrencyShortName.text = name
}

/**
* Sets full currency name to a corresponding text view
* @param name - full name of currency
*/
private fun bindCurrencyFullName(name: String?) {
if (name != null) binding.tvCurrencyFullName.text = name
}

/**
* Disables all the edit texts in the recycler view except for the top one.
* Sets rate value.
* @param value - a rate value
*/
private fun bindRate(value: String) {
binding.editText.removeTextChangedListener(textWatcher)
binding.editText.isEnabled = adapterPosition == 0
Expand All @@ -86,9 +111,14 @@ class CurrenciesAdapter(
binding.editText.addTextChangedListener(textWatcher)
itemClickListener.onEditTextChanged(binding.editText.text.toString())
binding.editText.requestFocus()
binding.editText.setSelection(getSelectionIndex(value.length, editTextMaxLength))
}
}

/**
* Loads image url into a image view using custom circle transformation
* @param imageUrl - country flag url
*/
private fun bindFlagImage(imageUrl: String?) {
Picasso.get()
.load(imageUrl)
Expand All @@ -97,6 +127,10 @@ class CurrenciesAdapter(
.into(binding.ivFlagImage)
}

/**
* Changes base currency to appear on the top, if not a first item was clicked
* @param view - recycler view item
*/
override fun onClick(view: View?) {
val clickedPosition = adapterPosition
if (clickedPosition != 0 && !currencies.isNullOrEmpty()) {
Expand All @@ -112,6 +146,10 @@ class CurrenciesAdapter(
}
}

/**
* Listens to a rate changing of a base currency
* @return
*/
private fun getTextWatcher(): TextWatcher {
return object : TextWatcher {

Expand Down
21 changes: 16 additions & 5 deletions app/src/main/java/com/avs/rates/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import com.avs.rates.ui.BaseActivity
import java.util.*
import javax.inject.Inject


class MainActivity : BaseActivity(), RatesListener {

@Inject
Expand Down Expand Up @@ -55,18 +54,30 @@ class MainActivity : BaseActivity(), RatesListener {
})
}

/**
* Triggered when used has selected new base currency
* @param newBaseCurrency - new base currency
*/
override fun onListItemClick(newBaseCurrency: BaseCurrency) {
binding.recyclerView.itemAnimator = defaultItemAnimator
viewModel.handleUserInteraction(newBaseCurrency)
binding.recyclerView.layoutManager?.scrollToPosition(0)
}

override fun onEditTextChanged(text: String) {
viewModel.updateBaseCurrencyValue(text)
/**
* Triggered when used has changed a rate of a base currency
* @param stringValue - updated rate value of a base currency
*/
override fun onEditTextChanged(stringValue: String) {
viewModel.updateBaseCurrencyValue(stringValue)
}

private fun handleErrorItemsAppearance(it: ErrorType?) {
when (it) {
/**
* Manages UI items visibility in a case when network error has occurred before recycler view was shown
* @param errorType - network related type of error
*/
private fun handleErrorItemsAppearance(errorType: ErrorType?) {
when (errorType) {
null -> {
binding.ivCloud.visibility = View.INVISIBLE
binding.ivMessage.visibility = View.INVISIBLE
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/avs/rates/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class MainViewModel @Inject constructor(
*/
fun updateBaseCurrencyValue(text: String) {
baseCurrencyValue = text.toDoubleOrNull() ?: -1.0
currenciesList.first.rate = baseCurrencyValue
}

/**
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/avs/rates/utils/Format.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ fun round(value: Double, decimalPlace: Int): Double {
var number = BigDecimal(value.toString())
number = number.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP)
return number.toDouble()
}

/**
*
* @param rateValueLength - rate value length
* @param maxLength - max allowed length of tne elements to be inserted in an edit text
* @return cursor position
*/
fun getSelectionIndex(rateValueLength: Int, maxLength: Int) : Int {
return if (rateValueLength > maxLength) maxLength else rateValueLength
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/rate_item_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
android:hint="@string/edit_text_hint"
android:inputType="numberDecimal"
android:maxEms="7"
android:maxLength="12"
android:maxLength="@integer/edit_text_max_length"
android:minEms="2"
android:textAlignment="textEnd"
android:textColor="@color/colorBlack"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<dimen name="dimen_16dp">16dp</dimen>
<dimen name="dimen_32dp">32dp</dimen>
<dimen name="dimen_56dp">56dp</dimen>
<integer name="edit_text_max_length">18</integer>
</resources>
11 changes: 11 additions & 0 deletions app/src/test/java/com/avs/rates/utils/FormatKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ class FormatKtTest {
assertEquals(round(value, 2), 1234.01, delta)
assertEquals(round(value, 3), 1234.01, delta)
}

@Test
fun getSelectionIndexTest() {
var valueLength = 10
val maxLength = 18
assertEquals(getSelectionIndex(valueLength, maxLength), valueLength)
valueLength = 22
assertEquals(getSelectionIndex(valueLength, maxLength), maxLength)
valueLength = 18
assertEquals(getSelectionIndex(valueLength, maxLength), valueLength)
}
}

0 comments on commit 1055c19

Please sign in to comment.