Skip to content

Commit

Permalink
Implement price formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
nuhkoca committed Sep 23, 2019
1 parent 61a6df3 commit 1817f11
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.techchallenge.core.util.ext.rotate
import com.techchallenge.core.util.ext.setDrawable
import com.techchallenge.core.util.ext.toMonthName
import com.techchallenge.core.util.ext.use
import com.techchallenge.core.util.ext.withCurrency
import com.techchallenge.data.ResponseViewItem
import com.techchallenge.marketim.R
import com.techchallenge.marketim.databinding.ViewOrdersLayoutBinding
Expand Down Expand Up @@ -41,11 +40,11 @@ class OrdersAdapter : DelegateAdapter {
tvMonth.text = month.toMonthName()
tvMarketName.text = marketName
tvOrderName.text = orderName
tvPrice.text = productPrice.withCurrency()
tvPrice.text = productPrice
tvStatusText.text = productState.type
ivStatus.setDrawable(productState.color)
tvDetail.text = productDetailRawViewItem.orderDetail
tvDetailPrice.text = productDetailRawViewItem.summaryPrice.withCurrency()
tvDetailPrice.text = productDetailRawViewItem.summaryPrice

ivExpandCollapseArrow.setOnClickListener {
setIsExpanded(getIsExpanded().not())
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/view_orders_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@

<TextView
android:id="@+id/tvDate"
android:layout_width="100dp"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:fontFamily="@font/google_sans"
android:gravity="center"
Expand Down
12 changes: 10 additions & 2 deletions core/src/main/java/com/techchallenge/core/CoreModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.techchallenge.core.util.executors.MarketimComputationThread
import com.techchallenge.core.util.executors.MarketimExecutionThread
import com.techchallenge.core.util.executors.MarketimPostExecutionThread
import com.techchallenge.core.util.executors.PostExecutionThread
import com.techchallenge.core.util.price.MarketPriceFormatter
import com.techchallenge.core.util.price.PriceFormatter
import com.techchallenge.core.util.validator.PasswordValidator
import com.techchallenge.core.util.validator.UsernameValidator
import com.techchallenge.core.util.validator.Validator
Expand All @@ -32,12 +34,18 @@ abstract class CoreModule {
@Binds
@Singleton
@Named(USERNAME)
internal abstract fun provideUsernameValidator(usernameValidator: UsernameValidator): Validator<String>
internal abstract fun provideUsernameValidator(usernameValidator: UsernameValidator):
Validator<String>

@Binds
@Singleton
@Named(PASSWORD)
internal abstract fun providePasswordValidator(passwordValidator: PasswordValidator): Validator<String>
internal abstract fun providePasswordValidator(passwordValidator: PasswordValidator):
Validator<String>

@Binds
@Singleton
abstract fun providePriceFormatter(marketPriceFormatter: MarketPriceFormatter): PriceFormatter

companion object {
const val USERNAME = "UsernameValidator"
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/java/com/techchallenge/core/util/ext/String.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import org.threeten.bp.Month
import org.threeten.bp.format.TextStyle.FULL_STANDALONE
import java.util.Locale

private const val NON_NUMERIC_REGEX = "[^0-9.,]"

fun String.toMonthName(): String {
return Month.of(this.toInt()).getDisplayName(FULL_STANDALONE, Locale.getDefault())
}

fun String.withCurrency() = "$this TL"
fun String.removeNonDigitCharacters() = replace(NON_NUMERIC_REGEX.toRegex(), "")
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.techchallenge.core.util.price

import com.techchallenge.core.util.ext.removeNonDigitCharacters
import javax.inject.Inject

class MarketPriceFormatter @Inject constructor() : PriceFormatter {
override fun format(price: Float?): String {
return if (price != null) String.format(PATTERN_PRICE, price) else EMPTY
}

override fun format(price: String?): String {
val priceOnlyDigits = price?.removeNonDigitCharacters()
return if (!priceOnlyDigits.isNullOrEmpty()) {
format(priceOnlyDigits.replace(DOT, COMMA).toFloat())
} else {
EMPTY
}
}

companion object {
private const val CURRENCY = "TL"
private const val PATTERN_PRICE = "%.2f $CURRENCY"
private const val EMPTY = ""
private const val COMMA = ","
private const val DOT = "."
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.techchallenge.core.util.price

interface PriceFormatter {
fun format(price: Float?): String
fun format(price: String?): String
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package com.techchallenge.data

import com.techchallenge.core.util.mapper.Mapper
import com.techchallenge.core.util.price.PriceFormatter
import com.techchallenge.data.ProductState.INVALID
import javax.inject.Inject

class MarketimViewItemMapper @Inject constructor() : Mapper<Response, ResponseViewItem> {
class MarketimViewItemMapper @Inject constructor(
private val priceFormatter: PriceFormatter
) : Mapper<Response, ResponseViewItem> {

override fun invoke(input: Response) = with(input) {
val productDetailViewItem = ProductDetailRawViewItem(
productDetail?.orderDetail.toString(),
productDetail?.summaryPrice.toString()
priceFormatter.format(productDetail?.summaryPrice)
)

ResponseViewItem(
date.toString(),
month.toString(),
marketName.toString(),
orderName.toString(),
productPrice.toString(),
priceFormatter.format(productPrice),
productState ?: INVALID,
productDetailViewItem
)
Expand Down

0 comments on commit 1817f11

Please sign in to comment.