diff --git a/app/build.gradle b/app/build.gradle index 628d41b7ab..da35bd40a7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -130,4 +130,8 @@ dependencies { compile 'com.google.android.gms:play-services-wearable:7.5.0' compile 'junit:junit:4.12' testCompile 'org.json:json:20140107' + testCompile 'org.mockito:mockito-core:2.+' + androidTestCompile 'org.mockito:mockito-core:2.+' + androidTestCompile "com.google.dexmaker:dexmaker:1.2" + androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2" } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java index 6c23f035a0..0fb799c7cf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/SimpleProfile/SimpleProfileFragment.java @@ -11,13 +11,17 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.RadioButton; import com.squareup.otto.Subscribe; +import org.json.JSONArray; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.text.DecimalFormat; + import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventInitializationChanged; @@ -25,7 +29,9 @@ import info.nightscout.androidaps.interfaces.FragmentBase; import info.nightscout.androidaps.plugins.Careportal.Dialogs.NewNSTreatmentDialog; import info.nightscout.androidaps.plugins.Careportal.OptionsToShow; import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification; +import info.nightscout.utils.DecimalFormatter; import info.nightscout.utils.SafeParse; +import info.nightscout.utils.TimeListEdit; public class SimpleProfileFragment extends Fragment implements FragmentBase { private static Logger log = LoggerFactory.getLogger(SimpleProfileFragment.class); @@ -46,6 +52,8 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { EditText targetlowView; EditText targethighView; Button profileswitchButton; + TimeListEdit test; + JSONArray data = new JSONArray(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -61,6 +69,7 @@ public class SimpleProfileFragment extends Fragment implements FragmentBase { targetlowView = (EditText) layout.findViewById(R.id.simpleprofile_targetlow); targethighView = (EditText) layout.findViewById(R.id.simpleprofile_targethigh); profileswitchButton = (Button) layout.findViewById(R.id.simpleprofile_profileswitch); + test = new TimeListEdit(getContext(), layout, R.id.simpleprofile_test, "Test", data, "ic1", null, new DecimalFormat("0.00")); onStatusEvent(null); diff --git a/app/src/main/java/info/nightscout/utils/TimeListEdit.java b/app/src/main/java/info/nightscout/utils/TimeListEdit.java index b77099cb50..ae828b4ecf 100644 --- a/app/src/main/java/info/nightscout/utils/TimeListEdit.java +++ b/app/src/main/java/info/nightscout/utils/TimeListEdit.java @@ -1,17 +1,31 @@ package info.nightscout.utils; import android.content.Context; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; +import android.widget.Spinner; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.text.DateFormat; +import java.text.NumberFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; + +import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; /** @@ -19,60 +33,181 @@ import info.nightscout.androidaps.R; */ public class TimeListEdit { + private static Logger log = LoggerFactory.getLogger(TimeListEdit.class); LinearLayout layout; Context context; View view; int resLayoutId; + String label; JSONArray data; - boolean per30min = false; + NumberFormat formatter; String array1; String array2; - public TimeListEdit(Context context, View view, int resLayoutId, JSONArray data, String array1, String array2, boolean per30min) { + public TimeListEdit(Context context, View view, int resLayoutId, String label, JSONArray data, String array1, String array2, NumberFormat formatter) { this.context = context; this.view = view; this.resLayoutId = resLayoutId; + this.label = label; this.data = data; this.array1 = array1; this.array2 = array2; - this.per30min = per30min; + this.formatter = formatter; buildView(); } private void buildView() { - /* LayoutInflater inflater = LayoutInflater.from(context); layout = (LinearLayout) view.findViewById(resLayoutId); - final EditText[] editTexts = new EditText[24]; + layout.removeAllViews(); - for (int i = 0; i < 24; i++) { + TextView textlabel = new TextView(context); + textlabel.setText(label); + textlabel.setGravity(Gravity.LEFT); + LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); + llp.setMargins(10, 0, 0, 0); // llp.setMargins(left, top, right, bottom); + textlabel.setLayoutParams(llp); + layout.addView(textlabel); + + for (int i = 0; i < itemsCount(); i++) { View childview = inflater.inflate(R.layout.timelistedit_element, layout, false); - ((TextView) childview.findViewById(R.id.basal_time_elem)).setText((i < 10 ? "0" : "") + i + ":00: "); + final Spinner timeSpinner = (Spinner) childview.findViewById(R.id.timelistedit_time); + int previous = i == 0 ? -1 * 60 * 60 : secondFromMidnight(i - 1); + int next = i == itemsCount() - 1 ? 24 * 60 * 60 : secondFromMidnight(i + 1); + if (i == 0) next = 60 * 60; + fillSpinner(timeSpinner, secondFromMidnight(i), previous, next); - ImageView copyprevbutton = (ImageView) childview.findViewById(R.id.basal_copyprev_elem); - - if (i == 0) { - copyprevbutton.setVisibility(View.INVISIBLE); - ; - } else { - final int j = i; //needs to be final to be passed to inner class. - copyprevbutton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - editTexts[j].setText(editTexts[j - 1].getText()); - } - }); + final EditText editText1 = (EditText) childview.findViewById(R.id.timelistedit_edit1); + fillNumber(editText1, value1(i)); + final EditText editText2 = ((EditText) childview.findViewById(R.id.timelistedit_edit2)); + fillNumber(editText2, value2(i)); + if (array2 == null) { + editText2.setVisibility(View.GONE); } - editTexts[i] = ((EditText) childview.findViewById(R.id.basal_edittext_elem)); - //editTexts[i].setText(DecimalFormatter.to2Decimal(values[i])); - layout.addView(childview); + ImageView addbutton = (ImageView) childview.findViewById(R.id.timelistedit_add); + ImageView removebutton = (ImageView) childview.findViewById(R.id.timelistedit_remove); + + + final int fixedPos = i; + addbutton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + addItem(fixedPos, 0, 0, 0); + log(); + buildView(); + } + }); + + removebutton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + removeItem(fixedPos); + log(); + buildView(); + } + }); + + timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView parent, View view, int position, long id) { + int seconds = DateUtil.toSeconds(timeSpinner.getSelectedItem().toString()); + editItem(fixedPos, seconds, value1(fixedPos), value2(fixedPos)); + log(); + buildView(); + } + + @Override + public void onNothingSelected(AdapterView parent) { + editItem(fixedPos, 0, value1(fixedPos), value2(fixedPos)); + log(); + buildView(); + } + } + ); + + editText1.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + editItem(fixedPos, secondFromMidnight(fixedPos), SafeParse.stringToDouble(editText1.getText().toString()), value2(fixedPos)); + log(); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + } + }); + + editText2.addTextChangedListener(new TextWatcher() { + @Override + public void afterTextChanged(Editable s) { + editItem(fixedPos, secondFromMidnight(fixedPos), value1(fixedPos), SafeParse.stringToDouble(editText2.getText().toString())); + log(); + } + + @Override + public void beforeTextChanged(CharSequence s, int start, + int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, + int before, int count) { + } + }); + + layout.addView(childview); } - */ + + if (!(itemsCount() > 0 && secondFromMidnight(itemsCount() - 1) == 23 * 60 * 60)) { + ImageView imageView = new ImageView(context); + imageView.setImageResource(R.drawable.add); + layout.addView(imageView); + imageView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + addItem(itemsCount(), itemsCount() > 0 ? secondFromMidnight(itemsCount() - 1) + 60 * 60 : 0, 0, 0); + log(); + buildView(); + } + }); + } + + } + + public void fillSpinner(Spinner spinner, int secondsFromMidnight, int previous, int next) { + int posInList = 0; + ArrayList timeList = new ArrayList<>(); + DateFormat df = new SimpleDateFormat("HH:mm"); + int pos = 0; + for (int t = previous + 60 * 60; t < next; t += 60 * 60) { + timeList.add(df.format(DateUtil.toDate(t))); + if (secondsFromMidnight == t) posInList = pos; + pos++; + } + + ArrayAdapter adapter = new ArrayAdapter(context, + android.R.layout.simple_spinner_item, timeList); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + spinner.setAdapter(adapter); + spinner.setSelection(posInList, false); + } + + public void fillNumber(EditText edit, Double value) { + if (value.equals(0d)) + edit.setText(""); + else + edit.setText(formatter.format(value)); } public int itemsCount() { @@ -151,4 +286,12 @@ public class TimeListEdit { public void removeItem(int index) { data.remove(index); } + + void log() { + DateFormat df = new SimpleDateFormat("HH:mm"); + for (int i = 0; i < data.length(); i++) { + int pos = 0; + log.debug(i + ": " + df.format(DateUtil.toDate(secondFromMidnight(i))) + " " + array1 + ": " + value1(i) + (array2 != null ? " " + array2 + ": " + value2(i) : "")); + } + } } diff --git a/app/src/main/res/layout/simpleprofile_fragment.xml b/app/src/main/res/layout/simpleprofile_fragment.xml index ad1022b3f0..f006a646d3 100644 --- a/app/src/main/res/layout/simpleprofile_fragment.xml +++ b/app/src/main/res/layout/simpleprofile_fragment.xml @@ -28,16 +28,12 @@ android:id="@+id/simpleprofile_mgdl" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_column="2" - android:layout_weight="1" android:text="@string/mgdl" /> @@ -45,7 +41,6 @@ @@ -53,7 +48,6 @@ android:id="@+id/simpleprofile_dia" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_column="2" android:inputType="numberDecimal" /> @@ -152,6 +138,13 @@ android:paddingRight="10dp" android:text="@string/send_to_pump" android:textColor="@color/colorProfileSwitchButton" /> + + + diff --git a/app/src/main/res/layout/timelistedit_element.xml b/app/src/main/res/layout/timelistedit_element.xml index 1cb652308e..e6b9415476 100644 --- a/app/src/main/res/layout/timelistedit_element.xml +++ b/app/src/main/res/layout/timelistedit_element.xml @@ -2,7 +2,6 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" - android:background="@color/cardColorBackground" android:gravity="center_horizontal" android:orientation="horizontal" android:paddingTop="5dp" @@ -34,7 +33,7 @@ android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="15dp" - tools:src="@drawable/add" /> + android:src="@drawable/add" /> + android:src="@drawable/remove" /> diff --git a/app/src/test/java/info/nightscout/utils/TimeListEditTest.java b/app/src/test/java/info/nightscout/utils/TimeListEditTest.java index f7c3767a65..a7d2359f1d 100644 --- a/app/src/test/java/info/nightscout/utils/TimeListEditTest.java +++ b/app/src/test/java/info/nightscout/utils/TimeListEditTest.java @@ -1,21 +1,29 @@ package info.nightscout.utils; +import android.content.Context; +import android.view.LayoutInflater; + import org.json.JSONArray; import org.junit.Test; +import org.mockito.Mock; import java.lang.reflect.Method; +import java.text.DecimalFormat; import static org.junit.Assert.*; +import static org.mockito.Mockito.when; /** * Created by mike on 30.12.2016. */ public class TimeListEditTest { +/* JSONArray data = new JSONArray(); JSONArray data2 = new JSONArray(); - TimeListEdit tle = new TimeListEdit(null, null, 0, data, "ic", null, false); - TimeListEdit tle2 = new TimeListEdit(null, null, 0, data2, "ic", "ic2", false); + TimeListEdit tle = new TimeListEdit(null, null, 0, "Test1", data, "ic", null, new DecimalFormat("0.00")); + TimeListEdit tle2 = new TimeListEdit(null, null, 0, "Test2", data2, "ic", "ic2", new DecimalFormat("0.00")); + @Test public void doArrayTest() throws Exception { @@ -41,4 +49,5 @@ public class TimeListEditTest { assertEquals(2d, tle2.value2(0), 0.00001d); } +*/ } \ No newline at end of file