-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
232d065
commit 04b5d63
Showing
8 changed files
with
212 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/src/main/kotlin/com/github/premnirmal/ticker/portfolio/search/TrendingStocksAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.github.premnirmal.ticker.portfolio.search | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import com.github.premnirmal.ticker.network.data.Quote | ||
import com.github.premnirmal.ticker.ui.bindStock | ||
import com.github.premnirmal.tickerwidget.databinding.ItemTrendingStockBinding | ||
|
||
class TrendingStocksAdapter(private val listener: (Quote) -> Unit) : RecyclerView.Adapter<TrendingStockVH>() { | ||
|
||
private val data = ArrayList<Quote>() | ||
|
||
fun setData(quotes: List<Quote>) { | ||
data.clear() | ||
data.addAll(quotes) | ||
notifyDataSetChanged() | ||
} | ||
|
||
override fun getItemCount(): Int = data.size | ||
|
||
override fun onCreateViewHolder( | ||
parent: ViewGroup, | ||
viewType: Int | ||
): TrendingStockVH { | ||
return TrendingStockVH( | ||
ItemTrendingStockBinding.inflate(LayoutInflater.from(parent.context), parent, false) | ||
) | ||
} | ||
|
||
override fun onBindViewHolder( | ||
holder: TrendingStockVH, | ||
position: Int | ||
) { | ||
holder.update(data[position], listener) | ||
} | ||
} | ||
|
||
class TrendingStockVH(private val binding: ItemTrendingStockBinding) : RecyclerView.ViewHolder( | ||
binding.root | ||
) { | ||
|
||
fun update( | ||
quote: Quote, | ||
listener: (Quote) -> Unit | ||
) { | ||
binding.bindStock(quote, listener) | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
app/src/main/kotlin/com/github/premnirmal/ticker/ui/TrendingStockBindingExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.github.premnirmal.ticker.ui | ||
|
||
import android.content.res.Resources.Theme | ||
import android.util.TypedValue | ||
import androidx.core.content.ContextCompat | ||
import com.github.premnirmal.ticker.network.data.Quote | ||
import com.github.premnirmal.tickerwidget.R | ||
import com.github.premnirmal.tickerwidget.databinding.ItemTrendingStockBinding | ||
import com.robinhood.ticker.TickerUtils | ||
|
||
fun ItemTrendingStockBinding.bindStock( | ||
quote: Quote, | ||
listener: (Quote) -> Unit | ||
) { | ||
val positiveColor: Int = ContextCompat.getColor(root.context, R.color.positive_green) | ||
val negativeColor: Int = ContextCompat.getColor(root.context, R.color.negative_red) | ||
val neutralColor: Int by lazy { | ||
try { | ||
val typedValue = TypedValue() | ||
val theme: Theme = root.context.theme | ||
theme.resolveAttribute( | ||
com.google.android.material.R.attr.colorOnSurfaceVariant, | ||
typedValue, | ||
true | ||
) | ||
ContextCompat.getColor(root.context, typedValue.data) | ||
} catch (e: Exception) { | ||
ContextCompat.getColor(root.context, R.color.text_2) | ||
} | ||
} | ||
this.changePercent.setCharacterLists(TickerUtils.provideNumberList()) | ||
this.changeValue.setCharacterLists(TickerUtils.provideNumberList()) | ||
this.totalValue.setCharacterLists(TickerUtils.provideNumberList()) | ||
|
||
root.setOnClickListener { | ||
listener.invoke(quote) | ||
} | ||
|
||
val tickerView = ticker | ||
val nameView = name | ||
|
||
tickerView.text = quote.symbol | ||
nameView.text = quote.name | ||
|
||
val totalValueText = totalValue | ||
totalValueText.text = quote.priceFormat.format(quote.lastTradePrice) | ||
|
||
val change: Float = quote.change | ||
val changePercent: Float = quote.changeInPercent | ||
val color = when { | ||
(change < 0f || changePercent < 0f) -> { | ||
negativeColor | ||
} | ||
(change == 0f) -> { | ||
neutralColor | ||
} | ||
else -> { | ||
positiveColor | ||
} | ||
} | ||
val changeInPercentView = this.changePercent | ||
changeInPercentView.text = quote.changePercentStringWithSign() | ||
val changeValueView = changeValue | ||
changeValueView.text = quote.changeStringWithSign() | ||
changeInPercentView.setTextColor(color) | ||
changeValueView.setTextColor(color) | ||
} |
Oops, something went wrong.