Skip to content

Commit

Permalink
Add option to define remove tag with a label
Browse files Browse the repository at this point in the history
  • Loading branch information
mklkj committed Sep 5, 2024
1 parent e31a6b8 commit 1fe9cfe
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
1 change: 1 addition & 0 deletions library/api/library.api
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public final class com/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;Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder;
public final fun removeTag ([Ljava/lang/String;)Lcom/pushpushgo/sdk/BeaconBuilder;
public final fun send ()V
public final fun set (Ljava/lang/String;Ljava/lang/Object;)Lcom/pushpushgo/sdk/BeaconBuilder;
Expand Down
35 changes: 29 additions & 6 deletions library/src/main/java/com/pushpushgo/sdk/BeaconBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class BeaconBuilder internal constructor(private val uploadDelegate: UploadDeleg

private val tags = mutableListOf<BeaconTag>()

private val tagsToDelete = mutableListOf<String>()
private val tagsToDelete = mutableMapOf<String, String>()

private var customId = ""

Expand Down Expand Up @@ -54,13 +54,27 @@ class BeaconBuilder internal constructor(private val uploadDelegate: UploadDeleg
* @return instance of builder
*/
fun removeTag(vararg name: String): BeaconBuilder {
tagsToDelete.addAll(name)
tagsToDelete.putAll(name.map { it to "default" })

return this
}

fun getTagsToDelete(): MutableList<String> {
return tagsToDelete
/**
* @param tags Map of tags to remove
*
* key: tag name
* value: tag label
*
* @return instance of builder
*/
fun removeTags(tags: Map<String, String>): BeaconBuilder {
tagsToDelete.putAll(tags)

return this
}

fun getTagsToDelete(): List<String> {
return tagsToDelete.keys.toList()
}

/**
Expand Down Expand Up @@ -125,8 +139,17 @@ class BeaconBuilder internal constructor(private val uploadDelegate: UploadDeleg
tagsToDelete.ifEmpty { return }

put("tagsToDelete", JSONArray().apply {
tagsToDelete.forEach {
put(it)
if (tagsToDelete.all { it.value == "default" }) {
tagsToDelete.forEach {
put(it.key)
}
} else {
tagsToDelete.forEach { (tag, label) ->
put(JSONObject().apply {
put("tag", tag)
put("label", label)
})
}
}
})
}
Expand Down
20 changes: 20 additions & 0 deletions library/src/test/java/com/pushpushgo/sdk/BeaconBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ internal class BeaconBuilderTest {
}
}

@Test
fun `remove tag with a custom label`() {
every { uploadDelegate.sendBeacon(any()) } just Runs
beaconBuilder = BeaconBuilder(uploadDelegate)

beaconBuilder.removeTags(mapOf("tag_name" to "test_label"))
beaconBuilder.send()

val tags = beaconBuilder.getTagsToDelete()

assertEquals(1, tags.size)
assertEquals(tags[0], "tag_name")

verify {
uploadDelegate.sendBeacon(match {
it["tagsToDelete"].toString() == """[{"tag":"tag_name","label":"test_label"}]"""
})
}
}

@Test
fun `set custom id`() {
every { uploadDelegate.sendBeacon(any()) } just Runs
Expand Down

0 comments on commit 1fe9cfe

Please sign in to comment.