From 79a35f8baa70f724ec1009245cd7e7a2b383b78a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Tue, 28 Mar 2023 16:11:07 +0200 Subject: [PATCH] Add support for setting strategy and ttl to beacon tags --- library-no-op/api/library-no-op.api | 3 +++ .../java/com/pushpushgo/sdk/BeaconBuilder.kt | 4 ++-- library/api/library.api | 3 +++ .../java/com/pushpushgo/sdk/BeaconBuilder.kt | 22 +++++++++---------- .../java/com/pushpushgo/sdk/PushPushGo.kt | 2 +- .../java/com/pushpushgo/sdk/data/BeaconTag.kt | 8 +++++++ .../com/pushpushgo/sdk/BeaconBuilderTest.kt | 9 ++++++-- settings.gradle | 2 +- 8 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 library/src/main/java/com/pushpushgo/sdk/data/BeaconTag.kt diff --git a/library-no-op/api/library-no-op.api b/library-no-op/api/library-no-op.api index 9ab7df2..ee2a82e 100644 --- a/library-no-op/api/library-no-op.api +++ b/library-no-op/api/library-no-op.api @@ -1,6 +1,9 @@ public final class com/pushpushgo/sdk/BeaconBuilder { public final fun appendTag (Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder; public final fun appendTag (Ljava/lang/String;Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder; + public final fun appendTag (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder; + public final fun appendTag (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)Lcom/pushpushgo/sdk/BeaconBuilder; + public static synthetic fun appendTag$default (Lcom/pushpushgo/sdk/BeaconBuilder;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IILjava/lang/Object;)Lcom/pushpushgo/sdk/BeaconBuilder; public final fun getTags ()Ljava/util/List; public final fun getTagsToDelete ()Ljava/util/List; public final fun removeTag ([Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder; diff --git a/library-no-op/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt b/library-no-op/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt index 9878ebd..4271dc0 100644 --- a/library-no-op/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt +++ b/library-no-op/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt @@ -2,8 +2,8 @@ package com.pushpushgo.sdk @Suppress("unused", "UNUSED_PARAMETER") class BeaconBuilder internal constructor() { - fun appendTag(tag: String): BeaconBuilder = this - fun appendTag(tag: String, label: String): BeaconBuilder = this + @JvmOverloads + fun appendTag(tag: String, label: String = "default", strategy: String = "append", ttl: Int = 0): BeaconBuilder = this fun getTags(): MutableList> = mutableListOf() fun getTagsToDelete(): MutableList = mutableListOf() fun removeTag(vararg name: String): BeaconBuilder = this diff --git a/library/api/library.api b/library/api/library.api index 9ab7df2..ee2a82e 100644 --- a/library/api/library.api +++ b/library/api/library.api @@ -1,6 +1,9 @@ public final class com/pushpushgo/sdk/BeaconBuilder { public final fun appendTag (Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder; public final fun appendTag (Ljava/lang/String;Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder; + public final fun appendTag (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder; + public final fun appendTag (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)Lcom/pushpushgo/sdk/BeaconBuilder; + public static synthetic fun appendTag$default (Lcom/pushpushgo/sdk/BeaconBuilder;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;IILjava/lang/Object;)Lcom/pushpushgo/sdk/BeaconBuilder; public final fun getTags ()Ljava/util/List; public final fun getTagsToDelete ()Ljava/util/List; public final fun removeTag ([Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder; diff --git a/library/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt b/library/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt index 535af09..32d4951 100644 --- a/library/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt +++ b/library/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt @@ -1,5 +1,6 @@ package com.pushpushgo.sdk +import com.pushpushgo.sdk.data.BeaconTag import com.pushpushgo.sdk.exception.PushPushException import com.pushpushgo.sdk.work.UploadDelegate import org.json.JSONArray @@ -9,7 +10,7 @@ class BeaconBuilder internal constructor(private val uploadDelegate: UploadDeleg private val selectors = mutableMapOf() - private val tags = mutableListOf>() + private val tags = mutableListOf() private val tagsToDelete = mutableListOf() @@ -31,23 +32,20 @@ class BeaconBuilder internal constructor(private val uploadDelegate: UploadDeleg /** * @param tag Tag name * @param label Tag label + * @param strategy Determining whether the tags assigned to the given label should be accumulated (append) or overwritten (rewrite) (then the subscriber may have only one, most up-to-date tag at a time) + * @param ttl Time to Live (TTL) is a parameter that specifies the time (in days or hours) after which a given tag is going to be removed. If you don't want to remove tags, type 0. * * @return instance of builder */ - fun appendTag(tag: String, label: String): BeaconBuilder { - tags.add(tag to label) - - return this - } - - fun appendTag(tag: String): BeaconBuilder { - tags.add(tag to "default") + @JvmOverloads + fun appendTag(tag: String, label: String = "default", strategy: String = "append", ttl: Int = 0): BeaconBuilder { + tags.add(BeaconTag(tag = tag, label = label, strategy = strategy, ttl = ttl)) return this } fun getTags(): MutableList> { - return tags + return tags.map { it.tag to it.label }.toMutableList() } /** @@ -112,10 +110,12 @@ class BeaconBuilder internal constructor(private val uploadDelegate: UploadDeleg tags.ifEmpty { return } put("tags", JSONArray().apply { - tags.forEach { (tag, label) -> + tags.forEach { (tag, label, strategy, ttl) -> put(JSONObject().apply { put("tag", tag) put("label", label) + put("strategy", strategy) + put("ttl", ttl) }) } }) diff --git a/library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt b/library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt index 2e6a83f..0fc1e97 100644 --- a/library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt +++ b/library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt @@ -31,7 +31,7 @@ class PushPushGo private constructor( ) { companion object { - const val VERSION = "2.0.0-20230328~1" + const val VERSION = "2.0.0-20230328~2" internal const val TAG = "PPGo" diff --git a/library/src/main/java/com/pushpushgo/sdk/data/BeaconTag.kt b/library/src/main/java/com/pushpushgo/sdk/data/BeaconTag.kt new file mode 100644 index 0000000..bfb471f --- /dev/null +++ b/library/src/main/java/com/pushpushgo/sdk/data/BeaconTag.kt @@ -0,0 +1,8 @@ +package com.pushpushgo.sdk.data + +internal data class BeaconTag( + val tag: String, + val label: String, + val strategy: String = "append", + val ttl: Int = 0, +) diff --git a/library/src/test/java/com/pushpushgo/sdk/BeaconBuilderTest.kt b/library/src/test/java/com/pushpushgo/sdk/BeaconBuilderTest.kt index 819703d..eebb945 100644 --- a/library/src/test/java/com/pushpushgo/sdk/BeaconBuilderTest.kt +++ b/library/src/test/java/com/pushpushgo/sdk/BeaconBuilderTest.kt @@ -145,8 +145,13 @@ internal class BeaconBuilderTest { assertEquals(tags[2].second, "label3") verify { - uploadDelegate.sendBeacon(match { - (it["tags"] as JSONArray).get(0).toString() == """{"tag":"tag1","label":"label1"}""" + uploadDelegate.sendBeacon(withArg { + val tag = (it["tags"] as JSONArray).getJSONObject(0) + + assertEquals("tag1", tag["tag"]) + assertEquals("label1", tag["label"]) + assertEquals("append", tag["strategy"]) + assertEquals(0, tag["ttl"]) }) } } diff --git a/settings.gradle b/settings.gradle index 321efbd..c7f3bd6 100644 --- a/settings.gradle +++ b/settings.gradle @@ -4,7 +4,7 @@ if (!System.env.JITPACK) include ':sample', ':samplehms', ':samplejava' dependencyResolutionManagement { versionCatalogs { libs { - version('sdk', '2.0.0') + version('sdk', '2.1.0-SNAPSHOT') version('kotlin', '1.8.10') version('coroutines', '1.6.4') version('kodein', '7.19.0')