reciper icon indicating copy to clipboard operation
reciper copied to clipboard

Fix encoding when importing recipies

Open RowEchelonForm opened this issue 8 months ago • 0 comments

Summary

Fix to import functionality when importing recipies that contain "special" characters.

Changes

  • Use XFile.readAsBytes() and Encoding.decode(...) instead of XFile.readAsString() in function userImport of class Utils
    • I have no previous experience with Dart so I have no idea why XFile.readAsString() didn't work :sweat_smile:
  • Change file writing code in function userExport of class Utils so that it uses Encoding.encode(...) and File.writeAsBytes() (this change is just for consistency)
  • Added some missing(?) await keywords to the import and export functions
    • The file export would sometimes just export an empty file, so that might have been an issue with the async code. Not sure. :thinking: Again, this is my first time using Dart so I might be wrong

Detailed problem description

Encoding seemed to break when recipies with certain special characters were imported (e.g. characters that are used in other languages besides English). Steps to reproduce:

  1. Create a new recipe
  2. Add with some non-English characters (like åäö) to one or all of the text fields
    • Or copy-paste the test data at the end of this PR description)
  3. Save the recipe
  4. Go to "Settings" -> "Export recipies" and export the data (e.g. to Google Drive)
  5. Go to "Home" and delete the test recipe
  6. Go to "Settings" -> "Import recipies" and import the file that was exported in step 4
  7. Go to "Home" and view the recipe => the non-English characters are not the same

I created a test recipe to demonstrate the issue:

Initial test recipe (image)

reciper-initial-encoding-test

When recipies were exported (e.g. to Google Drive), deleted from Reciper and imported back, this was the result:

Imported broken recipe (image)

reciper-broken-import-encoding

I viewed the exported json file and its contents were fine:

[["Recipes"],[[{"id":27,"steps":"-","title":"Encoding test","servings":"1","ingredients":"Numbers: 123\nLower case characters: abc\nUpper case characters: ABC\n\nLower case DK/FI/NO/SE characters: åäöæø\nUpper case DK/FI/NO/SE letters: ÅÄÖÆØ\n\nLower case IS characters: áðéíóúýþ\nUpper case IS characters: ÁÐÉÍÓÚÝÞ\n\nLower case DE characters: ßü\nUpper case DE characters: ẞÜ\n\nLower case FR characters: àâçèêëîïôœùûÿ\nUpper case FR characters: ÀÂÇÈÊËÎÏÔŒÙÛŸ\n\nRandom emojis: 😊🤣😎🍕🍔🥗🍝\n\nRandom kaomojis: (●'◡'●) ᓚᘏᗢ (╯°□°)╯︵ ┻━┻","source":""}]]]

Here's the text for the recipe that I used to initially create the recipe in the UI (can be just copy-pasted into the Ingredients field):

Data for creating the test recipe
Numbers: 123
Lower case characters: abc
Upper case characters: ABC

Lower case DK/FI/NO/SE characters: åäöæø
Upper case DK/FI/NO/SE letters: ÅÄÖÆØ

Lower case IS characters: áðéíóúýþ
Upper case IS characters: ÁÐÉÍÓÚÝÞ

Lower case DE characters: ßü
Upper case DE characters: ẞÜ

Lower case FR characters: àâçèêëîïôœùûÿ
Upper case FR characters: ÀÂÇÈÊËÎÏÔŒÙÛŸ

Random emojis:
😊🤣😎🍕🍔🥗🍝

Random kaomojis:
(●'◡'●)
ᓚᘏᗢ
(╯°□°)╯︵ ┻━┻

Feel free to ask questions or request changes!

RowEchelonForm avatar May 29 '24 20:05 RowEchelonForm