Skip to content

Commit

Permalink
Remove code related to the previous user state
Browse files Browse the repository at this point in the history
  • Loading branch information
jinliu9508 committed Dec 5, 2023
1 parent 689b2b9 commit 5aec11f
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import com.onesignal.sdktest.constant.Text;
import com.onesignal.sdktest.notification.OneSignalNotificationSender;
import com.onesignal.sdktest.util.SharedPreferenceUtil;
import com.onesignal.user.IUserStateObserver;
import com.onesignal.user.UserChangedState;
import com.onesignal.user.UserState;

import org.json.JSONObject;

Expand Down Expand Up @@ -116,6 +119,14 @@ public void onWillDisplay(@NonNull INotificationWillDisplayEvent event) {
}
});

OneSignal.getUser().addObserver(new IUserStateObserver() {
@Override
public void onUserStateChange(@NonNull UserChangedState state) {
UserState currentUserState = state.getCurrent();
Log.v(Tag.LOG_TAG, "onUserStateChange fired " + currentUserState.toJSONObject());
}
});

OneSignal.getInAppMessages().setPaused(true);
OneSignal.getLocation().setShared(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.app.Activity;
import android.content.Context;
import com.google.android.material.appbar.AppBarLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
Expand All @@ -13,10 +12,8 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.view.ViewTreeObserver;
Expand All @@ -25,17 +22,10 @@
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.onesignal.Continue;
import com.onesignal.OneSignal;
import com.onesignal.debug.internal.logging.Logging;
import com.onesignal.sdktest.adapter.SubscriptionRecyclerViewAdapter;
import com.onesignal.user.IUserStateObserver;
import com.onesignal.user.UserChangedState;
import com.onesignal.user.UserState;
import com.onesignal.user.subscriptions.IEmailSubscription;
import com.onesignal.user.subscriptions.IPushSubscription;
import com.onesignal.user.subscriptions.ISmsSubscription;
import com.onesignal.sdktest.R;
import com.onesignal.sdktest.activity.SecondaryActivity;
import com.onesignal.sdktest.adapter.InAppMessageRecyclerViewAdapter;
Expand All @@ -61,13 +51,12 @@
import com.onesignal.user.subscriptions.ISubscription;
import com.onesignal.user.subscriptions.IPushSubscriptionObserver;
import com.onesignal.user.subscriptions.PushSubscriptionChangedState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RequiresApi(api = Build.VERSION_CODES.N)
public class MainActivityViewModel implements ActivityViewModel, IPushSubscriptionObserver, IUserStateObserver {
public class MainActivityViewModel implements ActivityViewModel, IPushSubscriptionObserver {

private Animate animate;
private Dialog dialog;
Expand Down Expand Up @@ -297,7 +286,6 @@ public ActivityViewModel onActivityCreated(Context context) {
triggerArrayList = new ArrayList<>();

OneSignal.getUser().getPushSubscription().addObserver(this);
OneSignal.getUser().addObserver(this);
return this;
}

Expand Down Expand Up @@ -506,17 +494,6 @@ public void onPushSubscriptionChange(@NonNull PushSubscriptionChangedState state
refreshSubscriptionState();
}

@Override
public void onUserStateChange(@NonNull UserChangedState state) {
UserState currentState = state.getCurrent();
UserState prevState = state.getPrevious();
Logging.debug("onUserStateChanged; previous onesignalId: " + String.valueOf(prevState.getOnesignalId())
+ ", previous externalId: " + String.valueOf(prevState.getExternalId())
+ ", current onesignalId: " + String.valueOf(currentState.getOnesignalId())
+ ", current externalId: " + String.valueOf(currentState.getExternalId()),
null);
}

private class DummySubscription implements ISubscription {

private String _id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,4 @@ object IDManager {
* @return true if the [id] provided was created via [createLocalId].
*/
fun isLocalId(id: String): Boolean = id.startsWith(LOCAL_PREFIX)

/**
* Get non local ID or null.
*
* @param id The ID to test.
*
* @return [id] or null if the [id] provided was created via [createLocalId].
*/
fun getNonLocalIDOrNull(id: String): String? {
if (isLocalId(id)) {
return null
}

return id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ open class EventProducer<THandler> : IEventNotifier<THandler> {
*
* @param callback The callback will be invoked for each subscribed handler, allowing you to call the handler.
*/
open fun fire(callback: (THandler) -> Unit) {
fun fire(callback: (THandler) -> Unit) {
synchronized(subscribers) {
for (s in subscribers) {
callback(s)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,3 @@ class ModelChangedArgs(
*/
val newValue: Any?,
)

/**
* The arguments passed to the [IModelChangedHandler] handler when subscribed via [Model.subscribe]
*/
class ModelReplacedArgs<TModel>(
/**
* The full model in its previous and current state.
*/
val oldModel: TModel,
val newModel: TModel,
) where TModel : Model
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ISingletonModelStoreChangeHandler<TModel> where TModel : Model {
* @param tag The tag which identifies how/why the model was replaced.
*/
fun onModelReplaced(
model: ModelReplacedArgs<TModel>,
model: TModel,
tag: String,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ open class SingletonModelStore<TModel>(
tag: String,
) {
synchronized(replaceLock) {
val oldModel = this.store.create() ?: throw Exception("Unable to initialize model from store $store")
oldModel.initializeFromModel(singletonId, this.model)
val existingModel = this.model
existingModel.initializeFromModel(singletonId, model)
store.persist()
changeSubscription.fire { it.onModelReplaced(ModelReplacedArgs(oldModel, existingModel), tag) }
changeSubscription.fire { it.onModelReplaced(existingModel, tag) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.onesignal.common.exceptions.BackendException
import com.onesignal.common.modeling.ISingletonModelStoreChangeHandler
import com.onesignal.common.modeling.ModelChangeTags
import com.onesignal.common.modeling.ModelChangedArgs
import com.onesignal.common.modeling.ModelReplacedArgs
import com.onesignal.common.threading.suspendifyOnThread
import com.onesignal.core.internal.backend.IParamsBackendService
import com.onesignal.core.internal.config.ConfigModel
Expand Down Expand Up @@ -44,7 +43,7 @@ internal class ConfigModelStoreListener(
}

override fun onModelReplaced(
model: ModelReplacedArgs<ConfigModel>,
model: ConfigModel,
tag: String,
) {
if (tag != ModelChangeTags.NORMAL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.onesignal.common.modeling.ISingletonModelStoreChangeHandler
import com.onesignal.common.modeling.Model
import com.onesignal.common.modeling.ModelChangeTags
import com.onesignal.common.modeling.ModelChangedArgs
import com.onesignal.common.modeling.ModelReplacedArgs
import com.onesignal.common.modeling.SingletonModelStore
import com.onesignal.core.internal.operations.IOperationRepo
import com.onesignal.core.internal.operations.Operation
Expand All @@ -31,7 +30,7 @@ internal abstract class SingletonModelStoreListener<TModel>(
}

override fun onModelReplaced(
model: ModelReplacedArgs<TModel>,
model: TModel,
tag: String,
) {
if (tag != ModelChangeTags.NORMAL) {
Expand Down Expand Up @@ -63,7 +62,7 @@ internal abstract class SingletonModelStoreListener<TModel>(
*
* @return The operation to enqueue when the model has been replaced, or null if no operation should be enqueued.
*/
abstract fun getReplaceOperation(model: ModelReplacedArgs<TModel>): Operation?
abstract fun getReplaceOperation(model: TModel): Operation?

/**
* Called when the model has been updated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ interface IUserManager {
*/
val pushSubscription: IPushSubscription

/**
* onesignalId, null if this is current unavailable
*/
val onesignalId: String

/**
* externalId, null if this is current unavailable
*/
val externalId: String

/**
* Set the 2-character language either as a detected language or explicitly set for this user. See
* See [Supported Languages | OneSignal](https://documentation.onesignal.com/docs/language-localization#what-languages-are-supported)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package com.onesignal.user
import org.json.JSONObject

class UserChangedState(
val previous: UserState,
val current: UserState,
) {
fun toJSONObject(): JSONObject {
return JSONObject()
.put("previous", previous.toJSONObject())
.put("current", current.toJSONObject())
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
package com.onesignal.user

import com.onesignal.common.IDManager
import org.json.JSONObject

/**
* A user state.
*/
class UserState {
class UserState(
/**
* The unique identifier for your OneSignal account. This will be a null string until the
* The unique identifier for your OneSignal account. This will be an empty string until the
* user has been successfully logged in on the backend and assigned an ID.
* Use [addObserver] to be notified when the [onesignalId] has been successfully assigned.
*/
val onesignalId: String?

val onesignalId: String,
/**
* The external identifier that you use to identify users. This will be an null string
* until the user has been successfully logged in on the backend and
* assigned an ID. Use [addObserver] to be notified when the [externalId] has
* The external identifier that you use to identify users. This will be an empty string
* until the user has been successfully logged in on the backend and assigned an ID.
* Use [addObserver] to be notified when the [externalId] has
* been successfully assigned.
*/
val externalId: String?

constructor(onesignalId: String?, externalId: String?) {
if (onesignalId == null) {
this.onesignalId = null
} else {
this.onesignalId = IDManager.getNonLocalIDOrNull(onesignalId)
}
this.externalId = externalId
}

val externalId: String,
) {
fun toJSONObject(): JSONObject {
return JSONObject()
.put("onesignalId", onesignalId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import com.onesignal.common.OneSignalUtils
import com.onesignal.common.events.EventProducer
import com.onesignal.common.modeling.ISingletonModelStoreChangeHandler
import com.onesignal.common.modeling.ModelChangedArgs
import com.onesignal.common.modeling.ModelReplacedArgs
import com.onesignal.core.internal.language.ILanguageContext
import com.onesignal.debug.LogLevel
import com.onesignal.debug.internal.logging.Logging
Expand All @@ -28,11 +27,11 @@ internal open class UserManager(
private val _propertiesModelStore: PropertiesModelStore,
private val _languageContext: ILanguageContext,
) : IUserManager, ISingletonModelStoreChangeHandler<IdentityModel> {
val onesignalId: String?
get() = IDManager.getNonLocalIDOrNull(_identityModel.onesignalId)
override val onesignalId: String
get() = if (IDManager.isLocalId(_identityModel.onesignalId)) "" else _identityModel.onesignalId

val externalId: String?
get() = _identityModel.externalId
override val externalId: String
get() = _identityModel.externalId ?: ""

val aliases: Map<String, String>
get() = _identityModel.filter { it.key != IdentityModel::id.name }.toMap()
Expand Down Expand Up @@ -246,33 +245,18 @@ internal open class UserManager(
}

override fun onModelReplaced(
args: ModelReplacedArgs<IdentityModel>,
model: IdentityModel,
tag: String,
) {
val oldUserState = UserState(args.oldModel[IdentityConstants.ONESIGNAL_ID], args.oldModel.externalId)
val newUserState = UserState(args.newModel.onesignalId, args.newModel.externalId)

// do not fire the callback if onesignalId is null or the new model is identical to the old model
if (newUserState.onesignalId == null ||
oldUserState.onesignalId.equals(newUserState.onesignalId) &&
oldUserState.externalId.equals(newUserState.externalId)
) {
return
}
this.changeHandlersNotifier.fire {
it.onUserStateChange(UserChangedState(oldUserState, newUserState))
}
}
) { }

override fun onModelUpdated(
args: ModelChangedArgs,
tag: String,
) {
if (args.property.equals(IdentityConstants.ONESIGNAL_ID)) {
val oldUserState = UserState(args.oldValue.toString(), externalId)
if (args.property == IdentityConstants.ONESIGNAL_ID) {
val newUserState = UserState(args.newValue.toString(), externalId)
this.changeHandlersNotifier.fire {
it.onUserStateChange(UserChangedState(oldUserState, newUserState))
it.onUserStateChange(UserChangedState(newUserState))
}
}
Logging.debug(args.property)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.onesignal.user.internal.operations.impl.listeners

import com.onesignal.common.modeling.ModelReplacedArgs
import com.onesignal.core.internal.config.ConfigModelStore
import com.onesignal.core.internal.operations.IOperationRepo
import com.onesignal.core.internal.operations.Operation
Expand All @@ -15,7 +14,7 @@ internal class IdentityModelStoreListener(
opRepo: IOperationRepo,
private val _configModelStore: ConfigModelStore,
) : SingletonModelStoreListener<IdentityModel>(store, opRepo) {
override fun getReplaceOperation(model: ModelReplacedArgs<IdentityModel>): Operation? {
override fun getReplaceOperation(model: IdentityModel): Operation? {
// when the identity model is replaced, nothing to do on the backend. Already handled via login process.
return null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.onesignal.user.internal.operations.impl.listeners

import com.onesignal.common.modeling.ModelReplacedArgs
import com.onesignal.core.internal.config.ConfigModelStore
import com.onesignal.core.internal.operations.IOperationRepo
import com.onesignal.core.internal.operations.Operation
Expand All @@ -16,7 +15,7 @@ internal class PropertiesModelStoreListener(
opRepo: IOperationRepo,
private val _configModelStore: ConfigModelStore,
) : SingletonModelStoreListener<PropertiesModel>(store, opRepo) {
override fun getReplaceOperation(model: ModelReplacedArgs<PropertiesModel>): Operation? {
override fun getReplaceOperation(model: PropertiesModel): Operation? {
// when the property model is replaced, nothing to do on the backend. Already handled via login process.
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.onesignal.common.events.EventProducer
import com.onesignal.common.exceptions.BackendException
import com.onesignal.common.modeling.ISingletonModelStoreChangeHandler
import com.onesignal.common.modeling.ModelChangedArgs
import com.onesignal.common.modeling.ModelReplacedArgs
import com.onesignal.common.threading.suspendifyOnThread
import com.onesignal.core.internal.application.IApplicationService
import com.onesignal.core.internal.config.ConfigModel
Expand Down Expand Up @@ -185,7 +184,7 @@ internal class InAppMessagesManager(
}

override fun onModelReplaced(
model: ModelReplacedArgs<ConfigModel>,
model: ConfigModel,
tag: String,
) {
suspendifyOnThread {
Expand Down
Loading

0 comments on commit 5aec11f

Please sign in to comment.