now can add numerical habits

pull/415/head
Divya 7 years ago
parent bf8c14fc03
commit 4808941539

@ -20,11 +20,16 @@
package org.isoron.uhabits.activities.habits.edit; package org.isoron.uhabits.activities.habits.edit;
import android.content.*; import android.content.*;
import android.content.res.Resources;
import android.os.*; import android.os.*;
import android.support.annotation.*; import android.support.annotation.*;
import android.support.v7.app.*; import android.support.v7.app.*;
import android.text.format.*; import android.text.format.*;
import android.util.Log;
import android.view.*; import android.view.*;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;
import com.android.datetimepicker.time.*; import com.android.datetimepicker.time.*;
@ -180,7 +185,13 @@ public class EditHabitDialog extends AppCompatDialogFragment
habit.setFrequency(frequencyPanel.getFrequency()); habit.setFrequency(frequencyPanel.getFrequency());
habit.setUnit(targetPanel.getUnit()); habit.setUnit(targetPanel.getUnit());
habit.setTargetValue(targetPanel.getTargetValue()); habit.setTargetValue(targetPanel.getTargetValue());
habit.setType(type); // habit.setType(type); //removing
// adding below lines
Log.d("habit-typeNum",String.valueOf(habit.getType()));
if (targetPanel.getVisibility() == View.VISIBLE)
habit.setType(Habit.NUMBER_HABIT);
else
habit.setType(Habit.YES_NO_HABIT);
saveHabit(habit); saveHabit(habit);
dismiss(); dismiss();
@ -201,14 +212,74 @@ public class EditHabitDialog extends AppCompatDialogFragment
return habit; return habit;
} }
@BindView(R.id.spHabitType)
Spinner spHabitType;
@BindView(R.id.tvSpinnerHabitType)
TextView tvSpinnerHabitType;
@BindView(R.id.tvDescription)
ExampleEditText tvDescription;
private void setupHabitTypeController(Habit habit){
spHabitType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("spinner-position", String.valueOf(i));
Resources res = getResources();
if (i == Habit.NUMBER_HABIT){
habit.setType(Habit.NUMBER_HABIT);
}else {
habit.setType(Habit.YES_NO_HABIT);
}
if(i == Habit.NUMBER_HABIT)
tvDescription.setExample(res.getString(R.string.example_question_numerical));
else
tvDescription.setExample(res.getString(R.string.example_question_boolean));
// Copied from populateform()
if (habit.isNumerical()) {
frequencyPanel.setVisibility(GONE);
targetPanel.setVisibility(View.VISIBLE);
}
else {
targetPanel.setVisibility(GONE);
frequencyPanel.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
private void populateForm() private void populateForm()
{ {
Habit habit = modelFactory.buildHabit(); Habit habit = modelFactory.buildHabit();
habit.setFrequency(Frequency.DAILY); habit.setFrequency(Frequency.DAILY);
habit.setColor(prefs.getDefaultHabitColor(habit.getColor())); habit.setColor(prefs.getDefaultHabitColor(habit.getColor()));
habit.setType(getTypeFromArguments()); habit.setType(getTypeFromArguments());//commenting this line for test purpose
// habit.setType(Habit.NUMBER_HABIT);//To be deleted later
if (originalHabit != null) {//edit habit
Log.d("isEditHabit", "true");
habit.copyFrom(originalHabit);
//Hide the 'type of habit' question on edit habit
spHabitType.setVisibility(GONE);
tvSpinnerHabitType.setVisibility(GONE);
}
if (originalHabit != null) habit.copyFrom(originalHabit); //setting own habit controller
setupHabitTypeController(habit);
if (habit.isNumerical()) frequencyPanel.setVisibility(GONE); if (habit.isNumerical()) frequencyPanel.setVisibility(GONE);
else targetPanel.setVisibility(GONE); else targetPanel.setVisibility(GONE);

@ -54,6 +54,19 @@
</LinearLayout> </LinearLayout>
<TextView
android:id="@+id/tvSpinnerHabitType"
android:textColor="@color/black_a6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Type"/>
<Spinner
android:id="@+id/spHabitType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/habitType"
android:layout_marginBottom="10dp"/>
<android.support.design.widget.TextInputLayout <android.support.design.widget.TextInputLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">

@ -89,6 +89,10 @@
<item>@string/uncheck</item> <item>@string/uncheck</item>
<item>@string/toggle</item> <item>@string/toggle</item>
</string-array> </string-array>
<string-array name="habitType" translatable="false">
<item>With a simple yes or no</item>
<item>With a number</item>
</string-array>
<string-array name="strengthIntervalNames" translatable="false"> <string-array name="strengthIntervalNames" translatable="false">
<item>@string/day</item> <item>@string/day</item>

@ -187,10 +187,27 @@ public abstract class RepetitionList
@NonNull @NonNull
public synchronized Repetition toggle(Timestamp timestamp) public synchronized Repetition toggle(Timestamp timestamp)
{ {
if(habit.isNumerical()) Repetition rep = getByTimestamp(timestamp);
//Commenting below lines
if(habit.isNumerical()){
// TODO remove later
if (rep != null) remove(rep);
else
{
rep = new Repetition(timestamp, Checkmark.CHECKED_EXPLICITLY);
add(rep);
}
habit.invalidateNewerThan(timestamp);
// TODO remove later
// TODO write code to handle numerical habits
/*
throw new IllegalStateException("habit must NOT be numerical"); throw new IllegalStateException("habit must NOT be numerical");
handle numeric habit here
*/
}else {
Repetition rep = getByTimestamp(timestamp);
if (rep != null) remove(rep); if (rep != null) remove(rep);
else else
{ {
@ -199,6 +216,7 @@ public abstract class RepetitionList
} }
habit.invalidateNewerThan(timestamp); habit.invalidateNewerThan(timestamp);
}
return rep; return rep;
} }

Loading…
Cancel
Save