Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composing push handlers #84

Merged
merged 3 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'com.google.gms:google-services:3.1.1'
classpath 'com.google.gms:google-services:3.2.1'
classpath 'com.google.firebase:firebase-plugins:1.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'io.fabric.tools:gradle:1.24.4'
Expand Down
12 changes: 6 additions & 6 deletions client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ dependencies {
compile 'com.android.support:support-compat:27.1.1'
compile 'com.android.support:animated-vector-drawable:27.1.1'

compile 'com.google.firebase:firebase-core:15.0.0'
compile 'com.google.firebase:firebase-config:15.0.0'
compile 'com.google.firebase:firebase-perf:15.0.0'
compile 'com.google.firebase:firebase-messaging:15.0.0'
compile 'com.google.firebase:firebase-database:15.0.0'
compile 'com.crashlytics.sdk.android:crashlytics:2.9.1'
compile 'com.google.firebase:firebase-core:15.0.2'
compile 'com.google.firebase:firebase-config:15.0.2'
compile 'com.google.firebase:firebase-perf:15.1.0'
compile 'com.google.firebase:firebase-messaging:15.0.2'
compile 'com.google.firebase:firebase-database:15.0.1'
compile 'com.crashlytics.sdk.android:crashlytics:2.9.2'

compile 'android.arch.lifecycle:runtime:1.1.1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.jraska.github.client.analytics.AnalyticsProperty
import com.jraska.github.client.analytics.EventAnalytics
import com.jraska.github.client.http.HttpComponent
import com.jraska.github.client.logging.CrashReporter
import com.jraska.github.client.push.PushModule
import com.jraska.github.client.users.UserViewModelModule
import com.jraska.github.client.users.data.UsersDataModule
import dagger.Component
Expand All @@ -14,7 +15,7 @@ import retrofit2.Retrofit

@PerApp
@Component(modules = arrayOf(UsersDataModule::class, UserViewModelModule::class,
NavigationModule::class, AppModule::class, HttpComponentModule::class, CoreComponentModule::class))
NavigationModule::class, PushModule::class, AppModule::class, HttpComponentModule::class, CoreComponentModule::class))
interface AppComponent {
fun inject(app: GitHubClientApp)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jraska.github.client

import com.jraska.github.client.common.DeveloperError
import com.jraska.github.client.ui.BaseActivity
import com.jraska.github.client.ui.RepoDetailActivity
import com.jraska.github.client.ui.UserDetailActivity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.jraska.github.client.push

import com.jraska.github.client.Config
import com.jraska.github.client.analytics.AnalyticsProperty

class ConfigAsPropertyCommand constructor(private val config: Config,
private val analyticsProperty: AnalyticsProperty) : PushActionCommand {
override fun execute(action: PushAction): Boolean {
val key = action.parameters["config_key"] ?: return false

val value = config.getString(key)
analyticsProperty.setUserProperty(key, value)
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.jraska.github.client.push

interface PushActionCommand {
fun execute(action: PushAction): Boolean
}
81 changes: 5 additions & 76 deletions client/src/main/java/com/jraska/github/client/push/PushHandler.kt
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
package com.jraska.github.client.push

import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.support.v4.app.NotificationCompat
import com.jraska.github.client.Config
import com.jraska.github.client.NotificationSetup
import com.jraska.github.client.R
import com.jraska.github.client.analytics.AnalyticsEvent
import com.jraska.github.client.analytics.AnalyticsProperty
import com.jraska.github.client.analytics.EventAnalytics
import com.jraska.github.client.ui.UriHandlerActivity
import dagger.Lazy
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 config: Lazy<Config>,
private val analyticsProperty: Lazy<AnalyticsProperty>,
private val context: Context,
private val notificationManager: NotificationManager) {
private val pushCommands: Map<String, @JvmSuppressWildcards Provider<PushActionCommand>>) {


internal fun handlePush(action: PushAction) {
Timber.v("Push received action: %s", action.name)
Expand All @@ -44,58 +31,9 @@ class PushHandler @Inject internal constructor(
}

private fun handleInternal(action: PushAction): Boolean {
return when (action.name) {
ACTION_REFRESH_CONFIG -> refreshConfig()
ACTION_CONFIG_VALUE_AS_PROPERTY -> configAsProperty(action)
ACTION_SET_ANALYTICS_PROPERTY -> setAnalyticsProperty(action)
ACTION_NOTIFICATION -> showNotification(action)

else -> false
}
}

private fun showNotification(action: PushAction): Boolean {
val title = action.parameters["title"] ?: return false
val message = action.parameters["message"] ?: return false
val deepLink = action.parameters["clickDeepLink"] ?: return false

val intent = Intent(context, UriHandlerActivity::class.java)
intent.data = Uri.parse(deepLink)

val linkContentIntent = PendingIntent.getActivity(context, 0, intent, 0)

val notification = NotificationCompat.Builder(context, NotificationSetup.PUSH_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(linkContentIntent)
.setAutoCancel(true)
.build()

notificationManager.notify(PUSH_NOTIFICATION_ID, notification)
return true
}
val actionCommand = pushCommands.get(action.name) ?: return false

private fun setAnalyticsProperty(action: PushAction): Boolean {
val key = action.parameters["property_key"] ?: return false

val value = action.parameters["property_value"] ?: return false

analyticsProperty.get().setUserProperty(key, value)
return true
}

private fun configAsProperty(action: PushAction): Boolean {
val key = action.parameters["config_key"] ?: return false

val value = config.get().getString(key)
analyticsProperty.get().setUserProperty(key, value)
return true
}

private fun refreshConfig(): Boolean {
config.get().triggerRefresh()
return true
return actionCommand.get().execute(action)
}

internal fun onTokenRefresh() {
Expand All @@ -104,13 +42,4 @@ class PushHandler @Inject internal constructor(
val tokenEvent = AnalyticsEvent.create("push_token_refresh")
eventAnalytics.report(tokenEvent)
}

companion object {
private const val ACTION_REFRESH_CONFIG = "refresh_config"
private const val ACTION_CONFIG_VALUE_AS_PROPERTY = "set_config_as_property"
private const val ACTION_SET_ANALYTICS_PROPERTY = "set_analytics_property"
private const val ACTION_NOTIFICATION = "notification"

private const val PUSH_NOTIFICATION_ID: Int = 1
}
}
45 changes: 45 additions & 0 deletions client/src/main/java/com/jraska/github/client/push/PushModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.jraska.github.client.push

import android.app.NotificationManager
import android.content.Context
import com.jraska.github.client.Config
import com.jraska.github.client.analytics.AnalyticsProperty
import dagger.Module
import dagger.Provides
import dagger.multibindings.IntoMap
import dagger.multibindings.StringKey

@Module
object PushModule {
@JvmStatic
@Provides
@IntoMap
@StringKey("refresh_config")
fun refreshConfigCommand(config: Config): PushActionCommand {
return RefreshConfigCommand(config)
}

@JvmStatic
@Provides
@IntoMap
@StringKey("set_config_as_property")
fun configAsPropertyCommand(config: Config, analyticsProperty: AnalyticsProperty): PushActionCommand {
return ConfigAsPropertyCommand(config, analyticsProperty)
}

@JvmStatic
@Provides
@IntoMap
@StringKey("set_analytics_property")
fun setAnalyticsProperty(analyticsProperty: AnalyticsProperty): PushActionCommand {
return SetAnalyticsPropertyPushCommand(analyticsProperty)
}

@JvmStatic
@Provides
@IntoMap
@StringKey("notification")
fun notificationCommand(context: Context, notificationManager: NotificationManager): PushActionCommand {
return ShowNotificationPushCommand(context, notificationManager)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.jraska.github.client.push

import com.jraska.github.client.Config

class RefreshConfigCommand constructor(private val config: Config) : PushActionCommand {
override fun execute(action: PushAction): Boolean {
config.triggerRefresh()
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jraska.github.client.push

import com.jraska.github.client.analytics.AnalyticsProperty

class SetAnalyticsPropertyPushCommand constructor(private val analyticsProperty: AnalyticsProperty) : PushActionCommand {
override fun execute(action: PushAction): Boolean {
val key = action.parameters["property_key"] ?: return false

val value = action.parameters["property_value"] ?: return false

analyticsProperty.setUserProperty(key, value)
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.jraska.github.client.push

import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.support.v4.app.NotificationCompat
import com.jraska.github.client.NotificationSetup
import com.jraska.github.client.R
import com.jraska.github.client.ui.UriHandlerActivity

class ShowNotificationPushCommand constructor(private val context: Context,
private val notificationManager: NotificationManager) : PushActionCommand {
override fun execute(action: PushAction): Boolean {
val title = action.parameters["title"] ?: return false
val message = action.parameters["message"] ?: return false
val deepLink = action.parameters["clickDeepLink"] ?: return false

val intent = Intent(context, UriHandlerActivity::class.java)
intent.data = Uri.parse(deepLink)

val linkContentIntent = PendingIntent.getActivity(context, 0, intent, 0)

val notification = NotificationCompat.Builder(context, NotificationSetup.PUSH_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(linkContentIntent)
.setAutoCancel(true)
.build()

notificationManager.notify(1, notification)
return true
}
}