From 3d4b0e26ec2080fb9a2361a313ac42063d173ebf Mon Sep 17 00:00:00 2001 From: sim Date: Thu, 28 Nov 2024 14:45:24 +0000 Subject: [PATCH] Save last event id per urgency --- .../distributor/nextpush/AppStore.kt | 40 ++++++++++++++++--- .../distributor/nextpush/LastEventId.kt | 28 +++++++++++++ .../distributor/nextpush/api/Api.kt | 4 +- .../distributor/nextpush/api/SSEListener.kt | 3 +- 4 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/org/unifiedpush/distributor/nextpush/LastEventId.kt diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/AppStore.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/AppStore.kt index 7148fec..a47dfad 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/AppStore.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/AppStore.kt @@ -14,22 +14,52 @@ class AppStore(context: Context) : Store(context) { .putOrRemove(PREF_DEVICE_ID, value) .apply() - var lastEventId: String? + var lastHighEventId: String? get() = sharedPreferences - .getString(PREF_LAST_EVENT_ID, null) + .getString(PREF_LAST_EVENT_ID_HIGH, null) set(value) = sharedPreferences .edit() - .putOrRemove(PREF_LAST_EVENT_ID, value) + .putOrRemove(PREF_LAST_EVENT_ID_HIGH, value) + .apply() + + var lastNormalEventId: String? + get() = sharedPreferences + .getString(PREF_LAST_EVENT_ID_NORMAL, null) + set(value) = sharedPreferences + .edit() + .putOrRemove(PREF_LAST_EVENT_ID_NORMAL, value) + .apply() + + var lastLowEventId: String? + get() = sharedPreferences + .getString(PREF_LAST_EVENT_ID_LOW, null) + set(value) = sharedPreferences + .edit() + .putOrRemove(PREF_LAST_EVENT_ID_LOW, value) + .apply() + + var lastVeryLowEventId: String? + get() = sharedPreferences + .getString(PREF_LAST_EVENT_ID_VERY_LOW, null) + set(value) = sharedPreferences + .edit() + .putOrRemove(PREF_LAST_EVENT_ID_VERY_LOW, value) .apply() override fun wipe() { deviceId = null - lastEventId = null + lastHighEventId = null + lastNormalEventId = null + lastLowEventId = null + lastVeryLowEventId = null account.wipe() } companion object { private const val PREF_DEVICE_ID = "deviceId" - private const val PREF_LAST_EVENT_ID = "lastEventId" + private const val PREF_LAST_EVENT_ID_HIGH = "lastEventId.high" + private const val PREF_LAST_EVENT_ID_NORMAL = "lastEventId.normal" + private const val PREF_LAST_EVENT_ID_LOW = "lastEventId.low" + private const val PREF_LAST_EVENT_ID_VERY_LOW = "lastEventId.very-low" } } diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/LastEventId.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/LastEventId.kt new file mode 100644 index 0000000..ab227e6 --- /dev/null +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/LastEventId.kt @@ -0,0 +1,28 @@ +package org.unifiedpush.distributor.nextpush + +class LastEventId(private val store: AppStore) { + + fun save(id: String) { + when { + id.startsWith("high.") -> store.lastHighEventId = id + id.startsWith("normal.") -> store.lastNormalEventId = id + id.startsWith("low.") -> store.lastLowEventId = id + id.startsWith("very-low.") -> store.lastVeryLowEventId = id + } + } + + fun get(): String? { + return arrayOf( + store.lastHighEventId, + store.lastNormalEventId, + store.lastLowEventId, + store.lastVeryLowEventId + ).filterNotNull().let { + if (it.isEmpty()) { + null + } else { + it.joinToString(",") + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt index 7fe8b79..30839ff 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/Api.kt @@ -15,6 +15,7 @@ import okhttp3.Request import okhttp3.sse.EventSource import okhttp3.sse.EventSources import org.unifiedpush.distributor.nextpush.AppStore +import org.unifiedpush.distributor.nextpush.LastEventId import org.unifiedpush.distributor.nextpush.account.AccountFactory.getAccount import org.unifiedpush.distributor.nextpush.account.AccountType import org.unifiedpush.distributor.nextpush.api.provider.* @@ -95,7 +96,7 @@ class Api(context: Context) { val request = Request.Builder().url(url) .get() .apply { - store.lastEventId?.let { + LastEventId(store).get()?.let { header("Last-Event-ID", it) } } @@ -139,7 +140,6 @@ class Api(context: Context) { } }) store.deviceId = null - store.lastEventId = null } } catch (e: NoProviderException) { e.printStackTrace() diff --git a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt index ce4ea69..24638ec 100644 --- a/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt +++ b/app/src/main/java/org/unifiedpush/distributor/nextpush/api/SSEListener.kt @@ -16,6 +16,7 @@ import okhttp3.sse.EventSourceListener import org.unifiedpush.distributor.nextpush.AppCompanion import org.unifiedpush.distributor.nextpush.AppStore import org.unifiedpush.distributor.nextpush.Database +import org.unifiedpush.distributor.nextpush.LastEventId import org.unifiedpush.distributor.nextpush.WakeLock import org.unifiedpush.distributor.nextpush.api.response.SSEResponse import org.unifiedpush.distributor.nextpush.distributor.Distributor.deleteAppFromSSE @@ -85,7 +86,7 @@ class SSEListener(val context: Context) : EventSourceListener() { message.token, Base64.decode(message.message, Base64.DEFAULT) ) - store.lastEventId = id + id?.let { LastEventId(store).save(it) } } "deleteApp" -> {