Skip to content

Commit 613c6d1

Browse files
committed
Merge pull request #1893 from OneSignal/add-public-getTags-method
Add public get tags method
2 parents 11b402d + fea61e2 commit 613c6d1

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ The user name space is accessible via `OneSignal.User` (in Kotlin) or `OneSignal
271271
| `fun addTags(tags: Map<String, String>)` | `void addTags(Map<String, String> tags)` | *Add multiple tags for the current user. Tags are key:value pairs used as building blocks for targeting specific users and/or personalizing messages. If the tag key already exists, it will be replaced with the value provided here.* |
272272
| `fun removeTag(key: String)` | `void removeTag(String key)` | *Remove the data tag with the provided key from the current user.* |
273273
| `fun removeTags(keys: Collection<String>)` | `void removeTags(Collection<String> keys)` | *Remove multiple tags from the current user.* |
274-
274+
| `fun getTags()` | `Map<String, String> getTags()` | *Return a copy of all local tags from the current user.*
275275

276276
**Session Namespace**
277277
The session namespace is accessible via `OneSignal.Session` (in Kotlin) or `OneSignal.getSession()` (in Java) and provides access to session-scoped functionality.

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/IUserManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,9 @@ interface IUserManager {
133133
* @param keys The collection of keys, all of which will be removed from the current user.
134134
*/
135135
fun removeTags(keys: Collection<String>)
136+
137+
/**
138+
* Return a copy of all local tags from the current user.
139+
*/
140+
fun getTags(): Map<String, String>
136141
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@ internal open class UserManager(
2323
val externalId: String?
2424
get() = _identityModel.externalId
2525

26-
val tags: Map<String, String>
27-
get() = _propertiesModel.tags
28-
2926
val aliases: Map<String, String>
3027
get() = _identityModel.filter { it.key != IdentityModel::id.name }.toMap()
3128

@@ -218,4 +215,8 @@ internal open class UserManager(
218215
_propertiesModel.tags.remove(it)
219216
}
220217
}
218+
219+
override fun getTags(): Map<String, String> {
220+
return _propertiesModel.tags.toMap()
221+
}
221222
}

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/user/internal/UserManagerTests.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.onesignal.user.internal.subscriptions.ISubscriptionManager
66
import com.onesignal.user.internal.subscriptions.SubscriptionList
77
import io.kotest.core.spec.style.FunSpec
88
import io.kotest.matchers.shouldBe
9+
import io.kotest.matchers.shouldNotBe
910
import io.kotest.runner.junit4.KotestTestRunner
1011
import io.mockk.every
1112
import io.mockk.just
@@ -107,8 +108,8 @@ class UserManagerTests : FunSpec({
107108
UserManager(mockSubscriptionManager, MockHelper.identityModelStore(), propertiesModelStore, MockHelper.languageContext())
108109

109110
// When
110-
val tag1 = userManager.tags["my-tag-key1"]
111-
val tag2 = userManager.tags["my-tag-key2"]
111+
val tag1 = propertiesModelStore.model.tags["my-tag-key1"]
112+
val tag2 = propertiesModelStore.model.tags["my-tag-key2"]
112113

113114
// add
114115
userManager.addTag("my-tag-key5", "my-tag-value5")
@@ -135,6 +136,31 @@ class UserManagerTests : FunSpec({
135136
propertiesModelStore.model.tags["my-tag-key3"] shouldBe null
136137
}
137138

139+
test("getTags returns a copy of tags") {
140+
// Given
141+
val mockSubscriptionManager = mockk<ISubscriptionManager>()
142+
val propertiesModelStore =
143+
MockHelper.propertiesModelStore {
144+
it.tags["my-tag-key1"] = "my-tag-value1"
145+
}
146+
147+
val userManager = UserManager(mockSubscriptionManager, MockHelper.identityModelStore(), propertiesModelStore, MockHelper.languageContext())
148+
149+
// When
150+
val tagSnapshot1 = userManager.getTags()
151+
152+
// Then
153+
tagSnapshot1.size shouldBe propertiesModelStore.model.tags.size
154+
tagSnapshot1["my-tag-key1"] shouldBe propertiesModelStore.model.tags["my-tag-key1"]
155+
156+
// Modify
157+
userManager.addTag("my-tag-key2", "my-tag-value2")
158+
userManager.getTags().size shouldBe 2
159+
160+
// Then
161+
tagSnapshot1.size shouldNotBe userManager.getTags().size
162+
}
163+
138164
test("subscriptions are backed by the subscriptions manager") {
139165
// Given
140166
val subscriptionList = SubscriptionList(listOf(), UninitializedPushSubscription())

0 commit comments

Comments
 (0)