1+ package com.simplemobiletools.notes.dialogs
2+
3+ import android.support.v7.app.AlertDialog
4+ import android.view.ViewGroup
5+ import com.simplemobiletools.commons.extensions.*
6+ import com.simplemobiletools.notes.R
7+ import com.simplemobiletools.notes.activities.SimpleActivity
8+ import com.simplemobiletools.notes.extensions.dbHelper
9+ import com.simplemobiletools.notes.helpers.TYPE_NOTE
10+ import com.simplemobiletools.notes.models.Note
11+ import kotlinx.android.synthetic.main.dialog_import_folder.view.*
12+ import java.io.File
13+
14+ class ImportFolderDialog (val activity : SimpleActivity , val path : String , val callback : (id: Int ) -> Unit ) : AlertDialog.Builder(activity) {
15+ private var dialog: AlertDialog
16+
17+ init {
18+ val view = (activity.layoutInflater.inflate(R .layout.dialog_import_folder, null ) as ViewGroup ).apply {
19+ open_file_filename.text = activity.humanizePath(path)
20+ }
21+
22+ dialog = AlertDialog .Builder (activity)
23+ .setPositiveButton(R .string.ok, null )
24+ .setNegativeButton(R .string.cancel, null )
25+ .create().apply {
26+ activity.setupDialogStuff(view, this , R .string.import_folder) {
27+ getButton(AlertDialog .BUTTON_POSITIVE ).setOnClickListener {
28+ val updateFilesOnEdit = view.open_file_type.checkedRadioButtonId == R .id.open_file_update_file
29+ saveFolder(updateFilesOnEdit)
30+ }
31+ }
32+ }
33+ }
34+
35+ private fun saveFolder (updateFilesOnEdit : Boolean ) {
36+ val folder = File (path)
37+ var lastSavedNoteId = - 1 ;
38+ folder.listFiles({ file ->
39+ val filename = file.path.getFilenameFromPath()
40+ when {
41+ filename.isImageVideoGif() -> false
42+ file.length() > 10 * 1000 * 1000 -> false
43+ activity.dbHelper.doesTitleExist(filename) -> false
44+ else -> true
45+ }
46+ }).forEach {
47+ val storePath = if (updateFilesOnEdit) it.path else " "
48+ val storeContent = if (updateFilesOnEdit) " " else it.readText()
49+
50+ if (updateFilesOnEdit) {
51+ activity.handleSAFDialog(path) {
52+ lastSavedNoteId = saveNote(storeContent, storePath)
53+ }
54+ } else {
55+ lastSavedNoteId = saveNote(storeContent, storePath)
56+ }
57+ }
58+
59+ if (lastSavedNoteId != - 1 ) {
60+ callback(lastSavedNoteId)
61+ }
62+
63+ dialog.dismiss()
64+ }
65+
66+ private fun saveNote (storeContent : String , storePath : String ): Int {
67+ val filename = storePath.getFilenameFromPath()
68+ val note = Note (0 , filename, storeContent, TYPE_NOTE , storePath)
69+ val id = activity.dbHelper.insertNote(note)
70+ return id
71+ }
72+ }
0 commit comments