Skip to content

Commit f96f29a

Browse files
committed
user can pick a date for expiry
1 parent ae9b3b4 commit f96f29a

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package it.trade.android.japanapp.ui.orderinput
2+
3+
import android.app.DatePickerDialog
4+
import android.app.Dialog
5+
import android.os.Bundle
6+
import android.support.v4.app.DialogFragment
7+
import android.util.Log
8+
import android.widget.DatePicker
9+
import java.util.*
10+
11+
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
12+
13+
private lateinit var cb: (String) -> Unit
14+
15+
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
16+
val c = Calendar.getInstance()
17+
val year = c.get(Calendar.YEAR)
18+
val month = c.get(Calendar.MONTH)
19+
val day = c.get(Calendar.DAY_OF_MONTH)
20+
return DatePickerDialog(activity!!, this, year, month, day)
21+
}
22+
23+
fun setDateSetPickerCallBack(cb: (String) -> Unit) {
24+
this.cb = cb
25+
}
26+
27+
override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
28+
Log.d("OrderInputDatePicker", "picked a date: $year - ${month + 1} - $dayOfMonth")
29+
cb(String.format("%d%02d%02d", year, month + 1, dayOfMonth))
30+
}
31+
}
32+

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ class OrderInputFragment : Fragment() {
139139
viewModel.setExpiry(OrderExpiry.WEEK)
140140
}
141141
btTillDate.setOnClickListener {
142-
viewModel.setExpiry(OrderExpiry.TILL_DATE)
142+
btTillDate.isChecked = !btTillDate.isChecked
143+
val datePicker = DatePickerFragment()
144+
datePicker.setDateSetPickerCallBack { date ->
145+
Log.d(TAG, "picked $date")
146+
viewModel.setTillDate(date)
147+
}
148+
datePicker.show(activity!!.supportFragmentManager, "OrderInputDatePicker")
143149
}
144150
btSession.setOnClickListener {
145151
// the button status should only updated by the viewModel instead of the click event

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

+8-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ class OrderInputViewModel(private val symbol: String) : ViewModel() {
140140
}
141141
}
142142
}
143+
144+
fun setTillDate(date: String) {
145+
orderForm.value = orderForm.value?.apply {
146+
orderInfo = orderInfo.copy(expiry = OrderExpiry.TILL_DATE, expiryDate = date)
147+
}
148+
}
143149
}
144150

145151
class OrderInputViewModelFactory(private val symbol: String) : ViewModelProvider.NewInstanceFactory() {
@@ -156,6 +162,7 @@ class OrderForm(val symbol: JapanSymbol, val buyingPower: BuyingPower, val avail
156162
limitPrice = symbol.price,
157163
type = OrderType.LIMIT,
158164
expiry = OrderExpiry.DAY,
165+
expiryDate = "",
159166
accountType = AccountType.SPECIFIC
160167
)
161168

@@ -210,7 +217,7 @@ data class JapanSymbol(val name: String, val symbol: String, val exchange: Strin
210217
val lotSize: Int)
211218

212219
data class OrderInfo(val quantity: Int, val limitPrice: Double, val type: OrderType,
213-
val expiry: OrderExpiry, val accountType: AccountType)
220+
val expiry: OrderExpiry, val expiryDate: String, val accountType: AccountType)
214221

215222
data class BuyingPower(val availableCash: Double, val availableNisaLimit: Double)
216223

0 commit comments

Comments
 (0)