mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Merge pull request #1079 from hiqua/reimplement_custom_freq
Reimplement custom frequencies
This commit is contained in:
@@ -21,7 +21,6 @@ package org.isoron.uhabits.activities.common.dialogs
|
|||||||
|
|
||||||
import android.app.Dialog
|
import android.app.Dialog
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.util.Log
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
@@ -44,7 +43,7 @@ class FrequencyPickerDialog(
|
|||||||
constructor() : this(1, 1)
|
constructor() : this(1, 1)
|
||||||
|
|
||||||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
||||||
val inflater = LayoutInflater.from(activity!!)
|
val inflater = LayoutInflater.from(requireActivity())
|
||||||
contentView = inflater.inflate(R.layout.frequency_picker_dialog, null)
|
contentView = inflater.inflate(R.layout.frequency_picker_dialog, null)
|
||||||
|
|
||||||
addBeforeAfterText(
|
addBeforeAfterText(
|
||||||
@@ -62,6 +61,11 @@ class FrequencyPickerDialog(
|
|||||||
contentView.xTimesPerMonthContainer,
|
contentView.xTimesPerMonthContainer,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
addBeforeAfterText(
|
||||||
|
this.getString(R.string.x_times_per_y_days),
|
||||||
|
contentView.xTimesPerYDaysContainer,
|
||||||
|
)
|
||||||
|
|
||||||
contentView.everyDayRadioButton.setOnClickListener {
|
contentView.everyDayRadioButton.setOnClickListener {
|
||||||
check(contentView.everyDayRadioButton)
|
check(contentView.everyDayRadioButton)
|
||||||
unfocusAll()
|
unfocusAll()
|
||||||
@@ -95,7 +99,20 @@ class FrequencyPickerDialog(
|
|||||||
if (hasFocus) check(contentView.xTimesPerMonthRadioButton)
|
if (hasFocus) check(contentView.xTimesPerMonthRadioButton)
|
||||||
}
|
}
|
||||||
|
|
||||||
return AlertDialog.Builder(activity!!)
|
contentView.xTimesPerYDaysRadioButton.setOnClickListener {
|
||||||
|
check(contentView.xTimesPerYDaysRadioButton)
|
||||||
|
focus(contentView.xTimesPerYDaysXTextView)
|
||||||
|
}
|
||||||
|
|
||||||
|
contentView.xTimesPerYDaysXTextView.setOnFocusChangeListener { v, hasFocus ->
|
||||||
|
if (hasFocus) check(contentView.xTimesPerYDaysRadioButton)
|
||||||
|
}
|
||||||
|
|
||||||
|
contentView.xTimesPerYDaysYTextView.setOnFocusChangeListener { v, hasFocus ->
|
||||||
|
if (hasFocus) check(contentView.xTimesPerYDaysRadioButton)
|
||||||
|
}
|
||||||
|
|
||||||
|
return AlertDialog.Builder(requireActivity())
|
||||||
.setView(contentView)
|
.setView(contentView)
|
||||||
.setPositiveButton(R.string.save) { _, _ -> onSaveClicked() }
|
.setPositiveButton(R.string.save) { _, _ -> onSaveClicked() }
|
||||||
.create()
|
.create()
|
||||||
@@ -106,12 +123,11 @@ class FrequencyPickerDialog(
|
|||||||
container: LinearLayout
|
container: LinearLayout
|
||||||
) {
|
) {
|
||||||
val parts = str.split("%d")
|
val parts = str.split("%d")
|
||||||
container.addView(
|
for (i in parts.indices) {
|
||||||
TextView(activity).apply { text = parts[0].trim() }, 1,
|
container.addView(
|
||||||
)
|
TextView(activity).apply { text = parts[i].trim() }, 2 * i + 1,
|
||||||
container.addView(
|
)
|
||||||
TextView(activity).apply { text = parts[1].trim() }, 3,
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onSaveClicked() {
|
private fun onSaveClicked() {
|
||||||
@@ -132,6 +148,12 @@ class FrequencyPickerDialog(
|
|||||||
denominator = 7
|
denominator = 7
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
contentView.xTimesPerYDaysRadioButton.isChecked -> {
|
||||||
|
if (contentView.xTimesPerYDaysXTextView.text.isNotEmpty() && contentView.xTimesPerYDaysYTextView.text.isNotEmpty()) {
|
||||||
|
numerator = Integer.parseInt(contentView.xTimesPerYDaysXTextView.text.toString())
|
||||||
|
denominator = Integer.parseInt(contentView.xTimesPerYDaysYTextView.text.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
else -> {
|
else -> {
|
||||||
if (contentView.xTimesPerMonthTextView.text.isNotEmpty()) {
|
if (contentView.xTimesPerMonthTextView.text.isNotEmpty()) {
|
||||||
numerator = Integer.parseInt(contentView.xTimesPerMonthTextView.text.toString())
|
numerator = Integer.parseInt(contentView.xTimesPerMonthTextView.text.toString())
|
||||||
@@ -147,10 +169,10 @@ class FrequencyPickerDialog(
|
|||||||
dismiss()
|
dismiss()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun check(view: RadioButton?) {
|
private fun check(view: RadioButton) {
|
||||||
uncheckAll()
|
uncheckAll()
|
||||||
view?.isChecked = true
|
view.isChecked = true
|
||||||
view?.requestFocus()
|
view.requestFocus()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
@@ -179,8 +201,9 @@ class FrequencyPickerDialog(
|
|||||||
contentView.xTimesPerWeekTextView.setText(freqNumerator.toString())
|
contentView.xTimesPerWeekTextView.setText(freqNumerator.toString())
|
||||||
focus(contentView.xTimesPerWeekTextView)
|
focus(contentView.xTimesPerWeekTextView)
|
||||||
} else {
|
} else {
|
||||||
Log.w("FrequencyPickerDialog", "Unknown frequency: $freqNumerator/$freqDenominator")
|
contentView.xTimesPerYDaysRadioButton.isChecked = true
|
||||||
contentView.everyDayRadioButton.isChecked = true
|
contentView.xTimesPerYDaysXTextView.setText(freqNumerator.toString())
|
||||||
|
contentView.xTimesPerYDaysYTextView.setText(freqDenominator.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -196,6 +219,7 @@ class FrequencyPickerDialog(
|
|||||||
contentView.everyXDaysRadioButton.isChecked = false
|
contentView.everyXDaysRadioButton.isChecked = false
|
||||||
contentView.xTimesPerWeekRadioButton.isChecked = false
|
contentView.xTimesPerWeekRadioButton.isChecked = false
|
||||||
contentView.xTimesPerMonthRadioButton.isChecked = false
|
contentView.xTimesPerMonthRadioButton.isChecked = false
|
||||||
|
contentView.xTimesPerYDaysRadioButton.isChecked = false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun unfocusAll() {
|
private fun unfocusAll() {
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ fun formatFrequency(freqNum: Int, freqDen: Int, resources: Resources) = when {
|
|||||||
freqNum == 1 && freqDen == 7 -> resources.getString(R.string.every_week)
|
freqNum == 1 && freqDen == 7 -> resources.getString(R.string.every_week)
|
||||||
freqNum == 1 && freqDen > 1 -> resources.getString(R.string.every_x_days, freqDen)
|
freqNum == 1 && freqDen > 1 -> resources.getString(R.string.every_x_days, freqDen)
|
||||||
freqDen == 7 -> resources.getString(R.string.x_times_per_week, freqNum)
|
freqDen == 7 -> resources.getString(R.string.x_times_per_week, freqNum)
|
||||||
else -> "$freqNum/$freqDen"
|
else -> resources.getString(R.string.x_times_per_y_days, freqNum, freqDen)
|
||||||
}
|
}
|
||||||
|
|
||||||
class EditHabitActivity : AppCompatActivity() {
|
class EditHabitActivity : AppCompatActivity() {
|
||||||
|
|||||||
@@ -64,7 +64,7 @@
|
|||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:inputType="number"
|
android:inputType="number"
|
||||||
android:maxLength="2"
|
android:maxLength="3"
|
||||||
android:text="3" />
|
android:text="3" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -89,7 +89,7 @@
|
|||||||
android:layout_marginStart="8dp"
|
android:layout_marginStart="8dp"
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:inputType="number"
|
android:inputType="number"
|
||||||
android:maxLength="2"
|
android:maxLength="1"
|
||||||
android:text="3" />
|
android:text="3" />
|
||||||
|
|
||||||
|
|
||||||
@@ -120,4 +120,42 @@
|
|||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:id="@+id/xTimesPerYDaysContainer"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="48dp"
|
||||||
|
android:gravity="center_vertical">
|
||||||
|
|
||||||
|
<RadioButton
|
||||||
|
android:id="@+id/xTimesPerYDaysRadioButton"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/xTimesPerYDaysXTextView"
|
||||||
|
android:layout_width="50dp"
|
||||||
|
android:layout_height="40dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:background="@drawable/bg_input_box"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
|
android:layout_marginEnd="8dp"
|
||||||
|
android:inputType="number"
|
||||||
|
android:maxLength="3"
|
||||||
|
android:text="3" />
|
||||||
|
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/xTimesPerYDaysYTextView"
|
||||||
|
android:layout_width="50dp"
|
||||||
|
android:layout_height="40dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:background="@drawable/bg_input_box"
|
||||||
|
android:layout_marginStart="8dp"
|
||||||
|
android:layout_marginEnd="8dp"
|
||||||
|
android:inputType="number"
|
||||||
|
android:maxLength="3"
|
||||||
|
android:text="14" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
@@ -205,6 +205,7 @@
|
|||||||
<string name="measurable_example">e.g. How many miles did you run today? How many pages did you read?</string>
|
<string name="measurable_example">e.g. How many miles did you run today? How many pages did you read?</string>
|
||||||
<string name="x_times_per_week">%d times per week</string>
|
<string name="x_times_per_week">%d times per week</string>
|
||||||
<string name="x_times_per_month">%d times per month</string>
|
<string name="x_times_per_month">%d times per month</string>
|
||||||
|
<string name="x_times_per_y_days">%d times in %d days</string>
|
||||||
<string name="yes_or_no_short_example">e.g. Exercise</string>
|
<string name="yes_or_no_short_example">e.g. Exercise</string>
|
||||||
<string name="color">Color</string>
|
<string name="color">Color</string>
|
||||||
<string name="example_target">e.g. 15</string>
|
<string name="example_target">e.g. 15</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user