mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-03-20 02:20:06 +01:00
Got rid of NoteExporter and NoteImporter
This commit is contained in:
parent
9c2d0e5954
commit
7a15eef790
@ -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.*
|
||||
|
||||
|
@ -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<Note>, unlockedNoteIds: List<Long>, 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, "")
|
||||
}
|
||||
}
|
@ -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<List<Note>>() {}.type
|
||||
val notes = gson.fromJson<List<Note>>(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
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user