Skip to content

Commit

Permalink
Remove analytics responsibilities from Pushhandler and move them into…
Browse files Browse the repository at this point in the history
… separate class
  • Loading branch information
jraska committed Dec 26, 2019
1 parent 5f47509 commit b2f8442
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.jraska.github.client.push

import com.jraska.github.client.analytics.AnalyticsEvent
import com.jraska.github.client.analytics.EventAnalytics
import com.jraska.github.client.common.BooleanResult
import com.jraska.github.client.identity.IdentityProvider
import javax.inject.Inject

internal class PushAnalytics @Inject constructor(
private val identityProvider: IdentityProvider,
private val eventAnalytics: EventAnalytics
) {
fun onTokenRefresh() {
val tokenEvent = AnalyticsEvent.builder("push_token_refresh")
.addProperty("session_id", identityProvider.session().id.toString())
.build()
eventAnalytics.report(tokenEvent)
}

fun onPushHandled(action: PushAction, result: BooleanResult) {
if (result == BooleanResult.SUCCESS) {
val pushHandled = AnalyticsEvent.builder("push_handled")
.addProperty("push_action", action.name)
.build()
eventAnalytics.report(pushHandled)
} else {
val pushHandled = AnalyticsEvent.builder("push_not_handled")
.addProperty("push_action", action.name)
.build()
eventAnalytics.report(pushHandled)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import com.jraska.github.client.core.android.ServiceModel
import javax.inject.Inject

internal class PushHandleModel @Inject constructor(
private val pushHandler: PushHandler
private val pushHandler: PushHandler,
private val analytics: PushAnalytics,
private val tokenSynchronizer: PushTokenSynchronizer
) : ServiceModel {
fun onMessageReceived(remoteMessage: RemoteMessage) {
val action = RemoteMessageToActionConverter.convert(remoteMessage)
pushHandler.handlePush(action)

val pushResult = pushHandler.handlePush(action)

analytics.onPushHandled(action, pushResult)
}

fun onNewToken(token: String) {
pushHandler.onTokenRefresh(token)
analytics.onTokenRefresh()
tokenSynchronizer.onTokenRefresh(token)
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,25 @@
package com.jraska.github.client.push

import com.jraska.github.client.analytics.AnalyticsEvent
import com.jraska.github.client.analytics.EventAnalytics
import com.jraska.github.client.common.BooleanResult
import com.jraska.github.client.identity.IdentityProvider
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Provider

class PushHandler @Inject internal constructor(
private val eventAnalytics: EventAnalytics,
private val tokenSynchronizer: PushTokenSynchronizer,
private val identityProvider: IdentityProvider,
private val pushCommands: Map<String, @JvmSuppressWildcards Provider<PushActionCommand>>
) {

internal fun handlePush(action: PushAction) {
internal fun handlePush(action: PushAction): BooleanResult {
Timber.v("Push received action: %s", action.name)

val result = handleInternal(action)

if (result == BooleanResult.SUCCESS) {
val pushHandled = AnalyticsEvent.builder("push_handled")
.addProperty("push_action", action.name)
.build()
eventAnalytics.report(pushHandled)
} else {
val pushHandled = AnalyticsEvent.builder("push_not_handled")
.addProperty("push_action", action.name)
.build()
eventAnalytics.report(pushHandled)
}
return handleInternal(action)
}

private fun handleInternal(action: PushAction): BooleanResult {
val actionCommand = pushCommands[action.name] ?: return BooleanResult.FAILURE

return actionCommand.get().execute(action)
}

internal fun onTokenRefresh(token: String) {
tokenSynchronizer.synchronizeToken(token)

val tokenEvent = AnalyticsEvent.builder("push_token_refresh")
.addProperty("session_id", identityProvider.session().id.toString())
.build()
eventAnalytics.report(tokenEvent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class PushTokenSynchronizer @Inject constructor(
private val dateTimeProvider: DateTimeProvider
) {

fun synchronizeToken(token: String) {
fun onTokenRefresh(token: String) {
val instanceId = FirebaseInstanceId.getInstance()
val id = instanceId.id

Expand Down

0 comments on commit b2f8442

Please sign in to comment.