Skip to content

Commit

Permalink
Add support for setting strategy and ttl to beacon tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mklkj committed Mar 28, 2023
1 parent b0d3197 commit 79a35f8
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
3 changes: 3 additions & 0 deletions library-no-op/api/library-no-op.api
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pair<String, String>> = mutableListOf()
fun getTagsToDelete(): MutableList<String> = mutableListOf()
fun removeTag(vararg name: String): BeaconBuilder = this
Expand Down
3 changes: 3 additions & 0 deletions library/api/library.api
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
22 changes: 11 additions & 11 deletions library/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,7 +10,7 @@ class BeaconBuilder internal constructor(private val uploadDelegate: UploadDeleg

private val selectors = mutableMapOf<String, Any>()

private val tags = mutableListOf<Pair<String, String>>()
private val tags = mutableListOf<BeaconTag>()

private val tagsToDelete = mutableListOf<String>()

Expand All @@ -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<Pair<String, String>> {
return tags
return tags.map { it.tag to it.label }.toMutableList()
}

/**
Expand Down Expand Up @@ -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)
})
}
})
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/pushpushgo/sdk/PushPushGo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
8 changes: 8 additions & 0 deletions library/src/main/java/com/pushpushgo/sdk/data/BeaconTag.kt
Original file line number Diff line number Diff line change
@@ -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,
)
9 changes: 7 additions & 2 deletions library/src/test/java/com/pushpushgo/sdk/BeaconBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 79a35f8

Please sign in to comment.