Created
February 15, 2020 19:08
-
-
Save PamilerinId/f6591d54220ec564aa0938ded66b2806 to your computer and use it in GitHub Desktop.
PI changes
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
2020-02-15 20:07:09.389 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 | |
2020-02-15 20:07:09.673 20041-20041/ng.chaka.android.debug I/chatty: uid=10086(ng.chaka.android.debug) identical 199 lines | |
2020-02-15 20:07:09.675 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 | |
2020-02-15 20:07:09.677 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 | |
2020-02-15 20:07:09.681 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 | |
2020-02-15 20:07:10.613 20041-20041/ng.chaka.android.debug I/chatty: uid=10086(ng.chaka.android.debug) identical 521 lines | |
2020-02-15 20:07:10.622 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 | |
2020-02-15 20:07:10.625 20041-20052/ng.chaka.android.debug I/a.android.debu: Background concurrent copying GC freed 116048(3MB) AllocSpace objects, 0(0B) LOS objects, 30% free, 13MB/19MB, paused 911us total 317.415ms | |
2020-02-15 20:07:10.626 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 | |
2020-02-15 20:07:10.675 20041-20041/ng.chaka.android.debug I/chatty: uid=10086(ng.chaka.android.debug) identical 27 lines | |
2020-02-15 20:07:10.676 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 | |
2020-02-15 20:07:10.677 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 | |
2020-02-15 20:07:10.678 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 | |
2020-02-15 20:07:10.679 20041-20041/ng.chaka.android.debug I/System.out: 19.984424999999998 |
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
package ng.chaka.android.stocks.ui | |
import android.annotation.SuppressLint | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Bundle | |
import android.text.Editable | |
import android.text.TextWatcher | |
import android.widget.RadioButton | |
import android.widget.RadioGroup | |
import androidx.core.content.ContextCompat | |
import androidx.core.widget.addTextChangedListener | |
import androidx.lifecycle.Observer | |
import com.squareup.picasso.Picasso | |
import kotlinx.android.synthetic.main.activity_stock_buy.* | |
import ng.chaka.android.R | |
import ng.chaka.android.core.ServerResult | |
import ng.chaka.android.core.android.BaseActivity | |
import ng.chaka.android.core.android.Resource | |
import ng.chaka.android.core.android.Status | |
import ng.chaka.android.core.getFlag | |
import ng.chaka.android.core.hide | |
import ng.chaka.android.core.longToast | |
import ng.chaka.android.core.setBlockingOnClickListener | |
import ng.chaka.android.core.show | |
import ng.chaka.android.core.showErrorSnack | |
import ng.chaka.android.home.AppBar | |
import ng.chaka.android.home.compnents.InstrumentsChartView | |
import ng.chaka.android.home.compnents.convertToCurrency | |
import ng.chaka.android.home.tabs.category.api.Instrument | |
import ng.chaka.android.home.tabs.category.ui.CategoryStockViewModel | |
import ng.chaka.android.home.tabs.exchange.CountryCurrency | |
import ng.chaka.android.stocks.domain.PreOrderStock | |
import ng.chaka.android.stocks.ui.StockConfirmTransactionActivity.Companion.startStockConfirmTransactionForResult | |
class StockBuyActivity : BaseActivity(R.layout.activity_stock_buy) { | |
private val categoryStockViewModel: CategoryStockViewModel by lazyViewModel() | |
private val viewModel: StockBuyViewModel by lazyViewModel() | |
private var radioButtonChangedAmount = false | |
private var amountChangeRadioButton = false | |
private var isSellForm: Boolean = false | |
private var isLocalStock: Boolean = false | |
private lateinit var radioButtonByText: HashMap<String, RadioButton> | |
private var askPrice: Double = 1.0 | |
private var currency: String = "" | |
private lateinit var orderStockForm: OrderStockForm | |
val amountListener = object : TextWatcher{ | |
override fun afterTextChanged(p0: Editable?) { | |
amountErrorTextView.text = "" | |
val amount = p0.toString().toDoubleOrNull() | |
// Obviuosly enters a recursive problem | |
quantityEditText.removeTextChangedListener(quantityListener) | |
if (amount != null) { | |
quantityEditText.setText(String.format("%.4f", (amount / askPrice))) | |
} else { | |
quantityEditText.setText("") | |
} | |
quantityEditText.addTextChangedListener(quantityListener) | |
} | |
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} | |
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} | |
} | |
val quantityListener = object : TextWatcher { | |
override fun afterTextChanged(p0: Editable?) { | |
quantityErrorTextView.text = "" | |
val quantity = p0.toString().toDoubleOrNull() | |
//remove listener | |
amountEditText.removeTextChangedListener(amountListener) | |
if (quantity != null) { | |
println((quantity * askPrice)) | |
amountEditText.setText(String.format("%.4f", (quantity * askPrice))) | |
} else { | |
amountEditText.setText("") | |
} | |
//add listener | |
amountEditText.addTextChangedListener(amountListener) | |
} | |
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} | |
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {} | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setupAppbar() | |
val symbol = intent.getStringExtra(SYMBOL_KEY) ?: throw IllegalArgumentException() | |
isSellForm = intent.getBooleanExtra(SELL_KEY, false) | |
buyStockButton.setText(if (isSellForm) R.string.action_sell_stock else R.string.action_buy_stock) | |
categoryStockViewModel.tags = symbol | |
radioButtonByText = indexRadioButtonChildren() | |
fetchContent() | |
amountRadioGroup.setOnCheckedChangeListener { group: RadioGroup?, checkedId: Int -> | |
onCheckedChange(group, checkedId) | |
} | |
// amountEditText.addTextChangedListener { | |
// amountChanged(it.toString()) | |
// } | |
buyStockButton.setBlockingOnClickListener { | |
buyStockClick() | |
} | |
viewModel.preOrderStockTransaction.observe(this, Observer { resource -> | |
preOrderStockResource(resource) | |
}) | |
amountEditText.addTextChangedListener(amountListener) | |
quantityEditText.addTextChangedListener(quantityListener) | |
priceEditText.addTextChangedListener { | |
priceErrorTextView.text = "" | |
} | |
orderTypeRadioGroup.setOnCheckedChangeListener { _: RadioGroup?, checkedId: Int -> | |
orderTypeChanged(checkedId) | |
} | |
orderTypeChanged(R.id.marketOrderRadioButton) | |
} | |
private fun orderTypeChanged(checkedId: Int) { | |
if (checkedId == R.id.marketOrderRadioButton) { | |
amountViews.show() | |
quantityEditText.isEnabled = true | |
priceViews.hide() | |
} else if (checkedId == R.id.limitOrderRadioButton) { | |
amountViews.hide() | |
quantityEditText.isEnabled = true | |
priceViews.show() | |
} | |
} | |
private fun preOrderStockResource(resource: Resource<ServerResult<PreOrderStock>>) { | |
when (resource.status) { | |
Status.LOADING -> { | |
progressBar.show() | |
formLayout.hide() | |
} | |
Status.ERROR -> { | |
progressBar.hide() | |
formLayout.show() | |
showErrorSnack( | |
custom_coordinator, | |
resource.message ?: getString(R.string.generic_error) | |
) | |
} | |
Status.SUCCESS -> { | |
progressBar.hide() | |
formLayout.show() | |
val stockPreOrder = resource.data?.data | |
if (stockPreOrder != null) { | |
startStockConfirmTransactionForResult( | |
isSellForm, | |
stockPreOrder, orderStockForm, isLocalStock | |
) | |
finish() | |
} | |
} | |
} | |
} | |
private fun buyStockClick() { | |
val symbol = categoryStockViewModel.tags | |
val amount = amountEditText.text.toString().toFloatOrNull() | |
val price = priceEditText.text.toString().toFloatOrNull() | |
val quantity = quantityEditText.text.toString().toFloatOrNull() | |
val orderTypeId = orderTypeRadioGroup.checkedRadioButtonId | |
val orderType = when (orderTypeId) { | |
R.id.marketOrderRadioButton -> MARKET_ORDER | |
R.id.limitOrderRadioButton -> LIMIT_ORDER | |
else -> null | |
} | |
if (amountEditText.isShown) { | |
if (amount == null) { | |
amountErrorTextView.show() | |
amountErrorTextView.text = getString(R.string.amount_required) | |
return | |
} | |
if (amount <= 0) { | |
amountErrorTextView.show() | |
amountErrorTextView.text = getString(R.string.amount_negative_error) | |
return | |
} | |
} | |
if (priceEditText.isShown) { | |
if (price == null) { | |
priceErrorTextView.show() | |
priceErrorTextView.text = getString(R.string.amount_required) | |
return | |
} | |
if (price <= 0) { | |
priceErrorTextView.show() | |
priceErrorTextView.text = getString(R.string.amount_negative_error) | |
return | |
} | |
} | |
if (quantityEditText.isShown) { | |
if (quantity == null) { | |
quantityErrorTextView.show() | |
quantityErrorTextView.text = getString(R.string.amount_required) | |
return | |
} | |
if (quantity <= 0) { | |
quantityErrorTextView.show() | |
quantityErrorTextView.text = getString(R.string.amount_negative_error) | |
return | |
} | |
} | |
if (orderType == null) { | |
return | |
} | |
if (orderType == MARKET_ORDER) { | |
orderStockForm = OrderStockForm( | |
symbol, | |
amount?.times(kobo), | |
currency, | |
orderType, | |
orderSide(isSellForm) | |
) | |
viewModel.preOderStock(orderStockForm) | |
} else if (orderType == LIMIT_ORDER) { | |
orderStockForm = OrderStockForm( | |
symbol, | |
null, | |
currency, | |
orderType, | |
orderSide(isSellForm), | |
price?.times(kobo), | |
quantity?.times(kobo) | |
) | |
viewModel.preOderStock(orderStockForm) | |
} | |
} | |
private fun indexRadioButtonChildren(): HashMap<String, RadioButton> { | |
val count = amountRadioGroup.childCount | |
val radioButtonByText = HashMap<String, RadioButton>() | |
for (i in 0 until count) { | |
val radioButton = amountRadioGroup.getChildAt(i) | |
if (radioButton is RadioButton) { | |
radioButtonByText[radioButton.text.toString().skipFirstChar()] = radioButton | |
} | |
} | |
return radioButtonByText | |
} | |
private fun String.skipFirstChar() = if (this.isNotEmpty()) this.substring(1) else "" | |
private fun setupAppbar() { | |
app_bar.actions = listOf(AppBar.MenuItem.BACK, AppBar.MenuItem.CHAKA) | |
app_bar.delegate = ::onMenuItemClick | |
} | |
private fun amountChanged(amountStr: String) { | |
if (!radioButtonChangedAmount) { | |
val radioButton = radioButtonByText[amountStr] | |
amountChangeRadioButton = true | |
if (radioButton != null) { | |
radioButton.isChecked = true | |
} else { | |
amountRadioGroup.clearCheck() | |
} | |
} | |
amountChangeRadioButton = false | |
} | |
private fun checkedIdToString(checkedId: Int): String { | |
return findViewById<RadioButton?>(checkedId)?.text?.toString()?.skipFirstChar()?.replace( | |
"K", | |
"000" | |
) ?: "" | |
} | |
private fun onCheckedChange(group: RadioGroup?, checkedId: Int) { | |
if (group != null && !amountChangeRadioButton) { | |
radioButtonChangedAmount = true | |
amountEditText.setText(checkedIdToString(checkedId)) | |
} | |
radioButtonChangedAmount = false | |
} | |
private fun fetchContent() { | |
categoryStockViewModel.findStocksUsingStringTags(categoryStockViewModel.tags) | |
categoryStockViewModel.instrumentsLiveData.observe(this, Observer { resource -> | |
instrumentResource(resource) | |
}) | |
} | |
private fun prepareAmountSelector(currency: InstrumentsChartView.Currency) { | |
if (currency == InstrumentsChartView.Currency.Naira) { | |
getAmountForLocal(currency.symbol) | |
return | |
} | |
option20.text = getString(R.string.dollar_20, currency.symbol) | |
option50.text = getString(R.string.dollar_50, currency.symbol) | |
option100.text = getString(R.string.dollar_100, currency.symbol) | |
option200.text = getString(R.string.dollar_200, currency.symbol) | |
} | |
@SuppressLint("SetTextI18n") | |
private fun getAmountForLocal(symbol: String) { | |
// N1k | N5k | N10k | N50k | |
option20.text = "${symbol}1K" | |
option50.text = "${symbol}5K" | |
option100.text = "${symbol}10K" | |
option200.text = "${symbol}50K" | |
} | |
private fun instrumentResource(resource: Resource<List<Instrument>>) { | |
when (resource.status) { | |
Status.LOADING -> { | |
progressBar.show() | |
formLayout.hide() | |
} | |
Status.ERROR -> { | |
longToast(resource.message ?: getString(R.string.retrive_content_error)) | |
finish() | |
} | |
Status.SUCCESS -> { | |
progressBar.hide() | |
formLayout.show() | |
val instruments = resource.data?.getOrNull(0) | |
askPrice = instruments?.askPrice?.toDoubleOrNull() ?: 1.0 | |
isLocalStock = instruments?.isLocalStock() == true | |
currency = instruments?.currency | |
?: getString(CountryCurrency.LOCAL_CURRENCY.shortFmtCurrency) | |
instruments.let { instrument -> | |
if (instrument == null) return | |
companyNameTextView.text = instrument.name | |
companyExchangeTextView.text = instrument.exchangeID | |
stockPriceTextView.text = String.format( | |
"%s%,.2f", convertToCurrency(instrument.currency ?: "").symbol, | |
instrument.askPrice?.toDouble() ?: 0.0 | |
) | |
stockUnitTextView.text = String.format( | |
"%,.2f(%,.2f%%)", | |
instrument.derivedPrice ?: 0.0, | |
instrument.derivedPricePercentage ?: 0.0 | |
) | |
stockUnitTextView.setTextColor( | |
if (instrument.derivedPrice ?: 0.0 >= 0) { | |
ContextCompat.getColor( | |
this, | |
R.color.green | |
) | |
} else { | |
ContextCompat.getColor( | |
this, | |
R.color.red | |
) | |
} | |
) | |
prepareAmountSelector(convertToCurrency(currency)) | |
Picasso.get().load(instrument.logoUrl) | |
.placeholder(R.drawable.circle_grey).into(companyImageView) | |
companyStockImageView.setImageResource(getFlag(instrument.countryCode ?: "")) | |
} | |
} | |
} | |
} | |
private fun onMenuItemClick(menuItem: AppBar.MenuItem) { | |
if (menuItem == AppBar.MenuItem.BACK) onBackPressed() | |
} | |
companion object { | |
private const val MARKET_ORDER = "MARKET" | |
private const val LIMIT_ORDER = "LIMIT" | |
private const val SYMBOL_KEY = "symbol_key" | |
private const val SELL_KEY = "sell_key" | |
private const val kobo = 100 | |
fun Context.startStockBuyActivity(symbol: String) { | |
val intent = Intent(this, StockBuyActivity::class.java) | |
intent.putExtra(SYMBOL_KEY, symbol) | |
intent.putExtra(SELL_KEY, false) | |
this.startActivity(intent) | |
} | |
fun Context.startStockSellActivity(symbol: String) { | |
val intent = Intent(this, StockBuyActivity::class.java) | |
intent.putExtra(SYMBOL_KEY, symbol) | |
intent.putExtra(SELL_KEY, true) | |
this.startActivity(intent) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment