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

[Android] Add workarounds for ignoring state restoration and invalid line count #864

Merged
merged 6 commits into from
Nov 16, 2023

Conversation

jmartinesp
Copy link
Contributor

@jmartinesp jmartinesp commented Nov 14, 2023

I found 2 more bugs in the interop between Compose and the AndroidView:

  1. When restoring state in RealEditor's AndroidView, for some reason if there is a single NSBP char at the end of the HTML it'll just remove it, and it can cause crashes because the selection may become invalid. - this was fixed in Rust. In any case, I believe this state restoration is not needed because the state is kept at the ComposerModel and the text and selection are restored in the EditorEditText.onRestoreInstanceState method. This should fix the crashes we're seeing in EXA when a mention is added.
  2. Related to the previous issue, the factory contents run before this onRestoreInstanceState runs, so there's no way to know if we're dealing with a restoration, AFAICT, so by the time we're adding the text changed listener on the factory the state hasn't been restored and the lineCount will always be 0, which in turn will trigger a recomposition of the whole component, and then another one once we have the right lineCount = 1 value. I changed the code to only take into account lineCount if it's > 0, which should fit our usage since we don't care about value 0 (the edittext being empty).

@codecov-commenter
Copy link

codecov-commenter commented Nov 14, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b9fca08) 89.40% compared to head (73430cb) 89.35%.
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #864      +/-   ##
==========================================
- Coverage   89.40%   89.35%   -0.06%     
==========================================
  Files         118       86      -32     
  Lines       16845    15271    -1574     
  Branches      635        0     -635     
==========================================
- Hits        15061    13646    -1415     
+ Misses       1760     1625     -135     
+ Partials       24        0      -24     
Flag Coverage Δ
uitests ?
uitests-ios ?
unittests 89.35% <ø> (+1.32%) ⬆️
unittests-ios ?
unittests-rust 89.35% <ø> (ø)

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

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

 - Ignoring state restoration in Compose.
 - Invalid line count causing recompositions.
@jmartinesp jmartinesp force-pushed the fix/jme/android-crash-selection-on-view-restoration branch from 9a1ce4e to 3a78de1 Compare November 15, 2023 08:05
@jmartinesp jmartinesp force-pushed the fix/jme/android-crash-selection-on-view-restoration branch from 3a78de1 to cc467da Compare November 15, 2023 10:17
@jmartinesp
Copy link
Contributor Author

Can I get another review for this PR? I added tests for checking the text + selection restoration, as well as keeping the right line count. I also tested it on EXA and it seems to work fine.

Comment on lines 141 to 145
// Set initial HTML and selection, state will be restored automatically in the view
if (editableText.isEmpty()) {
setHtml(state.internalHtml)
setSelection(state.selection.first, state.selection.second)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

In EXA, the selection state is no longer restored when toggling the text formatting options:

Screen_recording_20231115_162355.webm

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. I'm reverting this code! It's weird, because it contradicts some of my previous tests while debugging, but maybe fixing the previous bug also changed that behaviour.

Copy link
Contributor

@jonnyandrew jonnyandrew Nov 15, 2023

Choose a reason for hiding this comment

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

I've added an extra assertion for this case to help catch this in future: #868

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh cool, I was adding something similar but your tests are more thorough so let's use them instead 👍 .

Copy link
Contributor Author

@jmartinesp jmartinesp Nov 15, 2023

Choose a reason for hiding this comment

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

Actually, I accidentally uploaded some changes, but those also verify line count is not 1 by default, so maybe we can change the #868 PR to be based on these? I'll squash the commits anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure that makes sense - I can update #868 to use two lines of text.

Copy link

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

@@ -135,7 +138,7 @@ private fun RealEditor(

applyDefaultStyle()

// Restore the state of the view with the saved state
// Set initial HTML and selection based on the provided state
Copy link
Contributor

Choose a reason for hiding this comment

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

This is less confusing, thanks!

@jmartinesp jmartinesp merged commit 581f1dd into main Nov 16, 2023
6 of 7 checks passed
@jmartinesp jmartinesp deleted the fix/jme/android-crash-selection-on-view-restoration branch November 16, 2023 09:54
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