Skip to content

Commit ab93f6a

Browse files
committed
user can set order expiry
1 parent 5ac6be9 commit ab93f6a

File tree

5 files changed

+111
-24
lines changed

5 files changed

+111
-24
lines changed

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

+50
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ import android.os.Bundle
66
import android.support.v4.app.Fragment
77
import android.text.Editable
88
import android.text.TextWatcher
9+
import android.util.Log
910
import android.view.LayoutInflater
1011
import android.view.View
1112
import android.view.ViewGroup
1213
import android.widget.EditText
14+
import android.widget.PopupMenu
1315
import it.trade.android.japanapp.R
1416
import kotlinx.android.synthetic.main.order_input_fragment.*
1517

18+
private const val TAG = "OrderInputFragment"
19+
1620
class OrderInputFragment : Fragment() {
1721

1822
companion object {
@@ -61,6 +65,14 @@ class OrderInputFragment : Fragment() {
6165
tvPriceLimit.text = "(値幅制限 $lower-$upper)"
6266
val estimated = String.format("%,.0f", estimatedValue)
6367
tvEstimatedValue.text = "$estimated"
68+
69+
Log.d(TAG, "current expiry is ${orderInfo.expiry}")
70+
btDay.isChecked = orderInfo.expiry == OrderExpiry.DAY
71+
btWeek.isChecked = orderInfo.expiry == OrderExpiry.WEEK
72+
btWeek.isEnabled = orderInfo.type == OrderType.LIMIT
73+
btTillDate.isChecked = orderInfo.expiry == OrderExpiry.TILL_DATE
74+
btTillDate.isEnabled = orderInfo.type == OrderType.LIMIT
75+
btSession.isChecked = orderInfo.expiry in listOf(OrderExpiry.OPENING, OrderExpiry.CLOSING, OrderExpiry.FUNARI)
6476
}
6577
})
6678
btQuantityPlus.setOnClickListener {
@@ -103,6 +115,44 @@ class OrderInputFragment : Fragment() {
103115
etQuantity.setSelection(selection)
104116
}
105117
}
118+
btDay.setOnClickListener {
119+
viewModel.setExpiry(OrderExpiry.DAY)
120+
}
121+
btWeek.setOnClickListener {
122+
viewModel.setExpiry(OrderExpiry.WEEK)
123+
}
124+
btTillDate.setOnClickListener {
125+
viewModel.setExpiry(OrderExpiry.TILL_DATE)
126+
}
127+
btSession.setOnClickListener {
128+
// the button status should only updated by the viewModel instead of the click event
129+
btSession.isChecked = !btSession.isChecked
130+
val popup = PopupMenu(activity!!, it)
131+
popup.inflate(R.menu.session_popup)
132+
if (btMarket.isChecked) {
133+
popup.menu.findItem(R.id.funari).isVisible = false
134+
}
135+
popup.setOnMenuItemClickListener { item ->
136+
when (item?.itemId) {
137+
R.id.opening -> {
138+
viewModel.setExpiry(OrderExpiry.OPENING)
139+
true
140+
}
141+
R.id.closing -> {
142+
viewModel.setExpiry(OrderExpiry.CLOSING)
143+
true
144+
}
145+
R.id.funari -> {
146+
viewModel.setExpiry(OrderExpiry.FUNARI)
147+
true
148+
}
149+
else -> false
150+
}
151+
}
152+
popup.setOnDismissListener { _ -> viewModel.dummyUpdate()}
153+
popup.show()
154+
}
155+
106156
}
107157

108158
private fun togglePriceType() {

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

+17-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.arch.lifecycle.MutableLiveData
55
import android.arch.lifecycle.ViewModel
66
import android.arch.lifecycle.ViewModelProvider
77
import android.util.Log
8+
import kotlin.math.exp
89

910
private const val TAG = "OrderInputViewModel"
1011

@@ -56,7 +57,11 @@ class OrderInputViewModel(private val symbol: String) : ViewModel() {
5657

5758
fun setMarketOrder() {
5859
orderForm.value = orderForm.value?.apply {
59-
orderInfo = orderInfo.copy(type = OrderType.MARKET, limitPrice = symbol.price)
60+
val expiry = if (orderInfo.expiry in listOf(OrderExpiry.WEEK, OrderExpiry.TILL_DATE, OrderExpiry.FUNARI))
61+
OrderExpiry.DAY
62+
else
63+
orderInfo.expiry
64+
orderInfo = orderInfo.copy(type = OrderType.MARKET, limitPrice = symbol.price, expiry = expiry)
6065
}
6166
}
6267

@@ -83,7 +88,7 @@ class OrderInputViewModel(private val symbol: String) : ViewModel() {
8388
isValid = true
8489
}
8590
}
86-
if (isValid){
91+
if (isValid) {
8792
orderForm.value = newValue
8893
}
8994
return isValid
@@ -111,6 +116,16 @@ class OrderInputViewModel(private val symbol: String) : ViewModel() {
111116
}
112117
return isValid
113118
}
119+
120+
fun setExpiry(expiry: OrderExpiry) {
121+
orderForm.value = orderForm.value?.apply {
122+
orderInfo = orderInfo.copy(expiry = expiry)
123+
}
124+
}
125+
126+
fun dummyUpdate() {
127+
orderForm.value = orderForm.value
128+
}
114129
}
115130

116131
class OrderInputViewModelFactory(private val symbol: String) : ViewModelProvider.NewInstanceFactory() {
@@ -144,15 +159,6 @@ class OrderForm(val symbol: JapanSymbol, val buyingPower: BuyingPower) {
144159
get() {
145160
return priceChange / symbol.previousDayPrice
146161
}
147-
148-
val availableExpiry: List<OrderExpiry>
149-
get() {
150-
return if (orderInfo.type == OrderType.LIMIT) {
151-
listOf(OrderExpiry.DAY, OrderExpiry.WEEK, OrderExpiry.TILL_DATE, OrderExpiry.OPENING, OrderExpiry.CLOSING, OrderExpiry.FUNARI)
152-
} else {
153-
listOf(OrderExpiry.DAY, OrderExpiry.OPENING, OrderExpiry.CLOSING)
154-
}
155-
}
156162
}
157163

158164
//TODO temp solutions below

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

+24-12
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@
152152
android:layout_marginTop="8dp"
153153
android:ems="8"
154154
android:inputType="number"
155-
android:textAlignment="center"
156155
android:selectAllOnFocus="true"
156+
android:textAlignment="center"
157157
app:layout_constraintEnd_toStartOf="@+id/btQuantityPlus"
158158
app:layout_constraintStart_toEndOf="@+id/btQuantityMinus" />
159159

@@ -256,8 +256,8 @@
256256
android:layout_marginTop="8dp"
257257
android:ems="8"
258258
android:inputType="number"
259-
android:textAlignment="center"
260259
android:selectAllOnFocus="true"
260+
android:textAlignment="center"
261261
app:layout_constraintEnd_toStartOf="@+id/btPricePlus"
262262
app:layout_constraintStart_toEndOf="@+id/btPriceMinus" />
263263

@@ -356,13 +356,15 @@
356356
android:layout_marginEnd="8dp">
357357

358358
<ToggleButton
359+
android:id="@+id/btDay"
359360
android:layout_width="wrap_content"
360-
android:layout_height="match_parent"
361+
android:layout_height="wrap_content"
361362
android:layout_weight="1"
362363
android:textOff="@string/day"
363364
android:textOn="@string/day" />
364365

365366
<ToggleButton
367+
android:id="@+id/btWeek"
366368
android:layout_width="wrap_content"
367369
android:layout_height="wrap_content"
368370
android:layout_weight="1"
@@ -377,19 +379,29 @@
377379
android:layout_marginStart="8dp"
378380
android:layout_marginEnd="8dp">
379381

380-
<Spinner
381-
android:layout_width="wrap_content"
382+
<ToggleButton
383+
android:id="@+id/btTillDate"
384+
android:layout_width="0dp"
382385
android:layout_height="wrap_content"
383-
android:layout_weight="1">
386+
android:layout_weight="1"
387+
android:drawableEnd="@drawable/ic_arrow_drop_down"
388+
android:textAlignment="center"
389+
android:textOff="@string/till_date"
390+
android:textOn="@string/till_date">
384391

385-
</Spinner>
392+
</ToggleButton>
386393

387-
<Spinner
388-
android:layout_width="wrap_content"
394+
<ToggleButton
395+
android:id="@+id/btSession"
396+
android:layout_width="0dp"
389397
android:layout_height="wrap_content"
390-
android:layout_weight="1">
398+
android:layout_weight="1"
399+
android:drawableEnd="@drawable/ic_arrow_drop_down"
400+
android:textAlignment="center"
401+
android:textOff="@string/opening"
402+
android:textOn="@string/opening">
391403

392-
</Spinner>
404+
</ToggleButton>
393405
</android.support.v7.widget.LinearLayoutCompat>
394406
</android.support.v7.widget.LinearLayoutCompat>
395407

@@ -481,8 +493,8 @@
481493
android:layout_width="match_parent"
482494
android:layout_height="wrap_content"
483495
android:layout_marginStart="16dp"
484-
android:layout_marginEnd="16dp"
485496
android:layout_marginTop="16dp"
497+
android:layout_marginEnd="16dp"
486498
android:text="@string/order_review"
487499
app:layout_constraintBottom_toBottomOf="parent"
488500
app:layout_constraintEnd_toEndOf="parent"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent">
5+
6+
<item
7+
android:id="@+id/opening"
8+
android:title="@string/opening" />
9+
10+
<item
11+
android:id="@+id/closing"
12+
android:title="@string/closing" />
13+
14+
<item
15+
android:id="@+id/funari"
16+
android:title="@string/funari" />
17+
</menu>

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
<string name="day">本日中</string>
1111
<string name="week">今週中</string>
1212
<string name="till_date">期間指定</string>
13-
<string name="session">寄付</string>
13+
<string name="opening">寄付</string>
14+
<string name="closing">引け</string>
15+
<string name="funari">不成</string>
1416
<string name="account_type">口座区分</string>
1517
<string name="specific">特定</string>
1618
<string name="general">一般</string>

0 commit comments

Comments
 (0)