@@ -2,8 +2,10 @@ package com.simplemobiletools.notes.pro.helpers
22
33import android.content.Context
44import com.google.gson.Gson
5+ import com.google.gson.JsonSyntaxException
56import com.google.gson.reflect.TypeToken
67import com.simplemobiletools.commons.extensions.showErrorToast
8+ import com.simplemobiletools.commons.helpers.PROTECTION_NONE
79import com.simplemobiletools.commons.helpers.ensureBackgroundThread
810import com.simplemobiletools.notes.pro.extensions.notesDB
911import com.simplemobiletools.notes.pro.models.Note
@@ -18,7 +20,7 @@ class NotesImporter(private val context: Context) {
1820 private var notesImported = 0
1921 private var notesFailed = 0
2022
21- fun importNotes (path : String , callback : (result: ImportResult ) -> Unit ) {
23+ fun importNotes (path : String , filename : String , callback : (result: ImportResult ) -> Unit ) {
2224 ensureBackgroundThread {
2325 try {
2426 val inputStream = if (path.contains(" /" )) {
@@ -45,6 +47,34 @@ class NotesImporter(private val context: Context) {
4547 }
4648 }
4749 }
50+ } catch (e: JsonSyntaxException ) {
51+ // 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
52+ val inputStream = if (path.contains(" /" )) {
53+ File (path).inputStream()
54+ } else {
55+ context.assets.open(path)
56+ }
57+
58+ inputStream.bufferedReader().use { reader ->
59+ val text = reader.readText()
60+ val note = Note (null , filename, text, NoteType .TYPE_TEXT .value, " " , PROTECTION_NONE , " " )
61+ var i = 1
62+ if (context.notesDB.getNoteIdWithTitle(note.title) != null ) {
63+ while (true ) {
64+ val tryTitle = " $filename ($i )"
65+ if (context.notesDB.getNoteIdWithTitle(tryTitle) == null ) {
66+ break
67+ }
68+ i++
69+ }
70+
71+ note.title = " $filename ($i )"
72+ }
73+
74+ context.notesDB.insertOrUpdate(note)
75+ notesImported++
76+ }
77+
4878 } catch (e: Exception ) {
4979 context.showErrorToast(e)
5080 notesFailed++
0 commit comments