From a7fa30f4d732879caa0a391b2a6cb763b96cac70 Mon Sep 17 00:00:00 2001 From: mueller-ma Date: Tue, 16 Jul 2024 13:31:35 +0200 Subject: [PATCH] Filter out 'hideNotification' in notification list Signed-off-by: mueller-ma --- .../openhab/habdroid/model/CloudNotification.kt | 17 ++++++++++++++++- .../ui/CloudNotificationListFragment.kt | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt index 4956565d8a..6fb2a4f47e 100644 --- a/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt +++ b/mobile/src/main/java/org/openhab/habdroid/model/CloudNotification.kt @@ -44,8 +44,21 @@ data class CloudNotificationId internal constructor( val notificationId get() = (referenceId ?: persistedId).hashCode() } +@Parcelize +enum class CloudNotificationType : Parcelable { + NOTIFICATION, + HIDE_NOTIFICATION +} + +fun String.toCloudNotificationType() = when (this) { + "notification" -> CloudNotificationType.NOTIFICATION + "hideNotification" -> CloudNotificationType.HIDE_NOTIFICATION + else -> null +} + @Parcelize data class CloudNotification internal constructor( + val type: CloudNotificationType, val id: CloudNotificationId, val title: String, val message: String, @@ -117,9 +130,11 @@ fun JSONObject.toCloudNotification(): CloudNotification { val payload = optJSONObject("payload") return CloudNotification( + // Old notifications don't contain "type", so fallback to normal notifications here. + type = payload?.optString("type")?.toCloudNotificationType() ?: CloudNotificationType.NOTIFICATION, id = CloudNotificationId(getString("_id"), payload?.optStringOrNull("reference-id")), title = payload?.optString("title").orEmpty(), - message = payload?.getString("message") ?: getString("message"), + message = payload?.optString("message") ?: optString("message"), createdTimestamp = created, icon = payload?.optStringOrNull("icon").toOH2IconResource() ?: optStringOrNull("icon").toOH2IconResource(), tag = payload?.optStringOrNull("tag") ?: optStringOrNull("severity"), diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/CloudNotificationListFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/CloudNotificationListFragment.kt index c2bdd55667..a213ff074b 100644 --- a/mobile/src/main/java/org/openhab/habdroid/ui/CloudNotificationListFragment.kt +++ b/mobile/src/main/java/org/openhab/habdroid/ui/CloudNotificationListFragment.kt @@ -36,6 +36,7 @@ import org.json.JSONArray import org.json.JSONException import org.openhab.habdroid.R import org.openhab.habdroid.core.connection.ConnectionFactory +import org.openhab.habdroid.model.CloudNotificationType import org.openhab.habdroid.model.ServerConfiguration import org.openhab.habdroid.model.toCloudNotification import org.openhab.habdroid.util.HttpClient @@ -152,6 +153,7 @@ class CloudNotificationListFragment : Fragment(), View.OnClickListener, SwipeRef try { val response = conn.httpClient.get(url).asText().response val items = JSONArray(response).map { obj -> obj.toCloudNotification() } + .filter { notification -> notification.type == CloudNotificationType.NOTIFICATION } Log.d(TAG, "Notifications request success, got ${items.size} items") loadOffset += items.size adapter.addLoadedItems(items, items.size == PAGE_SIZE)