fix Action instantiation

This commit is contained in:
Milos Kozak 2020-01-03 23:26:26 +01:00
parent 0ea47a333c
commit 23f5d29e6a

View file

@ -5,6 +5,7 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.RadioButton import android.widget.RadioButton
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R import info.nightscout.androidaps.R
import info.nightscout.androidaps.dialogs.DialogFragmentWithDate import info.nightscout.androidaps.dialogs.DialogFragmentWithDate
import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.bus.RxBusWrapper
@ -18,6 +19,7 @@ import javax.inject.Inject
class ChooseActionDialog : DialogFragmentWithDate() { class ChooseActionDialog : DialogFragmentWithDate() {
@Inject lateinit var automationPlugin: AutomationPlugin @Inject lateinit var automationPlugin: AutomationPlugin
@Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var rxBus: RxBusWrapper
@Inject lateinit var mainApp : MainApp
private var checkedIndex = -1 private var checkedIndex = -1
@ -38,7 +40,7 @@ class ChooseActionDialog : DialogFragmentWithDate() {
for (a in automationPlugin.getActionDummyObjects()) { for (a in automationPlugin.getActionDummyObjects()) {
val radioButton = RadioButton(context) val radioButton = RadioButton(context)
radioButton.setText(a.friendlyName()) radioButton.setText(a.friendlyName())
radioButton.tag = a.javaClass radioButton.tag = a.javaClass.name
automation_radioGroup.addView(radioButton) automation_radioGroup.addView(radioButton)
} }
@ -61,15 +63,16 @@ class ChooseActionDialog : DialogFragmentWithDate() {
private fun instantiateAction(): Action? { private fun instantiateAction(): Action? {
return getActionClass()?.let { return getActionClass()?.let {
it.newInstance() as Action val clazz = Class.forName(it).kotlin
clazz.constructors.first().call(mainApp) as Action
} }
} }
private fun getActionClass(): Class<*>? { private fun getActionClass(): String? {
val radioButtonID = automation_radioGroup.checkedRadioButtonId val radioButtonID = automation_radioGroup.checkedRadioButtonId
val radioButton = automation_radioGroup.findViewById<RadioButton>(radioButtonID) val radioButton = automation_radioGroup.findViewById<RadioButton>(radioButtonID)
return radioButton?.let { return radioButton?.let {
it.tag as Class<*> it.tag as String
} }
} }