@@ -40,11 +40,11 @@ class CurrencyFragment : Fragment() {
40
40
}
41
41
42
42
private val currencies = ArrayList <String >()
43
- private var currenciesAdapter: ArrayAdapter <String >? = null
44
- private var currencyFrom: String? = null
45
- private var currencyTo: String? = null
43
+ private lateinit var currenciesAdapter: ArrayAdapter <String >
44
+ private lateinit var currencyFrom: String
45
+ private lateinit var currencyTo: String
46
46
47
- private var currencyViewModel: CurrencyViewModel ? = null
47
+ private lateinit var currencyViewModel: CurrencyViewModel
48
48
49
49
override fun onCreate (savedInstanceState : Bundle ? ) {
50
50
super .onCreate(savedInstanceState)
@@ -64,8 +64,8 @@ class CurrencyFragment : Fragment() {
64
64
65
65
private fun initViewModel () {
66
66
currencyViewModel = ViewModelProviders .of(this ).get(CurrencyViewModel ::class .java)
67
- currencyViewModel? .let { lifecycle.addObserver(it) }
68
- currencyViewModel? .initLocalCurrencies()
67
+ currencyViewModel.let { lifecycle.addObserver(it) }
68
+ currencyViewModel.initLocalCurrencies()
69
69
}
70
70
71
71
private fun initUI () {
@@ -74,12 +74,12 @@ class CurrencyFragment : Fragment() {
74
74
}
75
75
76
76
private fun populateSpinnerAdapter () {
77
- currencyViewModel? .loadCurrencyList()?.observe(this , Observer { currencyList ->
78
- currencyList!! .forEach {
77
+ currencyViewModel.loadCurrencyList()?.observe(this , Observer { currencyList ->
78
+ currencyList? .forEach {
79
79
currencies.add(it.code + " " + it.country)
80
80
}
81
- currenciesAdapter!! .setDropDownViewResource(R .layout.item_spinner);
82
- currenciesAdapter!! .notifyDataSetChanged()
81
+ currenciesAdapter.setDropDownViewResource(R .layout.item_spinner)
82
+ currenciesAdapter.notifyDataSetChanged()
83
83
})
84
84
85
85
}
@@ -102,11 +102,13 @@ class CurrencyFragment : Fragment() {
102
102
val quantity = currency_edit.text.toString()
103
103
currencyFrom = getCurrencyCode(from_currency_spinner.selectedItem.toString())
104
104
currencyTo = getCurrencyCode(to_currency_spinner.selectedItem.toString())
105
- val currencies = currencyFrom + " , " + currencyTo
105
+ val currencies = " $currencyFrom , $ currencyTo"
106
106
107
107
if (quantity.isNotEmpty() && currencyFrom != currencyTo) {
108
- currencyViewModel?.getAvailableExchange(currencies)?.observe(this , Observer { availableExchange ->
109
- exchange(quantity.toDouble(), availableExchange!! .availableExchangesMap)
108
+ currencyViewModel.getAvailableExchange(currencies)?.observe(this , Observer { availableExchange ->
109
+ availableExchange?.run {
110
+ exchange(quantity.toDouble(), availableExchangesMap)
111
+ }
110
112
})
111
113
112
114
} else {
@@ -133,11 +135,10 @@ class CurrencyFragment : Fragment() {
133
135
}
134
136
135
137
private fun showResult (result : String ) {
136
- val builder: AlertDialog .Builder
137
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
138
- builder = AlertDialog .Builder (context!! , R .style.AppCompatAlertDialogStyle )
138
+ val builder: AlertDialog .Builder = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
139
+ AlertDialog .Builder (context!! , R .style.AppCompatAlertDialogStyle )
139
140
} else {
140
- builder = AlertDialog .Builder (context!! )
141
+ AlertDialog .Builder (context!! )
141
142
}
142
143
143
144
val setMessage = TextView (activity)
0 commit comments