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:
Daniel Shokouhi 2020-10-17 20:10:05 -07:00 committed by GitHub
parent 30cc3230bd
commit 37c176bed4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 32 deletions

View file

@ -17,8 +17,8 @@ interface NotificationDao {
@Query("SELECT * FROM notification_history ORDER BY received DESC")
fun getAll(): Array<NotificationItem>?
@Query("SELECT * FROM notification_history ORDER BY received DESC LIMIT 25")
fun getLast25(): Array<NotificationItem>?
@Query("SELECT * FROM notification_history ORDER BY received DESC LIMIT (:amount)")
fun getLastItems(amount: Int): Array<NotificationItem>?
@Query("DELETE FROM notification_history WHERE id = :id")
fun delete(id: Int)

View file

@ -2,12 +2,14 @@ package io.homeassistant.companion.android.settings.notification
import android.app.AlertDialog
import android.os.Bundle
import androidx.preference.DropDownPreference
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.database.AppDatabase
import io.homeassistant.companion.android.database.notification.NotificationDao
import io.homeassistant.companion.android.database.notification.NotificationItem
import java.util.Calendar
import java.util.GregorianCalendar
@ -27,45 +29,48 @@ class NotificationHistoryFragment : PreferenceFragmentCompat() {
super.onResume()
val notificationDao = AppDatabase.getInstance(requireContext()).notificationDao()
val notificationList = notificationDao.getLast25()
val notificationList = notificationDao.getLastItems(25)
val prefCategory = findPreference<PreferenceCategory>("list_notifications")
if (!notificationList.isNullOrEmpty()) {
prefCategory?.isVisible = true
prefCategory?.removeAll()
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)
}
prefCategory?.title = getString(R.string.last_num_notifications, 25)
reloadNotifications(notificationList, prefCategory)
findPreference<PreferenceCategory>("manage_notifications")?.let {
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 {
it.isVisible = true
it.setOnPreferenceClickListener { _ ->
it.setOnPreferenceClickListener {
deleteAllConfirmation(notificationDao)
return@setOnPreferenceClickListener true
}
@ -106,4 +111,35 @@ class NotificationHistoryFragment : PreferenceFragmentCompat() {
val alert: AlertDialog? = builder.create()
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)
}
}
}
}

View 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>

View 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>

View file

@ -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_message">Message</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="filter_notifications">Filter Notifications</string>
<string name="no_notifications_summary">You have not received any notifications yet</string>
<string name="other_settings">Other Settings</string>
<string name="password">Password</string>

View file

@ -15,6 +15,15 @@
android:title="@string/manage_all_notifications"
app:isPreferenceVisible="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
android:key="delete_all"
android:title="@string/delete_all_notifications"
@ -23,7 +32,7 @@
</PreferenceCategory>
<PreferenceCategory
android:key="list_notifications"
android:title="@string/last_25_notifications"
android:title="@string/last_num_notifications"
app:iconSpaceReserved="false"
app:isPreferenceVisible="false" />
</androidx.preference.PreferenceScreen>