diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt index a00fc15b..3670fa2a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt @@ -43,12 +43,10 @@ import com.simplemobiletools.notes.pro.dialogs.* import com.simplemobiletools.notes.pro.extensions.* import com.simplemobiletools.notes.pro.fragments.TextFragment import com.simplemobiletools.notes.pro.helpers.* -import com.simplemobiletools.notes.pro.helpers.NotesImporter.ImportResult import com.simplemobiletools.notes.pro.models.Note import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.item_checklist.* import java.io.File -import java.io.FileOutputStream import java.nio.charset.Charset import java.util.* diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesExporter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesExporter.kt deleted file mode 100644 index bc9ac4bf..00000000 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesExporter.kt +++ /dev/null @@ -1,48 +0,0 @@ -package com.simplemobiletools.notes.pro.helpers - -import android.content.Context -import com.google.gson.Gson -import com.google.gson.stream.JsonWriter -import com.simplemobiletools.commons.helpers.PROTECTION_NONE -import com.simplemobiletools.commons.helpers.ensureBackgroundThread -import com.simplemobiletools.notes.pro.models.Note -import java.io.OutputStream - -class NotesExporter(private val context: Context) { - enum class ExportResult { - EXPORT_FAIL, EXPORT_OK - } - - private val gson = Gson() - - fun exportNotes(notes: List, unlockedNoteIds: List, outputStream: OutputStream?, callback: (result: ExportResult) -> Unit) { - ensureBackgroundThread { - if (outputStream == null) { - callback.invoke(ExportResult.EXPORT_FAIL) - return@ensureBackgroundThread - } - val writer = JsonWriter(outputStream.bufferedWriter()) - writer.use { - try { - var written = 0 - writer.beginArray() - for (note in notes) { - if (!note.isLocked() || note.id in unlockedNoteIds) { - val noteToSave = getNoteToExport(note) - writer.jsonValue(gson.toJson(noteToSave)) - written++ - } - } - writer.endArray() - callback.invoke(ExportResult.EXPORT_OK) - } catch (e: Exception) { - callback.invoke(ExportResult.EXPORT_FAIL) - } - } - } - } - - private fun getNoteToExport(note: Note): Note { - return Note(null, note.title, note.getNoteStoredValue(context) ?: "", note.type, "", PROTECTION_NONE, "") - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesImporter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesImporter.kt deleted file mode 100644 index 8ec4fca0..00000000 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesImporter.kt +++ /dev/null @@ -1,101 +0,0 @@ -package com.simplemobiletools.notes.pro.helpers - -import android.content.Context -import com.google.gson.Gson -import com.google.gson.JsonSyntaxException -import com.google.gson.reflect.TypeToken -import com.simplemobiletools.commons.extensions.showErrorToast -import com.simplemobiletools.commons.helpers.PROTECTION_NONE -import com.simplemobiletools.commons.helpers.ensureBackgroundThread -import com.simplemobiletools.notes.pro.extensions.notesDB -import com.simplemobiletools.notes.pro.models.Note -import java.io.File - -class NotesImporter(private val context: Context) { - enum class ImportResult { - IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW - } - - private val gson = Gson() - private var notesImported = 0 - private var notesFailed = 0 - private var notesSkipped = 0 - - fun importNotes(path: String, filename: String, force: Boolean = false, callback: (result: ImportResult) -> Unit) { - ensureBackgroundThread { - try { - val inputStream = if (path.contains("/")) { - File(path).inputStream() - } else { - context.assets.open(path) - } - - inputStream.bufferedReader().use { reader -> - val json = reader.readText() - val type = object : TypeToken>() {}.type - val notes = gson.fromJson>(json, type) - val totalNotes = notes?.size ?: 0 - if (totalNotes <= 0) { - callback.invoke(ImportResult.IMPORT_FAIL) - return@ensureBackgroundThread - } - - for (note in notes) { - val exists = context.notesDB.getNoteIdWithTitle(note.title) != null - if (!exists) { - context.notesDB.insertOrUpdate(note) - notesImported++ - } else { - notesSkipped++ - } - } - } - } catch (e: JsonSyntaxException) { - if (force) { - callback(ImportResult.IMPORT_FAIL) - return@ensureBackgroundThread - } - - // Import notes expects a json with note name, content etc, but lets be more flexible and accept the basic files with note content only too - val inputStream = if (path.contains("/")) { - File(path).inputStream() - } else { - context.assets.open(path) - } - - inputStream.bufferedReader().use { reader -> - val text = reader.readText() - val note = Note(null, filename, text, NoteType.TYPE_TEXT.value, "", PROTECTION_NONE, "") - var i = 1 - if (context.notesDB.getNoteIdWithTitle(note.title) != null) { - while (true) { - val tryTitle = "$filename ($i)" - if (context.notesDB.getNoteIdWithTitle(tryTitle) == null) { - break - } - i++ - } - - note.title = "$filename ($i)" - } - - context.notesDB.insertOrUpdate(note) - notesImported++ - } - - } catch (e: Exception) { - context.showErrorToast(e) - notesFailed++ - } - - callback.invoke( - when { - notesSkipped > 0 && notesImported == 0 -> ImportResult.IMPORT_NOTHING_NEW - notesImported == 0 -> ImportResult.IMPORT_FAIL - notesFailed > 0 -> ImportResult.IMPORT_PARTIAL - else -> ImportResult.IMPORT_OK - } - ) - } - } -}