Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to change widget action #1960

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class EditHabitActivity : AppCompatActivity() {
var reminderMin = -1
var reminderDays: WeekdayList = WeekdayList.EVERY_DAY
var targetType = NumericalHabitType.AT_LEAST
var increment = 0.0

override fun onCreate(state: Bundle?) {
super.onCreate(state)
Expand All @@ -105,6 +106,7 @@ class EditHabitActivity : AppCompatActivity() {
freqNum = habit.frequency.numerator
freqDen = habit.frequency.denominator
targetType = habit.targetType
increment = habit.increment
habit.reminder?.let {
reminderHour = it.hour
reminderMin = it.minute
Expand All @@ -128,6 +130,7 @@ class EditHabitActivity : AppCompatActivity() {
reminderHour = state.getInt("reminderHour")
reminderMin = state.getInt("reminderMin")
reminderDays = WeekdayList(state.getInt("reminderDays"))
increment = state.getDouble("increment")
}

updateColors()
Expand All @@ -137,6 +140,7 @@ class EditHabitActivity : AppCompatActivity() {
binding.unitOuterBox.visibility = View.GONE
binding.targetOuterBox.visibility = View.GONE
binding.targetTypeOuterBox.visibility = View.GONE
binding.incrementOuterBox.visibility = View.GONE
}
HabitType.NUMERICAL -> {
binding.nameInput.hint = getString(R.string.measurable_short_example)
Expand Down Expand Up @@ -254,6 +258,23 @@ class EditHabitActivity : AppCompatActivity() {
for (fragment in supportFragmentManager.fragments) {
(fragment as DialogFragment).dismiss()
}

populateWidgetAction()
binding.widgetActionPicker.setOnClickListener {
val builder = AlertDialog.Builder(this)
val arrayAdapter = ArrayAdapter<String>(this, android.R.layout.select_dialog_item)
arrayAdapter.add(getString(R.string.widget_action_dialog))
arrayAdapter.add(getString(R.string.widget_action_increment))
builder.setAdapter(arrayAdapter) { dialog, which ->
increment = when (which) {
1 -> 1.0
else -> 0.0
}
populateWidgetAction()
dialog.dismiss()
}
builder.show()
}
}

private fun save() {
Expand Down Expand Up @@ -281,6 +302,7 @@ class EditHabitActivity : AppCompatActivity() {
habit.targetValue = binding.targetInput.text.toString().toDouble()
habit.targetType = targetType
habit.unit = binding.unitInput.text.trim().toString()
habit.increment = binding.incrementInput.text.toString().toDouble()
}
habit.type = habitType

Expand Down Expand Up @@ -341,6 +363,20 @@ class EditHabitActivity : AppCompatActivity() {
}
}

@SuppressLint("StringFormatMatches")
private fun populateWidgetAction() {
binding.widgetActionPicker.text = when (increment) {
0.0 -> getString(R.string.widget_action_dialog)
else -> getString(R.string.widget_action_increment)
}
binding.incrementInput.setText(increment.toString())
if (increment != 0.0) {
binding.incrementRightBox.visibility = View.VISIBLE
} else {
binding.incrementRightBox.visibility = View.GONE
}
}

private fun populateTargetType() {
binding.targetTypePicker.text = when (targetType) {
NumericalHabitType.AT_MOST -> getString(R.string.target_type_at_most)
Expand Down Expand Up @@ -375,6 +411,7 @@ class EditHabitActivity : AppCompatActivity() {
putInt("reminderHour", reminderHour)
putInt("reminderMin", reminderMin)
putInt("reminderDays", reminderDays.toInteger())
putDouble("increment", increment)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ class PendingIntentFactory
FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT
)

fun incrementNumerical(habit: Habit, timestamp: Long?): PendingIntent =
getBroadcast(
context,
2,
Intent(context, WidgetReceiver::class.java).apply {
data = Uri.parse(habit.uriString)
action = WidgetReceiver.ACTION_INCREMENT_NUMERICAL
if (timestamp != null) putExtra("timestamp", timestamp)
},
FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT
)

fun updateWidgets(): PendingIntent =
getBroadcast(
context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ class WidgetReceiver : BroadcastReceiver() {
widgetUpdater.updateWidgets()
widgetUpdater.scheduleStartDayWidgetUpdate()
}
ACTION_INCREMENT_NUMERICAL -> {
Log.d(
TAG,
String.format(
"onIncrementNumerical habit=%d timestamp=%d",
data!!.habit.id,
data.timestamp.unixTime
)
)
controller.onIncrement(data.habit, data.timestamp, (data.habit.increment * 1000).toInt())
}
}
} catch (e: RuntimeException) {
Log.e("WidgetReceiver", "could not process intent", e)
Expand All @@ -116,6 +127,7 @@ class WidgetReceiver : BroadcastReceiver() {
const val ACTION_DISMISS_REMINDER = "org.isoron.uhabits.ACTION_DISMISS_REMINDER"
const val ACTION_REMOVE_REPETITION = "org.isoron.uhabits.ACTION_REMOVE_REPETITION"
const val ACTION_TOGGLE_REPETITION = "org.isoron.uhabits.ACTION_TOGGLE_REPETITION"
const val ACTION_INCREMENT_NUMERICAL = "org.isoron.uhabits.ACTION_INCREMENT_NUMERICAL"
const val ACTION_UPDATE_WIDGETS_VALUE = "org.isoron.uhabits.ACTION_UPDATE_WIDGETS_VALUE"
private const val TAG = "WidgetReceiver"
var lastReceivedIntent: Intent? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ open class CheckmarkWidget(

override fun getOnClickPendingIntent(context: Context): PendingIntent? {
return if (habit.isNumerical) {
pendingIntentFactory.showNumberPicker(habit, DateUtils.getTodayWithOffset())
if (habit.increment == 0.0) {
pendingIntentFactory.showNumberPicker(habit, DateUtils.getTodayWithOffset())
} else {
pendingIntentFactory.incrementNumerical(habit, null)
}
} else {
pendingIntentFactory.toggleCheckmark(habit, null)
}
Expand Down
41 changes: 41 additions & 0 deletions uhabits-android/src/main/res/layout/activity_edit_habit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,47 @@
</FrameLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/incrementOuterBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<FrameLayout
style="@style/FormOuterBox"
android:layout_width="0dp"
android:layout_weight="1">
<LinearLayout style="@style/FormInnerBox">
<TextView
style="@style/FormLabel"
android:text="@string/widget_action" />
<TextView
style="@style/FormDropdown"
android:id="@+id/widgetActionPicker"
android:text="@string/widget_action_dialog"
android:textColor="?attr/contrast100"
/>
</LinearLayout>
</FrameLayout>
<FrameLayout
style="@style/FormOuterBox"
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/incrementRightBox">
<LinearLayout style="@style/FormInnerBox">
<TextView
style="@style/FormLabel"
android:text="@string/increment" />
<EditText
style="@style/FormInput"
android:id="@+id/incrementInput"
android:maxLines="1"
android:inputType="numberDecimal"
android:hint="@string/example_increment"/>
</LinearLayout>
</FrameLayout>
</LinearLayout>

<FrameLayout
android:id="@+id/targetTypeOuterBox"
style="@style/FormOuterBox">
Expand Down
4 changes: 4 additions & 0 deletions uhabits-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
<string name="developers">Developers</string>
<string name="version_n">Version %s</string>
<string name="frequency">Frequency</string>
<string name="widget_action">Widget Click Action</string>
<string name="widget_action_dialog">Open Dialog</string>
<string name="widget_action_increment">Increment</string>
<string name="checkmark">Checkmark</string>
<string name="checkmark_stack_widget" formatted="false">Checkmark Stack Widget</string>
<string name="frequency_stack_widget" formatted="false">Frequency Stack Widget</string>
Expand Down Expand Up @@ -212,6 +215,7 @@
<string name="yes_or_no_short_example">e.g. Exercise</string>
<string name="color">Color</string>
<string name="example_target">e.g. 15</string>
<string name="example_increment">e.g. 2.0</string>
<string name="measurable_short_example">e.g. Run</string>
<string name="measurable_question_example">e.g. How many miles did you run today?</string>
<string name="measurable_units_example">e.g. miles</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ package org.isoron.uhabits.core

const val DATABASE_FILENAME = "uhabits.db"

const val DATABASE_VERSION = 25
const val DATABASE_VERSION = 26
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ data class Habit(
val computedEntries: EntryList,
val originalEntries: EntryList,
val scores: ScoreList,
val streaks: StreakList
val streaks: StreakList,
var increment: Double = 0.0
) {
init {
if (uuid == null) this.uuid = UUID.randomUUID().toString().replace("-", "")
Expand Down Expand Up @@ -119,6 +120,7 @@ data class Habit(
this.type = other.type
this.unit = other.unit
this.uuid = other.uuid
this.increment = other.increment
}

override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ class HabitRecord {
@field:Column
var uuid: String? = null

@field:Column
var increment: Double? = 0.0

fun copyFrom(model: Habit) {
id = model.id
name = model.name
Expand All @@ -114,6 +117,7 @@ class HabitRecord {
reminderMin = reminder!!.minute
reminderDays = reminder.days.toInteger()
}
increment = model.increment
}

fun copyTo(habit: Habit) {
Expand All @@ -137,5 +141,6 @@ class HabitRecord {
WeekdayList(reminderDays!!)
)
}
habit.increment = increment!!
}
}
1 change: 1 addition & 0 deletions uhabits-core/src/jvmMain/resources/migrations/26.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table Habits add column increment;
Loading