Skip to content

Commit 978be0f

Browse files
committed
Move UserState and related classes into the user directory
1 parent 66a341d commit 978be0f

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,15 @@ interface IUserManager {
138138
* Return a copy of all local tags from the current user.
139139
*/
140140
fun getTags() : Map<String, String>
141+
142+
/**
143+
* Add an observer to the user state, allowing the provider to be
144+
* notified whenever the user state has changed.
145+
*/
146+
fun addObserver(observer: IUserStateObserver)
147+
148+
/**
149+
* Remove an observer from the user state.
150+
*/
151+
fun removeObserver(observer: IUserStateObserver)
141152
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.onesignal.user
2+
3+
/**
4+
* A user state changed handler. Implement this interface and provide the implementation
5+
* to be notified when the user has changed.
6+
*/
7+
interface IUserStateObserver {
8+
/**
9+
* Called when the user state this change handler was added to, has changed. A
10+
* user state can change when user has logged in or out
11+
*
12+
* @param state The user changed state.
13+
*/
14+
fun onUserStateChange(state: UserChangedState)
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.onesignal.user
2+
3+
import org.json.JSONObject
4+
5+
class UserChangedState(
6+
val previous: UserState,
7+
val current: UserState,
8+
val switchedUsers: Boolean,
9+
) {
10+
fun toJSONObject(): JSONObject {
11+
return JSONObject()
12+
.put("previous", previous.toJSONObject())
13+
.put("current", current.toJSONObject())
14+
.put("switchedUsers", switchedUsers)
15+
}
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.onesignal.user
2+
3+
import org.json.JSONObject
4+
5+
/**
6+
* A user state.
7+
*/
8+
class UserState (
9+
/**
10+
* The unique identifier for OneSignal account. This will be a null string until
11+
* the user has been successfully logged in on the backend and assigned an ID.
12+
* Use [addObserver] to be notified when the [onesignalId] has been successfully
13+
* assigned.
14+
*/
15+
val onesignalId: String?,
16+
17+
/**
18+
* The unique external identifier. This will be a null string until the id has
19+
* been successfully assigned and retrieved from the server.
20+
* Use [addObserver] to be notified when the [externalId] has been successfully
21+
* assigned.
22+
*/
23+
val externalId: String?,
24+
) {
25+
fun toJSONObject(): JSONObject {
26+
return JSONObject()
27+
.put("onesignalId", onesignalId)
28+
.put("externalId", externalId)
29+
}
30+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.onesignal.user.internal
22

33
import com.onesignal.common.OneSignalUtils
4+
import com.onesignal.common.events.EventProducer
45
import com.onesignal.core.internal.language.ILanguageContext
56
import com.onesignal.debug.LogLevel
67
import com.onesignal.debug.internal.logging.Logging
78
import com.onesignal.user.IUserManager
9+
import com.onesignal.user.IUserStateObserver
810
import com.onesignal.user.internal.backend.IdentityConstants
911
import com.onesignal.user.internal.identity.IdentityModel
1012
import com.onesignal.user.internal.identity.IdentityModelStore
@@ -29,6 +31,8 @@ internal open class UserManager(
2931
val subscriptions: SubscriptionList
3032
get() = _subscriptionManager.subscriptions
3133

34+
val changeHandlersNotifier = EventProducer<IUserStateObserver>()
35+
3236
override val pushSubscription: IPushSubscription
3337
get() = _subscriptionManager.subscriptions.push
3438

@@ -219,4 +223,8 @@ internal open class UserManager(
219223
override fun getTags(): Map<String, String> {
220224
return _propertiesModel.tags.toMap()
221225
}
226+
227+
override fun addObserver(observer: IUserStateObserver) = changeHandlersNotifier.subscribe(observer)
228+
229+
override fun removeObserver(observer: IUserStateObserver) = changeHandlersNotifier.unsubscribe(observer)
222230
}

OneSignalSDK/onesignal/notifications/consumer-rules.pro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
void onPushSubscriptionChange(com.onesignal.user.subscriptions.PushSubscriptionChangedState);
2121
}
2222

23+
-keep class ** implements com.onesignal.user.IUserStateObserver {
24+
void onUserChange(com.onesignal.user.UserChangedState);
25+
}
2326
-keep class ** implements com.onesignal.notifications.INotificationServiceExtension{
2427
void onNotificationReceived(com.onesignal.notifications.INotificationReceivedEvent);
2528
}

0 commit comments

Comments
 (0)