-
Notifications
You must be signed in to change notification settings - Fork 7
IAF | Persisting the webview after close #270
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
542a0e6
Move thread util to core package so it can be shared
7482dfc
Finish thread helper switchover and test coverage
1219884
Keep the webview in memory when detaching (i.e. after closing)
ec789ab
Adds company ID observer to re-initialize the webview when company ch…
b41e1ae
Add unregister and reinitialize methods
e2c9782
Unit tests and fixes from app testing
30458aa
Fix expected observers test
e4745ee
Persisting the webview uncovered an issue with presenting a second fo…
ea1de3f
Fixing up rebase issues
0ab8b20
Fixed presentation manager tests to account for mocking the "presenti…
74e044f
fix a few compiler warnings
2886deb
PR Feedback and timeout config validation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
sdk/analytics/src/main/java/com/klaviyo/analytics/model/StateKey.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.klaviyo.analytics.model | ||
|
||
sealed class StateKey(name: String) : Keyword(name) { | ||
/** | ||
* Key in state for storing the company ID aka public API key | ||
*/ | ||
object API_KEY : StateKey("api_key") | ||
|
||
/** | ||
* Key in state for storing the latest push status | ||
*/ | ||
internal object PUSH_STATE : StateKey("push_state") | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
sdk/analytics/src/main/java/com/klaviyo/analytics/state/StateChange.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
sdk/analytics/src/test/java/com/klaviyo/analytics/state/KlaviyoStateTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
sdk/core/src/main/java/com/klaviyo/core/utils/KlaviyoThreadHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.klaviyo.core.utils | ||
|
||
import android.os.Handler | ||
import android.os.HandlerThread | ||
import android.os.Looper | ||
|
||
/** | ||
* Abstraction of our interactions with handlers/threads for isolation purposes | ||
* @deprecated Use [com.klaviyo.core.Registry.threadHelper] instead | ||
*/ | ||
object KlaviyoThreadHelper : ThreadHelper { | ||
override fun getHandler(looper: Looper) = Handler(looper) | ||
override fun getHandlerThread(name: String?) = HandlerThread(name) | ||
evan-masseau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
override fun runOnUiThread(job: () -> Unit) { | ||
val mainLooper = Looper.getMainLooper() | ||
|
||
if (mainLooper == Looper.myLooper()) { | ||
// Already on main thread, run immediately | ||
job() | ||
} else { | ||
// Post to main thread | ||
getHandler(mainLooper).post(job) | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
sdk/core/src/main/java/com/klaviyo/core/utils/ThreadHelper.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.klaviyo.core.utils | ||
|
||
import android.os.Handler | ||
import android.os.HandlerThread | ||
import android.os.Looper | ||
|
||
/** | ||
* Abstraction of our interactions with handlers/threads for isolation purposes | ||
*/ | ||
interface ThreadHelper { | ||
fun getHandler(looper: Looper): Handler | ||
fun getHandlerThread(name: String?): HandlerThread | ||
fun runOnUiThread(job: () -> Unit) | ||
} |
74 changes: 74 additions & 0 deletions
74
sdk/core/src/test/java/com/klaviyo/core/utils/ThreadHelperTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.klaviyo.core.utils | ||
|
||
import android.os.Handler | ||
import android.os.HandlerThread | ||
import android.os.Looper | ||
import io.mockk.EqMatcher | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.mockkConstructor | ||
import io.mockk.mockkStatic | ||
import io.mockk.unmockkConstructor | ||
import io.mockk.unmockkStatic | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
|
||
class ThreadHelperTest { | ||
@Before | ||
fun setUp() { | ||
mockkStatic(Looper::class) | ||
mockkConstructor(Handler::class) | ||
mockkConstructor(HandlerThread::class) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
unmockkStatic(Looper::class) | ||
unmockkConstructor(Handler::class) | ||
unmockkConstructor(HandlerThread::class) | ||
} | ||
|
||
@Test | ||
fun `runOnUiThread runs block immediately on main thread`() { | ||
val mainLooper = mockk<Looper>() | ||
every { Looper.getMainLooper() } returns mainLooper | ||
every { Looper.myLooper() } returns mainLooper | ||
|
||
var ran = false | ||
KlaviyoThreadHelper.runOnUiThread { ran = true } | ||
|
||
assert(ran) { | ||
"Block should have run immediately on main thread" | ||
} | ||
} | ||
|
||
@Test | ||
fun `runOnUiThread posts block when not on main thread`() { | ||
val mainLooper = mockk<Looper>() | ||
val otherLooper = mockk<Looper>() | ||
every { Looper.getMainLooper() } returns mainLooper | ||
every { Looper.myLooper() } returns otherLooper | ||
|
||
every { constructedWith<Handler>(EqMatcher(mainLooper, true)).post(any()) } answers { | ||
firstArg<Runnable>().run() | ||
true | ||
} | ||
|
||
var ran = false | ||
KlaviyoThreadHelper.runOnUiThread { ran = true } | ||
|
||
assert(ran) { | ||
"Block should have run after being posted to main thread" | ||
} | ||
} | ||
|
||
@Test | ||
fun `getHandlerThread returns a HandlerThread for that name`() { | ||
val name = "TestThread" | ||
every { constructedWith<HandlerThread>(EqMatcher(name, true)).name } returns name | ||
val result = KlaviyoThreadHelper.getHandlerThread(name) | ||
assertEquals(name, result.name) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.