-
Notifications
You must be signed in to change notification settings - Fork 764
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate custom scoped pooling with ComposeComponent
Summary: As per title - in this diff we're making `ComposeComponent` to use custom scoped pooling. It'll dispose composition when the `ComposeView` is discarded from the pool. Reviewed By: kingsleyadio Differential Revision: D64236148 fbshipit-source-id: 680fa599f0082147eb9d82903bb5b7a724e500dd
- Loading branch information
1 parent
1cff5e2
commit de88eca
Showing
5 changed files
with
74 additions
and
4 deletions.
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
58 changes: 58 additions & 0 deletions
58
litho-compose/src/main/kotlin/com/facebook/litho/ComposeComponentViewCompositionStrategy.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,58 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.facebook.litho | ||
|
||
import android.view.View | ||
import androidx.compose.ui.platform.ViewCompositionStrategy.DisposeOnDetachedFromWindow | ||
import androidx.core.view.ancestors | ||
import com.facebook.compose.view.AbstractMetaComposeView | ||
import com.facebook.compose.view.MetaViewCompositionStrategy | ||
|
||
/** | ||
* The composition will be disposed automatically when the view is detached from a window, unless it | ||
* is pooled by Litho. | ||
* | ||
* When not pooled by Litho, this behaves exactly the same as [DisposeOnDetachedFromWindow]. | ||
*/ | ||
object ComposeComponentViewCompositionStrategy : MetaViewCompositionStrategy { | ||
override fun installFor(view: AbstractMetaComposeView): () -> Unit { | ||
val listener = | ||
object : View.OnAttachStateChangeListener { | ||
override fun onViewAttachedToWindow(v: View) = Unit | ||
|
||
override fun onViewDetachedFromWindow(v: View) { | ||
if (!view.isWithinLithoPoolingContainer) { | ||
view.disposeComposition() | ||
} | ||
} | ||
} | ||
view.addOnAttachStateChangeListener(listener) | ||
|
||
return { view.removeOnAttachStateChangeListener(listener) } | ||
} | ||
} | ||
|
||
/** Whether one of this View's ancestors has R.id.litho_pooling_container tag set to true. */ | ||
val View.isWithinLithoPoolingContainer: Boolean | ||
get() { | ||
ancestors.forEach { | ||
if (it is View && it.getTag(R.id.litho_pooling_container) == true) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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