-
Notifications
You must be signed in to change notification settings - Fork 101
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
3/4 fun View.setBackHandler()
replaces val View.backPressedHandler
#1028
Merged
Merged
Changes from all commits
Commits
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 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 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 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 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 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 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 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 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 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 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 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 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 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 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: 2 additions & 0 deletions
2
...-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/BackPressedHandlerTest.kt
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
@file:Suppress("DEPRECATION") | ||
|
||
package com.squareup.workflow1.ui | ||
|
||
import android.view.View | ||
|
54 changes: 54 additions & 0 deletions
54
...low-ui/core-android/src/androidTest/java/com/squareup/workflow1/ui/ViewBackHandlerTest.kt
This file contains 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,54 @@ | ||
package com.squareup.workflow1.ui | ||
|
||
import android.view.View | ||
import androidx.activity.ComponentActivity | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule | ||
import com.google.common.truth.Truth.assertThat | ||
import com.squareup.workflow1.ui.androidx.WorkflowLifecycleOwner | ||
import com.squareup.workflow1.ui.internal.test.IdlingDispatcherRule | ||
import leakcanary.DetectLeaksAfterTestSuccess | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.RuleChain | ||
|
||
@OptIn(WorkflowUiExperimentalApi::class) | ||
internal class ViewBackHandlerTest { | ||
private val scenarioRule = ActivityScenarioRule(ComponentActivity::class.java) | ||
private val scenario get() = scenarioRule.scenario | ||
|
||
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) | ||
.around(scenarioRule) | ||
.around(IdlingDispatcherRule) | ||
|
||
private var viewHandlerCount = 0 | ||
private fun viewBackHandler() { | ||
viewHandlerCount++ | ||
} | ||
|
||
@Test fun itWorksWhenHandlerIsAddedBeforeAttach() { | ||
scenario.onActivity { activity -> | ||
val view = View(activity) | ||
WorkflowLifecycleOwner.installOn(view, activity) | ||
view.setBackHandler { viewBackHandler() } | ||
|
||
activity.setContentView(view) | ||
assertThat(viewHandlerCount).isEqualTo(0) | ||
|
||
activity.onBackPressedDispatcher.onBackPressed() | ||
assertThat(viewHandlerCount).isEqualTo(1) | ||
} | ||
} | ||
|
||
@Test fun itWorksWhenHandlerIsAddedAfterAttach() { | ||
scenario.onActivity { activity -> | ||
val view = View(activity) | ||
activity.setContentView(view) | ||
|
||
view.setBackHandler { viewBackHandler() } | ||
assertThat(viewHandlerCount).isEqualTo(0) | ||
|
||
activity.onBackPressedDispatcher.onBackPressed() | ||
assertThat(viewHandlerCount).isEqualTo(1) | ||
} | ||
} | ||
} |
This file contains 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 | ||||
---|---|---|---|---|---|---|
|
@@ -13,6 +13,7 @@ import com.squareup.workflow1.ui.androidx.WorkflowAndroidXSupport.onBackPressedD | |||||
* A function passed to [View.backPressedHandler], to be called if the back | ||||||
* button is pressed while that view is attached to a window. | ||||||
*/ | ||||||
@Deprecated("Use View.backHandler()") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
@WorkflowUiExperimentalApi | ||||||
public typealias BackPressHandler = () -> Unit | ||||||
|
||||||
|
@@ -24,7 +25,9 @@ public typealias BackPressHandler = () -> Unit | |||||
* That means that this is a last-registered-first-served mechanism, and that it is | ||||||
* compatible with Compose back button handling. | ||||||
*/ | ||||||
@Suppress("DEPRECATION") | ||||||
@WorkflowUiExperimentalApi | ||||||
@Deprecated("Use setOrClearBackHandler") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I see you went through some naming iterations 😄 |
||||||
public var View.backPressedHandler: BackPressHandler? | ||||||
get() = observerOrNull?.handler | ||||||
set(value) { | ||||||
|
@@ -37,9 +40,9 @@ public var View.backPressedHandler: BackPressHandler? | |||||
|
||||||
@WorkflowUiExperimentalApi | ||||||
private var View.observerOrNull: AttachStateAndLifecycleObserver? | ||||||
get() = getTag(R.id.view_back_handler) as AttachStateAndLifecycleObserver? | ||||||
get() = getTag(R.id.view_deprecated_back_handler) as AttachStateAndLifecycleObserver? | ||||||
set(value) { | ||||||
setTag(R.id.view_back_handler, value) | ||||||
setTag(R.id.view_deprecated_back_handler, value) | ||||||
} | ||||||
|
||||||
/** | ||||||
|
@@ -79,7 +82,7 @@ private var View.observerOrNull: AttachStateAndLifecycleObserver? | |||||
@WorkflowUiExperimentalApi | ||||||
private class AttachStateAndLifecycleObserver( | ||||||
private val view: View, | ||||||
val handler: BackPressHandler | ||||||
@Suppress("DEPRECATION") val handler: BackPressHandler | ||||||
) : OnAttachStateChangeListener, DefaultLifecycleObserver { | ||||||
private val onBackPressedCallback = NullableOnBackPressedCallback() | ||||||
private var lifecycleOrNull: Lifecycle? = null | ||||||
|
@@ -130,6 +133,7 @@ private class AttachStateAndLifecycleObserver( | |||||
|
||||||
@WorkflowUiExperimentalApi | ||||||
internal class NullableOnBackPressedCallback : OnBackPressedCallback(false) { | ||||||
@Suppress("DEPRECATION") | ||||||
var handlerOrNull: BackPressHandler? = null | ||||||
|
||||||
override fun handleOnBackPressed() { | ||||||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: