Skip to content

Commit

Permalink
use view model factory to create the OrderInputViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
gigiyy committed Oct 5, 2018
1 parent f27283a commit 5570a92
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import android.arch.lifecycle.ViewModelProviders
import android.opengl.Visibility
import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import it.trade.android.japanapp.R
import kotlinx.android.synthetic.main.order_input_fragment.*
import kotlinx.android.synthetic.main.order_input_fragment.view.*

class OrderInputFragment : Fragment() {

companion object {
fun newInstance() = OrderInputFragment()
fun newInstance(symbol: String): OrderInputFragment {
val args = Bundle()
args.putString("symbol", symbol)
Expand All @@ -26,18 +28,17 @@ class OrderInputFragment : Fragment() {
}

private lateinit var viewModel: OrderInputViewModel
private lateinit var symbol: String

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
symbol = arguments?.getString("symbol") ?: "8703"
return inflater.inflate(R.layout.order_input_fragment, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
viewModel = ViewModelProviders.of(activity!!).get(OrderInputViewModel::class.java)
arguments?.getString("symbol")?.let {
viewModel.init(it)
}
viewModel = ViewModelProviders.of(activity!!, OrderInputViewModelFactory(symbol)).get(OrderInputViewModel::class.java)
viewModel.getOrderModel().observe(this, Observer { orderForm ->
orderForm?.run {
tvSymbolName.text = symbol.name
Expand Down Expand Up @@ -96,4 +97,16 @@ class OrderInputFragment : Fragment() {
}
}

fun EditText.onChange(cb: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
cb(s.toString())
}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
})
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package it.trade.android.japanapp.ui.orderinput
import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.util.Log

class OrderInputViewModel : ViewModel() {
private const val TAG = "OrderInputViewModel"

class OrderInputViewModel(private val symbol: String) : ViewModel() {
private lateinit var orderForm: MutableLiveData<OrderForm>

fun getOrderModel(): LiveData<OrderForm> {
if (!this::orderForm.isInitialized) {
Log.d(TAG, "initialized.")
orderForm = MutableLiveData()
orderForm.value = OrderForm(
TradeItSDKHolder.getSymbolProvider().getJapanSymbol("8703"),
TradeItSDKHolder.getSymbolProvider().getJapanSymbol(symbol),
TradeItSDKHolder.getBuyingPower())
}
return orderForm
Expand Down Expand Up @@ -49,18 +54,6 @@ class OrderInputViewModel : ViewModel() {
}
}

fun init(symbol: String?) {
// TODO need a cleaner way to initialize the OrderForm
if (symbol != null) {
if (!this::orderForm.isInitialized || (this::orderForm.isInitialized && symbol != orderForm.value?.symbol?.symbol)) {
orderForm = MutableLiveData()
orderForm.value = OrderForm(
TradeItSDKHolder.getSymbolProvider().getJapanSymbol(symbol),
TradeItSDKHolder.getBuyingPower())
}
}
}

fun setMarketOrder() {
orderForm.value = orderForm.value?.apply {
orderInfo = orderInfo.copy(type = OrderType.MARKET, limitPrice = symbol.price)
Expand All @@ -74,6 +67,13 @@ class OrderInputViewModel : ViewModel() {
}
}

class OrderInputViewModelFactory(private val symbol: String) : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return OrderInputViewModel(symbol) as T
}

}

class OrderForm(val symbol: JapanSymbol, val buyingPower: BuyingPower) {
var orderInfo: OrderInfo = OrderInfo(
quantity = symbol.lotSize,
Expand Down

0 comments on commit 5570a92

Please sign in to comment.