Skip to content

Commit

Permalink
user can pick a date for expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
gigiyy committed Oct 12, 2018
1 parent ae9b3b4 commit f96f29a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package it.trade.android.japanapp.ui.orderinput

import android.app.DatePickerDialog
import android.app.Dialog
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.util.Log
import android.widget.DatePicker
import java.util.*

class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {

private lateinit var cb: (String) -> Unit

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
return DatePickerDialog(activity!!, this, year, month, day)
}

fun setDateSetPickerCallBack(cb: (String) -> Unit) {
this.cb = cb
}

override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
Log.d("OrderInputDatePicker", "picked a date: $year - ${month + 1} - $dayOfMonth")
cb(String.format("%d%02d%02d", year, month + 1, dayOfMonth))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ class OrderInputFragment : Fragment() {
viewModel.setExpiry(OrderExpiry.WEEK)
}
btTillDate.setOnClickListener {
viewModel.setExpiry(OrderExpiry.TILL_DATE)
btTillDate.isChecked = !btTillDate.isChecked
val datePicker = DatePickerFragment()
datePicker.setDateSetPickerCallBack { date ->
Log.d(TAG, "picked $date")
viewModel.setTillDate(date)
}
datePicker.show(activity!!.supportFragmentManager, "OrderInputDatePicker")
}
btSession.setOnClickListener {
// the button status should only updated by the viewModel instead of the click event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ class OrderInputViewModel(private val symbol: String) : ViewModel() {
}
}
}

fun setTillDate(date: String) {
orderForm.value = orderForm.value?.apply {
orderInfo = orderInfo.copy(expiry = OrderExpiry.TILL_DATE, expiryDate = date)
}
}
}

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

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

data class OrderInfo(val quantity: Int, val limitPrice: Double, val type: OrderType,
val expiry: OrderExpiry, val accountType: AccountType)
val expiry: OrderExpiry, val expiryDate: String, val accountType: AccountType)

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

Expand Down

0 comments on commit f96f29a

Please sign in to comment.