mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-14 21:18:51 -06:00
Implement all widgets for sub habits
This commit is contained in:
@@ -110,15 +110,6 @@
|
|||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
<activity
|
|
||||||
android:name=".widgets.activities.BooleanHabitPickerDialog"
|
|
||||||
android:exported="true"
|
|
||||||
android:theme="@style/Theme.AppCompat.Light.Dialog">
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
|
|
||||||
</intent-filter>
|
|
||||||
</activity>
|
|
||||||
|
|
||||||
<activity
|
<activity
|
||||||
android:name=".widgets.activities.NumericalHabitPickerDialog"
|
android:name=".widgets.activities.NumericalHabitPickerDialog"
|
||||||
android:exported="true"
|
android:exported="true"
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ class ListHabitsActivity : AppCompatActivity(), Preferences.Listener {
|
|||||||
val habitId = intent.extras?.getLong("habit")
|
val habitId = intent.extras?.getLong("habit")
|
||||||
val timestamp = intent.extras?.getLong("timestamp")
|
val timestamp = intent.extras?.getLong("timestamp")
|
||||||
if (habitId != null && timestamp != null) {
|
if (habitId != null && timestamp != null) {
|
||||||
val habit = appComponent.habitList.getById(habitId)!!
|
val habit = appComponent.habitList.getById(habitId) ?: appComponent.habitGroupList.getHabitByID(habitId)!!
|
||||||
component.listHabitsBehavior.onEdit(habit, Timestamp(timestamp))
|
component.listHabitsBehavior.onEdit(habit, Timestamp(timestamp))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import android.content.Intent
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import org.isoron.uhabits.core.AppScope
|
import org.isoron.uhabits.core.AppScope
|
||||||
import org.isoron.uhabits.core.models.Habit
|
import org.isoron.uhabits.core.models.Habit
|
||||||
|
import org.isoron.uhabits.core.models.HabitGroupList
|
||||||
import org.isoron.uhabits.core.models.HabitList
|
import org.isoron.uhabits.core.models.HabitList
|
||||||
import org.isoron.uhabits.core.models.Timestamp
|
import org.isoron.uhabits.core.models.Timestamp
|
||||||
import org.isoron.uhabits.core.utils.DateUtils
|
import org.isoron.uhabits.core.utils.DateUtils
|
||||||
@@ -31,7 +32,10 @@ import javax.inject.Inject
|
|||||||
|
|
||||||
@AppScope
|
@AppScope
|
||||||
class IntentParser
|
class IntentParser
|
||||||
@Inject constructor(private val habits: HabitList) {
|
@Inject constructor(
|
||||||
|
private val habits: HabitList,
|
||||||
|
private val habitGroups: HabitGroupList
|
||||||
|
) {
|
||||||
|
|
||||||
fun parseCheckmarkIntent(intent: Intent): CheckmarkIntentData {
|
fun parseCheckmarkIntent(intent: Intent): CheckmarkIntentData {
|
||||||
val uri = intent.data ?: throw IllegalArgumentException("uri is null")
|
val uri = intent.data ?: throw IllegalArgumentException("uri is null")
|
||||||
@@ -45,6 +49,7 @@ class IntentParser
|
|||||||
|
|
||||||
private fun parseHabit(uri: Uri): Habit {
|
private fun parseHabit(uri: Uri): Habit {
|
||||||
return habits.getById(parseId(uri))
|
return habits.getById(parseId(uri))
|
||||||
|
?: habitGroups.getHabitByID(parseId(uri))
|
||||||
?: throw IllegalArgumentException("habit not found")
|
?: throw IllegalArgumentException("habit not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,15 +27,16 @@ import android.widget.RemoteViews
|
|||||||
import org.isoron.uhabits.HabitsApplication
|
import org.isoron.uhabits.HabitsApplication
|
||||||
import org.isoron.uhabits.R
|
import org.isoron.uhabits.R
|
||||||
import org.isoron.uhabits.core.models.Habit
|
import org.isoron.uhabits.core.models.Habit
|
||||||
|
import org.isoron.uhabits.core.models.HabitGroupList
|
||||||
import org.isoron.uhabits.core.models.HabitList
|
import org.isoron.uhabits.core.models.HabitList
|
||||||
import org.isoron.uhabits.core.models.HabitNotFoundException
|
import org.isoron.uhabits.core.models.HabitNotFoundException
|
||||||
import org.isoron.uhabits.core.preferences.Preferences
|
import org.isoron.uhabits.core.preferences.Preferences
|
||||||
import org.isoron.uhabits.core.preferences.WidgetPreferences
|
import org.isoron.uhabits.core.preferences.WidgetPreferences
|
||||||
import org.isoron.uhabits.utils.InterfaceUtils.dpToPixels
|
import org.isoron.uhabits.utils.InterfaceUtils.dpToPixels
|
||||||
import java.util.ArrayList
|
|
||||||
|
|
||||||
abstract class BaseWidgetProvider : AppWidgetProvider() {
|
abstract class BaseWidgetProvider : AppWidgetProvider() {
|
||||||
private lateinit var habits: HabitList
|
private lateinit var habits: HabitList
|
||||||
|
private lateinit var habitGroups: HabitGroupList
|
||||||
lateinit var preferences: Preferences
|
lateinit var preferences: Preferences
|
||||||
private set
|
private set
|
||||||
private lateinit var widgetPrefs: WidgetPreferences
|
private lateinit var widgetPrefs: WidgetPreferences
|
||||||
@@ -112,7 +113,8 @@ abstract class BaseWidgetProvider : AppWidgetProvider() {
|
|||||||
val selectedIds = widgetPrefs.getHabitIdsFromWidgetId(widgetId)
|
val selectedIds = widgetPrefs.getHabitIdsFromWidgetId(widgetId)
|
||||||
val selectedHabits = ArrayList<Habit>(selectedIds.size)
|
val selectedHabits = ArrayList<Habit>(selectedIds.size)
|
||||||
for (id in selectedIds) {
|
for (id in selectedIds) {
|
||||||
val h = habits.getById(id) ?: throw HabitNotFoundException()
|
val h = habits.getById(id) ?: habitGroups.getHabitByID(id)
|
||||||
|
?: throw HabitNotFoundException()
|
||||||
selectedHabits.add(h)
|
selectedHabits.add(h)
|
||||||
}
|
}
|
||||||
return selectedHabits
|
return selectedHabits
|
||||||
@@ -159,6 +161,7 @@ abstract class BaseWidgetProvider : AppWidgetProvider() {
|
|||||||
private fun updateDependencies(context: Context) {
|
private fun updateDependencies(context: Context) {
|
||||||
val app = context.applicationContext as HabitsApplication
|
val app = context.applicationContext as HabitsApplication
|
||||||
habits = app.component.habitList
|
habits = app.component.habitList
|
||||||
|
habitGroups = app.component.habitGroupList
|
||||||
preferences = app.component.preferences
|
preferences = app.component.preferences
|
||||||
widgetPrefs = app.component.widgetPreferences
|
widgetPrefs = app.component.widgetPreferences
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import android.appwidget.AppWidgetManager.INVALID_APPWIDGET_ID
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.widget.ArrayAdapter
|
import android.widget.ArrayAdapter
|
||||||
import android.widget.Button
|
|
||||||
import android.widget.ListView
|
import android.widget.ListView
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
import org.isoron.uhabits.HabitsApplication
|
import org.isoron.uhabits.HabitsApplication
|
||||||
@@ -34,11 +33,6 @@ import org.isoron.uhabits.activities.AndroidThemeSwitcher
|
|||||||
import org.isoron.uhabits.core.preferences.WidgetPreferences
|
import org.isoron.uhabits.core.preferences.WidgetPreferences
|
||||||
import org.isoron.uhabits.widgets.WidgetUpdater
|
import org.isoron.uhabits.widgets.WidgetUpdater
|
||||||
|
|
||||||
class BooleanHabitPickerDialog : HabitPickerDialog() {
|
|
||||||
override fun shouldHideNumerical() = true
|
|
||||||
override fun getEmptyMessage() = R.string.no_boolean_habits
|
|
||||||
}
|
|
||||||
|
|
||||||
class NumericalHabitPickerDialog : HabitPickerDialog() {
|
class NumericalHabitPickerDialog : HabitPickerDialog() {
|
||||||
override fun shouldHideBoolean() = true
|
override fun shouldHideBoolean() = true
|
||||||
override fun getEmptyMessage() = R.string.no_numerical_habits
|
override fun getEmptyMessage() = R.string.no_numerical_habits
|
||||||
@@ -50,7 +44,6 @@ open class HabitPickerDialog : Activity() {
|
|||||||
private lateinit var widgetPreferences: WidgetPreferences
|
private lateinit var widgetPreferences: WidgetPreferences
|
||||||
private lateinit var widgetUpdater: WidgetUpdater
|
private lateinit var widgetUpdater: WidgetUpdater
|
||||||
|
|
||||||
protected open fun shouldHideNumerical() = false
|
|
||||||
protected open fun shouldHideBoolean() = false
|
protected open fun shouldHideBoolean() = false
|
||||||
protected open fun getEmptyMessage() = R.string.no_habits
|
protected open fun getEmptyMessage() = R.string.no_habits
|
||||||
|
|
||||||
@@ -59,6 +52,7 @@ open class HabitPickerDialog : Activity() {
|
|||||||
val component = (applicationContext as HabitsApplication).component
|
val component = (applicationContext as HabitsApplication).component
|
||||||
AndroidThemeSwitcher(this, component.preferences).apply()
|
AndroidThemeSwitcher(this, component.preferences).apply()
|
||||||
val habitList = component.habitList
|
val habitList = component.habitList
|
||||||
|
val habitGroupList = component.habitGroupList
|
||||||
widgetPreferences = component.widgetPreferences
|
widgetPreferences = component.widgetPreferences
|
||||||
widgetUpdater = component.widgetUpdater
|
widgetUpdater = component.widgetUpdater
|
||||||
widgetId = intent.extras?.getInt(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID) ?: 0
|
widgetId = intent.extras?.getInt(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID) ?: 0
|
||||||
@@ -67,12 +61,22 @@ open class HabitPickerDialog : Activity() {
|
|||||||
val habitNames = ArrayList<String>()
|
val habitNames = ArrayList<String>()
|
||||||
for (h in habitList) {
|
for (h in habitList) {
|
||||||
if (h.isArchived) continue
|
if (h.isArchived) continue
|
||||||
if (h.isNumerical and shouldHideNumerical()) continue
|
|
||||||
if (!h.isNumerical and shouldHideBoolean()) continue
|
if (!h.isNumerical and shouldHideBoolean()) continue
|
||||||
habitIds.add(h.id!!)
|
habitIds.add(h.id!!)
|
||||||
habitNames.add(h.name)
|
habitNames.add(h.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (hgr in habitGroupList) {
|
||||||
|
if (hgr.isArchived) continue
|
||||||
|
|
||||||
|
for (h in hgr.habitList) {
|
||||||
|
if (h.isArchived) continue
|
||||||
|
if (!h.isNumerical and shouldHideBoolean()) continue
|
||||||
|
habitIds.add(h.id!!)
|
||||||
|
habitNames.add(h.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (habitNames.isEmpty()) {
|
if (habitNames.isEmpty()) {
|
||||||
setContentView(R.layout.widget_empty_activity)
|
setContentView(R.layout.widget_empty_activity)
|
||||||
findViewById<TextView>(R.id.message).setText(getEmptyMessage())
|
findViewById<TextView>(R.id.message).setText(getEmptyMessage())
|
||||||
@@ -81,7 +85,6 @@ open class HabitPickerDialog : Activity() {
|
|||||||
|
|
||||||
setContentView(R.layout.widget_configure_activity)
|
setContentView(R.layout.widget_configure_activity)
|
||||||
val listView = findViewById<ListView>(R.id.listView)
|
val listView = findViewById<ListView>(R.id.listView)
|
||||||
val saveButton = findViewById<Button>(R.id.buttonSave)
|
|
||||||
|
|
||||||
with(listView) {
|
with(listView) {
|
||||||
adapter = ArrayAdapter(
|
adapter = ArrayAdapter(
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
android:previewImage="@drawable/widget_preview_streaks"
|
android:previewImage="@drawable/widget_preview_streaks"
|
||||||
android:resizeMode="vertical|horizontal"
|
android:resizeMode="vertical|horizontal"
|
||||||
android:updatePeriodMillis="3600000"
|
android:updatePeriodMillis="3600000"
|
||||||
android:configure="org.isoron.uhabits.widgets.activities.BooleanHabitPickerDialog"
|
android:configure="org.isoron.uhabits.widgets.activities.HabitPickerDialog"
|
||||||
android:widgetCategory="home_screen">
|
android:widgetCategory="home_screen">
|
||||||
|
|
||||||
</appwidget-provider>
|
</appwidget-provider>
|
||||||
Reference in New Issue
Block a user