mirror of
https://github.com/SimpleMobileTools/Simple-Gallery.git
synced 2025-06-05 21:59:19 +02:00
move directory rename dialog in a separate file
This commit is contained in:
@ -129,7 +129,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||
if (dir.exists() && dir.isDirectory) {
|
||||
val res = dir.list { file, filename -> filename == ".nomedia" }
|
||||
|
||||
if (res.size > 0)
|
||||
if (res != null && res.size > 0)
|
||||
ignoreDirs.add(d)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,78 @@
|
||||
package com.simplemobiletools.gallery.dialogs
|
||||
|
||||
import android.app.Activity
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.view.LayoutInflater
|
||||
import android.view.WindowManager
|
||||
import com.simplemobiletools.gallery.R
|
||||
import com.simplemobiletools.gallery.Utils
|
||||
import com.simplemobiletools.gallery.extensions.toast
|
||||
import com.simplemobiletools.gallery.extensions.value
|
||||
import kotlinx.android.synthetic.main.rename_directory.view.*
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
|
||||
class RenameDirectoryDialog(val activity: Activity, val dir: File, val listener: OnRenameDirListener) {
|
||||
val context = activity
|
||||
|
||||
init {
|
||||
val view = LayoutInflater.from(context).inflate(R.layout.rename_directory, null)
|
||||
|
||||
view.directory_name.setText(dir.name)
|
||||
view.directory_path.text = "${dir.parent}/"
|
||||
|
||||
AlertDialog.Builder(context)
|
||||
.setTitle(context.resources.getString(R.string.rename_folder))
|
||||
.setView(view)
|
||||
.setPositiveButton(R.string.ok, null)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.create().apply {
|
||||
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||
show()
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
|
||||
val newDirName = view.directory_name.value
|
||||
|
||||
if (newDirName.isEmpty()) {
|
||||
context.toast(R.string.rename_folder_empty)
|
||||
return@setOnClickListener
|
||||
}
|
||||
|
||||
val updatedFiles = ArrayList<String>()
|
||||
updatedFiles.add(dir.absolutePath)
|
||||
val newDir = File(dir.parent, newDirName)
|
||||
|
||||
if (Utils.needsStupidWritePermissions(context, dir.absolutePath)) {
|
||||
if (Utils.isShowingWritePermissions(activity, dir))
|
||||
return@setOnClickListener
|
||||
|
||||
val document = Utils.Companion.getFileDocument(context, dir.absolutePath)
|
||||
if (document.canWrite())
|
||||
document.renameTo(newDirName)
|
||||
sendSuccess(updatedFiles, newDir)
|
||||
dismiss()
|
||||
} else if (dir.renameTo(newDir)) {
|
||||
sendSuccess(updatedFiles, newDir)
|
||||
dismiss()
|
||||
} else {
|
||||
context.toast(R.string.rename_folder_error)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private fun sendSuccess(updatedFiles: ArrayList<String>, newDir: File) {
|
||||
context.toast(R.string.renaming_folder)
|
||||
val files = newDir.listFiles()
|
||||
for (file in files) {
|
||||
updatedFiles.add(file.absolutePath)
|
||||
}
|
||||
|
||||
updatedFiles.add(newDir.absolutePath)
|
||||
val changedFiles = updatedFiles.toTypedArray()
|
||||
listener.onRenameDirSuccess(changedFiles)
|
||||
}
|
||||
|
||||
interface OnRenameDirListener {
|
||||
fun onRenameDirSuccess(changedFiles: Array<String>)
|
||||
}
|
||||
}
|
@ -12,7 +12,7 @@ import com.simplemobiletools.gallery.extensions.value
|
||||
import kotlinx.android.synthetic.main.rename_file.view.*
|
||||
import java.io.File
|
||||
|
||||
class RenameFileDialog(val activity: Activity, val file: File, val listener: OnRenameItemListener) {
|
||||
class RenameFileDialog(val activity: Activity, val file: File, val listener: OnRenameFileListener) {
|
||||
|
||||
init {
|
||||
val context = activity
|
||||
@ -71,10 +71,10 @@ class RenameFileDialog(val activity: Activity, val file: File, val listener: OnR
|
||||
private fun sendSuccess(currFile: File, newFile: File) {
|
||||
val changedFiles = arrayOf(currFile.absolutePath, newFile.absolutePath)
|
||||
MediaScannerConnection.scanFile(activity.applicationContext, changedFiles, null, null)
|
||||
listener.onRenameSuccess(newFile)
|
||||
listener.onRenameFileSuccess(newFile)
|
||||
}
|
||||
|
||||
interface OnRenameItemListener {
|
||||
fun onRenameSuccess(newFile: File)
|
||||
interface OnRenameFileListener {
|
||||
fun onRenameFileSuccess(newFile: File)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user