Skip to content

Commit

Permalink
Reimplement custom frequencies
Browse files Browse the repository at this point in the history
Closes iSoron#961.
  • Loading branch information
hiqua committed Aug 22, 2021
1 parent 79f5b8b commit 4804a48
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package org.isoron.uhabits.activities.common.dialogs

import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
Expand All @@ -44,7 +43,7 @@ class FrequencyPickerDialog(
constructor() : this(1, 1)

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)

addBeforeAfterText(
Expand All @@ -62,6 +61,11 @@ class FrequencyPickerDialog(
contentView.xTimesPerMonthContainer,
)

addBeforeAfterText(
this.getString(R.string.x_times_per_y_days),
contentView.xTimesPerYDaysContainer,
)

contentView.everyDayRadioButton.setOnClickListener {
check(contentView.everyDayRadioButton)
unfocusAll()
Expand Down Expand Up @@ -95,7 +99,20 @@ class FrequencyPickerDialog(
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)
.setPositiveButton(R.string.save) { _, _ -> onSaveClicked() }
.create()
Expand All @@ -106,12 +123,11 @@ class FrequencyPickerDialog(
container: LinearLayout
) {
val parts = str.split("%d")
container.addView(
TextView(activity).apply { text = parts[0].trim() }, 1,
)
container.addView(
TextView(activity).apply { text = parts[1].trim() }, 3,
)
for (i in parts.indices) {
container.addView(
TextView(activity).apply { text = parts[i].trim() }, 2 * i + 1,
)
}
}

private fun onSaveClicked() {
Expand All @@ -132,6 +148,12 @@ class FrequencyPickerDialog(
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 -> {
if (contentView.xTimesPerMonthTextView.text.isNotEmpty()) {
numerator = Integer.parseInt(contentView.xTimesPerMonthTextView.text.toString())
Expand All @@ -147,10 +169,10 @@ class FrequencyPickerDialog(
dismiss()
}

private fun check(view: RadioButton?) {
private fun check(view: RadioButton) {
uncheckAll()
view?.isChecked = true
view?.requestFocus()
view.isChecked = true
view.requestFocus()
}

override fun onResume() {
Expand Down Expand Up @@ -179,8 +201,9 @@ class FrequencyPickerDialog(
contentView.xTimesPerWeekTextView.setText(freqNumerator.toString())
focus(contentView.xTimesPerWeekTextView)
} else {
Log.w("FrequencyPickerDialog", "Unknown frequency: $freqNumerator/$freqDenominator")
contentView.everyDayRadioButton.isChecked = true
contentView.xTimesPerYDaysRadioButton.isChecked = true
contentView.xTimesPerYDaysXTextView.setText(freqNumerator.toString())
contentView.xTimesPerYDaysYTextView.setText(freqDenominator.toString())
}
}
}
Expand All @@ -196,6 +219,7 @@ class FrequencyPickerDialog(
contentView.everyXDaysRadioButton.isChecked = false
contentView.xTimesPerWeekRadioButton.isChecked = false
contentView.xTimesPerMonthRadioButton.isChecked = false
contentView.xTimesPerYDaysRadioButton.isChecked = false
}

private fun unfocusAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 > 1 -> resources.getString(R.string.every_x_days, freqDen)
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() {
Expand Down
40 changes: 39 additions & 1 deletion uhabits-android/src/main/res/layout/frequency_picker_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
1 change: 1 addition & 0 deletions uhabits-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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="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_y_days">%d times in %d days</string>
<string name="yes_or_no_short_example">e.g. Exercise</string>
<string name="color">Color</string>
<string name="example_target">e.g. 15</string>
Expand Down

0 comments on commit 4804a48

Please sign in to comment.