Fix scoped storage permission issues causing a crash when opening a typst project folder that was not just created by the app (+ manual refresh for big documents + auto refresh onResume for cloud syncing + background preview refresh without freezing UI) #151
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Before this fix, creating a folder worked fine, but accessing an already existing folder crashed. The only exception is when we create a folder, and then try to reopen it just after. But once we erase the app's cache, or reinstall it, or create another folder, then trying to open the previously created folder will crash, just like for any other folder.
This is because of scoped storage limitations introduced in Android 10+. An app can freely create a folder, and access all files inside (files are then considered as "media" files, and the folder a media database of resources), but this is only temporary: the access to this "media" folder will be lost once the app requests the access to another folder or is reinstalled or its cache is erased. This is exactly what happened here.
The solution is to go through the hoops of the scoped storage new system to ask specifically to access an already existing folder and then ask for a permission to write in it. The functions all changed and it's very complicated to get it right.
Fortunately, there is the SimpleStorage library. I implented it here only to open already existing Typst folders, the biggest issue currently IMHO.
The same fix should be applied to creating a Typst folder (although it works currently, the folder is created with the improper method, as a "media" collection instead of being just a raw folder of files - in practice it doesn't change much in this case I think but to be more future-proof it would be better to also use SimpleStorage for creation too) ; and also the fix should be applied to opening files. I did not try to open markdown files because I am using another editor for this purpose, but I guess the same issue will arise.
This PR may also be unclean, I did it very roughly and quickly as I am currently lacking time, it's more a proof of concept than a polished implementation, so please feel free to cherry pick and edit as you see fit.
Note that it includes additional edits than the scoped storage fix (all the changes in TypstProjectViewModel.kt were made prior to try to make rustService more robust against crashes or more explicit with log messages, you can keep or remove as you prefer).
Fixes #149
For those who want to try the debug apk: https://github.com/lrq3000/BeauTyXT/releases/tag/scoped-storage-fix4-and-background-refresh
/EDIT: Since I aim to use this app to write my whole PhD thesis, I am going to use it extensively on a daily basis. I added a few other features:
TODO: