Simple-Notes/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt

82 lines
3.4 KiB
Kotlin
Raw Normal View History

2018-11-07 11:48:09 +01:00
package com.simplemobiletools.notes.pro.dialogs
2018-05-08 09:43:37 +02:00
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PROTECTION_NONE
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
2018-11-07 11:48:09 +01:00
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.activities.SimpleActivity
import com.simplemobiletools.notes.pro.databinding.DialogImportFolderBinding
2018-11-07 20:39:17 +01:00
import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.extensions.parseChecklistItems
2018-11-07 19:57:30 +01:00
import com.simplemobiletools.notes.pro.helpers.NotesHelper
2018-11-07 11:48:09 +01:00
import com.simplemobiletools.notes.pro.models.Note
import com.simplemobiletools.notes.pro.models.NoteType
2018-05-08 09:43:37 +02:00
import java.io.File
2018-11-07 19:00:06 +01:00
class ImportFolderDialog(val activity: SimpleActivity, val path: String, val callback: () -> Unit) : AlertDialog.Builder(activity) {
private var dialog: AlertDialog? = null
2018-05-08 09:43:37 +02:00
init {
val binding = DialogImportFolderBinding.inflate(activity.layoutInflater).apply {
openFileFilename.setText(activity.humanizePath(path))
2018-05-08 09:43:37 +02:00
}
activity.getAlertDialogBuilder()
.setPositiveButton(com.simplemobiletools.commons.R.string.ok, null)
.setNegativeButton(com.simplemobiletools.commons.R.string.cancel, null)
.apply {
activity.setupDialogStuff(binding.root, this, R.string.import_folder) { alertDialog ->
dialog = alertDialog
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val updateFilesOnEdit = binding.openFileType.checkedRadioButtonId == R.id.open_file_update_file
ensureBackgroundThread {
saveFolder(updateFilesOnEdit)
2018-05-08 09:43:37 +02:00
}
}
}
}
2018-05-08 09:43:37 +02:00
}
private fun saveFolder(updateFilesOnEdit: Boolean) {
val folder = File(path)
2018-05-16 23:11:25 +02:00
folder.listFiles { file ->
2018-05-08 09:43:37 +02:00
val filename = file.path.getFilenameFromPath()
when {
file.isDirectory -> false
2018-09-11 13:14:44 +02:00
filename.isMediaFile() -> false
file.length() > 1000 * 1000 -> false
2018-11-07 20:39:17 +01:00
activity.notesDB.getNoteIdWithTitle(filename) != null -> false
2018-05-08 09:43:37 +02:00
else -> true
}
}?.forEach {
val storePath = if (updateFilesOnEdit) it.absolutePath else ""
val title = it.absolutePath.getFilenameFromPath()
val value = if (updateFilesOnEdit) "" else it.readText()
val fileText = it.readText().trim()
val checklistItems = fileText.parseChecklistItems()
if (checklistItems != null) {
saveNote(title.substringBeforeLast('.'), fileText, NoteType.TYPE_CHECKLIST, "")
2018-05-08 09:43:37 +02:00
} else {
if (updateFilesOnEdit) {
activity.handleSAFDialog(path) {
saveNote(title, value, NoteType.TYPE_TEXT, storePath)
}
} else {
saveNote(title, value, NoteType.TYPE_TEXT, storePath)
}
2018-05-08 09:43:37 +02:00
}
}
2018-11-07 20:39:17 +01:00
activity.runOnUiThread {
callback()
dialog?.dismiss()
2018-11-07 20:39:17 +01:00
}
2018-05-08 09:43:37 +02:00
}
private fun saveNote(title: String, value: String, type: NoteType, path: String) {
val note = Note(null, title, value, type, path, PROTECTION_NONE, "")
2018-11-07 19:57:30 +01:00
NotesHelper(activity).insertOrUpdateNote(note)
2018-05-08 09:43:37 +02:00
}
2018-05-16 23:11:25 +02:00
}