Skip to content

Commit cb20ffd

Browse files
committed
use EditText to input the price/quantity
1 parent 45765f2 commit cb20ffd

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

exampleAppJapan/src/main/kotlin/it/trade/android/japanapp/ui/orderinput/OrderInputFragment.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package it.trade.android.japanapp.ui.orderinput
22

33
import android.arch.lifecycle.Observer
44
import android.arch.lifecycle.ViewModelProviders
5-
import android.opengl.Visibility
65
import android.os.Bundle
76
import android.support.v4.app.Fragment
87
import android.text.Editable
@@ -13,7 +12,6 @@ import android.view.ViewGroup
1312
import android.widget.EditText
1413
import it.trade.android.japanapp.R
1514
import kotlinx.android.synthetic.main.order_input_fragment.*
16-
import kotlinx.android.synthetic.main.order_input_fragment.view.*
1715

1816
class OrderInputFragment : Fragment() {
1917

@@ -85,6 +83,26 @@ class OrderInputFragment : Fragment() {
8583
viewModel.setLimitOrder()
8684
togglePriceType()
8785
}
86+
etPrice.onChange { price ->
87+
val selection = etPrice.selectionStart
88+
val result = viewModel.setLimitPrice(price)
89+
etPrice.error = null
90+
if (!result) {
91+
etPrice.error = getString(R.string.invalid_price)
92+
} else if (selection >= 0) {
93+
etPrice.setSelection(selection)
94+
}
95+
}
96+
etQuantity.onChange { quantity ->
97+
val selection = etQuantity.selectionStart
98+
val result = viewModel.setQuantity(quantity)
99+
etQuantity.error = null
100+
if (!result) {
101+
etQuantity.error = getString(R.string.invalid_quantity)
102+
} else if (selection >= 0) {
103+
etQuantity.setSelection(selection)
104+
}
105+
}
88106
}
89107

90108
private fun togglePriceType() {
@@ -97,7 +115,7 @@ class OrderInputFragment : Fragment() {
97115
}
98116
}
99117

100-
fun EditText.onChange(cb: (String) -> Unit) {
118+
private fun EditText.onChange(cb: (String) -> Unit) {
101119
this.addTextChangedListener(object : TextWatcher {
102120
override fun afterTextChanged(s: Editable?) {
103121
cb(s.toString())

exampleAppJapan/src/main/kotlin/it/trade/android/japanapp/ui/orderinput/OrderInputViewModel.kt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,52 @@ class OrderInputViewModel(private val symbol: String) : ViewModel() {
6565
orderInfo = orderInfo.copy(type = OrderType.LIMIT)
6666
}
6767
}
68+
69+
fun setLimitPrice(price: String): Boolean {
70+
val newPrice = try {
71+
price.toDouble()
72+
} catch (e: NumberFormatException) {
73+
return false
74+
}
75+
if (newPrice == orderForm.value?.orderInfo?.limitPrice) {
76+
// to avoid infinite loop, don't update model when value is not actually updated
77+
return true
78+
}
79+
var isValid = false
80+
val newValue = orderForm.value?.apply {
81+
if (newPrice >= symbol.priceLowerLimit && newPrice <= symbol.priceUpperLimit) {
82+
orderInfo = orderInfo.copy(limitPrice = newPrice)
83+
isValid = true
84+
}
85+
}
86+
if (isValid){
87+
orderForm.value = newValue
88+
}
89+
return isValid
90+
}
91+
92+
fun setQuantity(quantity: String): Boolean {
93+
val newQuantity = try {
94+
quantity.toInt()
95+
} catch (e: NumberFormatException) {
96+
return false
97+
}
98+
if (newQuantity == orderForm.value?.orderInfo?.quantity) {
99+
// to avoid infinite loop, don't update model when value is not actually updated
100+
return true
101+
}
102+
var isValid = false
103+
val newValue = orderForm.value?.apply {
104+
if (newQuantity % symbol.lotSize == 0) {
105+
orderInfo = orderInfo.copy(quantity = newQuantity)
106+
isValid = true
107+
}
108+
}
109+
if (isValid) {
110+
orderForm.value = newValue
111+
}
112+
return isValid
113+
}
68114
}
69115

70116
class OrderInputViewModelFactory(private val symbol: String) : ViewModelProvider.NewInstanceFactory() {

exampleAppJapan/src/main/res/layout/order_input_fragment.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@
150150
android:layout_width="wrap_content"
151151
android:layout_height="wrap_content"
152152
android:layout_marginTop="8dp"
153-
android:ems="6"
153+
android:ems="8"
154154
android:inputType="number"
155155
android:textAlignment="center"
156+
android:selectAllOnFocus="true"
156157
app:layout_constraintEnd_toStartOf="@+id/btQuantityPlus"
157158
app:layout_constraintStart_toEndOf="@+id/btQuantityMinus" />
158159

@@ -253,9 +254,10 @@
253254
android:layout_width="wrap_content"
254255
android:layout_height="wrap_content"
255256
android:layout_marginTop="8dp"
256-
android:ems="6"
257+
android:ems="8"
257258
android:inputType="number"
258259
android:textAlignment="center"
260+
android:selectAllOnFocus="true"
259261
app:layout_constraintEnd_toStartOf="@+id/btPricePlus"
260262
app:layout_constraintStart_toEndOf="@+id/btPriceMinus" />
261263

exampleAppJapan/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@
1616
<string name="general">一般</string>
1717
<string name="nisa">NISA</string>
1818
<string name="order_review">注文内容の確認へ</string>
19+
<string name="invalid_price">指定価格が値幅制限以内にしてください。</string>
20+
<string name="invalid_quantity">指定株数をロットサイズの整数倍にしてください</string>
1921
</resources>

0 commit comments

Comments
 (0)