Skip to content

Commit ae9b3b4

Browse files
committed
user can pick account type
1 parent ab93f6a commit ae9b3b4

File tree

3 files changed

+62
-45
lines changed

3 files changed

+62
-45
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import android.util.Log
1010
import android.view.LayoutInflater
1111
import android.view.View
1212
import android.view.ViewGroup
13+
import android.widget.AdapterView
14+
import android.widget.ArrayAdapter
1315
import android.widget.EditText
1416
import android.widget.PopupMenu
1517
import it.trade.android.japanapp.R
@@ -31,6 +33,7 @@ class OrderInputFragment : Fragment() {
3133

3234
private lateinit var viewModel: OrderInputViewModel
3335
private lateinit var symbol: String
36+
private lateinit var accountAdapter: ArrayAdapter<String>
3437

3538
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
3639
savedInstanceState: Bundle?): View {
@@ -73,6 +76,20 @@ class OrderInputFragment : Fragment() {
7376
btTillDate.isChecked = orderInfo.expiry == OrderExpiry.TILL_DATE
7477
btTillDate.isEnabled = orderInfo.type == OrderType.LIMIT
7578
btSession.isChecked = orderInfo.expiry in listOf(OrderExpiry.OPENING, OrderExpiry.CLOSING, OrderExpiry.FUNARI)
79+
80+
val accountTypes = viewModel.getAvailabeAccountTypes()
81+
accountAdapter.clear()
82+
val selected = orderInfo.accountType
83+
Log.d(TAG, "current account type: $selected")
84+
for ((index, accountType) in accountTypes.withIndex()) {
85+
when (accountType) {
86+
AccountType.SPECIFIC -> accountAdapter.add(getString(R.string.specific))
87+
AccountType.GENERAL -> accountAdapter.add(getString(R.string.general))
88+
AccountType.NISA -> accountAdapter.add(getString(R.string.nisa))
89+
}
90+
if (accountType == selected) spAccount.setSelection(index)
91+
}
92+
7693
}
7794
})
7895
btQuantityPlus.setOnClickListener {
@@ -152,6 +169,18 @@ class OrderInputFragment : Fragment() {
152169
popup.setOnDismissListener { _ -> viewModel.dummyUpdate()}
153170
popup.show()
154171
}
172+
accountAdapter = ArrayAdapter<String>(activity!!, android.R.layout.simple_spinner_item).apply{
173+
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
174+
}
175+
spAccount.adapter = accountAdapter
176+
spAccount.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
177+
override fun onNothingSelected(parent: AdapterView<*>?) { }
178+
179+
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
180+
viewModel.setAccountType(position)
181+
}
182+
183+
}
155184

156185
}
157186

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ 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
98

109
private const val TAG = "OrderInputViewModel"
1110

@@ -18,11 +17,17 @@ class OrderInputViewModel(private val symbol: String) : ViewModel() {
1817
orderForm = MutableLiveData()
1918
orderForm.value = OrderForm(
2019
TradeItSDKHolder.getSymbolProvider().getJapanSymbol(symbol),
21-
TradeItSDKHolder.getBuyingPower())
20+
TradeItSDKHolder.getBuyingPower(),
21+
TradeItSDKHolder.getAccountTypes())
2222
}
2323
return orderForm
2424
}
2525

26+
fun getAvailabeAccountTypes():List<AccountType> {
27+
// Japan user should always has a GENERAL account type
28+
return orderForm.value?.availableAccounts ?: listOf(AccountType.GENERAL)
29+
}
30+
2631
fun increaseQuantity() {
2732
orderForm.value = orderForm.value?.apply {
2833
orderInfo = orderInfo.copy(quantity = orderInfo.quantity + symbol.lotSize)
@@ -126,6 +131,15 @@ class OrderInputViewModel(private val symbol: String) : ViewModel() {
126131
fun dummyUpdate() {
127132
orderForm.value = orderForm.value
128133
}
134+
135+
fun setAccountType(position: Int) {
136+
val accountType = getAvailabeAccountTypes()[position]
137+
if (accountType != orderForm.value?.orderInfo?.accountType) {
138+
orderForm.value = orderForm.value?.apply {
139+
orderInfo = orderInfo.copy(accountType = accountType)
140+
}
141+
}
142+
}
129143
}
130144

131145
class OrderInputViewModelFactory(private val symbol: String) : ViewModelProvider.NewInstanceFactory() {
@@ -136,7 +150,7 @@ class OrderInputViewModelFactory(private val symbol: String) : ViewModelProvider
136150

137151
}
138152

139-
class OrderForm(val symbol: JapanSymbol, val buyingPower: BuyingPower) {
153+
class OrderForm(val symbol: JapanSymbol, val buyingPower: BuyingPower, val availableAccounts: List<AccountType>) {
140154
var orderInfo: OrderInfo = OrderInfo(
141155
quantity = symbol.lotSize,
142156
limitPrice = symbol.price,
@@ -179,6 +193,10 @@ object TradeItSDKHolder {
179193
return SampleJapanSymbol()
180194
}
181195

196+
fun getAccountTypes(): List<AccountType> {
197+
return listOf(AccountType.SPECIFIC, AccountType.GENERAL, AccountType.NISA)
198+
}
199+
182200
// need broker/account
183201
fun getBuyingPower(): BuyingPower {
184202
return BuyingPower(500000.toDouble(), 100000.toDouble())

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

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
android:background="?android:attr/listDivider"
119119
app:layout_constraintEnd_toEndOf="parent"
120120
app:layout_constraintStart_toStartOf="parent"
121-
app:layout_constraintTop_toBottomOf="@+id/tvNisaLimit" />
121+
app:layout_constraintTop_toBottomOf="@+id/accountType" />
122122

123123
<TextView
124124
android:id="@+id/tvQuantityCaps"
@@ -415,7 +415,7 @@
415415
android:background="?android:attr/listDivider"
416416
app:layout_constraintEnd_toEndOf="parent"
417417
app:layout_constraintStart_toStartOf="parent"
418-
app:layout_constraintTop_toBottomOf="@+id/orderCondition" />
418+
app:layout_constraintTop_toBottomOf="@+id/tvNisaLimit" />
419419

420420
<TextView
421421
android:id="@+id/tvAccountCaps"
@@ -432,49 +432,19 @@
432432
android:layout_width="0dp"
433433
android:layout_height="wrap_content"
434434
android:orientation="vertical"
435+
android:gravity="end"
435436
app:layout_constraintEnd_toEndOf="@+id/quantityInput"
436437
app:layout_constraintStart_toStartOf="@+id/quantityInput"
437438
app:layout_constraintTop_toBottomOf="@+id/divider5">
438439

439-
<android.support.v7.widget.LinearLayoutCompat
440-
android:layout_width="match_parent"
441-
android:layout_height="wrap_content"
442-
android:layout_marginStart="8dp"
443-
android:layout_marginEnd="8dp">
444-
445-
<ToggleButton
446-
android:layout_width="wrap_content"
447-
android:layout_height="wrap_content"
448-
android:layout_weight="1"
449-
android:textOff="@string/specific"
450-
android:textOn="@string/specific" />
451-
452-
<ToggleButton
453-
android:layout_width="wrap_content"
454-
android:layout_height="wrap_content"
455-
android:layout_weight="1"
456-
android:textOff="@string/general"
457-
android:textOn="@string/general" />
458-
</android.support.v7.widget.LinearLayoutCompat>
459-
460-
<android.support.v7.widget.LinearLayoutCompat
461-
android:layout_width="match_parent"
462-
android:layout_height="wrap_content"
463-
android:layout_marginStart="8dp"
464-
android:layout_marginEnd="8dp">
465-
466-
<ToggleButton
467-
android:layout_width="0dp"
468-
android:layout_height="wrap_content"
469-
android:layout_weight="1"
470-
android:textOff="@string/nisa"
471-
android:textOn="@string/nisa" />
472-
473-
<TextView
474-
android:layout_width="wrap_content"
475-
android:layout_height="wrap_content"
476-
android:layout_weight="1" />
477-
</android.support.v7.widget.LinearLayoutCompat>
440+
<android.support.v7.widget.AppCompatSpinner
441+
android:id="@+id/spAccount"
442+
android:paddingTop="8dp"
443+
android:paddingStart="8dp"
444+
android:paddingEnd="8dp"
445+
android:layout_width="wrap_content"
446+
android:layout_height="wrap_content">
447+
</android.support.v7.widget.AppCompatSpinner>
478448
</android.support.v7.widget.LinearLayoutCompat>
479449

480450
<View
@@ -487,7 +457,7 @@
487457
android:background="?android:attr/listDivider"
488458
app:layout_constraintEnd_toEndOf="parent"
489459
app:layout_constraintStart_toStartOf="parent"
490-
app:layout_constraintTop_toBottomOf="@+id/accountType" />
460+
app:layout_constraintTop_toBottomOf="@+id/orderCondition" />
491461

492462
<Button
493463
android:layout_width="match_parent"

0 commit comments

Comments
 (0)