Skip to content

Commit d32966b

Browse files
authored
Merge pull request #1707 from OneSignal/user-model/prealpha-fixes
Pre-alpha polish and fixes
2 parents 45516c6 + 6cde347 commit d32966b

File tree

27 files changed

+143
-132
lines changed

27 files changed

+143
-132
lines changed

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/application/MainApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public void inAppMessageClicked(@Nullable IInAppMessageClickResult result) {
9292
notificationReceivedEvent.complete(notification);
9393
});
9494

95-
OneSignal.getNotifications().setUnsubscribeWhenNotificationsAreDisabled(true);
95+
// OneSignal.getNotifications().setUnsubscribeWhenNotificationsAreDisabled(true);
9696
OneSignal.getInAppMessages().setPaused(true);
97-
OneSignal.getLocation().setLocationShared(false);
97+
OneSignal.getLocation().setShared(false);
9898

9999
Log.d(Tag.DEBUG, Text.ONESIGNAL_SDK_INIT);
100100
}

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/MainActivityViewModel.java

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ public class MainActivityViewModel implements ActivityViewModel, ISubscriptionCh
167167

168168
private Context context;
169169

170-
private Boolean isLoggedIn = false;
171170
private HashMap<String, Object> aliasSet;
172171
private ArrayList<Map.Entry> aliasArrayList;
173172
private ArrayList<ISubscription> emailArrayList;
@@ -290,7 +289,7 @@ public ActivityViewModel onActivityCreated(Context context) {
290289
triggerSet = new HashMap<>();
291290
triggerArrayList = new ArrayList<>();
292291

293-
OneSignal.getUser().getSubscriptions().getPush().addChangeHandler(this);
292+
OneSignal.getUser().getPushSubscription().addChangeHandler(this);
294293
return this;
295294
}
296295

@@ -402,26 +401,25 @@ public void onScrollChanged() {
402401
private void setupAppLayout() {
403402
revokeConsentButton.setOnClickListener(v -> togglePrivacyConsent(false));
404403

405-
if(OneSignal.getUser().getExternalId() != null) {
406-
isLoggedIn = true;
404+
if(SharedPreferenceUtil.getCachedIsLoggedIn(context)) {
407405
switchUserButton.setText(R.string.logout_user);
408406
}
409407

410408
switchUserButton.setOnClickListener(v -> {
411-
if(isLoggedIn) {
409+
if(SharedPreferenceUtil.getCachedIsLoggedIn(context)) {
412410
OneSignal.logout(Continue.with(r -> {
413-
isLoggedIn = false;
411+
SharedPreferenceUtil.cacheIsLoggedIn(context, false);
414412
switchUserButton.setText(R.string.login_user);
415413
refreshState();
416414
}));
417415
}
418416
else {
419-
dialog.createUpdateAlertDialog(OneSignal.getUser().getExternalId(), Dialog.DialogAction.LOGIN, ProfileUtil.FieldType.EXTERNAL_USER_ID, new UpdateAlertDialogCallback() {
417+
dialog.createUpdateAlertDialog("", Dialog.DialogAction.LOGIN, ProfileUtil.FieldType.EXTERNAL_USER_ID, new UpdateAlertDialogCallback() {
420418
@Override
421419
public void onSuccess(String update) {
422420
if (update != null && !update.isEmpty()) {
423421
OneSignal.login(update, Continue.with(r -> {
424-
isLoggedIn = true;
422+
SharedPreferenceUtil.cacheIsLoggedIn(context, true);
425423
switchUserButton.setText(R.string.logout_user);
426424
refreshState();
427425
}));
@@ -505,6 +503,30 @@ public void run() {
505503
});
506504
}
507505

506+
private class DummySubscription implements ISubscription {
507+
508+
private String _id;
509+
public DummySubscription(String id) {
510+
_id = id;
511+
}
512+
513+
@NonNull
514+
@Override
515+
public String getId() {
516+
return _id;
517+
}
518+
519+
@Override
520+
public void addChangeHandler(@NonNull ISubscriptionChangedHandler handler) {
521+
522+
}
523+
524+
@Override
525+
public void removeChangeHandler(@NonNull ISubscriptionChangedHandler handler) {
526+
527+
}
528+
}
529+
508530
private void setupEmailLayout() {
509531
setupEmailRecyclerView();
510532

@@ -514,9 +536,7 @@ private void setupEmailLayout() {
514536
public void onSuccess(String value) {
515537
if (value != null && !value.isEmpty()) {
516538
OneSignal.getUser().addEmailSubscription(value);
517-
IEmailSubscription newSubscription = OneSignal.getUser().getSubscriptions().getByEmail(value);
518-
newSubscription.addChangeHandler(self);
519-
emailArrayList.add(OneSignal.getUser().getSubscriptions().getByEmail(value));
539+
emailArrayList.add(new DummySubscription(value));
520540
toaster.makeCustomViewToast("Added email " + value, ToastType.SUCCESS);
521541
}
522542

@@ -539,9 +559,7 @@ private void setupSMSLayout() {
539559
public void onSuccess(String value) {
540560
if (value != null && !value.isEmpty()) {
541561
OneSignal.getUser().addSmsSubscription(value);
542-
ISmsSubscription newSubscription = OneSignal.getUser().getSubscriptions().getBySMS(value);
543-
newSubscription.addChangeHandler(self);
544-
smsArrayList.add(newSubscription);
562+
smsArrayList.add(new DummySubscription(value));
545563
toaster.makeCustomViewToast("Added SMS " + value, ToastType.SUCCESS);
546564
}
547565

@@ -774,7 +792,7 @@ private void setupLocationSharedSwitch() {
774792
locationSharedSwitch.setChecked(isLocationShared);
775793
locationSharedSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
776794
SharedPreferenceUtil.cacheLocationSharedStatus(context, isChecked);
777-
OneSignal.getLocation().setLocationShared(isChecked);
795+
OneSignal.getLocation().setShared(isChecked);
778796
});
779797
}
780798

@@ -800,7 +818,7 @@ private void setupSubscriptionSwitch() {
800818

801819
// Add a listener to toggle the push notification enablement for the push subscription.
802820
pushSubscriptionEnabledSwitch.setOnClickListener(v -> {
803-
IPushSubscription subscription = OneSignal.getUser().getSubscriptions().getPush();
821+
IPushSubscription subscription = OneSignal.getUser().getPushSubscription();
804822
if(pushSubscriptionEnabledSwitch.isChecked()) {
805823
subscription.optIn();
806824
}
@@ -831,7 +849,7 @@ else if(subscription instanceof ISmsSubscription) {
831849

832850
private void refreshSubscriptionState() {
833851
boolean isPermissionEnabled = OneSignal.getNotifications().getPermission();
834-
IPushSubscription pushSubscription = OneSignal.getUser().getSubscriptions().getPush();
852+
IPushSubscription pushSubscription = OneSignal.getUser().getPushSubscription();
835853

836854
pushSubscriptionIdTextView.setText(pushSubscription.getId());
837855
promptPushBottonLayout.setVisibility(isPermissionEnabled ? View.GONE : View.VISIBLE);
@@ -911,34 +929,34 @@ private void refreshState() {
911929
appIdTextView.setText(getOneSignalAppId());
912930

913931
// aliases
914-
aliasSet.clear();
915-
for (Map.Entry<String, String> aliasEntry :OneSignal.getUser().getAliases().entrySet()) {
916-
aliasSet.put(aliasEntry.getKey(), aliasEntry.getValue());
917-
}
918-
refreshAliasRecyclerView();
932+
// aliasSet.clear();
933+
// for (Map.Entry<String, String> aliasEntry :OneSignal.getUser().getAliases().entrySet()) {
934+
// aliasSet.put(aliasEntry.getKey(), aliasEntry.getValue());
935+
// }
936+
// refreshAliasRecyclerView();
919937

920938
// email subscriptions
921-
emailArrayList.clear();
922-
List<IEmailSubscription> emailSubs = OneSignal.getUser().getSubscriptions().getEmails();
923-
for (IEmailSubscription emailSub: emailSubs) {
924-
emailArrayList.add(emailSub);
925-
}
926-
refreshEmailRecyclerView();
939+
// emailArrayList.clear();
940+
// List<IEmailSubscription> emailSubs = OneSignal.getUser().getSubscriptions().getEmails();
941+
// for (IEmailSubscription emailSub: emailSubs) {
942+
// emailArrayList.add(emailSub);
943+
// }
944+
// refreshEmailRecyclerView();
927945

928946
// sms subscriptions
929-
smsArrayList.clear();
930-
List<ISmsSubscription> smsSubs = OneSignal.getUser().getSubscriptions().getSmss();
931-
for (ISmsSubscription smsSub: smsSubs) {
932-
smsArrayList.add(smsSub);
933-
}
934-
refreshSMSRecyclerView();
947+
// smsArrayList.clear();
948+
// List<ISmsSubscription> smsSubs = OneSignal.getUser().getSubscriptions().getSmss();
949+
// for (ISmsSubscription smsSub: smsSubs) {
950+
// smsArrayList.add(smsSub);
951+
// }
952+
// refreshSMSRecyclerView();
935953

936954
// tags
937-
tagSet.clear();
938-
for (Map.Entry<String, String> tagEntry :OneSignal.getUser().getTags().entrySet()) {
939-
tagSet.put(tagEntry.getKey(), tagEntry.getValue());
940-
}
941-
refreshTagRecyclerView();
955+
// tagSet.clear();
956+
// for (Map.Entry<String, String> tagEntry :OneSignal.getUser().getTags().entrySet()) {
957+
// tagSet.put(tagEntry.getKey(), tagEntry.getValue());
958+
// }
959+
// refreshTagRecyclerView();
942960

943961
// triggers
944962
triggerSet.clear();

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/model/SplashActivityViewModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private void setupOneSignalSDK() {
6666
OneSignal.setRequiresPrivacyConsent(privacyConsent);
6767

6868
boolean isLocationShared = SharedPreferenceUtil.getCachedLocationSharedStatus(context);
69-
OneSignal.getLocation().setLocationShared(isLocationShared);
69+
OneSignal.getLocation().setShared(isLocationShared);
7070

7171
boolean isInAppMessagingPaused = SharedPreferenceUtil.getCachedInAppMessagingPausedStatus(context);
7272
OneSignal.getInAppMessages().setPaused(isInAppMessagingPaused);

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/notification/OneSignalNotificationSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static void setAppId(String appId) {
3333
*/
3434
public static void sendDeviceNotification(final Notification notification) {
3535
new Thread(() -> {
36-
IPushSubscription subscription = OneSignal.getUser().getSubscriptions().getPush();
36+
IPushSubscription subscription = OneSignal.getUser().getPushSubscription();
3737

3838
if (!subscription.getOptedIn())
3939
return;

Examples/OneSignalDemo/app/src/main/java/com/onesignal/sdktest/util/SharedPreferenceUtil.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class SharedPreferenceUtil {
1414
public static final String USER_EXTERNAL_USER_ID_SHARED_PREF = "USER_EXTERNAL_USER_ID_SHARED_PREF";
1515
private static final String LOCATION_SHARED_PREF = "LOCATION_SHARED_PREF";
1616
private static final String IN_APP_MESSAGING_PAUSED_PREF = "IN_APP_MESSAGING_PAUSED_PREF";
17+
private static final String IS_LOGGED_IN = "IS_LOGGED_IN";
1718

1819
private static SharedPreferences getSharedPreference(Context context) {
1920
return context.getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
@@ -43,6 +44,10 @@ public static boolean getCachedInAppMessagingPausedStatus(Context context) {
4344
return getSharedPreference(context).getBoolean(IN_APP_MESSAGING_PAUSED_PREF, true);
4445
}
4546

47+
public static boolean getCachedIsLoggedIn(Context context) {
48+
return getSharedPreference(context).getBoolean(IS_LOGGED_IN, false);
49+
}
50+
4651
public static void cacheOneSignalAppId(Context context, String appId) {
4752
getSharedPreference(context).edit().putString(OS_APP_ID_SHARED_PREF, appId).apply();
4853
}
@@ -62,4 +67,8 @@ public static void cacheLocationSharedStatus(Context context, boolean subscribed
6267
public static void cacheInAppMessagingPausedStatus(Context context, boolean paused) {
6368
getSharedPreference(context).edit().putBoolean(IN_APP_MESSAGING_PAUSED_PREF, paused).apply();
6469
}
70+
71+
public static void cacheIsLoggedIn(Context context, boolean isLoggedIn) {
72+
getSharedPreference(context).edit().putBoolean(IS_LOGGED_IN, isLoggedIn).apply();
73+
}
6574
}

OneSignalSDK/onesignal/inAppMessages/src/main/java/com/onesignal/inAppMessages/internal/InAppMessagesManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ internal class InAppMessagesManager(
317317
Logging.debug("IAMManager.attemptToShowInAppMessage: $_messageDisplayQueue")
318318
// If there are IAMs in the queue and nothing showing, show first in the queue
319319
if (paused) {
320-
Logging.verbose("IAMManager.attemptToShowInAppMessage: In app messaging is currently paused, in app messages will not be shown!")
320+
Logging.warn("IAMManager.attemptToShowInAppMessage: In app messaging is currently paused, in app messages will not be shown!")
321321
} else if (_messageDisplayQueue.isEmpty()) {
322322
Logging.debug("IAMManager.attemptToShowInAppMessage: There are no IAMs left in the queue!")
323323
} else if (_state.inAppMessageIdShowing != null) {

OneSignalSDK/onesignal/location/src/main/java/com/onesignal/location/internal/LocationManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal class LocationManager(
2222
private val _locationPermissionController: LocationPermissionController
2323
) : ILocationManager, IStartableService, ILocationPermissionChangedHandler {
2424

25-
override var isLocationShared: Boolean = false
25+
override var isShared: Boolean = false
2626

2727
override fun start() {
2828
_locationPermissionController.subscribe(this)
@@ -61,7 +61,7 @@ internal class LocationManager(
6161
override suspend fun requestPermission(fallbackToSettings: Boolean): Boolean {
6262
Logging.log(LogLevel.DEBUG, "LocationManager.requestPermission()")
6363

64-
if (!isLocationShared) {
64+
if (!isShared) {
6565
return false
6666
}
6767

OneSignalSDK/onesignal/location/src/main/java/com/onesignal/location/internal/background/LocationBackgroundService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class LocationBackgroundService(
1919
) : IBackgroundService {
2020
override val scheduleBackgroundRunIn: Long?
2121
get() {
22-
if (!_locationManager.isLocationShared) {
22+
if (!_locationManager.isShared) {
2323
Logging.debug("LocationController scheduleUpdate not possible, location shared not enabled")
2424
return null
2525
}

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/NotificationsManager.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ internal class NotificationsManager(
4242
IApplicationLifecycleHandler {
4343

4444
override var permission: Boolean = NotificationHelper.areNotificationsEnabled(_applicationService.appContext)
45-
override val canRequestPermission: Boolean = true // TODO: Implement
46-
override var unsubscribeWhenNotificationsAreDisabled: Boolean = false
4745

4846
private val _permissionChangedNotifier = EventProducer<IPermissionChangedHandler>()
4947

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/listeners/DeviceRegistrationListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ internal class DeviceRegistrationListener(
6464
private fun retrievePushTokenAndUpdateSubscription() {
6565
val pushSubscription = _subscriptionManager.subscriptions.push
6666

67-
if (pushSubscription.pushToken.isNotEmpty()) {
67+
if (pushSubscription.token.isNotEmpty()) {
6868
val permission = _notificationsManager.permission
6969
_subscriptionManager.addOrUpdatePushSubscription(null, if (permission) SubscriptionStatus.SUBSCRIBED else SubscriptionStatus.NO_PERMISSION)
7070
} else {

OneSignalSDK/onesignal/src/main/java/com/onesignal/common/modeling/Model.kt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ open class Model(
4444
* to this model will *also* be propagated up to it's parent for notification. When
4545
* this is specified, must also specify [_parentProperty]
4646
*/
47-
private val _parentModel: Model? = null,
47+
private var _parentModel: Model? = null,
4848

4949
/**
5050
* The optional parent model property that references this model. When this is
@@ -118,10 +118,21 @@ open class Model(
118118
* @param id The id of the model to initialze to.
119119
* @param model The model to initialize this model from.
120120
*/
121-
fun initializeFromModel(id: String, model: Model) {
121+
fun initializeFromModel(id: String?, model: Model) {
122122
data.clear()
123-
data.putAll(model.data)
124-
data[::id.name] = id
123+
for (item in model.data) {
124+
if (item.value is Model) {
125+
val childModel = item.value as Model
126+
childModel._parentModel = this
127+
data[item.key] = childModel
128+
} else {
129+
data[item.key] = item.value
130+
}
131+
}
132+
133+
if (id != null) {
134+
data[::id.name] = id
135+
}
125136
}
126137

127138
/**
@@ -286,7 +297,7 @@ open class Model(
286297
// if there is a parent model, propagate the change up to the parent for it's own processing.
287298
if (_parentModel != null) {
288299
val parentPath = "$_parentProperty.$path"
289-
_parentModel.notifyChanged(parentPath, property, tag, oldValue, newValue)
300+
_parentModel!!.notifyChanged(parentPath, property, tag, oldValue, newValue)
290301
}
291302
}
292303

OneSignalSDK/onesignal/src/main/java/com/onesignal/core/internal/config/ConfigModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ConfigModel : Model() {
8787
* the queue.
8888
*/
8989
var opRepoExecutionInterval: Long
90-
get() = getLongProperty(::opRepoExecutionInterval.name) { 5000 }
90+
get() = getLongProperty(::opRepoExecutionInterval.name) { 10000 }
9191
set(value) { setLongProperty(::opRepoExecutionInterval.name, value) }
9292

9393
/**

OneSignalSDK/onesignal/src/main/java/com/onesignal/core/internal/operations/impl/OperationRepo.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ internal class OperationRepo(
118118
// operations are set sequentially.
119119
val newTime = _time.currentTimeMillis
120120

121-
val delay = lastSyncTime - newTime + _configModelStore.model.opRepoExecutionInterval
121+
val delay = (lastSyncTime - newTime) + _configModelStore.model.opRepoExecutionInterval
122122
lastSyncTime = newTime
123-
124123
if (delay > 0) {
125124
withTimeoutOrNull(delay) {
126125
// wait to be woken up for the next pass
@@ -130,6 +129,8 @@ internal class OperationRepo(
130129
// This secondary delay allows for any subsequent operations (beyond the first one
131130
// that woke us) to be enqueued before we pull from the queue.
132131
delay(_configModelStore.model.opRepoPostWakeDelay)
132+
133+
lastSyncTime = _time.currentTimeMillis
133134
}
134135
}
135136
} catch (e: Throwable) {

OneSignalSDK/onesignal/src/main/java/com/onesignal/core/internal/preferences/impl/PreferencesService.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ internal class PreferencesService(
9999
}
100100

101101
private fun doWorkAsync() = GlobalScope.async(Dispatchers.IO) {
102-
try {
103-
var lastSyncTime = _time.currentTimeMillis
102+
var lastSyncTime = _time.currentTimeMillis
104103

105-
while (true) {
104+
while (true) {
105+
try {
106106
// go through all outstanding items to process
107107
for (storeKey in _prefsToApply.keys) {
108108
val storeMap = _prefsToApply[storeKey]!!
@@ -146,10 +146,9 @@ internal class PreferencesService(
146146

147147
// wait to be woken up for the next pass
148148
_waiter.waitForWake()
149+
} catch (e: Throwable) {
150+
Logging.log(LogLevel.ERROR, "Error with Preference work loop", e)
149151
}
150-
} catch (e: Throwable) {
151-
Logging.log(LogLevel.ERROR, "Error with Preference work loop", e)
152-
// TODO: Restart/crash logic
153152
}
154153
}
155154

0 commit comments

Comments
 (0)