Skip to content

Commit

Permalink
add event listeners for price, quantity and price type chages
Browse files Browse the repository at this point in the history
  • Loading branch information
gigiyy committed Oct 4, 2018
1 parent 79fcb33 commit 243c209
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package it.trade.android.japanapp.ui.orderinput

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.opengl.Visibility
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
Expand Down Expand Up @@ -34,7 +35,7 @@ class OrderInputFragment : Fragment() {
tvSymbol.text = "${symbol.symbol} ${symbol.exchange}"
tvCurrentTime.text = "13:00"

tvPrice.text = symbol.price.toString()
tvPrice.text = String.format("%,.0f", symbol.price)
val change = String.format("%+,.0f", priceChange)
val percentage = String.format("%+.2f", priceChangePercentage * 100)
tvPriceChange.text = "$change ($percentage%)"
Expand All @@ -52,6 +53,40 @@ class OrderInputFragment : Fragment() {
tvEstimatedValue.text = "$estimated"
}
})
btQuantityPlus.setOnClickListener {
viewModel.increaseQuantity()
}
btQuantityMinus.setOnClickListener {
viewModel.decreaseQuantity()
}
btPricePlus.setOnClickListener {
viewModel.increasePrice()
}
btPriceMinus.setOnClickListener {
viewModel.decreasePrice()
}
btLimit.isChecked = true
btMarket.isChecked = false
btMarket.setOnClickListener {
btLimit.isChecked = false
btMarket.isChecked = true
togglePriceType()
}
btLimit.setOnClickListener {
btLimit.isChecked = true
btMarket.isChecked = false
togglePriceType()
}
}

private fun togglePriceType() {
if (btLimit.isChecked) {
priceInput.visibility = View.VISIBLE
tvPriceLimit.visibility = View.VISIBLE
} else {
priceInput.visibility = View.GONE
tvPriceLimit.visibility = View.GONE
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,74 @@ class OrderInputViewModel : ViewModel() {
if (!this::orderForm.isInitialized) {
orderForm = MutableLiveData()
orderForm.value = OrderForm(
TradeItSDKHolder.getSymoblProvider().getJapanSymbol("8703"),
TradeItSDKHolder.getSymbolProvider().getJapanSymbol("8703"),
TradeItSDKHolder.getBuyingPower())
}
return orderForm
}

}

interface JapanSymbolProvider {
fun getJapanSymbol(symol: String): JapanSymbol
}
fun increaseQuantity() {
val value = orderForm.value
orderForm.value = value?.apply {
orderInfo = orderInfo.copy(quantity = orderInfo.quantity + symbol.lotSize)
}
}

class SampleJapanSymbol : JapanSymbolProvider {
override fun getJapanSymbol(symol: String): JapanSymbol {
return JapanSymbol("カブドットコム証券(株)",
"8703", "東証1部", 386.0,
384.0, 286.0, 486.0, 100)
fun decreaseQuantity() {
val value = orderForm.value
if (value?.orderInfo?.quantity == value?.symbol?.lotSize) {
// do not decrease further when it's lotsize already
return
} else {
orderForm.value = value?.apply {
orderInfo = orderInfo.copy(quantity = orderInfo.quantity - symbol.lotSize)
}
}
}
}

object TradeItSDKHolder {
fun getSymoblProvider(): JapanSymbolProvider {
return SampleJapanSymbol()
fun increasePrice() {
val value = orderForm.value
orderForm.value = value?.apply {
if (orderInfo.limitPrice < symbol.priceUpperLimit) {
orderInfo = orderInfo.copy(limitPrice = orderInfo.limitPrice + 1)
}
}
}
// need broker/account
fun getBuyingPower(): BuyingPower {
return BuyingPower(500000.toDouble(), 100000.toDouble())

fun decreasePrice() {
val value = orderForm.value
orderForm.value = value?.apply {
if (orderInfo.limitPrice > symbol.priceLowerLimit) {
orderInfo = orderInfo.copy(limitPrice = orderInfo.limitPrice - 1)
}
}
}

}

class OrderForm(val symbol: JapanSymbol, val buyingPower: BuyingPower) {
val orderInfo: OrderInfo = OrderInfo(
var orderInfo: OrderInfo = OrderInfo(
quantity = symbol.lotSize,
limitPrice = symbol.price,
type = OrderType.LIMIT,
expiry = OrderExpiry.DAY,
accountType = AccountType.SPECIFIC
)

val estimatedValue: Double = orderInfo.quantity * orderInfo.limitPrice
val estimatedValue: Double
get() {
return orderInfo.quantity * orderInfo.limitPrice
}

val priceChange: Double = symbol.price - symbol.previousDayPrice
val priceChange: Double
get() {
return symbol.price - symbol.previousDayPrice
}

val priceChangePercentage: Double = priceChange / symbol.previousDayPrice
val priceChangePercentage: Double
get() {
return priceChange / symbol.previousDayPrice
}

val availableExpiry: List<OrderExpiry>
get() {
Expand All @@ -68,6 +92,31 @@ class OrderForm(val symbol: JapanSymbol, val buyingPower: BuyingPower) {
}
}

//TODO temp solutions below
interface JapanSymbolProvider {
fun getJapanSymbol(symol: String): JapanSymbol
}

class SampleJapanSymbol : JapanSymbolProvider {
override fun getJapanSymbol(symol: String): JapanSymbol {
return JapanSymbol("カブドットコム証券(株)",
"8703", "東証1部", 386.0,
384.0, 286.0, 486.0, 100)
}
}

object TradeItSDKHolder {
fun getSymbolProvider(): JapanSymbolProvider {
return SampleJapanSymbol()
}

// need broker/account
fun getBuyingPower(): BuyingPower {
return BuyingPower(500000.toDouble(), 100000.toDouble())
}
}

// TODO need to separate the price portion out
data class JapanSymbol(val name: String, val symbol: String, val exchange: String,
val price: Double, val previousDayPrice: Double,
val priceLowerLimit: Double, val priceUpperLimit: Double,
Expand Down

0 comments on commit 243c209

Please sign in to comment.