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

use SaveInstanceState to avoid crash in export file when Activity is recreated #14797

Merged
merged 2 commits into from
Dec 20, 2023

Conversation

criticalAY
Copy link
Contributor

@criticalAY criticalAY commented Nov 22, 2023

Purpose / Description

Remove the lateinit var and use shared pref and nullable string to avoid crash in case the developer's option is turned on

Fixes

Fixes #14795

How Has This Been Tested?

google emulator
Screen_recording_20231124_152918.webm

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

@criticalAY
Copy link
Contributor Author

I came across this when I was reproducing #14795, and regarding this issue we can use shared prefs to overcome it what say?
I can update this PR only if the reviewer agrees to this updating the message and commits do let me know

@lukstbit
Copy link
Contributor

lukstbit commented Nov 24, 2023

Is this really needed? I've tested the collection/apkg export and the SnackBar appears where it should, above the FAB without the changes from this PR.

Edit, forgot: I'm working on #14604 and I might be able to delete ActivityExportingDelegate, so you shouldn't spend time on #14795 .

@lukstbit lukstbit added the Needs Author Reply Waiting for a reply from the original author label Nov 24, 2023
@criticalAY
Copy link
Contributor Author

image
This was in case the "dont keep activity" was turned on by the user but after fixing the crash I noticed it still persist I am updating the PR to fix the crash first

@criticalAY criticalAY changed the title anchor the snackbar on fab when exporting col use shared pref to avoid crash in case "don't keep activity" is turned on Nov 24, 2023
@criticalAY
Copy link
Contributor Author

Edit, forgot: I'm working on #14604 and I might be able to delete ActivityExportingDelegate, so you shouldn't spend time on #14795 .

ok, leaving it for now then 👍

Copy link
Contributor

github-actions bot commented Dec 8, 2023

Hello 👋, this PR has had no activity for more than 2 weeks and needs a reply from the author. If you think this is a mistake please comment and ping a maintainer to get this merged ASAP! Thanks for contributing! You have 7 days until this gets closed automatically

@github-actions github-actions bot added the Stale label Dec 8, 2023
@criticalAY
Copy link
Contributor Author

@lukstbit any updates on the issue?

@github-actions github-actions bot removed the Stale label Dec 8, 2023
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.

I've implemented the changes I wanted but it didn't touched the mExportFileName part of the code. So this PR is needed, I left some review notes on this.

@@ -277,7 +284,7 @@ class ActivityExportingDelegate(private val activity: AnkiActivity, private val
* This will allow to check whether a recent export was made, hence scoped storage migration is safe.
*/
private fun saveSuccessfulCollectionExportIfRelevant() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Method should be changed to receive the path as a parameter.

Copy link
Member

Choose a reason for hiding this comment

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

this was not handled.

Copy link
Contributor

Choose a reason for hiding this comment

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

This method is called from two other methods, in one the path is available and could be passed directly and in the other method, saveFileCallback you could retrieve the filename from preferences and pass it to saveSuccessfillColl... and also to exportToProvider() which would allow us to avoid exportfilename?:"".

@david-allison david-allison added Needs Review and removed Needs Author Reply Waiting for a reply from the original author labels Dec 9, 2023
@david-allison
Copy link
Member

Minor: we should remove the name from ACRA reports in the method: createAcraCoreConfigBuilder

@mikehardy mikehardy changed the title use shared pref to avoid crash in case "don't keep activity" is turned on use shared pref to avoid crash in export file when Activity is recreated Dec 11, 2023
@@ -277,7 +279,7 @@ class ActivityExportingDelegate(private val activity: AnkiActivity, private val
* This will allow to check whether a recent export was made, hence scoped storage migration is safe.
*/
private fun saveSuccessfulCollectionExportIfRelevant() {
if (::mExportFileName.isInitialized && !mExportFileName.endsWith(".colpkg")) return
if (!mExportFileName!!.endsWith(".colpkg")) return
Copy link
Member

Choose a reason for hiding this comment

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

we are in a new method here, and the activity may be recreated, but I don't see the filename fetched from prefs and initialized again?

If lukstbit had been handled instead of just resolved and closed without handling, then it would be a method parameter (non-null...) and you wouldn't need the dangerous !! on a var that I think is not initialized at this point

Copy link
Contributor Author

Choose a reason for hiding this comment

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

there is still a risk of it being null (it is 99.9% sure though it won't be null) as getString(EXPORT_FILE_NAME_KEY, "") automatically assigns null value to the pref in case it is null.

Copy link
Member

Choose a reason for hiding this comment

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

I thought it automatically assigned "" to the pref in case it is null.
This doesn't seem to actually address the substance of my comment tho?

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 am pushing the changes for that part I have used elvis operator though in case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can't escape the evil "!!" here if (!mExportFileName!!.endsWith(".colpkg")) return

@mikehardy mikehardy added the Needs Author Reply Waiting for a reply from the original author label Dec 11, 2023
@david-allison
Copy link
Member

Is it feasible to use savedInstanceState here instead of preferences?

@criticalAY
Copy link
Contributor Author

Is it feasible to use savedInstanceState here instead of preferences?

I guess that creates the need for extra calls from the activity where we are using this class, we can avoid that by using the prefs here itself

@criticalAY
Copy link
Contributor Author

Using saved state now instead shared pref

Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

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

one issue regarding invalid states, rest looks great, thanks!

Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

@david-allison david-allison removed Needs Author Reply Waiting for a reply from the original author Needs Review labels Dec 18, 2023
@mikehardy
Copy link
Member

At some point @criticalAY you're going to need to learn how to use git rebase and clean up your commits - this should be a one commit PR, I have no idea why there are 9 different commits most with same title

image

@criticalAY
Copy link
Contributor Author

At some point @criticalAY you're going to need to learn how to use git rebase and clean up your commits - this should be a one commit PR, I have no idea why there are 9 different commits most with same title

image

Give me a moment, will do it

@@ -83,7 +83,7 @@ object CrashReportService {
private fun createAcraCoreConfigBuilder(): CoreConfigurationBuilder {
val builder = CoreConfigurationBuilder()
.withBuildConfigClass(com.ichi2.anki.BuildConfig::class.java) // AnkiDroid BuildConfig - Acrarium#319
.withExcludeMatchingSharedPreferencesKeys("username", "hkey")
.withExcludeMatchingSharedPreferencesKeys("username", "hkey", "file_prefs")
Copy link
Member

Choose a reason for hiding this comment

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

Is this still a thing? Seems related to the previous design that was saved preferences based, but now you're using the bundle passed between previous-killed-activity and newly-resumed-activity ?

Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

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

This looks clean + correct to me know and also "lighter" using the saved instance state style. It's changed a lot and with the rebase + push I'm going to ask for one more approval but I think it's smooth sailing. Thanks!

Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

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

Sorry, another round due to the use of "" as a path

@criticalAY criticalAY changed the title use shared pref to avoid crash in export file when Activity is recreated use SaveInstanceState to avoid crash in export file when Activity is recreated Dec 19, 2023
@david-allison david-allison added Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) squash-merge The pull request currently requires maintainers to "Squash Merge" and removed Needs Second Approval Has one approval, one more approval to merge labels Dec 19, 2023
@david-allison
Copy link
Member

squash-merge The pull request currently requires maintainers to "Squash Merge"

@criticalAY
Copy link
Contributor Author

there was a linting error causing the code quality failure so the latest commit fixes that nothing else

@david-allison david-allison merged commit 675f5f4 into ankidroid:main Dec 20, 2023
7 checks passed
@github-actions github-actions bot added this to the 2.17 release milestone Dec 20, 2023
@github-actions github-actions bot removed Pending Merge Things with approval that are waiting future merge (e.g. targets a future release, CI wait, etc) squash-merge The pull request currently requires maintainers to "Squash Merge" labels Dec 20, 2023
bkzhn pushed a commit to bkzhn/Anki-Android that referenced this pull request Jan 14, 2024
…s recreated (ankidroid#14797)

* use saved instance state to avoid the crash if activity is destroyed

* fixing test issues
Copy link
Contributor

Hi there @criticalAY! This is the OpenCollective Notice for PRs merged from 2023-12-01 through 2023-12-31

If you are interested in compensation for this work, the process with details is here:

https://github.com/ankidroid/Anki-Android/wiki/OpenCollective-Payment-Process#how-to-get-paid

We only post one comment per person per month to avoid spamming you, regardless of the number of PRs merged, but this note applies to all PRs merged for this month

Please understand that our monthly budget is never guaranteed to cover all claims - the cap on payments-per-person may be lower, but we try to make our process as fair and transparent as possible, we just need your understanding.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants