add a new dialog for importing files

This commit is contained in:
tibbi
2017-03-08 20:43:18 +01:00
parent 86fb6bc7dd
commit 4b0f90a20a
3 changed files with 94 additions and 15 deletions

View File

@ -21,10 +21,7 @@ import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.notes.BuildConfig
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.adapters.NotesPagerAdapter
import com.simplemobiletools.notes.dialogs.NewNoteDialog
import com.simplemobiletools.notes.dialogs.OpenNoteDialog
import com.simplemobiletools.notes.dialogs.RenameNoteDialog
import com.simplemobiletools.notes.dialogs.SaveAsDialog
import com.simplemobiletools.notes.dialogs.*
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.extensions.getTextSize
import com.simplemobiletools.notes.helpers.DBHelper
@ -167,19 +164,13 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
val file = File(it)
if (file.isImageVideoGif()) {
toast(R.string.invalid_file_format)
return@FilePickerDialog
}
if (file.length() > 10 * 1000 * 1000) {
} else if (file.length() > 10 * 1000 * 1000) {
toast(R.string.file_too_large)
} else if (mDb.doesTitleExist(it.getFilenameFromPath())) {
toast(R.string.title_taken)
} else {
val filename = it.getFilenameFromPath()
if (mDb.doesTitleExist(filename)) {
toast(R.string.title_taken)
} else {
val content = file.readText()
val note = Note(0, filename, content, TYPE_NOTE, it)
addNewNote(note)
OpenFileDialog(this, it) {
addNewNote(it)
}
}
}

View File

@ -0,0 +1,37 @@
package com.simplemobiletools.notes.dialogs
import android.app.Activity
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.ViewGroup
import com.simplemobiletools.commons.extensions.getFilenameFromPath
import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.notes.R
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 OpenFileDialog(val activity: Activity, val path: String, val callback: (note: Note) -> Unit) : AlertDialog.Builder(activity) {
init {
val view = (LayoutInflater.from(activity).inflate(R.layout.dialog_open_file, null) as ViewGroup).apply {
open_file_filename.text = activity.humanizePath(path)
}
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this, R.string.open_file)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val file = File(path)
val filename = path.getFilenameFromPath()
val content = file.readText()
val note = Note(0, filename, content, TYPE_NOTE, path)
callback.invoke(note)
dismiss()
})
}
}
}