Skip to content

Commit

Permalink
Merge branch 'feature/22-summary-amount-color' into 'master'
Browse files Browse the repository at this point in the history
Set near-zero summary amount color to neutral

Closes #22

See merge request flat/flat-android!12
  • Loading branch information
Radek Pieja committed Jan 23, 2018
2 parents 27e88db + 40bb446 commit 02e7065
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class ChargeLikeFragment<T: ChargeLike, VH: RecyclerView.ViewHolder, VM
abstract fun extractEntityFromDTO(dto: DTO): List<T>
abstract fun updateItemView(viewHolder: VH, item: T)
abstract fun createViewHolder(view: View): VH
abstract fun formatAmount(amountTextView: TextView, amount: Double)

private var recyclerView: RecyclerView? = null
private var elements: List<T>? = null
Expand Down Expand Up @@ -94,8 +95,12 @@ abstract class ChargeLayoutFragment<T: ChargeLike, VM: ViewModel, DTO>:

override fun updateItemView(viewHolder: ChargeViewHolder, item: T) {
viewHolder.chargeName.text = item.chargeName
viewHolder.chargeAmount.text = currencyFormat.format(item.chargeAmount).toString()
formatAmount(viewHolder.chargeAmount, item.chargeAmount)
viewHolder.chargeUsers.text = android.text.TextUtils.join(", ",
getUsers(item).map { user -> user.name })
}

override fun formatAmount(amountTextView: TextView, amount: Double) {
amountTextView.text = currencyFormat.format(amount).toString()
}
}
38 changes: 26 additions & 12 deletions app/src/main/java/pl/rpieja/flat/fragment/SummaryLayoutFragment.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package pl.rpieja.flat.fragment

import android.arch.lifecycle.ViewModel
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.TextView
import pl.rpieja.flat.R
import pl.rpieja.flat.dto.Summary
import pl.rpieja.flat.dto.User
import kotlin.math.absoluteValue

class SummaryViewHolder(view: View): RecyclerView.ViewHolder(view) {
val amountTextView: TextView = itemView.findViewById(R.id.summaryAmount)
Expand All @@ -20,21 +22,33 @@ abstract class SummaryLayoutFragment<VM: ViewModel, DTO>:
override fun getUsers(item: Summary): List<User> = item.fromUsers

override fun updateItemView(viewHolder: SummaryViewHolder, item: Summary) {
val colorNegative = context!!.getColor(R.color.amountNegative)
val colorPositive = context!!.getColor(R.color.amountPositive)
val colorNeutral = context!!.getColor(R.color.amountNeutral)
formatAmount(viewHolder.amountTextView, item.amount)
viewHolder.userTextView.text = item.chargeName
}

val amountColor = when {
item.chargeAmount > 0 -> colorNegative
item.chargeAmount < 0 -> colorPositive
else -> colorNeutral
}
override fun createViewHolder(view: View): SummaryViewHolder = SummaryViewHolder(view)

viewHolder.amountTextView.setTextColor(amountColor)
viewHolder.amountTextView.text = currencyFormat.format(item.chargeAmount)
// getColor() requires context, available after onAttach
private var colorNegative: Int? = null
private var colorPositive: Int? = null
private var colorNeutral: Int? = null

viewHolder.userTextView.text = item.chargeName
override fun onAttach(context: Context?) {
super.onAttach(context)
colorNegative = context!!.getColor(R.color.amountNegative)
colorPositive = context.getColor(R.color.amountPositive)
colorNeutral = context.getColor(R.color.amountNeutral)
}

override fun createViewHolder(view: View): SummaryViewHolder = SummaryViewHolder(view)
override fun formatAmount(amountTextView: TextView, amount: Double) {
val roundAmount = if (amount.absoluteValue < 0.01) 0.0 else amount
val color = when {
amount >= 0.01 -> colorNegative
amount <= -0.01 -> colorPositive
else -> colorNeutral
}

amountTextView.text = currencyFormat.format(roundAmount).toString()
amountTextView.setTextColor(color!!)
}
}

0 comments on commit 02e7065

Please sign in to comment.