From b66da24e3968ecfcc4d5f465ef6575d5c62163ef Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Mon, 27 Mar 2017 18:41:52 -0400 Subject: [PATCH] Fix the form fields containing examples --- .../habits/edit/ExampleEditText.java | 109 ++++++++++++++++++ .../habits/edit/NumericalHabitDialog.java | 31 ----- .../edit/NumericalHabitDialogHelper.java | 14 ++- .../main/res/layout/edit_numerical_habit.xml | 19 ++- app/src/main/res/values/constants.xml | 1 + app/src/main/res/values/strings.xml | 2 + 6 files changed, 129 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/org/isoron/uhabits/activities/habits/edit/ExampleEditText.java diff --git a/app/src/main/java/org/isoron/uhabits/activities/habits/edit/ExampleEditText.java b/app/src/main/java/org/isoron/uhabits/activities/habits/edit/ExampleEditText.java new file mode 100644 index 000000000..6449d4db0 --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/activities/habits/edit/ExampleEditText.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.activities.habits.edit; + +import android.content.*; +import android.support.annotation.*; +import android.text.*; +import android.util.*; +import android.view.*; +import android.widget.*; + +import org.isoron.uhabits.*; +import org.isoron.uhabits.utils.*; + +import static org.isoron.uhabits.utils.AttributeSetUtils.*; + +public class ExampleEditText extends EditText + implements View.OnFocusChangeListener +{ + + private String example; + + private String realText; + + private int color; + + private int exampleColor; + + private int inputType; + + public ExampleEditText(Context context, @Nullable AttributeSet attrs) + { + super(context, attrs); + + if (attrs != null) + example = getAttribute(context, attrs, "example", ""); + + inputType = getInputType(); + realText = getText().toString(); + color = getCurrentTextColor(); + init(); + } + + public String getRealText() + { + return realText; + } + + @Override + public void onFocusChange(View v, boolean hasFocus) + { + if (!hasFocus) realText = getText().toString(); + updateText(); + } + + public void setExample(String example) + { + this.example = example; + updateText(); + } + + public void setRealText(String realText) + { + this.realText = realText; + updateText(); + } + + private void init() + { + StyledResources sr = new StyledResources(getContext()); + exampleColor = sr.getColor(R.attr.mediumContrastTextColor); + setOnFocusChangeListener(this); + updateText(); + } + + private void updateText() + { + if (realText.isEmpty() && !isFocused()) + { + setTextColor(exampleColor); + setText(example); + setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); + } + else + { + setText(realText); + setTextColor(color); + setInputType(inputType); + } + + } +} diff --git a/app/src/main/java/org/isoron/uhabits/activities/habits/edit/NumericalHabitDialog.java b/app/src/main/java/org/isoron/uhabits/activities/habits/edit/NumericalHabitDialog.java index 87e90c7f5..ed2c01fa8 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/habits/edit/NumericalHabitDialog.java +++ b/app/src/main/java/org/isoron/uhabits/activities/habits/edit/NumericalHabitDialog.java @@ -23,7 +23,6 @@ import android.os.*; import android.support.annotation.*; import android.support.v7.app.*; import android.view.*; -import android.widget.*; import org.isoron.uhabits.*; import org.isoron.uhabits.R; @@ -32,12 +31,9 @@ import org.isoron.uhabits.activities.common.dialogs.*; import org.isoron.uhabits.commands.*; import org.isoron.uhabits.models.*; import org.isoron.uhabits.preferences.*; -import org.isoron.uhabits.utils.*; import butterknife.*; -import static org.isoron.uhabits.R.id.*; - public abstract class NumericalHabitDialog extends AppCompatDialogFragment { @Nullable @@ -60,10 +56,6 @@ public abstract class NumericalHabitDialog extends AppCompatDialogFragment private NumericalHabitDialogHelper helper; - private boolean tvDescriptionInitialized = false; - - private boolean tvUnitInitialized = false; - @Override public int getTheme() { @@ -130,29 +122,6 @@ public abstract class NumericalHabitDialog extends AppCompatDialogFragment protected abstract void saveHabit(); - @OnFocusChange(tvDescription) - void clearDefaultDescription(boolean focused) - { - if (!focused || tvDescriptionInitialized) return; - tvDescriptionInitialized = true; - clearDefaultText(helper.tvDescription); - } - - private void clearDefaultText(TextView textView) - { - StyledResources sr = new StyledResources(getContext()); - int color = sr.getColor(R.attr.highContrastTextColor); - textView.setText(""); - textView.setTextColor(color); - } - - @OnFocusChange(tvUnit) - void clearDefaultUnit(boolean focused) - { - if (!focused || tvUnitInitialized) return; - tvUnitInitialized = true; - clearDefaultText(helper.tvUnit); - } @OnClick(R.id.buttonDiscard) void onButtonDiscardClick() diff --git a/app/src/main/java/org/isoron/uhabits/activities/habits/edit/NumericalHabitDialogHelper.java b/app/src/main/java/org/isoron/uhabits/activities/habits/edit/NumericalHabitDialogHelper.java index 73ea43a13..5122ef504 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/habits/edit/NumericalHabitDialogHelper.java +++ b/app/src/main/java/org/isoron/uhabits/activities/habits/edit/NumericalHabitDialogHelper.java @@ -40,10 +40,10 @@ public class NumericalHabitDialogHelper TextView tvName; @BindView(R.id.tvDescription) - TextView tvDescription; + ExampleEditText tvDescription; @BindView(R.id.tvUnit) - TextView tvUnit; + ExampleEditText tvUnit; @BindView(R.id.tvTargetCount) TextView tvTargetValue; @@ -59,12 +59,14 @@ public class NumericalHabitDialogHelper public void parseForm(Habit habit) { + tvUnit.clearFocus(); + tvDescription.clearFocus(); habit.setName(tvName.getText().toString().trim()); - habit.setDescription(tvDescription.getText().toString().trim()); + habit.setDescription(tvDescription.getRealText().trim()); habit.setTargetType(tvTargetType.getSelectedItemPosition()); habit.setTargetValue( Double.parseDouble(tvTargetValue.getText().toString())); - habit.setUnit(tvUnit.getText().toString().trim()); + habit.setUnit(tvUnit.getRealText().trim()); } public void populateColor(int paletteColor) @@ -78,10 +80,10 @@ public class NumericalHabitDialogHelper tvName.setText(habit.getName()); if(!habit.getDescription().isEmpty()) - tvDescription.setText(habit.getDescription()); + tvDescription.setRealText(habit.getDescription()); if(!habit.getUnit().isEmpty()) - tvUnit.setText(habit.getUnit()); + tvUnit.setRealText(habit.getUnit()); tvTargetType.setSelection(habit.getTargetType()); tvTargetValue.setText(valueFormatter.format(habit.getTargetValue())); diff --git a/app/src/main/res/layout/edit_numerical_habit.xml b/app/src/main/res/layout/edit_numerical_habit.xml index 7137d07bb..c57c954df 100644 --- a/app/src/main/res/layout/edit_numerical_habit.xml +++ b/app/src/main/res/layout/edit_numerical_habit.xml @@ -22,7 +22,8 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".activities.habits.edit.BooleanHabitDialog" - tools:ignore="MergeRootFrame"> + tools:ignore="MergeRootFrame" + xmlns:app="http://isoron.org/android"> - + app:example="e.g. How many steps did you walk today?"/> @@ -89,9 +89,9 @@ style="@style/dialogFormInput" android:layout_width="match_parent" android:layout_height="wrap_content" - android:hint="Count" + android:hint="@string/count" android:inputType="numberDecimal" - android:text="100" + android:text="@string/default_count" /> @@ -100,14 +100,13 @@ android:layout_height="wrap_content" android:layout_weight="2"> - diff --git a/app/src/main/res/values/constants.xml b/app/src/main/res/values/constants.xml index 147b322e8..3d43c1aa0 100644 --- a/app/src/main/res/values/constants.xml +++ b/app/src/main/res/values/constants.xml @@ -94,4 +94,5 @@ 15 + 100 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 95ec945ee..5e610d2e8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -208,4 +208,6 @@ value Change value Calendar + Unit + Count \ No newline at end of file