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

[CHNL-16941] Refactor / Cleanup Forms Module #222

Open
wants to merge 7 commits into
base: feature/IAM-post-3.0.3-rebase
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.app.Application
import android.os.Bundle
import com.klaviyo.core.Registry
import java.lang.ref.WeakReference
import java.util.Collections

/**
Expand All @@ -17,6 +18,11 @@ internal object KlaviyoLifecycleMonitor : LifecycleMonitor, Application.Activity
mutableListOf<ActivityObserver>()
)

private lateinit var weakActivityReference: WeakReference<Activity>

override val currentActivity: Activity?
get() = weakActivityReference.get()

override fun onActivityEvent(observer: ActivityObserver) {
activityObservers += observer
}
Expand Down Expand Up @@ -44,6 +50,7 @@ internal object KlaviyoLifecycleMonitor : LifecycleMonitor, Application.Activity
}

override fun onActivityResumed(activity: Activity) {
weakActivityReference = WeakReference(activity)
broadcastEvent(ActivityEvent.Resumed(activity))
}

Expand All @@ -52,10 +59,18 @@ internal object KlaviyoLifecycleMonitor : LifecycleMonitor, Application.Activity
}

override fun onActivityPaused(activity: Activity) {
checkActivityClear(activity)
broadcastEvent(ActivityEvent.Paused(activity))
}

private fun checkActivityClear(activity: Activity) {
if (activity == weakActivityReference.get()) {
weakActivityReference.clear()
}
}

override fun onActivityStopped(activity: Activity) {
checkActivityClear(activity)
if (activeActivities == 0) return

activeActivities--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ sealed class ActivityEvent(val activity: Activity? = null, val bundle: Bundle? =
*/
interface LifecycleMonitor {

val currentActivity: Activity?

/**
* Register an observer to be notified when all application activities stopped
*
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions sdk/messaging/build.gradle → sdk/forms/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
project.description = 'In-app messaging functionality for the Klaviyo SDK suite'
project.description = 'In-app forms functionality for the Klaviyo SDK suite'
evaluationDependsOn(':sdk')

def ext = rootProject.ext

android {
namespace "${klaviyoGroupId}.messaging"
namespace "${klaviyoGroupId}.forms"

publishing {
singleVariant(ext.publishBuildVariant) {
Expand All @@ -30,7 +30,7 @@ afterEvaluate {
release(MavenPublication) {
from components[ext.publishBuildVariant]
groupId = klaviyoGroupId
artifactId = 'messaging'
artifactId = 'forms'
version = readXmlValue('src/main/res/values/strings.xml','klaviyo_sdk_version_override', project(":sdk:core"))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.klaviyo.messaging
package com.klaviyo.forms

import org.json.JSONArray

Expand Down
25 changes: 25 additions & 0 deletions sdk/forms/src/main/java/com/klaviyo/forms/InAppForms.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.klaviyo.forms

import com.klaviyo.analytics.Klaviyo
import com.klaviyo.core.Registry
import com.klaviyo.core.safeApply
import java.io.BufferedReader

fun Klaviyo.registerForInAppForms(): Klaviyo = safeApply {
val webView = KlaviyoWebViewDelegate()
val klaviyoJsUrl =
// todo remove the asset source from url
"${Registry.config.baseCdnUrl}/onsite/js/klaviyo.js?env=in-app&company_id=${Registry.config.apiKey}&assetSource=pr-38360"
val html = Registry.config.applicationContext
.assets
.open("InAppFormsTemplate.html")
.bufferedReader()
.use(BufferedReader::readText)
.replace(IAF_BRIDGE_NAME_PLACEHOLDER, IAF_BRIDGE_NAME)
.replace(IAF_SDK_NAME_PLACEHOLDER, Registry.config.sdkName)
.replace(IAF_SDK_VERSION_PLACEHOLDER, Registry.config.sdkVersion)
.replace(IAF_HANDSHAKE_PLACEHOLDER, IAF_HANDSHAKE)
.replace(IAF_KLAVIYO_JS_PLACEHOLDER, klaviyoJsUrl)

webView.loadHtml(html)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.klaviyo.messaging
package com.klaviyo.forms

import com.klaviyo.analytics.model.Event
import com.klaviyo.analytics.networking.requests.AggregateEventPayload
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.klaviyo.messaging
package com.klaviyo.forms

import android.annotation.SuppressLint
import android.app.Activity
Expand All @@ -23,16 +23,14 @@ import com.klaviyo.analytics.networking.ApiClient
import com.klaviyo.core.BuildConfig
import com.klaviyo.core.Registry
import com.klaviyo.core.config.Clock
import java.lang.ref.WeakReference

class KlaviyoWebView(
activity: Activity
) : WebViewClient(), WebViewCompat.WebMessageListener {
internal class KlaviyoWebViewDelegate() : WebViewClient(), WebViewCompat.WebMessageListener {
companion object {
private const val MIME_TYPE = "text/html"
}

private val activityRef = WeakReference(activity)
private val activity: Activity?
get() = Registry.lifecycleMonitor.currentActivity

// for timeout on js communications
private var timer: Clock.Cancellable? = null
Expand All @@ -42,7 +40,7 @@ class KlaviyoWebView(
}

@SuppressLint("SetJavaScriptEnabled")
private val webView = WebView(activity).also {
private val webView = WebView(Registry.config.applicationContext).also {
it.setBackgroundColor(Color.TRANSPARENT)
it.webViewClient = this
it.settings.userAgentString = DeviceProperties.userAgent
Expand All @@ -53,8 +51,7 @@ class KlaviyoWebView(
WebViewCompat.addWebMessageListener(
it,
IAF_BRIDGE_NAME,
// TODO - sort out how to toggle between local and production.
setOf("http://a.local-klaviyo.com:8080"),
setOf(Registry.config.baseCdnUrl),
this
)
} else {
Expand Down Expand Up @@ -87,20 +84,18 @@ class KlaviyoWebView(
data = request.url
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
activityRef.get()?.let { startActivity(it, intent, null) }
activity?.let {
startActivity(it, intent, null)
} ?: run {
Registry.log.error("Unable to launch external browser, null activity reference")
}
return true
}
return super.shouldOverrideUrlLoading(view, request)
}

fun addTo(view: ViewGroup) {
webView.visibility = View.INVISIBLE
view.addView(webView)
}

fun loadHtml(html: String) = webView.loadDataWithBaseURL(
// TODO - sort out how to toggle between local and production.
"http://a.local-klaviyo.com:8080",
Registry.config.baseCdnUrl,
html,
MIME_TYPE,
null,
Expand All @@ -119,7 +114,14 @@ class KlaviyoWebView(
}

private fun show() {
webView.post { webView.visibility = View.VISIBLE }
activity?.let {
it.window.decorView
.findViewById<ViewGroup>(android.R.id.content)
.addView(webView)
webView.post { webView.visibility = View.VISIBLE }
} ?: run {
Registry.log.error("Unable to show IAF - null activity context reference")
}
}

private fun close() = webView.post {
Expand Down Expand Up @@ -160,7 +162,7 @@ class KlaviyoWebView(

private fun deepLink(messageType: KlaviyoWebFormMessageType.DeepLink) {
val uri = Uri.parse(messageType.route)
activityRef.get()?.let { activity ->
activity?.let { activity ->
activity.startActivity(
Intent().apply {
data = uri
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.klaviyo.messaging
package com.klaviyo.forms

import com.klaviyo.analytics.model.Event
import com.klaviyo.analytics.model.EventKey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.klaviyo.messaging
package com.klaviyo.forms

import com.klaviyo.analytics.model.EventKey
import com.klaviyo.analytics.model.EventMetric
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.klaviyo.messaging
package com.klaviyo.forms

import org.junit.Assert.assertEquals
import org.junit.Test
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencyResolutionManagement {

// sdk modules
include ":sdk"
include ":sdk:core", ":sdk:analytics", ":sdk:push-fcm", ":sdk:fixtures", ":sdk:messaging"
include ":sdk:core", ":sdk:analytics", ":sdk:push-fcm", ":sdk:fixtures", ":sdk:forms"

refreshVersions {
rejectVersionIf {
Expand Down