Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

[Android] Add releaseOnDetach parameter to EditorStyledText #914

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Changes from 2 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 @@ -27,6 +27,8 @@ import io.element.android.wysiwyg.display.TextDisplay
* @param resolveMentionDisplay A function to resolve the [TextDisplay] of a mention.
* @param resolveRoomMentionDisplay A function to resolve the [TextDisplay] of an `@room` mention.
* @param style The styles to use for any customisable elements.
* @param releaseOnDetach Whether to release the view when the composable is detached from the composition or not.
* Setting this to `false` is specially useful in Lazy composables that need to reuse these views. Defaults to `true`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am reading the documentation of AndroidView.onReset:

onReset - A callback invoked as a signal that the view is about to be attached to the composition hierarchy in a different context than its original creation. This callback is invoked before update and should prepare the view for general reuse. If null or not specified, the AndroidView instance will not support reuse, and the View instance will always be discarded whenever the AndroidView is moved or removed from the composition hierarchy.

So I understand that passing true for releaseOnDetach will allow the re-usage of the View, which is the opposite of the documentation here, and the description of the PR. Maybe I miss something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, thanks for double checking! I'll upload a fix.

*/
@Composable
fun EditorStyledText(
Expand All @@ -37,6 +39,7 @@ fun EditorStyledText(
onLinkClickedListener: ((String) -> Unit) = {},
onTextLayout: (Layout) -> Unit = {},
style: RichTextEditorStyle = RichTextEditorDefaults.style(),
releaseOnDetach: Boolean = true,
) {
val typeface by style.text.rememberTypeface()
val mentionDisplayHandler = remember(resolveMentionDisplay, resolveRoomMentionDisplay) {
Expand Down Expand Up @@ -70,6 +73,9 @@ fun EditorStyledText(
}
view.onLinkClickedListener = onLinkClickedListener
view.onTextLayout = onTextLayout
}
},
onReset = { view: EditorStyledTextView ->
view.setText("", TextView.BufferType.SPANNABLE)
}.takeIf { releaseOnDetach },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe you want to use takeUnless here?

)
}
Loading