-
Notifications
You must be signed in to change notification settings - Fork 298
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
Add import data #1333
Add import data #1333
Changes from all commits
61f44af
f0ccfa1
21c6bdf
c5e2bdd
4857d46
cdab029
5f8a42e
cb158b0
ade7ae0
0ea324a
7d93384
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.automattic.simplenote; | ||
|
||
import android.net.Uri; | ||
|
||
import androidx.fragment.app.Fragment; | ||
|
||
import com.automattic.simplenote.models.Note; | ||
import com.automattic.simplenote.models.Tag; | ||
import com.automattic.simplenote.utils.FileUtils; | ||
import com.automattic.simplenote.utils.TagUtils; | ||
import com.simperium.client.Bucket; | ||
import com.simperium.client.BucketObjectNameInvalid; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
import java.text.ParseException; | ||
import java.util.ArrayList; | ||
|
||
public class Importer { | ||
private Bucket<Note> mNotesBucket; | ||
private Bucket<Tag> mTagsBucket; | ||
|
||
public Importer(Simplenote simplenote) { | ||
mNotesBucket = simplenote.getNotesBucket(); | ||
mTagsBucket = simplenote.getTagsBucket(); | ||
} | ||
|
||
public static void fromUri(Fragment fragment, Uri uri) throws ImportException { | ||
try { | ||
new Importer((Simplenote) fragment.getActivity().getApplication()) | ||
.dispatchFileImport( | ||
FileUtils.getFileExtension(fragment.requireContext(), uri), | ||
FileUtils.readFile(fragment.requireContext(), uri) | ||
); | ||
} catch (IOException e) { | ||
throw new ImportException(FailureReason.FileError); | ||
} | ||
} | ||
|
||
private void dispatchFileImport(String fileType, String content) throws ImportException { | ||
switch (fileType) { | ||
case "json": | ||
importJsonFile(content); | ||
break; | ||
case "md": | ||
importMarkdown(content); | ||
break; | ||
case "txt": | ||
importPlaintext(content); | ||
break; | ||
default: | ||
throw new ImportException(FailureReason.UnknownExportType); | ||
} | ||
} | ||
|
||
private void importPlaintext(String content) { | ||
addNote(Note.fromContent(mNotesBucket, content)); | ||
} | ||
|
||
private void importMarkdown(String content) { | ||
Note note = Note.fromContent(mNotesBucket, content); | ||
note.enableMarkdown(); | ||
|
||
addNote(note); | ||
} | ||
|
||
private void addNote(Note note) { | ||
for (String tagName : note.getTags()) { | ||
try { | ||
TagUtils.createTagIfMissing(mTagsBucket, tagName); | ||
} catch (BucketObjectNameInvalid e) { | ||
// if it can't be added then remove it, we can't keep it anyway | ||
note.removeTag(tagName); | ||
} | ||
} | ||
|
||
note.save(); | ||
} | ||
|
||
private void importJsonFile(String content) throws ImportException { | ||
try { | ||
importJsonExport(new JSONObject(content)); | ||
} catch (JSONException | ParseException e) { | ||
throw new ImportException(FailureReason.ParseError); | ||
} | ||
} | ||
|
||
private void importJsonExport(JSONObject export) throws JSONException, ParseException { | ||
JSONArray activeNotes = export.optJSONArray("activeNotes"); | ||
JSONArray trashedNotes = export.optJSONArray("trashedNotes"); | ||
|
||
ArrayList<Note> notesList = new ArrayList<>(); | ||
|
||
for (int i = 0; activeNotes != null && i < activeNotes.length(); i++) { | ||
Note note = Note.fromExportedJson(mNotesBucket, activeNotes.getJSONObject(i)); | ||
notesList.add(note); | ||
} | ||
|
||
for (int j = 0; trashedNotes != null && j < trashedNotes.length(); j++) { | ||
Note note = Note.fromExportedJson(mNotesBucket, trashedNotes.getJSONObject(j)); | ||
note.setDeleted(true); | ||
|
||
notesList.add(note); | ||
} | ||
|
||
for (Note note : notesList) { | ||
addNote(note); | ||
} | ||
} | ||
|
||
public enum FailureReason { | ||
FileError, | ||
UnknownExportType, | ||
ParseError | ||
} | ||
|
||
public static class ImportException extends Exception { | ||
private FailureReason mReason; | ||
|
||
ImportException(FailureReason reason) { | ||
mReason = reason; | ||
} | ||
|
||
public FailureReason getReason() { | ||
return mReason; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import android.text.format.DateFormat; | ||
import android.text.format.DateUtils; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Locale; | ||
|
@@ -37,4 +38,12 @@ public static String getDateTextString(Context context, Calendar calendar) { | |
); | ||
return new SimpleDateFormat(pattern, Locale.getDefault()).format(calendar.getTime()); | ||
} | ||
|
||
public static Calendar getDateCalendar(String json) throws ParseException { | ||
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this looks like an ISO8601 string. I see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll look for solutions! |
||
SimpleDateFormat dateFormat = new SimpleDateFormat(pattern, Locale.getDefault()); | ||
Calendar date = Calendar.getInstance(); | ||
date.setTime(dateFormat.parse(json)); | ||
return date; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these two might be great in a
FileUtils
class in theutils
folder.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes! Wanted to do it but was afraid to create new files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all good. I know that fear, especially entering an open-source repo for new contributions.
we'll let you know if we want something to change, or we'll just end up changing it on merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thank you! 😀