From 063c27fb89d795b5e04a64cecde898ef2bbbbd33 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 22 Jul 2019 22:47:26 +0200 Subject: [PATCH] ChooseActionDialog to kotlin --- .../dialogs/ChooseActionDialog.java | 109 ------------------ .../automation/dialogs/ChooseActionDialog.kt | 85 ++++++++++++++ .../automation_dialog_choose_action.xml | 4 +- app/src/main/res/layout/okcancel.xml | 44 +++++++ 4 files changed, 131 insertions(+), 111 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.kt create mode 100644 app/src/main/res/layout/okcancel.xml diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java deleted file mode 100644 index 19504ec5d4..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java +++ /dev/null @@ -1,109 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.dialogs; - -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.RadioButton; -import android.widget.RadioGroup; - -import androidx.annotation.NonNull; -import androidx.fragment.app.DialogFragment; - -import butterknife.BindView; -import butterknife.ButterKnife; -import butterknife.OnClick; -import butterknife.Unbinder; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin; -import info.nightscout.androidaps.plugins.general.automation.actions.Action; -import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationAddAction; -import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui; - -public class ChooseActionDialog extends DialogFragment { - - private Unbinder mUnbinder; - - @BindView(R.id.radioGroup) - RadioGroup mRadioGroup; - - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.automation_dialog_choose_action, container, false); - mUnbinder = ButterKnife.bind(this, view); - - for (Action a : AutomationPlugin.INSTANCE.getActionDummyObjects()) { - RadioButton radioButton = new RadioButton(getContext()); - radioButton.setText(a.friendlyName()); - radioButton.setTag(a); - mRadioGroup.addView(radioButton); - } - - // restore checked radio button - int checkedIndex = 0; - if (savedInstanceState != null) { - checkedIndex = savedInstanceState.getInt("checkedIndex"); - } - - ((RadioButton) mRadioGroup.getChildAt(checkedIndex)).setChecked(true); - - return view; - } - - private int getCheckedIndex() { - for (int i = 0; i < mRadioGroup.getChildCount(); ++i) { - if (((RadioButton) mRadioGroup.getChildAt(i)).isChecked()) - return i; - } - return -1; - } - - private Class getActionClass() { - int radioButtonID = mRadioGroup.getCheckedRadioButtonId(); - RadioButton radioButton = mRadioGroup.findViewById(radioButtonID); - if (radioButton != null) { - Object tag = radioButton.getTag(); - if (tag instanceof Action) - return tag.getClass(); - } - return null; - } - - private Action instantiateAction() { - Class actionClass = getActionClass(); - if (actionClass != null) { - try { - return (Action) actionClass.newInstance(); - } catch (Exception e) { - e.printStackTrace(); - } - } - return null; - } - - - @Override - public void onDestroyView() { - mUnbinder.unbind(); - super.onDestroyView(); - } - - @OnClick(R.id.ok) - public void onButtonOk(View unused) { - dismiss(); - RxBus.INSTANCE.send(new EventAutomationAddAction(instantiateAction())); - RxBus.INSTANCE.send(new EventAutomationUpdateGui()); - } - - @OnClick(R.id.cancel) - public void onButtonCancel(View unused) { - dismiss(); - } - - @Override - public void onSaveInstanceState(Bundle bundle) { - bundle.putInt("checkedIndex", getCheckedIndex()); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.kt new file mode 100644 index 0000000000..079858ce43 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.kt @@ -0,0 +1,85 @@ +package info.nightscout.androidaps.plugins.general.automation.dialogs + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.RadioButton +import androidx.fragment.app.DialogFragment +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin +import info.nightscout.androidaps.plugins.general.automation.actions.Action +import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationAddAction +import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui +import kotlinx.android.synthetic.main.automation_dialog_choose_action.* +import kotlinx.android.synthetic.main.okcancel.* + +class ChooseActionDialog : DialogFragment() { + + var checkedIndex = -1 + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + // restore checked radio button + savedInstanceState?.let { bundle -> + checkedIndex = bundle.getInt("checkedIndex") + } + + dialog.setCanceledOnTouchOutside(false) + return inflater.inflate(R.layout.automation_dialog_choose_action, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + for (a in AutomationPlugin.getActionDummyObjects()) { + val radioButton = RadioButton(context) + radioButton.setText(a.friendlyName()) + radioButton.tag = a.javaClass + automation_radioGroup.addView(radioButton) + } + + if (checkedIndex != -1) + (automation_radioGroup.getChildAt(checkedIndex) as RadioButton).isChecked = true + + // OK button + ok.setOnClickListener { + dismiss() + instantiateAction()?.let { + RxBus.send(EventAutomationAddAction(it)) + RxBus.send(EventAutomationUpdateGui()) + } + } + + // Cancel button + cancel.setOnClickListener { dismiss() } + } + + override fun onSaveInstanceState(bundle: Bundle) { + bundle.putInt("checkedIndex", determineCheckedIndex()) + } + + private fun instantiateAction(): Action? { + return getActionClass()?.let { + it.newInstance() as Action + } + } + + private fun getActionClass(): Class<*>? { + val radioButtonID = automation_radioGroup.checkedRadioButtonId + val radioButton = automation_radioGroup.findViewById(radioButtonID) + return radioButton?.let { + it.tag as Class<*> + } + } + + private fun determineCheckedIndex(): Int { + for (i in 0 until automation_radioGroup.childCount) { + if ((automation_radioGroup.getChildAt(i) as RadioButton).isChecked) + return i + } + return -1 + } + +} diff --git a/app/src/main/res/layout/automation_dialog_choose_action.xml b/app/src/main/res/layout/automation_dialog_choose_action.xml index 5929736550..3545ff3174 100644 --- a/app/src/main/res/layout/automation_dialog_choose_action.xml +++ b/app/src/main/res/layout/automation_dialog_choose_action.xml @@ -18,12 +18,12 @@ android:text="@string/please_choose_an_action_type" /> - + diff --git a/app/src/main/res/layout/okcancel.xml b/app/src/main/res/layout/okcancel.xml new file mode 100644 index 0000000000..f1322fd566 --- /dev/null +++ b/app/src/main/res/layout/okcancel.xml @@ -0,0 +1,44 @@ + + + + +