use the shared rename dialog for renaming files and folders

This commit is contained in:
tibbi 2017-03-14 23:25:22 +01:00
parent 5b26bbb410
commit 3fabc6a93f
9 changed files with 24 additions and 150 deletions

View file

@ -32,7 +32,7 @@ android {
}
dependencies {
compile 'com.simplemobiletools:commons:2.10.12'
compile 'com.simplemobiletools:commons:2.10.14'
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0'
compile 'com.theartofdev.edmodo:android-image-cropper:2.3.1'
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'

View file

@ -18,12 +18,12 @@ import android.view.View
import com.simplemobiletools.commons.asynctasks.CopyMoveTask
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.PropertiesDialog
import com.simplemobiletools.commons.dialogs.RenameItemDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.adapters.MyPagerAdapter
import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask
import com.simplemobiletools.gallery.dialogs.CopyDialog
import com.simplemobiletools.gallery.dialogs.RenameFileDialog
import com.simplemobiletools.gallery.dialogs.SaveAsDialog
import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.fragments.PhotoFragment
@ -336,8 +336,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
}
private fun renameFile() {
RenameFileDialog(this, getCurrentFile()) {
mMedia[mPos].path = it.absolutePath
RenameItemDialog(this, getCurrentFile().absolutePath) {
mMedia[mPos].path = it
updateActionbarTitle()
}
}

View file

@ -15,7 +15,7 @@ import com.bumptech.glide.signature.StringSignature
import com.simplemobiletools.commons.asynctasks.CopyMoveTask
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.PropertiesDialog
import com.simplemobiletools.commons.dialogs.RenameFolderDialog
import com.simplemobiletools.commons.dialogs.RenameItemDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
@ -165,11 +165,10 @@ class DirectoryAdapter(val activity: SimpleActivity, val dirs: MutableList<Direc
return
}
RenameFolderDialog(activity, dir) {
RenameItemDialog(activity, dir.absolutePath) {
activity.runOnUiThread {
actMode?.finish()
listener?.refreshItems()
activity.toast(R.string.rename_folder_ok)
}
}
}

View file

@ -15,11 +15,11 @@ import com.bumptech.glide.signature.StringSignature
import com.simplemobiletools.commons.asynctasks.CopyMoveTask
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.PropertiesDialog
import com.simplemobiletools.commons.dialogs.RenameItemDialog
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.dialogs.CopyDialog
import com.simplemobiletools.gallery.dialogs.RenameFileDialog
import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.photo_video_item.view.*
@ -121,7 +121,7 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
}
private fun renameFile() {
RenameFileDialog(activity, getCurrentFile()) {
RenameItemDialog(activity, getCurrentFile().absolutePath) {
listener?.refreshItems()
activity.runOnUiThread {
actMode?.finish()
@ -237,8 +237,8 @@ class MediaAdapter(val activity: SimpleActivity, var media: MutableList<Medium>,
fun bindView(activity: SimpleActivity, multiSelectorCallback: ModalMultiSelectorCallback, multiSelector: MultiSelector, medium: Medium, pos: Int): View {
itemView.apply {
play_outline.visibility = if (medium.isVideo) View.VISIBLE else View.GONE
file_name.beVisibleIf(displayFilenames)
file_name.text = medium.name
save_as_name.beVisibleIf(displayFilenames)
save_as_name.text = medium.name
toggleItemSelection(this, markedItems.contains(pos), pos)
val path = medium.path

View file

@ -1,88 +0,0 @@
package com.simplemobiletools.gallery.dialogs
import android.provider.DocumentsContract
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.WindowManager
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.extensions.config
import kotlinx.android.synthetic.main.rename_file.view.*
import java.io.File
class RenameFileDialog(val activity: SimpleActivity, val file: File, val callback: (newFile: File) -> Unit) {
init {
val view = LayoutInflater.from(activity).inflate(R.layout.rename_file, null)
val fullName = file.name
val dotAt = fullName.lastIndexOf(".")
var name = fullName
if (dotAt > 0) {
name = fullName.substring(0, dotAt)
val extension = fullName.substring(dotAt + 1)
view.file_extension.setText(extension)
}
view.file_name.setText(name)
view.file_path.text = activity.humanizePath(file.parent) + "/"
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
activity.setupDialogStuff(view, this, R.string.rename_file)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val filename = view.file_name.value
val extension = view.file_extension.value
if (filename.isEmpty()) {
context.toast(R.string.filename_cannot_be_empty)
return@setOnClickListener
}
if (extension.isEmpty()) {
context.toast(R.string.extension_cannot_be_empty)
return@setOnClickListener
}
val newFile = File(file.parent, "$filename.$extension")
if (!newFile.name.isAValidFilename()) {
context.toast(R.string.invalid_name)
return@setOnClickListener
}
if (context.needsStupidWritePermissions(file.absolutePath)) {
activity.handleSAFDialog(file) {
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)
sendSuccess(file, newFile)
dismiss()
}
} else if (file.renameTo(newFile)) {
sendSuccess(file, newFile)
dismiss()
} else {
context.toast(R.string.rename_file_error)
}
})
}
}
private fun sendSuccess(oldFile: File, newFile: File) {
if (activity.updateInMediaStore(oldFile, newFile)) {
callback.invoke(newFile)
} else {
val changedFiles = arrayListOf(oldFile, newFile)
activity.scanFiles(changedFiles) {
callback.invoke(newFile)
}
}
}
}

View file

@ -6,7 +6,7 @@ import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity
import kotlinx.android.synthetic.main.rename_file.view.*
import kotlinx.android.synthetic.main.dialog_save_as.view.*
import java.io.File
class SaveAsDialog(val activity: SimpleActivity, val path: String, val callback: (savePath: String) -> Unit) {
@ -14,7 +14,7 @@ class SaveAsDialog(val activity: SimpleActivity, val path: String, val callback:
init {
var realPath = File(path).parent.trimEnd('/')
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_save_as, null).apply {
file_path.text = activity.humanizePath(realPath)
save_as_path.text = activity.humanizePath(realPath)
val fullName = path.getFilenameFromPath()
val dotAt = fullName.lastIndexOf(".")
@ -23,13 +23,13 @@ class SaveAsDialog(val activity: SimpleActivity, val path: String, val callback:
if (dotAt > 0) {
name = fullName.substring(0, dotAt)
val extension = fullName.substring(dotAt + 1)
file_extension.setText(extension)
save_as_extension.setText(extension)
}
file_name.setText(name)
file_path.setOnClickListener {
save_as_name.setText(name)
save_as_path.setOnClickListener {
FilePickerDialog(activity, realPath, false, false, true) {
file_path.text = activity.humanizePath(it)
save_as_path.text = activity.humanizePath(it)
realPath = it
}
}
@ -41,8 +41,8 @@ class SaveAsDialog(val activity: SimpleActivity, val path: String, val callback:
.create().apply {
activity.setupDialogStuff(view, this, R.string.save_as)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val filename = view.file_name.value
val extension = view.file_extension.value
val filename = view.save_as_name.value
val extension = view.save_as_extension.value
if (filename.isEmpty()) {
context.toast(R.string.filename_cannot_be_empty)

View file

@ -8,14 +8,14 @@
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/file_path_label"
android:id="@+id/save_as_path_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/path"
android:textSize="@dimen/smaller_text_size"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/file_path"
android:id="@+id/save_as_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
@ -24,7 +24,7 @@
android:paddingTop="@dimen/small_margin"/>
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/file_name"
android:id="@+id/save_as_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
@ -32,13 +32,13 @@
android:textCursorDrawable="@null"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/file_extension_label"
android:id="@+id/save_as_extension_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/extension"/>
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/file_extension"
android:id="@+id/save_as_extension"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"

View file

@ -20,7 +20,7 @@
android:visibility="gone"/>
<TextView
android:id="@+id/file_name"
android:id="@+id/save_as_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"

View file

@ -1,37 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rename_file_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/file_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/file_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:singleLine="true"
android:textCursorDrawable="@null"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/file_extension_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/extension"/>
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/file_extension"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:singleLine="true"
android:textCursorDrawable="@null"/>
</LinearLayout>