Skip to content

Commit

Permalink
Fix NaN and price format for 'my portfolio' widget
Browse files Browse the repository at this point in the history
  • Loading branch information
premnirmal committed Jun 27, 2022
1 parent 44979c2 commit b241ae2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ data class Position(
holdings.remove(holding)
}

fun averagePrice(): Float =
holdings.sumByDouble {
it.totalValue()
.toDouble()
}.div(totalShares()).toFloat()

fun totalShares(): Float = holdings.sumByDouble { it.shares.toDouble() }.toFloat()

fun totalPaidPrice(): Float = holdings.sumByDouble {
it.totalValue()
.toDouble()
}.toFloat()
fun averagePrice(): Float {
val totalShares = totalShares()
if (totalShares == 0f) return 0f
val sum = holdings.sumOf {
it.totalValue().toDouble()
}
return sum.div(totalShares).toFloat()
}

fun totalShares(): Float = holdings.sumOf { it.shares.toDouble() }.toFloat()

fun totalPaidPrice(): Float = holdings.sumOf { it.totalValue().toDouble() }.toFloat()
}

@Parcelize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ data class Quote constructor(
return gainLossString
}

private fun gainLossPercent(): Float = (gainLoss() / totalPositionPrice()) * 100f
private fun gainLossPercent(): Float {
if (totalPositionPrice() == 0f) return 0f
return (gainLoss() / totalPositionPrice()) * 100f
}

fun gainLossPercentString(): String {
val gainLossPercent = gainLossPercent()
Expand Down Expand Up @@ -163,7 +166,7 @@ data class Quote constructor(

override operator fun compareTo(other: Quote): Int =
other.changeInPercent.compareTo(changeInPercent)

fun copyValues(data: Quote) {
this.name = data.name
this.lastTradePrice = data.lastTradePrice
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class WidgetPreviewAdapter(private var widgetData: WidgetData) : BaseAdapter() {

layout.findViewById<TextView>(R.id.ticker)?.text = stock.symbol
layout.findViewById<TextView>(R.id.holdings)?.text = if (widgetData.isCurrencyEnabled()) {
"${stock.priceFormat}${stock.holdingsString()}"
stock.priceFormat.format(stock.holdings())
} else {
stock.holdingsString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.github.premnirmal.ticker.network.data.Quote
import com.github.premnirmal.ticker.widget.WidgetData.Companion.ChangeType
import com.github.premnirmal.tickerwidget.R
import timber.log.Timber
import java.util.ArrayList
import javax.inject.Inject

/**
Expand Down Expand Up @@ -84,7 +83,7 @@ class RemoteStockViewAdapter(private val widgetId: Int) : RemoteViewsService.Rem

remoteViews.setTextViewText(R.id.ticker, stock.symbol)
remoteViews.setTextViewText(R.id.holdings, if (widgetData.isCurrencyEnabled()) {
"${stock.priceFormat}${stock.holdingsString()}"
stock.priceFormat.format(stock.holdings())
} else {
stock.holdingsString()
})
Expand Down
4 changes: 2 additions & 2 deletions app/version.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# this file is purely for f-droid because it cannot infer the version name/code from the git tag
versionName=3.9.790
versionCode=300900790
versionName=3.9.791
versionCode=300900791

0 comments on commit b241ae2

Please sign in to comment.