Fix the form fields containing examples

pull/157/merge
Alinson S. Xavier 9 years ago
parent 3dd33274e4
commit b66da24e39

@ -0,0 +1,109 @@
/*
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}
}
}

@ -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()

@ -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()));

@ -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">
<LinearLayout
android:id="@+id/formPanel"
@ -60,14 +61,13 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
<org.isoron.uhabits.activities.habits.edit.ExampleEditText
android:id="@+id/tvDescription"
style="@style/dialogFormInputMultiline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Question"
android:text="e.g. How many steps did you walk today?"
android:textColor="?attr/mediumContrastTextColor"/>
app:example="e.g. How many steps did you walk today?"/>
</android.support.design.widget.TextInputLayout>
<LinearLayout style="@style/dialogFormRow">
@ -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"
/>
</android.support.design.widget.TextInputLayout>
@ -100,14 +100,13 @@
android:layout_height="wrap_content"
android:layout_weight="2">
<EditText
<org.isoron.uhabits.activities.habits.edit.ExampleEditText
android:id="@+id/tvUnit"
style="@style/dialogFormInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Unit"
android:text="e.g. steps"
android:textColor="?attr/mediumContrastTextColor"
android:hint="@string/unit"
app:example="e.g. steps"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>

@ -94,4 +94,5 @@
</string-array>
<string name="snooze_interval_default" translatable="false">15</string>
<string name="default_count" translatable="false">100</string>
</resources>

@ -208,4 +208,6 @@
value</string>
<string name="change_value">Change value</string>
<string name="calendar">Calendar</string>
<string name="unit">Unit</string>
<string name="count">Count</string>
</resources>
Loading…
Cancel
Save