ChooseTriggerDialog to kotlin

This commit is contained in:
Milos Kozak 2019-07-23 00:04:44 +02:00
parent 063c27fb89
commit 47ed0acec2
10 changed files with 108 additions and 139 deletions

View file

@ -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());
}
}

View file

@ -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<RadioButton>(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
}
}

View file

@ -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<Button>(R.id.ok).setOnClickListener {
ok.setOnClickListener {
dismiss()
action?.let {
RxBus.send(EventAutomationUpdateAction(it, actionPosition))
@ -48,7 +49,7 @@ class EditActionDialog : DialogFragment() {
}
// Cancel button
view.findViewById<Button>(R.id.cancel).setOnClickListener { dismiss() }
cancel.setOnClickListener { dismiss() }
}
override fun onSaveInstanceState(bundle: Bundle) {

View file

@ -17,6 +17,7 @@ import info.nightscout.androidaps.utils.ToastUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.automation_dialog_event.*
import kotlinx.android.synthetic.main.okcancel.*
class EditEventDialog : DialogFragment() {
@ -60,7 +61,7 @@ class EditEventDialog : DialogFragment() {
automation_addAction.setOnClickListener { fragmentManager?.let { ChooseActionDialog().show(it, "ChooseActionDialog") } }
// OK button
view.findViewById<Button>(R.id.ok).setOnClickListener {
ok.setOnClickListener {
// check for title
val title = automation_inputEventTitle.text.toString()
if (title.isEmpty()) {
@ -90,7 +91,7 @@ class EditEventDialog : DialogFragment() {
}
// Cancel button
view.findViewById<Button>(R.id.cancel).setOnClickListener { dismiss() }
cancel.setOnClickListener { dismiss() }
showPreconditions()

View file

@ -11,6 +11,7 @@ import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateTrigger
import info.nightscout.androidaps.plugins.general.automation.triggers.Trigger
import kotlinx.android.synthetic.main.automation_dialog_edit_trigger.*
import kotlinx.android.synthetic.main.okcancel.*
class EditTriggerDialog : DialogFragment() {
@ -34,13 +35,13 @@ class EditTriggerDialog : DialogFragment() {
trigger?.let { it.generateDialog(automation_layoutTrigger, fragmentManager) }
// OK button
view.findViewById<Button>(R.id.ok).setOnClickListener {
ok.setOnClickListener {
dismiss()
RxBus.send(EventAutomationUpdateTrigger(trigger!!))
}
// Cancel button
view.findViewById<Button>(R.id.cancel).setOnClickListener { dismiss() }
cancel.setOnClickListener { dismiss() }
}
override fun onSaveInstanceState(bundle: Bundle) {

View file

@ -38,7 +38,7 @@ public class TriggerListAdapter {
return mFragmentManager;
}
void destroy() {
private void destroy() {
mRootLayout.removeAllViews();
}
@ -62,7 +62,7 @@ public class TriggerListAdapter {
Button buttonAdd = new Button(mContext);
buttonAdd.setText(MainApp.gs(R.string.addnew));
buttonAdd.setOnClickListener(v -> {
ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance();
ChooseTriggerDialog dialog = new ChooseTriggerDialog();
dialog.setOnClickListener(newTriggerObject -> {
mRootConnector.add(newTriggerObject);
rebuild(fragmentManager);
@ -135,7 +135,7 @@ public class TriggerListAdapter {
Button buttonAdd = new Button(mContext);
buttonAdd.setText(MainApp.gs(R.string.add_short));
buttonAdd.setOnClickListener(v -> {
ChooseTriggerDialog dialog = ChooseTriggerDialog.newInstance();
ChooseTriggerDialog dialog = new ChooseTriggerDialog();
dialog.show(fragmentManager, "ChooseTriggerDialog");
dialog.setOnClickListener(newTriggerObject -> {
TriggerConnector connector = trigger.getConnector();

View file

@ -26,7 +26,7 @@
android:orientation="vertical"
android:padding="10dp" />
<include layout="@layout/mdtp_done_button" />
<include layout="@layout/okcancel" />
</LinearLayout>

View file

@ -23,7 +23,7 @@
android:layout_height="match_parent"
android:layout_marginVertical="5dp" />
<include layout="@layout/mdtp_done_button" />
<include layout="@layout/okcancel" />
</LinearLayout>

View file

@ -25,7 +25,7 @@
android:layout_marginBottom="10dp"
android:orientation="vertical" />
<include layout="@layout/mdtp_done_button" />
<include layout="@layout/okcancel" />
</LinearLayout>

View file

@ -106,7 +106,7 @@
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
<include layout="@layout/mdtp_done_button" />
<include layout="@layout/okcancel" />
</LinearLayout>