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

fix stats orientation change #16411

Merged
merged 1 commit into from
May 20, 2024
Merged

fix stats orientation change #16411

merged 1 commit into from
May 20, 2024

Conversation

MorenoTropical
Copy link
Contributor

Purpose / Description

Describe the problem or feature and motivation

Fixes

Approach

How does this change address the problem?

How Has This Been Tested?

Rotating statistics and it doesnt vanish

Learning (optional, can help others)

Describe the research stage

Links to blog posts, patterns, libraries or addons used to solve this problem

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

@@ -422,6 +422,7 @@
<activity
android:name="com.ichi2.anki.SingleFragmentActivity"
android:exported="false"
android:configChanges="orientation|screenSize"
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 it is feasible to use it for the SingleFragmentActivity as it is also used for other fragments

Copy link
Member

Choose a reason for hiding this comment

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

a reasonable point @criticalAY but doesn't that imply that the other anki backend pages will also have state loss on orientation changes (Previewer, NoteEditor eventually, deck options etc... ?) - may need a more general solution (like an abstract method to save/restore state to enforce they all implement it, or something)

Copy link
Member

Choose a reason for hiding this comment

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

My (unconfirmed) understanding is that WebViews don't handle state restoration correctly (each rotation counts as a refresh).

I may be mistaken here, and it would be extremely useful to have up-to-date documentation

Copy link
Contributor

Choose a reason for hiding this comment

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

We should avoid using configChanges for activities.

There's even a note in the documentation:

Note: Use this attribute only in special cases to improve application performance and responsiveness.

Copy link
Member

Choose a reason for hiding this comment

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

We should avoid using configChanges for activities.

I agree that we should, but I don't think that we can.

I haven't found any Android way to restore the webview state after a configuration change. So any elements that can be changed by the user, like Stats's <input> boxes, will be reset after it.

This would only matter if we had a different layout for landscape mode or different screen sizes, but we don't, so IMO that's fine. criticalAY's concern is valid, but we are more in a point that ignoring these config changes is better than not ignoring them.

Copy link
Member

Choose a reason for hiding this comment

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

Okay, so this is an important issue already, and will only get more important as we get deeper into exposing anki backend pages through AnkiDroid, and some of the pages have the potential to contain a lot of temporary user data that would feel awful to discard on a screen rotation (think: a carefully crafted card template edit, not saved yet, and you accidentally tilt your phone just enough it gets a rotation signal and poof your edits are gone...)

So, within these constraints:

  • overriding config changes is discouraged because then screen rotation is not even possible (for example) and you have to override a list of changes that will grow over time, meaning it's a bit of an "arms race" over time
  • webview state restore cannot be trusted to restore completely or correctly
  • the data we are saving is unbounded, and card templates (to use my favorite example) are specifically known to be larger than Bundles can handle, requiring a file-backed solution

That means that we are on our own basically. We need a solution where Activity restarts are okay, but we handle save/restore somehow in our own code, and the data goes to a file.

I propose a design where we do override onSaveInstanceState and onRestoreInstanceState, and we do put some information in the passed Bundle signaling whether our AnkiDroid-specific WebView state solution has worked, but we don't store our actual data in the Bundle

I then further propose a design where, unfortunately we have to know the HTML elements (DOM tree access paths perhaps?) we care about preserving, and their type, for each page we care about. Then we somehow (perhaps using JSI to inject javascript that iterates on this list of elements?) grab the values and save them to a file to save, and read the file them set the elements on restore

Or something, anything else that seems reasonable (and perhaps better than that 🤷 )

Until that happens, we need to decide what's worse:

  • discarding webview state in our anki backend pages
  • disabling configuration changes (temporarily, until a real state save/restore solution appears)

I submit that discarding state is not that bad currently, with our current set of pages, but I'd feel differently if it was card template editor, and I may be missing some page that already has lots of user-created temp data vs just a search string or deck options change...

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

overriding config changes is discouraged because then screen rotation is not even possible

Screen rotations still works and the user will see the screen in landscape. Just the activity isn't recreated after a rotation. Since we don't have a layout-land for any of the affected screens, nor anything on those screens should change with the device rotation, as they are just a toolbar with a webview, the result is the same.

webview state restore cannot be trusted to restore completely or correctly

that only saves WebBackForwardList, so it's useless for our goal


If we wanted to use different layouts for that, I think that's easier to just handle the configuration change ourselves than trying to restore the webview.

For now, this PR has the best solution IMO

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

This happens in screens like CSV import and deck options as well. Creating a separate activity seems like the correct move it was one or two screens, but is basically all of them

Copy link
Contributor

@lukstbit lukstbit left a comment

Choose a reason for hiding this comment

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

Looking for alternatives brought nothing so I think we can go with this.

@lukstbit lukstbit added Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels May 19, 2024
@BrayanDSO BrayanDSO added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) and removed Needs Second Approval Has one approval, one more approval to merge labels May 20, 2024
@BrayanDSO BrayanDSO merged commit b7ac28c into ankidroid:main May 20, 2024
11 checks passed
@github-actions github-actions bot removed the Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) label May 20, 2024
@github-actions github-actions bot added this to the 2.19 release milestone May 20, 2024
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.

[BUG]: Changing screen orientation clears stats' search options
6 participants