Skip to content

Commit 78383da

Browse files
authored
Merge pull request #1689 from OneSignal/user-model/minor-cleanup
[User Model] Minor cleanup
2 parents 4fa327f + 7fe6aca commit 78383da

File tree

28 files changed

+179
-168
lines changed

28 files changed

+179
-168
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.onesignal.common.AndroidUtils
55
import com.onesignal.common.IDManager
66
import com.onesignal.common.JSONUtils
77
import com.onesignal.common.events.CallbackProducer
8-
import com.onesignal.common.events.ICallbackProducer
98
import com.onesignal.common.exceptions.BackendException
109
import com.onesignal.common.modeling.ISingletonModelStoreChangeHandler
1110
import com.onesignal.common.modeling.ModelChangedArgs
@@ -43,10 +42,8 @@ import com.onesignal.session.internal.session.ISessionService
4342
import com.onesignal.user.IUserManager
4443
import com.onesignal.user.internal.subscriptions.ISubscriptionChangedHandler
4544
import com.onesignal.user.internal.subscriptions.ISubscriptionManager
46-
import kotlinx.coroutines.Dispatchers
4745
import kotlinx.coroutines.sync.Mutex
4846
import kotlinx.coroutines.sync.withLock
49-
import kotlinx.coroutines.withContext
5047

5148
internal class InAppMessagesManager(
5249
private val _applicationService: IApplicationService,
@@ -74,8 +71,8 @@ internal class InAppMessagesManager(
7471
ITriggerHandler,
7572
ISessionLifecycleHandler {
7673

77-
private val _lifecycleCallback: ICallbackProducer<IInAppMessageLifecycleHandler> = CallbackProducer()
78-
private val _messageClickCallback: ICallbackProducer<IInAppMessageClickHandler> = CallbackProducer()
74+
private val _lifecycleCallback = CallbackProducer<IInAppMessageLifecycleHandler>()
75+
private val _messageClickCallback = CallbackProducer<IInAppMessageClickHandler>()
7976

8077
// IAMs loaded remotely from on_session
8178
// If on_session won't be called this will be loaded from cache
@@ -376,7 +373,7 @@ internal class InAppMessagesManager(
376373
Logging.verbose("IAMManager.messageWasDismissed: inAppMessageLifecycleHandler is null")
377374
return
378375
}
379-
_lifecycleCallback.fire { it.onDidDismissInAppMessage(message) }
376+
_lifecycleCallback.fireOnMain { it.onDidDismissInAppMessage(message) }
380377

381378
_state.inAppMessageIdShowing = null
382379

@@ -475,15 +472,15 @@ internal class InAppMessagesManager(
475472
Logging.verbose("IAMManager.onMessageWillDisplay: inAppMessageLifecycleHandler is null")
476473
return
477474
}
478-
_lifecycleCallback.fire { it.onWillDisplayInAppMessage(message) }
475+
_lifecycleCallback.fireOnMain { it.onWillDisplayInAppMessage(message) }
479476
}
480477

481478
override fun onMessageWasDisplayed(message: InAppMessage) {
482479
if (!_lifecycleCallback.hasCallback) {
483480
Logging.verbose("IAMManager.onMessageWasDisplayed: inAppMessageLifecycleHandler is null")
484481
return
485482
}
486-
_lifecycleCallback.fire { it.onDidDisplayInAppMessage(message) }
483+
_lifecycleCallback.fireOnMain { it.onDidDisplayInAppMessage(message) }
487484

488485
if (message.isPreview) {
489486
return
@@ -552,7 +549,7 @@ internal class InAppMessagesManager(
552549
Logging.verbose("IAMManager.onMessageWillDismiss: inAppMessageLifecycleHandler is null")
553550
return
554551
}
555-
_lifecycleCallback.fire { it.onWillDismissInAppMessage(message) }
552+
_lifecycleCallback.fireOnMain { it.onWillDismissInAppMessage(message) }
556553
}
557554

558555
override fun onMessageWasDismissed(message: InAppMessage) {
@@ -707,10 +704,7 @@ internal class InAppMessagesManager(
707704
// Check that only on the handler
708705
// Any outcome sent on this callback should count as DIRECT from this IAM
709706
_influenceManager.onDirectInfluenceFromIAM(messageId)
710-
711-
withContext(Dispatchers.Main) {
712-
_messageClickCallback.fire { it.inAppMessageClicked(action) }
713-
}
707+
_messageClickCallback.suspendingFireOnMain { it.inAppMessageClicked(action) }
714708
}
715709

716710
private suspend fun fireRESTCallForPageChange(message: InAppMessage, page: InAppMessagePage) {

OneSignalSDK/onesignal/inAppMessages/src/main/java/com/onesignal/inAppMessages/internal/backend/impl/InAppBackendService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ internal class InAppBackendService(
9494
val json: JSONObject = object : JSONObject() {
9595
init {
9696
put("app_id", appId)
97-
put("device_type", _deviceService.deviceType)
97+
put("device_type", _deviceService.deviceType.value)
9898
put("player_id", subscriptionId)
9999
put("click_id", clickId)
100100
put("variant_id", variantId)
@@ -129,7 +129,7 @@ internal class InAppBackendService(
129129
put("app_id", appId)
130130
put("player_id", subscriptionId)
131131
put("variant_id", variantId)
132-
put("device_type", _deviceService.deviceType)
132+
put("device_type", _deviceService.deviceType.value)
133133
put("page_id", pageId)
134134
}
135135
}
@@ -155,7 +155,7 @@ internal class InAppBackendService(
155155
put("app_id", appId)
156156
put("player_id", subscriptionId)
157157
put("variant_id", variantId)
158-
put("device_type", _deviceService.deviceType)
158+
put("device_type", _deviceService.deviceType.value)
159159
put("first_impression", true)
160160
}
161161
}

OneSignalSDK/onesignal/inAppMessages/src/test/java/com/onesignal/inAppMessages/mocks/MockHelper.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ object MockHelper {
110110
return mockLanguageContext
111111
}
112112

113-
const val DEFAULT_DEVICE_TYPE = 1
114-
115113
fun deviceService(): IDeviceService {
116114
val deviceService = mockk<IDeviceService>()
117-
every { deviceService.deviceType } returns DEFAULT_DEVICE_TYPE
115+
every { deviceService.deviceType } returns IDeviceService.DeviceType.Android
118116
return deviceService
119117
}
120118
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.onesignal.notifications.internal
33
import android.app.Activity
44
import com.onesignal.common.events.EventProducer
55
import com.onesignal.common.exceptions.BackendException
6-
import com.onesignal.common.threading.suspendifyOnMain
76
import com.onesignal.common.threading.suspendifyOnThread
87
import com.onesignal.core.internal.application.IApplicationLifecycleHandler
98
import com.onesignal.core.internal.application.IApplicationService
@@ -100,9 +99,7 @@ internal class NotificationsManager(
10099

101100
if (oldPermissionStatus != isEnabled) {
102101
// switch over to the main thread for the firing of the event
103-
suspendifyOnMain {
104-
_permissionChangedNotifier.fire { it.onPermissionChanged(isEnabled) }
105-
}
102+
_permissionChangedNotifier.fireOnMain { it.onPermissionChanged(isEnabled) }
106103
}
107104
}
108105

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/backend/INotificationBackendService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onesignal.notifications.internal.backend
22

33
import com.onesignal.common.exceptions.BackendException
4+
import com.onesignal.core.internal.device.IDeviceService
45
import org.json.JSONObject
56

67
/**
@@ -18,7 +19,7 @@ internal interface INotificationBackendService {
1819
* @param subscriptionId The specific subscription within the [appId] the notification has been received for.
1920
* @param deviceType The type of device the notification was received at.
2021
*/
21-
suspend fun updateNotificationAsReceived(appId: String, notificationId: String, subscriptionId: String, deviceType: Int)
22+
suspend fun updateNotificationAsReceived(appId: String, notificationId: String, subscriptionId: String, deviceType: IDeviceService.DeviceType)
2223

2324
/**
2425
* Update the provided notification as opened by a specific subscription.
@@ -30,7 +31,7 @@ internal interface INotificationBackendService {
3031
* @param subscriptionId The specific subscription within the [appId] the notification has been received for.
3132
* @param deviceType The type of device the notification was received at.
3233
*/
33-
suspend fun updateNotificationAsOpened(appId: String, notificationId: String, subscriptionId: String, deviceType: Int)
34+
suspend fun updateNotificationAsOpened(appId: String, notificationId: String, subscriptionId: String, deviceType: IDeviceService.DeviceType)
3435

3536
/**
3637
* Send a notification using the provided payload.

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/backend/impl/NotificationBackendService.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onesignal.notifications.internal.backend.impl
22

33
import com.onesignal.common.exceptions.BackendException
4+
import com.onesignal.core.internal.device.IDeviceService
45
import com.onesignal.core.internal.http.IHttpClient
56
import com.onesignal.notifications.internal.backend.INotificationBackendService
67
import org.json.JSONObject
@@ -9,11 +10,11 @@ internal class NotificationBackendService(
910
private val _httpClient: IHttpClient
1011
) : INotificationBackendService {
1112

12-
override suspend fun updateNotificationAsReceived(appId: String, notificationId: String, subscriptionId: String, deviceType: Int) {
13+
override suspend fun updateNotificationAsReceived(appId: String, notificationId: String, subscriptionId: String, deviceType: IDeviceService.DeviceType) {
1314
val jsonBody: JSONObject = JSONObject()
1415
.put("app_id", appId)
1516
.put("player_id", subscriptionId)
16-
.put("device_type", deviceType)
17+
.put("device_type", deviceType.value)
1718

1819
var response = _httpClient.put("notifications/$notificationId/report_received", jsonBody)
1920

@@ -22,12 +23,12 @@ internal class NotificationBackendService(
2223
}
2324
}
2425

25-
override suspend fun updateNotificationAsOpened(appId: String, notificationId: String, subscriptionId: String, deviceType: Int) {
26+
override suspend fun updateNotificationAsOpened(appId: String, notificationId: String, subscriptionId: String, deviceType: IDeviceService.DeviceType) {
2627
val jsonBody = JSONObject()
2728
jsonBody.put("app_id", appId)
2829
jsonBody.put("player_id", subscriptionId)
2930
jsonBody.put("opened", true)
30-
jsonBody.put("device_type", deviceType)
31+
jsonBody.put("device_type", deviceType.value)
3132

3233
var response = _httpClient.put("notifications/$notificationId", jsonBody)
3334

OneSignalSDK/onesignal/notifications/src/main/java/com/onesignal/notifications/internal/lifecycle/impl/NotificationLifecycleService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ internal class NotificationLifecycleService(
4444
if (_extOpenedCallback.hasCallback && _unprocessedOpenedNotifs.any()) {
4545
for (data in _unprocessedOpenedNotifs) {
4646
val openedResult = NotificationHelper.generateNotificationOpenedResult(data, _time)
47-
_extOpenedCallback.fire { it.notificationOpened(openedResult) }
47+
_extOpenedCallback.fireOnMain { it.notificationOpened(openedResult) }
4848
}
4949
}
5050
}
@@ -76,7 +76,7 @@ internal class NotificationLifecycleService(
7676
// we will immediately fire the handler.
7777
if (_extOpenedCallback.hasCallback) {
7878
val openResult = NotificationHelper.generateNotificationOpenedResult(data, _time)
79-
_extOpenedCallback.fire { it.notificationOpened(openResult) }
79+
_extOpenedCallback.fireOnMain { it.notificationOpened(openResult) }
8080
} else {
8181
_unprocessedOpenedNotifs.add(data)
8282
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/common/events/CallbackProducer.kt

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.onesignal.common.events
22

3+
import com.onesignal.common.threading.suspendifyOnMain
4+
import kotlinx.coroutines.Dispatchers
5+
import kotlinx.coroutines.withContext
6+
37
/**
4-
* A standard implementation that implements [ICallbackProducer] to make callbacks less burdensome
5-
* to the user.
8+
* A standard implementation that implements [ICallbackNotifier] and additional functionality to
9+
* make callbacks less burdensome to the user.
610
*/
7-
open class CallbackProducer<THandler>() : ICallbackProducer<THandler> {
11+
open class CallbackProducer<THandler>() : ICallbackNotifier<THandler> {
812

913
private var _callback: THandler? = null
1014

@@ -22,15 +26,55 @@ open class CallbackProducer<THandler>() : ICallbackProducer<THandler> {
2226
*
2327
* @param callback The callback will be invoked if one exists, allowing you to call the handler.
2428
*/
25-
override fun fire(callback: (THandler) -> Unit) {
29+
fun fire(callback: (THandler) -> Unit) {
2630
if (_callback != null) {
2731
callback(_callback!!)
2832
}
2933
}
3034

35+
/**
36+
* Call this to fire the callback which will allow the caller to drive the calling of the
37+
* callback handler if one exists. It is done this way to avoid this abstract class from
38+
* knowing the specific signature of the handler. The callback will be invoked asynchronously
39+
* on the main thread. Control will be returned immediately, most likely prior to the callbacks
40+
* being invoked.
41+
*
42+
* @param callback The callback will be invoked if one exists, allowing you to call the handler.
43+
*/
44+
fun fireOnMain(callback: (THandler) -> Unit) {
45+
suspendifyOnMain {
46+
if (_callback != null) {
47+
callback(_callback!!)
48+
}
49+
}
50+
}
51+
52+
/**
53+
* Call this to fire the callback which will allow the caller to drive the calling of the
54+
* callback handler if one exists. It is done this way to avoid this abstract class from
55+
* knowing the specific signature of the handler.
56+
*
57+
* @param callback The callback will be invoked if one exists, allowing you to call the handler.
58+
*/
3159
suspend fun suspendingFire(callback: suspend (THandler) -> Unit) {
3260
if (_callback != null) {
3361
callback(_callback!!)
3462
}
3563
}
64+
65+
/**
66+
* Call this to fire the callback which will allow the caller to drive the calling of the
67+
* callback handler if one exists. It is done this way to avoid this abstract class from
68+
* knowing the specific signature of the handler. The callback will be invoked on the main
69+
* thread.
70+
*
71+
* @param callback The callback will be invoked if one exists, allowing you to call the handler.
72+
*/
73+
suspend fun suspendingFireOnMain(callback: suspend (THandler) -> Unit) {
74+
if (_callback != null) {
75+
withContext(Dispatchers.Main) {
76+
callback(_callback!!)
77+
}
78+
}
79+
}
3680
}
Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.onesignal.common.events
22

3+
import com.onesignal.common.threading.suspendifyOnMain
4+
import kotlinx.coroutines.Dispatchers
5+
import kotlinx.coroutines.withContext
6+
37
/**
4-
* A standard implementation that implements [IEventProducer] to make event firing less burdensome
5-
* to the user.
8+
* A standard implementation that implements [IEventNotifier] and additional functionality to make
9+
* event firing less burdensome to the user.
610
*/
7-
open class EventProducer<THandler> : IEventProducer<THandler> {
11+
open class EventProducer<THandler> : IEventNotifier<THandler> {
812

913
private val _subscribers: MutableList<THandler> = mutableListOf()
1014

@@ -16,12 +20,6 @@ open class EventProducer<THandler> : IEventProducer<THandler> {
1620
_subscribers.remove(handler)
1721
}
1822

19-
override fun fire(callback: (THandler) -> Unit) {
20-
for (s in _subscribers) {
21-
callback(s)
22-
}
23-
}
24-
2523
/**
2624
* Subscribe all from an existing producer to this subscriber.
2725
*/
@@ -32,25 +30,55 @@ open class EventProducer<THandler> : IEventProducer<THandler> {
3230
}
3331

3432
/**
35-
* Conditional fire all subscribers *until one indicates to stop firing*
36-
*
37-
* @param callback The callback, returns true when no more subscribers should be called.
33+
* Call this to fire the callback which will allow the caller to drive the calling of the
34+
* callback handlers if there are any.
3835
*
39-
* @return Whether a subscriber callback indicated to stop firing.
36+
* @param callback The callback will be invoked for each subscribed handler, allowing you to call the handler.
4037
*/
41-
fun conditionalFire(callback: (THandler) -> Boolean): Boolean {
38+
fun fire(callback: (THandler) -> Unit) {
4239
for (s in _subscribers) {
43-
if (!callback(s)) {
44-
return true
45-
}
40+
callback(s)
4641
}
42+
}
4743

48-
return true
44+
/**
45+
* Call this to fire the callback which will allow the caller to drive the calling of the
46+
* callback handlers if there are any. The callback will be invoked asynchronously on the main
47+
* thread. Control will be returned immediately, most likely prior to the callbacks being invoked.
48+
*
49+
* @param callback The callback will be invoked for each subscribed handler, allowing you to call the handler.
50+
*/
51+
fun fireOnMain(callback: (THandler) -> Unit) {
52+
suspendifyOnMain {
53+
for (s in _subscribers) {
54+
callback(s)
55+
}
56+
}
4957
}
5058

59+
/**
60+
* Call this to fire the callback which will allow the caller to drive the calling of the
61+
* callback handlers if there are any.
62+
*
63+
* @param callback The callback will be invoked for each subscribed handler, allowing you to call the handler.
64+
*/
5165
suspend fun suspendingFire(callback: suspend (THandler) -> Unit) {
5266
for (s in _subscribers) {
5367
callback(s)
5468
}
5569
}
70+
71+
/**
72+
* Call this to fire the callback which will allow the caller to drive the calling of the
73+
* callback handlers if there are any. The callback will be invoked on the main thread.
74+
*
75+
* @param callback The callback will be invoked for each subscribed handler, allowing you to call the handler.
76+
*/
77+
suspend fun suspendingFireOnMain(callback: suspend (THandler) -> Unit) {
78+
withContext(Dispatchers.Main) {
79+
for (s in _subscribers) {
80+
callback(s)
81+
}
82+
}
83+
}
5684
}

0 commit comments

Comments
 (0)