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

Safe epoxy char sequence #4837

Merged
merged 6 commits into from Jan 4, 2022
Merged

Conversation

bmarty
Copy link
Member

@bmarty bmarty commented Jan 3, 2022

Using CharSequence in Epoxy item is not safe, since CharSequence are no immutable.
This PR introduce a wrapper for CharSequences used in Epoxy. If CharSequence is not mandatory, use immutable String.

@bmarty bmarty force-pushed the feature/bma/safe_epoxy_char_sequence branch from 9c602a7 to 4642299 Compare January 3, 2022 13:30
private val hash = charSequence.toString().hashCode()

override fun hashCode() = hash
override fun equals(other: Any?) = other is EpoxyCharSequence && other.hash == hash
Copy link
Member Author

Choose a reason for hiding this comment

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

We do not compare the String content, since the risk of collision is low enough, and Epoxy is mainly using hashCode() to compare items

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 New Vector Ltd
* Copyright (c) 2022 New Vector Ltd
Copy link
Member Author

Choose a reason for hiding this comment

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

Happy new year!

@@ -33,7 +33,7 @@ import im.vector.app.core.extensions.setAttributeTintedImageResource
abstract class RadioButtonItem : VectorEpoxyModel<RadioButtonItem.Holder>() {

@EpoxyAttribute
var title: CharSequence? = null
var title: String? = null
Copy link
Member Author

Choose a reason for hiding this comment

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

Whenever a CharSequence is not required, it simpler to use an immutable String

@bmarty bmarty mentioned this pull request Jan 3, 2022
@github-actions
Copy link

github-actions bot commented Jan 3, 2022

Unit Test Results

  66 files  ±0    66 suites  ±0   53s ⏱️ -13s
135 tests ±0  135 ✔️ ±0  0 💤 ±0  0 ±0 
418 runs  ±0  418 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit e03c806. ± Comparison against base commit ce0a582.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@ganfra ganfra left a comment

Choose a reason for hiding this comment

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

Just one comment, otherwise LGTM

/**
* Wrapper for a CharSequence, which support mutation of the CharSequence, which can happen during rendering
*/
class EpoxyCharSequence(val charSequence: CharSequence) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe let the EpoxyCharSequence inherits CharSequence like this, to avoid accessing charSequence value everywhere

class EpoxyCharSequence(val charSequence: CharSequence) : CharSequence by charSequence

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, let me try it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It does not work, the TextView displays the Object and not the CharSequence... Weird

image

Copy link
Member Author

Choose a reason for hiding this comment

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

According to @ganfra investigation, it can work using

class EpoxyCharSequence(val charSequence: CharSequence) : Spannable by SpannableString(charSequence) {...}

but I prefer not to create a new SpannableString for each charSequence.
Using .charSequence whenever it is necesssary is probably better, because it forces the developer to do it (no "hidden magic")

@bmarty bmarty merged commit 279f9e0 into develop Jan 4, 2022
@bmarty bmarty deleted the feature/bma/safe_epoxy_char_sequence branch January 4, 2022 08:53
bmarty added a commit that referenced this pull request Jan 4, 2022
bmarty added a commit that referenced this pull request Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when codeblocks contain emojis
2 participants