Allow renaming files by just changing a letter casing

- the RenameItemDialog's positive button is disabled if the name entered is the same as the previous file name.
- when renaming, a temporary file/directory is created and the old file/directory is renamed to the temporary file/directory, thereafter, the temporary file/directory is renamed to the new file/directory.
This commit is contained in:
Paul Akhamiogu 2021-08-20 09:56:40 +01:00
parent af724077af
commit 3e1f7ef4ba
3 changed files with 51 additions and 24 deletions

View file

@ -32,7 +32,12 @@ class RenameItemDialog(val activity: BaseSimpleActivity, val path: String, val c
.create().apply {
activity.setupDialogStuff(view, this, R.string.rename) {
showKeyboard(view.rename_item_name)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val positiveButton = getButton(AlertDialog.BUTTON_POSITIVE)
positiveButton.isEnabled = false
view.rename_item_name.onTextChangeListener { newName->
positiveButton.isEnabled = name != newName
}
positiveButton.setOnClickListener {
if (ignoreClicks) {
return@setOnClickListener
}
@ -62,7 +67,7 @@ class RenameItemDialog(val activity: BaseSimpleActivity, val path: String, val c
}
val newPath = "${path.getParentPath()}/$newName"
if (activity.getDoesFilePathExist(newPath)) {
if(!path.equals(newPath, ignoreCase = true) && activity.getDoesFilePathExist(newPath)){
activity.toast(R.string.name_taken)
return@setOnClickListener
}

View file

@ -6,6 +6,7 @@ import android.content.*
import android.content.Intent.EXTRA_STREAM
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.media.RingtoneManager
import android.net.Uri
import android.os.TransactionTooLargeException
@ -696,29 +697,37 @@ fun BaseSimpleActivity.renameFile(oldPath: String, newPath: String, callback: ((
}
}
}
} else if (File(oldPath).renameTo(File(newPath))) {
if (File(newPath).isDirectory) {
deleteFromMediaStore(oldPath)
rescanPath(newPath) {
runOnUiThread {
callback?.invoke(true)
} else {
val oldFile = File(oldPath)
val newFile = File(newPath)
val tempFile = oldFile.createTempFile()
val oldToTempSucceeds = oldFile.renameTo(tempFile)
val tempToNewSucceeds = tempFile.renameTo(newFile)
if (oldToTempSucceeds && tempToNewSucceeds) {
if (newFile.isDirectory) {
deleteFromMediaStore(oldPath)
rescanPath(newPath) {
runOnUiThread {
callback?.invoke(true)
}
scanPathRecursively(newPath)
}
} else {
if (!baseConfig.keepLastModified) {
newFile.setLastModified(System.currentTimeMillis())
}
deleteFromMediaStore(oldPath)
scanPathsRecursively(arrayListOf(newPath)) {
runOnUiThread {
callback?.invoke(true)
}
}
scanPathRecursively(newPath)
}
} else {
if (!baseConfig.keepLastModified) {
File(newPath).setLastModified(System.currentTimeMillis())
tempFile.delete()
runOnUiThread {
callback?.invoke(false)
}
deleteFromMediaStore(oldPath)
scanPathsRecursively(arrayListOf(newPath)) {
runOnUiThread {
callback?.invoke(true)
}
}
}
} else {
runOnUiThread {
callback?.invoke(false)
}
}
}
@ -928,15 +937,20 @@ fun Activity.setupDialogStuff(view: View, dialog: AlertDialog, titleId: Int = 0,
}
}
val buttonStates = arrayOf(intArrayOf(android.R.attr.state_enabled), intArrayOf(-android.R.attr.state_enabled))
val adjustedPrimaryColor50Alpha = adjustedPrimaryColor.adjustAlpha(0.5f)
val buttonColors = intArrayOf(adjustedPrimaryColor, adjustedPrimaryColor50Alpha)
val primaryButtonColor = ColorStateList(buttonStates, buttonColors)
dialog.apply {
setView(view)
requestWindowFeature(Window.FEATURE_NO_TITLE)
setCustomTitle(title)
setCanceledOnTouchOutside(cancelOnTouchOutside)
show()
getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(adjustedPrimaryColor)
getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(adjustedPrimaryColor)
getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(adjustedPrimaryColor)
getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(primaryButtonColor)
getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(primaryButtonColor)
getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(primaryButtonColor)
val bgDrawable = resources.getColoredDrawableWithColor(R.drawable.dialog_bg, baseConfig.backgroundColor)
window?.setBackgroundDrawable(bgDrawable)

View file

@ -141,3 +141,11 @@ fun File.getDigest(algorithm: String): String {
}
fun File.md5(): String = this.getDigest(MD5)
fun File.createTempFile(): File {
return if (isDirectory) {
createTempDir("temp", "${System.currentTimeMillis()}", parentFile)
} else {
createTempFile("temp", "${System.currentTimeMillis()}", parentFile)
}
}