Skip to content

Commit

Permalink
update iSoron#1, new option for daily habits
Browse files Browse the repository at this point in the history
  • Loading branch information
chloesheen committed Nov 17, 2018
1 parent 4e23286 commit 4d43676
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
package org.isoron.uhabits.activities.habits.edit;

import android.content.*;
import android.content.res.Resources;
import android.os.*;
import android.support.annotation.*;
import android.support.v7.app.*;
import android.text.format.*;
import android.util.Log;
import android.view.*;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.datetimepicker.time.*;

Expand Down Expand Up @@ -167,8 +172,8 @@ void onSaveButtonClick()
int type = getTypeFromArguments();

if (!namePanel.validate()) return;
if (type == Habit.YES_NO_HABIT && !frequencyPanel.validate()) return;
if (type == Habit.NUMBER_HABIT && !targetPanel.validate()) return;
if (type == Habit.WEEKLY_HABIT && !frequencyPanel.validate()) return;
if (type == Habit.DAILY_HABIT && !targetPanel.validate()) return;

Habit habit = modelFactory.buildHabit();
if( originalHabit != null )
Expand All @@ -180,8 +185,13 @@ void onSaveButtonClick()
habit.setFrequency(frequencyPanel.getFrequency());
habit.setUnit(targetPanel.getUnit());
habit.setTargetValue(targetPanel.getTargetValue());
habit.setType(type);
//habit.setType(type);

Log.d("habit-typeDaily",String.valueOf(habit.getType()));
if (targetPanel.getVisibility() == View.VISIBLE)
habit.setType(Habit.DAILY_HABIT);
else
habit.setType(Habit.WEEKLY_HABIT);
saveHabit(habit);
dismiss();
}
Expand All @@ -201,6 +211,41 @@ private Habit parseHabitFromArguments()
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.DAILY_HABIT){
habit.setType(Habit.DAILY_HABIT);
tvDescription.setExample(res.getString(R.string.example_question_numerical));
} else {
habit.setType(Habit.WEEKLY_HABIT);
tvDescription.setExample(res.getString(R.string.example_question_boolean));
}

if (habit.isNumerical()) { // show targetPanel
frequencyPanel.setVisibility(GONE);
targetPanel.setVisibility(View.VISIBLE);
} else { // show frequencyPanel instead
targetPanel.setVisibility(GONE);
frequencyPanel.setVisibility(View.VISIBLE);
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}

private void populateForm()
{
Habit habit = modelFactory.buildHabit();
Expand All @@ -218,8 +263,12 @@ private void populateForm()
targetPanel.setTargetValue(habit.getTargetValue());
targetPanel.setUnit(habit.getUnit());
if (habit.hasReminder()) reminderPanel.setReminder(habit.getReminder());

setupHabitTypeController(habit);

}


private void setupNameController()
{
namePanel.setController(new NameDescriptionPanel.Controller()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public EditHabitDialog createBoolean()
{
EditHabitDialog dialog = new EditHabitDialog();
Bundle args = new Bundle();
args.putInt(BUNDLE_HABIT_TYPE, Habit.YES_NO_HABIT);
args.putInt(BUNDLE_HABIT_TYPE, Habit.WEEKLY_HABIT);
dialog.setArguments(args);
return dialog;
}
Expand All @@ -48,7 +48,7 @@ public EditHabitDialog createNumerical()
{
EditHabitDialog dialog = new EditHabitDialog();
Bundle args = new Bundle();
args.putInt(BUNDLE_HABIT_TYPE, Habit.NUMBER_HABIT);
args.putInt(BUNDLE_HABIT_TYPE, Habit.DAILY_HABIT);
dialog.setArguments(args);
return dialog;
}
Expand Down
15 changes: 15 additions & 0 deletions uhabits-android/src/main/res/layout/edit_habit_name.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@

</LinearLayout>

<TextView
android:id="@+id/tvSpinnerHabitType"
android:textColor="@color/black_a6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Habit 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:layout_width="match_parent"
android:layout_height="wrap_content">
Expand Down
5 changes: 5 additions & 0 deletions uhabits-android/src/main/res/values/constants.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<item>@string/toggle</item>
</string-array>

<string-array name="habitType" translatable="false">
<item>Weekly habit</item>
<item>Daily habit</item>
</string-array>

<string-array name="strengthIntervalNames" translatable="false">
<item>@string/day</item>
<item>@string/week</item>
Expand Down
4 changes: 2 additions & 2 deletions uhabits-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@
<string name="unit">Unit</string>
<string name="count">Count</string>
<string name="validation_show_not_be_blank">This field should not be blank</string>
<string name="example_question_numerical">e.g. How many steps did you walk today?</string>
<string name="example_units">e.g. steps</string>
<string name="example_question_numerical">e.g. How much water did you drink today?</string>
<string name="example_units">e.g. glasses</string>
<string name="example_question_boolean">e.g. Did you exercise today?</string>
<string name="question">Question</string>
<string name="target">Target</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class Habit
public static final String HABIT_URI_FORMAT =
"content://org.isoron.uhabits/habit/%d";

public static final int NUMBER_HABIT = 1;
public static final int DAILY_HABIT = 1;

public static final int YES_NO_HABIT = 0;
public static final int WEEKLY_HABIT = 0;

@Nullable
public Long id;
Expand Down Expand Up @@ -262,7 +262,7 @@ public synchronized int getType()

public synchronized void setType(int type)
{
if (type != YES_NO_HABIT && type != NUMBER_HABIT)
if (type != WEEKLY_HABIT && type != DAILY_HABIT)
throw new IllegalArgumentException();

data.type = type;
Expand Down Expand Up @@ -336,7 +336,7 @@ public synchronized boolean isCompletedToday()

public synchronized boolean isNumerical()
{
return data.type == NUMBER_HABIT;
return data.type == DAILY_HABIT;
}

public HabitData getData()
Expand Down Expand Up @@ -388,11 +388,11 @@ public HabitData()
this.color = 8;
this.archived = false;
this.frequency = new Frequency(3, 7);
this.type = YES_NO_HABIT;
this.type = WEEKLY_HABIT;
this.name = "";
this.description = "";
this.targetType = AT_LEAST;
this.targetValue = 100;
this.targetValue = 8;
this.unit = "";
this.position = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Habit createLongHabit()
public Habit createNumericalHabit()
{
Habit habit = modelFactory.buildHabit();
habit.setType(Habit.NUMBER_HABIT);
habit.setType(Habit.DAILY_HABIT);
habit.setName("Run");
habit.setDescription("How many miles did you run today?");
habit.setUnit("miles");
Expand All @@ -96,10 +96,10 @@ public Habit createNumericalHabit()
public Habit createLongNumericalHabit(Timestamp reference)
{
Habit habit = modelFactory.buildHabit();
habit.setType(Habit.NUMBER_HABIT);
habit.setName("Walk");
habit.setDescription("How many steps did you walk today?");
habit.setUnit("steps");
habit.setType(Habit.DAILY_HABIT);
habit.setName("Drink");
habit.setDescription("Did you drink water eight times today?");
habit.setUnit("glasses");
habit.setTargetType(Habit.AT_LEAST);
habit.setTargetValue(100);
habit.setColor(1);
Expand Down

0 comments on commit 4d43676

Please sign in to comment.