mirror of
https://github.com/home-assistant/android
synced 2024-09-18 23:52:51 +00:00
Notification history filter options (#1069)
* Add history filter for notifications * Remove command filters and reorganize the history * Clean up category string * Add a missing string, clean up some warnings
This commit is contained in:
parent
30cc3230bd
commit
37c176bed4
|
@ -17,8 +17,8 @@ interface NotificationDao {
|
||||||
@Query("SELECT * FROM notification_history ORDER BY received DESC")
|
@Query("SELECT * FROM notification_history ORDER BY received DESC")
|
||||||
fun getAll(): Array<NotificationItem>?
|
fun getAll(): Array<NotificationItem>?
|
||||||
|
|
||||||
@Query("SELECT * FROM notification_history ORDER BY received DESC LIMIT 25")
|
@Query("SELECT * FROM notification_history ORDER BY received DESC LIMIT (:amount)")
|
||||||
fun getLast25(): Array<NotificationItem>?
|
fun getLastItems(amount: Int): Array<NotificationItem>?
|
||||||
|
|
||||||
@Query("DELETE FROM notification_history WHERE id = :id")
|
@Query("DELETE FROM notification_history WHERE id = :id")
|
||||||
fun delete(id: Int)
|
fun delete(id: Int)
|
||||||
|
|
|
@ -2,12 +2,14 @@ package io.homeassistant.companion.android.settings.notification
|
||||||
|
|
||||||
import android.app.AlertDialog
|
import android.app.AlertDialog
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import androidx.preference.DropDownPreference
|
||||||
import androidx.preference.Preference
|
import androidx.preference.Preference
|
||||||
import androidx.preference.PreferenceCategory
|
import androidx.preference.PreferenceCategory
|
||||||
import androidx.preference.PreferenceFragmentCompat
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
import io.homeassistant.companion.android.R
|
import io.homeassistant.companion.android.R
|
||||||
import io.homeassistant.companion.android.database.AppDatabase
|
import io.homeassistant.companion.android.database.AppDatabase
|
||||||
import io.homeassistant.companion.android.database.notification.NotificationDao
|
import io.homeassistant.companion.android.database.notification.NotificationDao
|
||||||
|
import io.homeassistant.companion.android.database.notification.NotificationItem
|
||||||
import java.util.Calendar
|
import java.util.Calendar
|
||||||
import java.util.GregorianCalendar
|
import java.util.GregorianCalendar
|
||||||
|
|
||||||
|
@ -27,45 +29,48 @@ class NotificationHistoryFragment : PreferenceFragmentCompat() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
|
|
||||||
val notificationDao = AppDatabase.getInstance(requireContext()).notificationDao()
|
val notificationDao = AppDatabase.getInstance(requireContext()).notificationDao()
|
||||||
val notificationList = notificationDao.getLast25()
|
val notificationList = notificationDao.getLastItems(25)
|
||||||
|
|
||||||
val prefCategory = findPreference<PreferenceCategory>("list_notifications")
|
val prefCategory = findPreference<PreferenceCategory>("list_notifications")
|
||||||
if (!notificationList.isNullOrEmpty()) {
|
if (!notificationList.isNullOrEmpty()) {
|
||||||
prefCategory?.isVisible = true
|
prefCategory?.isVisible = true
|
||||||
prefCategory?.removeAll()
|
prefCategory?.title = getString(R.string.last_num_notifications, 25)
|
||||||
for (item in notificationList) {
|
reloadNotifications(notificationList, prefCategory)
|
||||||
val pref = Preference(preferenceScreen.context)
|
|
||||||
val cal: Calendar = GregorianCalendar()
|
|
||||||
cal.timeInMillis = item.received
|
|
||||||
pref.key = item.id.toString()
|
|
||||||
pref.title = cal.time.toString()
|
|
||||||
pref.summary = item.message
|
|
||||||
pref.isIconSpaceReserved = false
|
|
||||||
|
|
||||||
pref.setOnPreferenceClickListener {
|
|
||||||
parentFragmentManager
|
|
||||||
.beginTransaction()
|
|
||||||
.replace(
|
|
||||||
R.id.content,
|
|
||||||
NotificationDetailFragment.newInstance(
|
|
||||||
item
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.addToBackStack("Notification Detail")
|
|
||||||
.commit()
|
|
||||||
return@setOnPreferenceClickListener true
|
|
||||||
}
|
|
||||||
|
|
||||||
prefCategory?.addPreference(pref)
|
|
||||||
}
|
|
||||||
|
|
||||||
findPreference<PreferenceCategory>("manage_notifications")?.let {
|
findPreference<PreferenceCategory>("manage_notifications")?.let {
|
||||||
it.isVisible = true
|
it.isVisible = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
findPreference<DropDownPreference>("filter_notifications")?.let {
|
||||||
|
it.isVisible = true
|
||||||
|
it.setOnPreferenceChangeListener { _, newValue ->
|
||||||
|
when (newValue) {
|
||||||
|
"last25" -> {
|
||||||
|
prefCategory?.title = getString(R.string.last_num_notifications, 25)
|
||||||
|
reloadNotifications(notificationList, prefCategory)
|
||||||
|
}
|
||||||
|
"last50" -> {
|
||||||
|
val newList = notificationDao.getLastItems(50)
|
||||||
|
prefCategory?.title = getString(R.string.last_num_notifications, 50)
|
||||||
|
reloadNotifications(newList, prefCategory)
|
||||||
|
}
|
||||||
|
"last100" -> {
|
||||||
|
val newList = notificationDao.getLastItems(100)
|
||||||
|
prefCategory?.title = getString(R.string.last_num_notifications, 100)
|
||||||
|
reloadNotifications(newList, prefCategory)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
prefCategory?.title = getString(R.string.last_num_notifications, 25)
|
||||||
|
reloadNotifications(notificationList, prefCategory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return@setOnPreferenceChangeListener true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
findPreference<Preference>("delete_all")?.let {
|
findPreference<Preference>("delete_all")?.let {
|
||||||
it.isVisible = true
|
it.isVisible = true
|
||||||
it.setOnPreferenceClickListener { _ ->
|
it.setOnPreferenceClickListener {
|
||||||
deleteAllConfirmation(notificationDao)
|
deleteAllConfirmation(notificationDao)
|
||||||
return@setOnPreferenceClickListener true
|
return@setOnPreferenceClickListener true
|
||||||
}
|
}
|
||||||
|
@ -106,4 +111,35 @@ class NotificationHistoryFragment : PreferenceFragmentCompat() {
|
||||||
val alert: AlertDialog? = builder.create()
|
val alert: AlertDialog? = builder.create()
|
||||||
alert?.show()
|
alert?.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun reloadNotifications(notificationList: Array<NotificationItem>?, prefCategory: PreferenceCategory?) {
|
||||||
|
prefCategory?.removeAll()
|
||||||
|
if (notificationList != null) {
|
||||||
|
for (item in notificationList) {
|
||||||
|
val pref = Preference(preferenceScreen.context)
|
||||||
|
val cal: Calendar = GregorianCalendar()
|
||||||
|
cal.timeInMillis = item.received
|
||||||
|
pref.key = item.id.toString()
|
||||||
|
pref.title = cal.time.toString()
|
||||||
|
pref.summary = item.message
|
||||||
|
pref.isIconSpaceReserved = false
|
||||||
|
|
||||||
|
pref.setOnPreferenceClickListener {
|
||||||
|
parentFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(
|
||||||
|
R.id.content,
|
||||||
|
NotificationDetailFragment.newInstance(
|
||||||
|
item
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.addToBackStack("Notification Detail")
|
||||||
|
.commit()
|
||||||
|
return@setOnPreferenceClickListener true
|
||||||
|
}
|
||||||
|
|
||||||
|
prefCategory?.addPreference(pref)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
9
app/src/main/res/drawable/ic_filter.xml
Normal file
9
app/src/main/res/drawable/ic_filter.xml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="24dp"
|
||||||
|
android:height="24dp"
|
||||||
|
android:viewportHeight="24"
|
||||||
|
android:viewportWidth="24">
|
||||||
|
<path
|
||||||
|
android:fillColor="@color/colorAccent"
|
||||||
|
android:pathData="M3,2H21V2H21V4H20.92L15,9.92V22.91L9,16.91V9.91L3.09,4H3V2M11,16.08L13,18.08V9H13.09L18.09,4H5.92L10.92,9H11V16.08Z"/>
|
||||||
|
</vector>
|
13
app/src/main/res/values/arrays.xml
Normal file
13
app/src/main/res/values/arrays.xml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
<string-array name="filter_notifications_entries">
|
||||||
|
<item>Show last 25 notifications</item>
|
||||||
|
<item>Show last 50 notifications</item>
|
||||||
|
<item>Show last 100 notifications</item>
|
||||||
|
</string-array>
|
||||||
|
<string-array name="filter_notifications_values">
|
||||||
|
<item>last25</item>
|
||||||
|
<item>last50</item>
|
||||||
|
<item>last100</item>
|
||||||
|
</string-array>
|
||||||
|
</resources>
|
|
@ -180,8 +180,9 @@ Home Assistant instance</string>
|
||||||
<string name="notification_history_summary">History of Notifications (currently displays the last 25 notifications received)</string>
|
<string name="notification_history_summary">History of Notifications (currently displays the last 25 notifications received)</string>
|
||||||
<string name="notification_message">Message</string>
|
<string name="notification_message">Message</string>
|
||||||
<string name="notification_received_at">Notification Received At</string>
|
<string name="notification_received_at">Notification Received At</string>
|
||||||
<string name="last_25_notifications">Last 25 Notifications</string>
|
<string name="last_num_notifications">Last %1$d Notifications</string>
|
||||||
<string name="no_notifications">No Notifications</string>
|
<string name="no_notifications">No Notifications</string>
|
||||||
|
<string name="filter_notifications">Filter Notifications</string>
|
||||||
<string name="no_notifications_summary">You have not received any notifications yet</string>
|
<string name="no_notifications_summary">You have not received any notifications yet</string>
|
||||||
<string name="other_settings">Other Settings</string>
|
<string name="other_settings">Other Settings</string>
|
||||||
<string name="password">Password</string>
|
<string name="password">Password</string>
|
||||||
|
|
|
@ -15,6 +15,15 @@
|
||||||
android:title="@string/manage_all_notifications"
|
android:title="@string/manage_all_notifications"
|
||||||
app:isPreferenceVisible="false"
|
app:isPreferenceVisible="false"
|
||||||
app:iconSpaceReserved="false">
|
app:iconSpaceReserved="false">
|
||||||
|
<androidx.preference.DropDownPreference
|
||||||
|
android:key="filter_notifications"
|
||||||
|
android:title="@string/filter_notifications"
|
||||||
|
android:entries="@array/filter_notifications_entries"
|
||||||
|
android:entryValues="@array/filter_notifications_values"
|
||||||
|
app:defaultValue="last25"
|
||||||
|
app:icon="@drawable/ic_filter"
|
||||||
|
app:isPreferenceVisible="false"
|
||||||
|
app:useSimpleSummaryProvider="true" />
|
||||||
<Preference
|
<Preference
|
||||||
android:key="delete_all"
|
android:key="delete_all"
|
||||||
android:title="@string/delete_all_notifications"
|
android:title="@string/delete_all_notifications"
|
||||||
|
@ -23,7 +32,7 @@
|
||||||
</PreferenceCategory>
|
</PreferenceCategory>
|
||||||
<PreferenceCategory
|
<PreferenceCategory
|
||||||
android:key="list_notifications"
|
android:key="list_notifications"
|
||||||
android:title="@string/last_25_notifications"
|
android:title="@string/last_num_notifications"
|
||||||
app:iconSpaceReserved="false"
|
app:iconSpaceReserved="false"
|
||||||
app:isPreferenceVisible="false" />
|
app:isPreferenceVisible="false" />
|
||||||
</androidx.preference.PreferenceScreen>
|
</androidx.preference.PreferenceScreen>
|
Loading…
Reference in a new issue