mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-03-19 10:00:16 +01:00
Added importNotes fun in NotesHelper
This commit is contained in:
parent
a305a2b485
commit
323ca2c56e
@ -1,9 +1,9 @@
|
|||||||
package com.simplemobiletools.notes.pro.helpers
|
package com.simplemobiletools.notes.pro.helpers
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
||||||
import com.simplemobiletools.commons.helpers.PROTECTION_NONE
|
import com.simplemobiletools.commons.helpers.PROTECTION_NONE
|
||||||
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||||
import com.simplemobiletools.notes.pro.R
|
import com.simplemobiletools.notes.pro.R
|
||||||
@ -73,4 +73,58 @@ class NotesHelper(val context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun insertOrUpdateNotes(notes: List<Note>, callback: ((newNoteIds: List<Long>) -> Unit)? = null) {
|
||||||
|
ensureBackgroundThread {
|
||||||
|
val noteIds = context.notesDB.insertOrUpdate(notes)
|
||||||
|
Handler(Looper.getMainLooper()).post {
|
||||||
|
callback?.invoke(noteIds)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun importNotes(activity: BaseSimpleActivity, notes: List<Note>, callback: (ImportResult) -> Unit) {
|
||||||
|
ensureBackgroundThread {
|
||||||
|
val currentNotes = activity.notesDB.getNotes()
|
||||||
|
if (currentNotes.isEmpty()) {
|
||||||
|
insertOrUpdateNotes(notes) { savedNotes ->
|
||||||
|
|
||||||
|
val newCurrentNotes = activity.notesDB.getNotes()
|
||||||
|
|
||||||
|
val result = when {
|
||||||
|
currentNotes.size == newCurrentNotes.size -> ImportResult.IMPORT_NOTHING_NEW
|
||||||
|
notes.size == savedNotes.size -> ImportResult.IMPORT_OK
|
||||||
|
savedNotes.isEmpty() -> ImportResult.IMPORT_FAIL
|
||||||
|
else -> ImportResult.IMPORT_PARTIAL
|
||||||
|
}
|
||||||
|
callback(result)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var imported = 0
|
||||||
|
var skipped = 0
|
||||||
|
|
||||||
|
notes.forEach { note ->
|
||||||
|
val exists = context.notesDB.getNoteIdWithTitle(note.title) != null
|
||||||
|
if (!exists) {
|
||||||
|
context.notesDB.insertOrUpdate(note)
|
||||||
|
imported++
|
||||||
|
} else {
|
||||||
|
skipped++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val result = when {
|
||||||
|
skipped == notes.size -> ImportResult.IMPORT_NOTHING_NEW
|
||||||
|
imported == notes.size -> ImportResult.IMPORT_OK
|
||||||
|
imported == 0 -> ImportResult.IMPORT_FAIL
|
||||||
|
else -> ImportResult.IMPORT_PARTIAL
|
||||||
|
}
|
||||||
|
callback(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enum class ImportResult {
|
||||||
|
IMPORT_FAIL, IMPORT_OK, IMPORT_PARTIAL, IMPORT_NOTHING_NEW
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user