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

[Android] Add support for mentions to the Compose library #854

Merged
merged 31 commits into from
Nov 3, 2023

Conversation

jmartinesp
Copy link
Contributor

@jmartinesp jmartinesp commented Oct 23, 2023

Expose replaceSuggestion and setLinkSuggestion insertMentionAtSuggestion in RichTextEditorState. Also add suggestions UI to the compose example.

Also added MentionDetector to replace the client-side parsing of mentions with something from the RTE library.

@codecov-commenter
Copy link

codecov-commenter commented Oct 23, 2023

Codecov Report

Attention: 16 lines in your changes are missing coverage. Please review.

Comparison is base (28de74e) 89.50% compared to head (ac1c05f) 89.34%.
Report is 8 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #854      +/-   ##
==========================================
- Coverage   89.50%   89.34%   -0.17%     
==========================================
  Files         116       86      -30     
  Lines       16801    15247    -1554     
  Branches      633        0     -633     
==========================================
- Hits        15038    13622    -1416     
+ Misses       1740     1625     -115     
+ Partials       23        0      -23     
Flag Coverage Δ
uitests ?
uitests-ios ?
unittests 89.34% <0.00%> (+1.09%) ⬆️
unittests-ios ?
unittests-rust 89.34% <0.00%> (-0.10%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
crates/matrix_mentions/src/lib.rs 0.00% <0.00%> (ø)
bindings/wysiwyg-ffi/src/lib.rs 0.00% <0.00%> (ø)
bindings/wysiwyg-ffi/src/ffi_mention_detector.rs 0.00% <0.00%> (ø)

... and 32 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@jonnyandrew jonnyandrew left a comment

Choose a reason for hiding this comment

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

Looks good! Just one comment about a potential performance issue with the composable

Comment on lines 142 to 143
if (style != previousStyle) {
view.setStyleConfig(style.toStyleConfig(view.context))
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this should be necessary because the update block should only run when one of the variables used inside it is changed. When I tested it looks like there issue causing the update method to fire every time the state is changed which is a problem.

Are you able to check and see if there is a stability issue using the compose compiler reports tool?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MentionDisplayHandler is an interface, so I think it's impossible for compose to check if it has changed. Maybe that's what's causing the unnecessary recompositions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrapped the mentionDisplayHandler parameter in a lambda to make it stable and prevent recompositions, but there's still some weird issue with style and textStyleTypeface, where they won't cause recompositions when the code is build from a clean state, but if anything in the code changes and we re-build, the update block will recompose with every change in the composer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jonnyandrew could you double check if you can also reproduce the issue above? If not, it might just be an issue on my side and we can assume this is fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Very odd - I can reproduce this unfortunately.

Copy link
Contributor

@jonnyandrew jonnyandrew Nov 2, 2023

Choose a reason for hiding this comment

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

Now that we have a bit more time to work with, is there any way we can eliminate these workarounds (remember and previousStyle)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It depends on whether we want to avoid creating a new HtmlConverter every time anything changes in this component or not. The remember makes the update more predictable, but it will still be triggered when any of its params change.

The current AndroidHtmlConverter is quite light-weight since it only creates a lambda, so we could allow this, or even create the converter on demand whenever it's supposed to be used, but this might not be the case for future or alternative implementations.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was hoping we could try to remove the remember altogether if we can find out why update is being called in an unpredictable way 😅

In terms of the HtmlConverter, I don't see to much problem with doing this in update, given it should only be called infrequently in practice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll remove the previousStyle check, but the remember will have to stay for now, I don't really see what's causing the lambda to be re-created...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, previousStyle has already been removed 😅 . I completely forgot about that.

Copy link
Contributor

@jonnyandrew jonnyandrew left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! I had a few comments

@jmartinesp
Copy link
Contributor Author

I don't like the way we need to provide the styleConfig and mentionDisplayHandler to create the HtmlConverter after c9af2ab, but I can't think of a better way at the moment and this PR is taking ages to merge. Could we address that in a separate one, unless the solution is obvious?

@jmartinesp
Copy link
Contributor Author

The UI tests do work locally, btw. It seems like the project has had issues running these tests for a while, from what I've seen in the results of run actions for other PRs.

…ons.

Also:

- Add missing `mentionDetector` lambda to `StyledHtmlConverter`.
- Add optimizations for recompositions in `EditorStyledText`.
Copy link
Contributor

@jonnyandrew jonnyandrew left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! Sorry it's taking so long to merge this. I've added a few more comments.

@jonnyandrew
Copy link
Contributor

The UI tests do work locally, btw. It seems like the project has had issues running these tests for a while, from what I've seen in the results of run actions for other PRs.

Yeah indeed, I think there must be a caching issue because the tests seem to pass fairly reliably on the second CI run.

return resolveRoomMentionDisplay?.invoke() ?: TextDisplay.Plain
}

}, update = remember(style, typeface, mentionDisplayHandler) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This workaround seems to work, thank you! Please could you add some comments to explain the problem and why this is needed? I've created an issue to link to as well #856. Please feel free to add more detail that could help us in future.

Copy link
Contributor

Choose a reason for hiding this comment

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

(friendly reminder to please link to the issue in the code comments)

Comment on lines 54 to 60
}, update = remember(style != previousStyle, typeface, mentionDisplayHandler, text) {
{ view ->
if (style != previousStyle) {
view.styleConfig = style.toStyleConfig(view.context)
view.applyStyleInCompose(style)
previousStyle = style
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, thanks for the workaround! Please could you add some comments explaining the problem and why this is needed, linking to the GH issue?

Also, unify setting the `StyleConfig` and `MentionDisplayHandler` with a new `setupHtmlConverter` method.
…del` when trying to generate a spanned text from HTML
@jmartinesp
Copy link
Contributor Author

All mentioned issues should have been fixed.

Copy link
Contributor

@jonnyandrew jonnyandrew left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! I have a few more comments as there are quite a few more changes. I've made a few suggestions - Ii'd be great to address the public API issues to minimise breaking changes that we'd need to release in order to fix them.

Copy link
Contributor

@jonnyandrew jonnyandrew left a comment

Choose a reason for hiding this comment

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

There are some mentions related tests failing when I run them locally - please can you fix those?

After that I think we are OK to merge this in its current state and unblock the work on mentions.

@jmartinesp
Copy link
Contributor Author

I think the last issues should have been fixed in ac1c05f (it seems like when I paused this work I forgot to migrate the mention detection logic). The tests work locally for me with those changes, but I can't make them work in the CI.

Copy link

sonarcloud bot commented Nov 3, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@jmartinesp
Copy link
Contributor Author

Thanks again for the latest changes, I was over complicating things quite a bit 😅 . Should this be ready to merge now?

Copy link
Contributor

@jonnyandrew jonnyandrew left a comment

Choose a reason for hiding this comment

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

Nice work, let's keep an eye out for any issues around #856

@jmartinesp jmartinesp merged commit 57d97a7 into main Nov 3, 2023
8 checks passed
@jmartinesp jmartinesp deleted the feat/jme/add-suggestion-replacement-android branch November 3, 2023 11:15
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants