Skip to content

Commit

Permalink
Filter out 'hideNotification' in notification list
Browse files Browse the repository at this point in the history
Signed-off-by: mueller-ma <mueller-ma@users.noreply.github.com>
  • Loading branch information
mueller-ma committed Jul 16, 2024
1 parent fcd993c commit a7fa30f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a7fa30f

Please sign in to comment.