diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java deleted file mode 100644 index 0a22261fbd..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.java +++ /dev/null @@ -1,126 +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.R; -import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin; -import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger; - -public class ChooseTriggerDialog extends DialogFragment { - - public interface OnClickListener { - void onClick(Trigger newTriggerObject); - } - - private Unbinder mUnbinder; - private OnClickListener mClickListener = null; - - @BindView(R.id.automation_chooseTriggerRadioGroup) - RadioGroup mRadioGroup; - - public static ChooseTriggerDialog newInstance() { - Bundle args = new Bundle(); - - ChooseTriggerDialog fragment = new ChooseTriggerDialog(); - fragment.setArguments(args); - - return fragment; - } - - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.automation_dialog_choose_trigger, container, false); - mUnbinder = ButterKnife.bind(this, view); - - for (Trigger t : AutomationPlugin.INSTANCE.getTriggerDummyObjects()) { - RadioButton radioButton = new RadioButton(getContext()); - radioButton.setText(t.friendlyName()); - radioButton.setTag(t); - 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 getTriggerClass() { - int radioButtonID = mRadioGroup.getCheckedRadioButtonId(); - RadioButton radioButton = mRadioGroup.findViewById(radioButtonID); - if (radioButton != null) { - Object tag = radioButton.getTag(); - if (tag instanceof Trigger) - return tag.getClass(); - } - return null; - } - - private Trigger instantiateTrigger() { - Class triggerClass = getTriggerClass(); - if (triggerClass != null) { - try { - return (Trigger) triggerClass.newInstance(); - } catch (Exception e) { - e.printStackTrace(); - } - } - return null; - } - - - public void setOnClickListener(OnClickListener clickListener) { - mClickListener = clickListener; - } - - @Override - public void onDestroyView() { - mUnbinder.unbind(); - super.onDestroyView(); - } - - @OnClick(R.id.ok) - @SuppressWarnings("unused") - public void onButtonOk(View view) { - if (mClickListener != null) - mClickListener.onClick(instantiateTrigger()); - - dismiss(); - } - - @OnClick(R.id.cancel) - @SuppressWarnings("unused") - public void onButtonCancel(View view) { - 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/ChooseTriggerDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.kt new file mode 100644 index 0000000000..a9f96f9de4 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseTriggerDialog.kt @@ -0,0 +1,92 @@ +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.general.automation.AutomationPlugin +import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger +import kotlinx.android.synthetic.main.automation_dialog_choose_trigger.* +import kotlinx.android.synthetic.main.okcancel.* + +class ChooseTriggerDialog : DialogFragment() { + + private var checkedIndex = -1 + + private var mClickListener: OnClickListener? = null + + interface OnClickListener { + fun onClick(newTriggerObject: Trigger?) + } + + 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_trigger, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + for (t in AutomationPlugin.getTriggerDummyObjects()) { + val radioButton = RadioButton(context) + radioButton.setText(t.friendlyName()) + radioButton.tag = t.javaClass + automation_chooseTriggerRadioGroup.addView(radioButton) + } + + if (checkedIndex != -1) + (automation_chooseTriggerRadioGroup.getChildAt(checkedIndex) as RadioButton).isChecked = true + + // OK button + ok.setOnClickListener { + dismiss() + if (mClickListener != null) + mClickListener!!.onClick(instantiateTrigger()) + + dismiss() + } + + // Cancel button + cancel.setOnClickListener { dismiss() } + } + + fun setOnClickListener(clickListener: OnClickListener) { + mClickListener = clickListener + } + + override fun onSaveInstanceState(bundle: Bundle) { + bundle.putInt("checkedIndex", determineCheckedIndex()) + } + + private fun instantiateTrigger(): Trigger? { + return getTriggerClass()?.let { + it.newInstance() as Trigger + } + } + + private fun getTriggerClass(): Class<*>? { + val radioButtonID = automation_chooseTriggerRadioGroup.checkedRadioButtonId + val radioButton = automation_chooseTriggerRadioGroup.findViewById(radioButtonID) + return radioButton?.let { + it.tag as Class<*> + } + } + + private fun determineCheckedIndex(): Int { + for (i in 0 until automation_chooseTriggerRadioGroup.childCount) { + if ((automation_chooseTriggerRadioGroup.getChildAt(i) as RadioButton).isChecked) + return i + } + return -1 + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt index 0ebc60837c..bb4ffcbebf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt @@ -11,6 +11,7 @@ import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.general.automation.actions.Action import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateAction import kotlinx.android.synthetic.main.automation_dialog_action.* +import kotlinx.android.synthetic.main.okcancel.* import org.json.JSONObject class EditActionDialog : DialogFragment() { @@ -40,7 +41,7 @@ class EditActionDialog : DialogFragment() { } // OK button - view.findViewById