@@ -5,17 +5,19 @@ import android.arch.lifecycle.ViewModelProviders
5
5
import android.opengl.Visibility
6
6
import android.os.Bundle
7
7
import android.support.v4.app.Fragment
8
+ import android.text.Editable
9
+ import android.text.TextWatcher
8
10
import android.view.LayoutInflater
9
11
import android.view.View
10
12
import android.view.ViewGroup
13
+ import android.widget.EditText
11
14
import it.trade.android.japanapp.R
12
15
import kotlinx.android.synthetic.main.order_input_fragment.*
13
16
import kotlinx.android.synthetic.main.order_input_fragment.view.*
14
17
15
18
class OrderInputFragment : Fragment () {
16
19
17
20
companion object {
18
- fun newInstance () = OrderInputFragment ()
19
21
fun newInstance (symbol : String ): OrderInputFragment {
20
22
val args = Bundle ()
21
23
args.putString(" symbol" , symbol)
@@ -26,18 +28,17 @@ class OrderInputFragment : Fragment() {
26
28
}
27
29
28
30
private lateinit var viewModel: OrderInputViewModel
31
+ private lateinit var symbol: String
29
32
30
33
override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? ,
31
34
savedInstanceState : Bundle ? ): View {
35
+ symbol = arguments?.getString(" symbol" ) ? : " 8703"
32
36
return inflater.inflate(R .layout.order_input_fragment, container, false )
33
37
}
34
38
35
39
override fun onActivityCreated (savedInstanceState : Bundle ? ) {
36
40
super .onActivityCreated(savedInstanceState)
37
- viewModel = ViewModelProviders .of(activity!! ).get(OrderInputViewModel ::class .java)
38
- arguments?.getString(" symbol" )?.let {
39
- viewModel.init (it)
40
- }
41
+ viewModel = ViewModelProviders .of(activity!! , OrderInputViewModelFactory (symbol)).get(OrderInputViewModel ::class .java)
41
42
viewModel.getOrderModel().observe(this , Observer { orderForm ->
42
43
orderForm?.run {
43
44
tvSymbolName.text = symbol.name
@@ -96,4 +97,16 @@ class OrderInputFragment : Fragment() {
96
97
}
97
98
}
98
99
100
+ fun EditText.onChange (cb : (String ) -> Unit ) {
101
+ this .addTextChangedListener(object : TextWatcher {
102
+ override fun afterTextChanged (s : Editable ? ) {
103
+ cb(s.toString())
104
+ }
105
+
106
+ override fun beforeTextChanged (s : CharSequence? , start : Int , count : Int , after : Int ) {}
107
+
108
+ override fun onTextChanged (s : CharSequence? , start : Int , before : Int , count : Int ) {}
109
+ })
110
+ }
111
+
99
112
}
0 commit comments