mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-06-05 17:00:23 +02:00
Add Import Folder option
This commit is contained in:
@ -14,10 +14,7 @@ import android.view.MenuItem
|
||||
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
||||
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.commons.helpers.LICENSE_LEAK_CANARY
|
||||
import com.simplemobiletools.commons.helpers.LICENSE_RTL
|
||||
import com.simplemobiletools.commons.helpers.LICENSE_STETHO
|
||||
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
|
||||
import com.simplemobiletools.commons.helpers.*
|
||||
import com.simplemobiletools.commons.models.FAQItem
|
||||
import com.simplemobiletools.commons.models.FileDirItem
|
||||
import com.simplemobiletools.commons.models.RadioItem
|
||||
@ -138,6 +135,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
||||
R.id.rename_note -> displayRenameDialog()
|
||||
R.id.share -> shareText()
|
||||
R.id.open_file -> tryOpenFile()
|
||||
R.id.import_folder -> tryOpenFolder()
|
||||
R.id.export_as_file -> tryExportAsFile()
|
||||
R.id.export_all_notes -> tryExportAllNotes()
|
||||
R.id.delete_note -> displayDeleteNotePrompt()
|
||||
@ -309,6 +307,13 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
||||
}
|
||||
}
|
||||
|
||||
private fun openFolder(path: String, onChecksPassed: (file: File) -> Unit) {
|
||||
val file = File(path)
|
||||
if (file.isDirectory) {
|
||||
onChecksPassed(file)
|
||||
}
|
||||
}
|
||||
|
||||
private fun importFileWithSync(uri: Uri) {
|
||||
when (uri.scheme) {
|
||||
"file" -> openPath(uri.path)
|
||||
@ -334,6 +339,31 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
|
||||
}
|
||||
}
|
||||
|
||||
private fun tryOpenFolder() {
|
||||
handlePermission(PERMISSION_READ_STORAGE) {
|
||||
if (it) {
|
||||
openFolder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun openFolder() {
|
||||
FilePickerDialog(this, pickFile = false) {
|
||||
openFolder(it) {
|
||||
ImportFolderDialog(this, it.path) {
|
||||
mNotes = dbHelper.getNotes()
|
||||
showSaveButton = false
|
||||
invalidateOptionsMenu()
|
||||
initViewPager()
|
||||
updateSelectedNote(it)
|
||||
view_pager.onGlobalLayout {
|
||||
mAdapter?.focusEditText(getNoteIndexWithId(it))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun tryExportAsFile() {
|
||||
handlePermission(PERMISSION_WRITE_STORAGE) {
|
||||
if (it) {
|
||||
|
@ -0,0 +1,72 @@
|
||||
package com.simplemobiletools.notes.dialogs
|
||||
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.view.ViewGroup
|
||||
import com.simplemobiletools.commons.extensions.*
|
||||
import com.simplemobiletools.notes.R
|
||||
import com.simplemobiletools.notes.activities.SimpleActivity
|
||||
import com.simplemobiletools.notes.extensions.dbHelper
|
||||
import com.simplemobiletools.notes.helpers.TYPE_NOTE
|
||||
import com.simplemobiletools.notes.models.Note
|
||||
import kotlinx.android.synthetic.main.dialog_open_file.view.*
|
||||
import java.io.File
|
||||
|
||||
class ImportFolderDialog(val activity: SimpleActivity, val path: String, val callback: (id: Int) -> Unit) : AlertDialog.Builder(activity) {
|
||||
private var dialog: AlertDialog
|
||||
|
||||
init {
|
||||
val view = (activity.layoutInflater.inflate(R.layout.dialog_import_folder, null) as ViewGroup).apply {
|
||||
open_file_filename.text = activity.humanizePath(path)
|
||||
}
|
||||
|
||||
dialog = AlertDialog.Builder(activity)
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.create().apply {
|
||||
activity.setupDialogStuff(view, this, R.string.import_folder) {
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||
val updateFilesOnEdit = view.open_file_type.checkedRadioButtonId == R.id.open_file_update_file
|
||||
saveFolder(updateFilesOnEdit)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun saveFolder(updateFilesOnEdit: Boolean) {
|
||||
val folder = File(path)
|
||||
var lastSavedNoteId = -1;
|
||||
folder.listFiles({ file ->
|
||||
val filename = file.path.getFilenameFromPath()
|
||||
when {
|
||||
filename.isImageVideoGif() -> false
|
||||
file.length() > 10 * 1000 * 1000 -> false
|
||||
activity.dbHelper.doesTitleExist(filename) -> false
|
||||
else -> true
|
||||
}
|
||||
}).forEach {
|
||||
val storePath = if (updateFilesOnEdit) it.path else ""
|
||||
val storeContent = if (updateFilesOnEdit) "" else it.readText()
|
||||
|
||||
if (updateFilesOnEdit) {
|
||||
activity.handleSAFDialog(path) {
|
||||
lastSavedNoteId = saveNote(storeContent, storePath)
|
||||
}
|
||||
} else {
|
||||
lastSavedNoteId = saveNote(storeContent, storePath)
|
||||
}
|
||||
}
|
||||
|
||||
if (lastSavedNoteId != -1) {
|
||||
callback(lastSavedNoteId)
|
||||
}
|
||||
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
private fun saveNote(storeContent: String, storePath: String): Int {
|
||||
val filename = storePath.getFilenameFromPath()
|
||||
val note = Note(0, filename, storeContent, TYPE_NOTE, storePath)
|
||||
val id = activity.dbHelper.insertNote(note)
|
||||
return id
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user