Implement reward value form field

This commit is contained in:
Phillip Thelen 2019-04-05 11:16:44 +02:00
parent 1850805910
commit 008fdc1d22
9 changed files with 152 additions and 4 deletions

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="48dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="48dp">
<path android:fillColor="#878190" android:pathData="M7,10l5,5 5,-5z"/>
</vector>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="48dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="48dp">
<path android:fillColor="#33878190" android:pathData="M7,10l5,5 5,-5z"/>
</vector>

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@drawable/ic_arrow_drop_down_gray_48dp_disabled" />
<item android:drawable="@drawable/ic_arrow_drop_down_gray_48dp" />
</selector>

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="48dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="48dp">
<path android:fillColor="#878190" android:pathData="M7,14l5,-5 5,5z"/>
</vector>

View file

@ -74,6 +74,16 @@
android:id="@+id/habit_scoring_buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/reward_value_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/checklist"
style="@style/TaskFormSectionheader"/>
<com.habitrpg.android.habitica.ui.views.tasks.form.RewardValueFormView
android:id="@+id/reward_value"
android:layout_width="match_parent"
android:layout_height="56dp" />
<TextView
android:id="@+id/checklist_title"
android:layout_width="match_parent"

View file

@ -11,7 +11,8 @@
android:paddingEnd="@dimen/spacing_medium"
android:paddingTop="@dimen/spacing_small"
android:paddingBottom="@dimen/spacing_small"
android:layout_marginBottom="@dimen/spacing_medium"
android:layout_marginBottom="@dimen/spacing_small"
android:layout_marginTop="@dimen/spacing_small"
tools:parentTag="android.widget.LinearLayout">
<ImageButton
android:id="@+id/button"

View file

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="56dp"
tools:parentTag="android.widget.RelativeLayout">
<View android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray_700"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"/>
<View android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/gray_500"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"/>
<ImageButton
android:id="@+id/up_button"
android:layout_width="56dp"
android:layout_height="match_parent"
android:background="@color/gray_600"
android:src="@drawable/ic_arrow_drop_up_gray_48dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
/>
<ImageButton
android:id="@+id/down_button"
android:layout_width="56dp"
android:layout_height="match_parent"
android:background="@color/gray_600"
android:src="@drawable/ic_arrow_drop_down_gray_48dp_states"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
/>
<EditText
android:id="@+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="@color/transparent"
android:textColor="@color/gray_100"
android:textSize="16sp"
tools:text="100.0"
android:inputType="numberDecimal"
android:drawablePadding="@dimen/spacing_medium"
android:paddingStart="@dimen/spacing_large"
android:paddingEnd="@dimen/spacing_large"/>
</merge>

View file

@ -75,6 +75,9 @@ class TaskFormActivity : BaseActivity() {
private val statConstitutionButton: TextView by bindView(R.id.stat_constitution_button)
private val statPerceptionButton: TextView by bindView(R.id.stat_perception_button)
private val rewardValueTitleView: TextView by bindView(R.id.reward_value_title)
private val rewardValueFormView: RewardValueFormView by bindView(R.id.reward_value)
private val tagsWrapper: LinearLayout by bindView(R.id.tags_wrapper)
private var isCreating = true
@ -189,13 +192,17 @@ class TaskFormActivity : BaseActivity() {
remindersTitleView.visibility = todoDailyViewsVisibility
remindersContainer.visibility = todoDailyViewsVisibility
taskSchedulingTitleView.visibility = todoDailyViewsVisibility
taskSchedulingControls.visibility = todoDailyViewsVisibility
taskSchedulingControls.taskType = taskType
val rewardHideViews = if (taskType == Task.TYPE_REWARD) View.GONE else View.VISIBLE
taskDifficultyTitleView.visibility = rewardHideViews
taskDifficultyButtons.visibility = rewardHideViews
taskSchedulingTitleView.visibility = todoDailyViewsVisibility
taskSchedulingControls.visibility = todoDailyViewsVisibility
taskSchedulingControls.taskType = taskType
val rewardViewsVisibility = if (taskType == Task.TYPE_REWARD) View.VISIBLE else View.GONE
rewardValueTitleView.visibility = rewardViewsVisibility
rewardValueFormView.visibility = rewardViewsVisibility
statWrapper.visibility = if (usesTaskAttributeStats) View.VISIBLE else View.GONE
if (isCreating) {
@ -246,6 +253,7 @@ class TaskFormActivity : BaseActivity() {
habitAdjustPositiveStreakView.setText((task.streak ?: 0).toString())
}
Task.TYPE_TODO -> taskSchedulingControls.dueDate = task.dueDate
Task.TYPE_REWARD -> rewardValueFormView.value = task.value
}
if (taskType == Task.TYPE_DAILY || taskType == Task.TYPE_TODO) {
task.checklist?.let { checklistContainer.checklistItems = it }
@ -307,6 +315,8 @@ class TaskFormActivity : BaseActivity() {
if (habitAdjustPositiveStreakView.text.isNotEmpty()) thisTask.streak = habitAdjustPositiveStreakView.text.toString().toInt()
} else if (taskType == Task.TYPE_TODO) {
thisTask.dueDate = taskSchedulingControls.dueDate
} else if (taskType == Task.TYPE_REWARD) {
thisTask.value = rewardValueFormView.value
}
if (taskType == Task.TYPE_DAILY || taskType == Task.TYPE_TODO) {

View file

@ -0,0 +1,61 @@
package com.habitrpg.android.habitica.ui.views.tasks.form
import android.content.Context
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.widget.EditText
import android.widget.ImageButton
import android.widget.RelativeLayout
import com.habitrpg.android.habitica.R
import com.habitrpg.android.habitica.extensions.asDrawable
import com.habitrpg.android.habitica.extensions.inflate
import com.habitrpg.android.habitica.ui.helpers.bindView
import com.habitrpg.android.habitica.ui.views.HabiticaIconsHelper
import java.text.DecimalFormat
class RewardValueFormView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr), TextWatcher {
private val editText: EditText by bindView(R.id.edit_text)
private val upButton: ImageButton by bindView(R.id.up_button)
private val downButton: ImageButton by bindView(R.id.down_button)
private val decimalFormat = DecimalFormat("0.#")
var value = 0.0
set(value) {
field = if (value >= 0) value else 0.0
val stringValue = decimalFormat.format(field)
if (editText.text.toString() != stringValue) {
editText.setText(stringValue)
}
downButton.isEnabled = field > 0
}
init {
inflate(R.layout.task_form_reward_value, true)
editText.setCompoundDrawablesWithIntrinsicBounds(HabiticaIconsHelper.imageOfGold().asDrawable(context.resources), null, null, null)
upButton.setOnClickListener {
value += 1
}
downButton.setOnClickListener {
value -= 1
}
editText.addTextChangedListener(this)
}
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
value = s.toString().toDoubleOrNull() ?: 0.0
}
}