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 Android.saveFile if available #59

Merged
merged 1 commit into from
Mar 18, 2024

Conversation

joserebelo
Copy link
Contributor

@joserebelo joserebelo commented Mar 16, 2024

Use the Android javascript interface implemented in https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/3631 if available, allowing us to get the filename and mimetype.

@@ -153,6 +153,12 @@ const Util = {
Util.domModal.classList.remove("active");
},
saveFile : function saveFile(filename, mimeType, dataAsString) {
if (typeof Android !== "undefined" && typeof Android.saveFile === 'function') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not clear to me why we have both customize.js and interface.js, so I updated both.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks! Customise is for when an app is about to be uploaded, and interface is for when it's already loaded.

I know there's bags of overlap now, but initially they were quite separate (customise didn't used to have anything in it to get data from the watch)

@gfwilliams gfwilliams merged commit 2603ca6 into espruino:master Mar 18, 2024
1 check passed
@joserebelo joserebelo deleted the save-file branch March 18, 2024 09:26
@gfwilliams
Copy link
Member

Thanks for this! It's a shame that the existing save implementation wasn't able to get the filename somehow.

There are other cases in the main app loader where we save to a file as well. Although maybe only backing up so far: https://github.com/search?q=repo%3Aespruino%2FBangleApps%20fileSaveDialog&type=code

Espruino.Core.Utils.fileSaveDialog is provided by the espruinotools.js file which is generated from the Web IDE which probably doesn't want any Gadgetbridge-specific stuff - so maybe we should just add something like this in android.html?:

{
  let oldFileSaveDialog = Espruino.Core.Utils.fileSaveDialog;
  Espruino.Core.Utils.fileSaveDialog = function(content, filename) {
      if (typeof Android !== "undefined" && typeof Android.saveFile === 'function') {
        Android.saveFile(filename, "??", btoa(content));
        return;
    };
    return oldFileSaveDialog(content, filename);
}

@joserebelo
Copy link
Contributor Author

@gfwilliams I was not aware of that one, I only found Utils.saveFile used in the apps.

That overwrite from android.html is an interesting idea, and it leaves me wondering if we should adopt the same strategy for saveFile too, as it is an android-specific thing that could be kept ouf ot this core repo..

@gfwilliams
Copy link
Member

I think in this case what you've done is good - we can put individual overrides in android.html easily, but we can't for customise/interface files as they're inside an iframe so will be running the same code on android or other platforms.

I guess in theory Utils.saveFile could postMessage the file to the main app and that could do the saving, but for the sake of a few lines I think your solution here is best.

I'm reasonably confident the code I posted above will work, but is it possible for you to check it out with your saveFile changes to Gadgetbridge? To trigger it, it's app loader -> more -> backup (although that can take a while if your watch has lots of stuff on it)

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.

2 participants