add some renaming related stuff

This commit is contained in:
tibbi 2017-03-08 23:21:40 +01:00
parent 59e29c2452
commit 5ed43bb870
6 changed files with 47 additions and 7 deletions

View File

@ -32,7 +32,7 @@ android {
}
dependencies {
compile 'com.simplemobiletools:commons:2.9.3'
compile 'com.simplemobiletools:commons:2.9.4'
compile 'com.facebook.stetho:stetho:1.4.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

View File

@ -102,6 +102,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
saveCurrentNote()
when (item.itemId) {
R.id.open_note -> displayOpenNoteDialog()
R.id.new_note -> displayNewNoteDialog()
@ -231,6 +232,8 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private fun getCurrentNoteText() = (view_pager.adapter as NotesPagerAdapter).getCurrentNoteViewText(view_pager.currentItem)
private fun saveCurrentNote() = (view_pager.adapter as NotesPagerAdapter).saveCurrentNote(view_pager.currentItem)
private fun displayDeleteNotePrompt() {
val message = String.format(getString(R.string.delete_note_prompt_message), mCurrentNote.title)
ConfirmationDialog(this, message) {

View File

@ -32,5 +32,7 @@ class NotesPagerAdapter(fm: FragmentManager, private val notes: List<Note>) : Fr
fun getCurrentNoteViewText(position: Int) = fragments[position].getCurrentNoteViewText()
fun saveCurrentNote(position: Int) = fragments[position].saveText()
fun showKeyboard(position: Int) = fragments[position]?.showKeyboard()
}

View File

@ -1,18 +1,19 @@
package com.simplemobiletools.notes.dialogs
import android.app.Activity
import android.content.DialogInterface.BUTTON_POSITIVE
import android.provider.DocumentsContract
import android.support.v7.app.AlertDialog
import android.view.WindowManager
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.activities.SimpleActivity
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.helpers.DBHelper
import com.simplemobiletools.notes.models.Note
import kotlinx.android.synthetic.main.new_note.view.*
import java.io.File
class RenameNoteDialog(val activity: Activity, val db: DBHelper, val note: Note, callback: (note: Note) -> Unit) {
class RenameNoteDialog(val activity: SimpleActivity, val db: DBHelper, val note: Note, callback: (note: Note) -> Unit) {
init {
val view = activity.layoutInflater.inflate(R.layout.rename_note, null)
@ -32,6 +33,37 @@ class RenameNoteDialog(val activity: Activity, val db: DBHelper, val note: Note,
activity.toast(R.string.title_taken)
} else {
note.title = title
val path = note.path
if (path.isNotEmpty()) {
if (title.isEmpty()) {
context.toast(R.string.filename_cannot_be_empty)
return@setOnClickListener
}
val file = File(path)
val newFile = File(file.parent, title)
if (!newFile.name.isAValidFilename()) {
context.toast(R.string.invalid_name)
return@setOnClickListener
}
if (context.needsStupidWritePermissions(newFile.absolutePath)) {
if (activity.isShowingPermDialog(file))
return@setOnClickListener
var document = context.getFastDocument(file)
if (document?.isFile == false) {
document = context.getFileDocument(file.absolutePath, context.config.treeUri)
}
DocumentsContract.renameDocument(context.contentResolver, document!!.uri, newFile.name)
} else if (!file.renameTo(newFile)) {
activity.toast(R.string.rename_file_error)
return@setOnClickListener
}
note.path = newFile.absolutePath
db.updateNotePath(note)
}
db.updateNoteTitle(note)
dismiss()
callback.invoke(note)

View File

@ -59,6 +59,9 @@ class NoteFragment : Fragment() {
}
fun saveText() {
if (note.path.isNotEmpty() && !File(note.path).exists())
return
val newText = getCurrentNoteViewText()
val oldText = getNoteStoredValue()
if (newText != oldText) {

View File

@ -1,3 +1,3 @@
package com.simplemobiletools.notes.models
data class Note(var id: Int, var title: String, var value: String, val type: Int, val path: String = "")
data class Note(var id: Int, var title: String, var value: String, val type: Int, var path: String = "")