fix EditQuickWizardDialog, -> kotlin

This commit is contained in:
Milos Kozak 2019-10-01 17:44:05 +02:00
parent ee26795d97
commit e93b05c73e
5 changed files with 128 additions and 253 deletions

View file

@ -54,7 +54,7 @@ class QuickWizardListActivity : NoSplashAppCompatActivity() {
editButton.setOnClickListener {
val manager = fragmentManager
val editQuickWizardDialog = EditQuickWizardDialog()
editQuickWizardDialog.setData(QuickWizard[adapterPosition])
editQuickWizardDialog.entry = QuickWizard[adapterPosition]
editQuickWizardDialog.show(manager, "EditQuickWizardDialog")
}
removeButton.setOnClickListener {

View file

@ -1,216 +0,0 @@
package info.nightscout.androidaps.plugins.general.overview.dialogs;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import androidx.fragment.app.DialogFragment;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.QuickWizard;
import info.nightscout.androidaps.data.QuickWizardEntry;
import info.nightscout.androidaps.plugins.bus.RxBus;
import info.nightscout.androidaps.plugins.general.overview.events.EventQuickWizardChange;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.SafeParse;
public class EditQuickWizardDialog extends DialogFragment implements View.OnClickListener {
private static Logger log = LoggerFactory.getLogger(EditQuickWizardDialog.class);
QuickWizardEntry entry = QuickWizard.INSTANCE.newEmptyItem();
EditText buttonEdit;
EditText carbsEdit;
Spinner fromSpinner;
Spinner toSpinner;
Spinner useBGSpinner;
Spinner useCOBSpinner;
Spinner useBolusIOBSpinner;
Spinner useBasalIOBSpinner;
Spinner useTrendSpinner;
Spinner useSuperBolusSpinner;
Spinner useTempTargetSpinner;
public EditQuickWizardDialog() {
}
public void setData(QuickWizardEntry data) {
entry = data;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
View view = inflater.inflate(R.layout.overview_editquickwizard_dialog, container, false);
buttonEdit = view.findViewById(R.id.overview_editquickwizard_button_edit);
carbsEdit = view.findViewById(R.id.overview_editquickwizard_carbs_edit);
fromSpinner = view.findViewById(R.id.overview_editquickwizard_from_spinner);
toSpinner = view.findViewById(R.id.overview_editquickwizard_to_spinner);
useBGSpinner = view.findViewById(R.id.overview_editquickwizard_usebg_spinner);
useCOBSpinner = view.findViewById(R.id.overview_editquickwizard_usecob_spinner);
useBolusIOBSpinner = view.findViewById(R.id.overview_editquickwizard_usebolusiob_spinner);
useBasalIOBSpinner = view.findViewById(R.id.overview_editquickwizard_usebasaliob_spinner);
useTrendSpinner = view.findViewById(R.id.overview_editquickwizard_usetrend_spinner);
useSuperBolusSpinner = view.findViewById(R.id.overview_editquickwizard_usesuperbolus_spinner);
useTempTargetSpinner = view.findViewById(R.id.overview_editquickwizard_usetemptarget_spinner);
view.findViewById(R.id.ok).setOnClickListener(this);
view.findViewById(R.id.cancel).setOnClickListener(this);
int posFrom = 0;
int posTo = 95;
ArrayList<CharSequence> timeList = new ArrayList<>();
int pos = 0;
for (int t = 0; t < 24 * 60 * 60; t += 15 * 60) {
timeList.add(DateUtil.timeString(DateUtil.toDate(t)));
if (entry.validFrom() == t) posFrom = pos;
if (entry.validTo() == t) posTo = pos;
pos++;
}
timeList.add(DateUtil.timeString(DateUtil.toDate(24 * 60 * 60 - 60)));
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(),
R.layout.spinner_centered, timeList);
fromSpinner.setAdapter(adapter);
toSpinner.setAdapter(adapter);
buttonEdit.setText(entry.buttonText());
carbsEdit.setText(entry.carbs().toString());
fromSpinner.setSelection(posFrom);
toSpinner.setSelection(posTo);
setSelection(useBGSpinner, entry.useBG());
setSelection(useCOBSpinner, entry.useCOB());
setSelection(useBolusIOBSpinner, entry.useBolusIOB());
setSelection(useBasalIOBSpinner, entry.useBasalIOB());
setSelection(useTrendSpinner, entry.useTrend());
setSelection(useSuperBolusSpinner, entry.useSuperBolus());
setSelection(useTempTargetSpinner, entry.useTempTarget());
useCOBSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
processCob();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
processCob();
return view;
}
@Override
public void onResume() {
super.onResume();
if (getDialog() != null)
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ok:
if (fromSpinner.getSelectedItem() == null) return;
if (toSpinner.getSelectedItem() == null) return;
try {
entry.storage.put("buttonText", buttonEdit.getText().toString());
entry.storage.put("carbs", SafeParse.stringToInt(carbsEdit.getText().toString()));
int validFromInt = DateUtil.toSeconds(fromSpinner.getSelectedItem().toString());
entry.storage.put("validFrom", validFromInt);
int validToInt = DateUtil.toSeconds(toSpinner.getSelectedItem().toString());
entry.storage.put("validTo", validToInt);
entry.storage.put("useBG", getSelection(useBGSpinner));
entry.storage.put("useCOB", getSelection(useCOBSpinner));
entry.storage.put("useBolusIOB", getSelection(useBolusIOBSpinner));
entry.storage.put("useBasalIOB", getSelection(useBasalIOBSpinner));
entry.storage.put("useTrend", getSelection(useTrendSpinner));
entry.storage.put("useSuperBolus", getSelection(useSuperBolusSpinner));
entry.storage.put("useTempTarget", getSelection(useTempTargetSpinner));
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
QuickWizard.INSTANCE.addOrUpdate(entry);
dismiss();
RxBus.INSTANCE.send(new EventQuickWizardChange());
break;
case R.id.cancel:
dismiss();
break;
}
}
private void processCob() {
if (getSelection(useCOBSpinner) == QuickWizardEntry.YES) {
useBolusIOBSpinner.setEnabled(false);
useBasalIOBSpinner.setEnabled(false);
setSelection(useBolusIOBSpinner, QuickWizardEntry.YES);
setSelection(useBasalIOBSpinner, QuickWizardEntry.YES);
} else {
useBolusIOBSpinner.setEnabled(true);
useBasalIOBSpinner.setEnabled(true);
}
}
int getSelection(Spinner spinner) {
String value = spinner.getSelectedItem().toString();
if (value.equals(MainApp.gs(R.string.yes)))
return QuickWizardEntry.YES;
if (value.equals(MainApp.gs(R.string.no)))
return QuickWizardEntry.NO;
if (value.equals(MainApp.gs(R.string.positiveonly)))
return QuickWizardEntry.POSITIVE_ONLY;
if (value.equals(MainApp.gs(R.string.negativeonly)))
return QuickWizardEntry.NEGATIVE_ONLY;
return QuickWizardEntry.NO;
}
void setSelection(Spinner spinner, int value) {
String selection;
switch (value) {
case QuickWizardEntry.YES:
selection = MainApp.gs(R.string.yes);
break;
case QuickWizardEntry.NO:
selection = MainApp.gs(R.string.no);
break;
case QuickWizardEntry.POSITIVE_ONLY:
selection = MainApp.gs(R.string.positiveonly);
break;
case QuickWizardEntry.NEGATIVE_ONLY:
selection = MainApp.gs(R.string.negativeonly);
break;
default:
selection = MainApp.gs(R.string.no);
break;
}
for (int i = 0; i < spinner.getCount(); i++) {
if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(selection)) {
spinner.setSelection(i);
break;
}
}
}
}

View file

@ -0,0 +1,118 @@
package info.nightscout.androidaps.plugins.general.overview.dialogs
import android.os.Bundle
import android.view.*
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.fragment.app.DialogFragment
import info.nightscout.androidaps.R
import info.nightscout.androidaps.data.QuickWizard
import info.nightscout.androidaps.data.QuickWizardEntry
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.general.overview.events.EventQuickWizardChange
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.SafeParse
import kotlinx.android.synthetic.main.okcancel.*
import kotlinx.android.synthetic.main.overview_editquickwizard_dialog.*
import org.json.JSONException
import org.slf4j.LoggerFactory
import java.util.*
class EditQuickWizardDialog : DialogFragment() {
private val log = LoggerFactory.getLogger(EditQuickWizardDialog::class.java)
internal var entry = QuickWizard.newEmptyItem()
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
dialog?.window?.requestFeature(Window.FEATURE_NO_TITLE)
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
isCancelable = true
dialog?.setCanceledOnTouchOutside(false)
return inflater.inflate(R.layout.overview_editquickwizard_dialog, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
ok.setOnClickListener {
if (overview_editquickwizard_from_spinner.selectedItem == null) return@setOnClickListener
if (overview_editquickwizard_to_spinner.selectedItem == null) return@setOnClickListener
try {
entry.storage.put("buttonText", overview_editquickwizard_button_edit.text.toString())
entry.storage.put("carbs", SafeParse.stringToInt(overview_editquickwizard_carbs_edit.text.toString()))
val validFromInt = DateUtil.toSeconds(overview_editquickwizard_from_spinner.selectedItem.toString())
entry.storage.put("validFrom", validFromInt)
val validToInt = DateUtil.toSeconds(overview_editquickwizard_to_spinner.selectedItem.toString())
entry.storage.put("validTo", validToInt)
entry.storage.put("useBG", overview_editquickwizard_usebg_spinner.selectedItemPosition)
entry.storage.put("useCOB", overview_editquickwizard_usecob_spinner.selectedItemPosition)
entry.storage.put("useBolusIOB", overview_editquickwizard_usebolusiob_spinner.selectedItemPosition)
entry.storage.put("useBasalIOB", overview_editquickwizard_usebasaliob_spinner.selectedItemPosition)
entry.storage.put("useTrend", overview_editquickwizard_usetrend_spinner.selectedItemPosition)
entry.storage.put("useSuperBolus", overview_editquickwizard_usesuperbolus_spinner.selectedItemPosition)
entry.storage.put("useTempTarget", overview_editquickwizard_usetemptarget_spinner.selectedItemPosition)
} catch (e: JSONException) {
log.error("Unhandled exception", e)
}
QuickWizard.addOrUpdate(entry)
RxBus.send(EventQuickWizardChange())
dismiss()
}
cancel.setOnClickListener { dismiss() }
var posFrom = 0
var posTo = 95
val timeList = ArrayList<CharSequence>()
var pos = 0
var t = 0
while (t < 24 * 60 * 60) {
timeList.add(DateUtil.timeString(DateUtil.toDate(t)))
if (entry.validFrom() == t) posFrom = pos
if (entry.validTo() == t) posTo = pos
pos++
t += 15 * 60
}
timeList.add(DateUtil.timeString(DateUtil.toDate(24 * 60 * 60 - 60)))
val adapter = context?.let { context -> ArrayAdapter(context, R.layout.spinner_centered, timeList) }
overview_editquickwizard_from_spinner.adapter = adapter
overview_editquickwizard_to_spinner.adapter = adapter
overview_editquickwizard_button_edit.setText(entry.buttonText())
overview_editquickwizard_carbs_edit.setText(entry.carbs().toString())
overview_editquickwizard_from_spinner.setSelection(posFrom)
overview_editquickwizard_to_spinner.setSelection(posTo)
overview_editquickwizard_usebg_spinner.setSelection(entry.useBG())
overview_editquickwizard_usecob_spinner.setSelection(entry.useCOB())
overview_editquickwizard_usebolusiob_spinner.setSelection(entry.useBolusIOB())
overview_editquickwizard_usebasaliob_spinner.setSelection(entry.useBasalIOB())
overview_editquickwizard_usetrend_spinner.setSelection(entry.useTrend())
overview_editquickwizard_usesuperbolus_spinner.setSelection(entry.useSuperBolus())
overview_editquickwizard_usetemptarget_spinner.setSelection(entry.useTempTarget())
overview_editquickwizard_usecob_spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) = processCob()
override fun onNothingSelected(parent: AdapterView<*>) {}
}
processCob()
}
override fun onResume() {
super.onResume()
dialog?.window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
}
private fun processCob() {
if (overview_editquickwizard_usecob_spinner.selectedItemPosition == QuickWizardEntry.YES) {
overview_editquickwizard_usebolusiob_spinner.isEnabled = false
overview_editquickwizard_usebasaliob_spinner.isEnabled = false
overview_editquickwizard_usebolusiob_spinner.setSelection(QuickWizardEntry.YES)
overview_editquickwizard_usebasaliob_spinner.setSelection(QuickWizardEntry.YES)
} else {
overview_editquickwizard_usebolusiob_spinner.isEnabled = true
overview_editquickwizard_usebasaliob_spinner.isEnabled = true
}
}
}

View file

@ -76,7 +76,7 @@
android:id="@+id/overview_editquickwizard_usebg_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/quickWizardUseBGArray" />
android:entries="@array/quickWizardYesNo"/>
<TextView
android:layout_width="match_parent"
@ -88,7 +88,7 @@
android:id="@+id/overview_editquickwizard_usebolusiob_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/quickWizardUseBolusIOBArray" />
android:entries="@array/quickWizardYesNo"/>
<TextView
android:layout_width="match_parent"
@ -100,7 +100,7 @@
android:id="@+id/overview_editquickwizard_usebasaliob_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/quickWizardUseBasalOBArray" />
android:entries="@array/quickWizardYesNoPosNeg"/>
<TextView
android:layout_width="match_parent"
@ -112,7 +112,7 @@
android:id="@+id/overview_editquickwizard_usecob_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/quickWizardUseCOBArray" />
android:entries="@array/quickWizardYesNo"/>
<TextView
android:layout_width="match_parent"
@ -124,7 +124,7 @@
android:id="@+id/overview_editquickwizard_usetrend_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/quickWizardUseTrendArray" />
android:entries="@array/quickWizardYesNoPosNeg"/>
<TextView
android:layout_width="match_parent"
@ -136,7 +136,7 @@
android:id="@+id/overview_editquickwizard_usesuperbolus_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/quickWizardUseSuperBolusArray" />
android:entries="@array/quickWizardYesNo"/>
<TextView
android:layout_width="match_parent"
@ -148,7 +148,7 @@
android:id="@+id/overview_editquickwizard_usetemptarget_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/quickWizardUseTempTargetArray" />
android:entries="@array/quickWizardYesNo"/>
<include layout="@layout/mdtp_done_button" />

View file

@ -79,45 +79,18 @@
<item>2</item>
</string-array>
<string-array name="quickWizardUseBGArray">
<string-array name="quickWizardYesNo">
<item>@string/yes</item>
<item>@string/no</item>
</string-array>
<string-array name="quickWizardUseCOBArray">
<item>@string/yes</item>
<item>@string/no</item>
</string-array>
<string-array name="quickWizardUseBolusIOBArray">
<item>@string/yes</item>
<item>@string/no</item>
</string-array>
<string-array name="quickWizardUseBasalOBArray">
<string-array name="quickWizardYesNoPosNeg">
<item>@string/yes</item>
<item>@string/no</item>
<item>@string/positiveonly</item>
<item>@string/negativeonly</item>
</string-array>
<string-array name="quickWizardUseTrendArray">
<item>@string/no</item>
<item>@string/yes</item>
<item>@string/positiveonly</item>
<item>@string/negativeonly</item>
</string-array>
<string-array name="quickWizardUseSuperBolusArray">
<item>@string/no</item>
<item>@string/yes</item>
</string-array>
<string-array name="quickWizardUseTempTargetArray">
<item>@string/no</item>
<item>@string/yes</item>
</string-array>
<string-array name="location">
<item>@string/use_passive_location</item>
<item>@string/use_network_location</item>