implementing the simple renaming dialog

This commit is contained in:
tibbi 2019-07-31 22:57:29 +02:00
parent e70331cd97
commit 359a277d23
5 changed files with 92 additions and 21 deletions

View file

@ -1,23 +1,23 @@
package com.simplemobiletools.commons.adapters
import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.interfaces.RenameTab
import java.util.*
class RenameAdapter(val context: Context, val paths: ArrayList<String>) : PagerAdapter() {
class RenameAdapter(val activity: BaseSimpleActivity, val paths: ArrayList<String>) : PagerAdapter() {
private val tabs = SparseArray<RenameTab>()
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val view = LayoutInflater.from(context).inflate(layoutSelection(position), container, false)
val view = LayoutInflater.from(activity).inflate(layoutSelection(position), container, false)
container.addView(view)
tabs.put(position, view as RenameTab)
(view as RenameTab).initTab(paths)
(view as RenameTab).initTab(activity, paths)
return view
}
@ -36,7 +36,7 @@ class RenameAdapter(val context: Context, val paths: ArrayList<String>) : PagerA
else -> throw RuntimeException("Only 2 tabs allowed")
}
fun dialogConfirmed(position: Int) {
tabs[position].dialogConfirmed()
fun dialogConfirmed(position: Int, callback: () -> Unit) {
tabs[position].dialogConfirmed(callback)
}
}

View file

@ -1,10 +1,10 @@
package com.simplemobiletools.commons.dialogs
import android.app.Activity
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.adapters.RenameAdapter
import com.simplemobiletools.commons.extensions.onPageChangeListener
import com.simplemobiletools.commons.extensions.onTabSelectionChanged
@ -15,7 +15,7 @@ import com.simplemobiletools.commons.views.MyViewPager
import kotlinx.android.synthetic.main.dialog_rename.view.*
import java.util.*
class RenameDialog(val activity: Activity, val paths: ArrayList<String>) {
class RenameDialog(val activity: BaseSimpleActivity, val paths: ArrayList<String>) {
var dialog: AlertDialog? = null
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_rename, null)
var tabsAdapter: RenameAdapter
@ -24,7 +24,7 @@ class RenameDialog(val activity: Activity, val paths: ArrayList<String>) {
init {
view.apply {
viewPager = findViewById(R.id.dialog_tab_view_pager)
tabsAdapter = RenameAdapter(context, paths)
tabsAdapter = RenameAdapter(activity, paths)
viewPager.adapter = tabsAdapter
viewPager.onPageChangeListener {
dialog_tab_layout.getTabAt(it)!!.select()
@ -40,18 +40,20 @@ class RenameDialog(val activity: Activity, val paths: ArrayList<String>) {
dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel) { dialog, which -> onCancelFail() }
.setNegativeButton(R.string.cancel) { dialog, which -> dismissDialog() }
.create().apply {
activity.setupDialogStuff(view, this).apply {
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
tabsAdapter.dialogConfirmed(viewPager.currentItem)
})
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
tabsAdapter.dialogConfirmed(viewPager.currentItem) {
dismissDialog()
}
}
}
}
}
private fun onCancelFail() {
private fun dismissDialog() {
dialog!!.dismiss()
}
}

View file

@ -1,7 +1,9 @@
package com.simplemobiletools.commons.interfaces
interface RenameTab {
fun initTab(paths: ArrayList<String>)
import com.simplemobiletools.commons.activities.BaseSimpleActivity
fun dialogConfirmed()
interface RenameTab {
fun initTab(activity: BaseSimpleActivity, paths: ArrayList<String>)
fun dialogConfirmed(callback: () -> Unit)
}

View file

@ -3,15 +3,16 @@ package com.simplemobiletools.commons.views
import android.content.Context
import android.util.AttributeSet
import android.widget.RelativeLayout
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.interfaces.RenameTab
class RenamePatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs), RenameTab {
var paths = ArrayList<String>()
override fun initTab(paths: ArrayList<String>) {
override fun initTab(activity: BaseSimpleActivity, paths: ArrayList<String>) {
this.paths = paths
}
override fun dialogConfirmed() {
override fun dialogConfirmed(callback: () -> Unit) {
}
}

View file

@ -3,15 +3,81 @@ package com.simplemobiletools.commons.views
import android.content.Context
import android.util.AttributeSet
import android.widget.RelativeLayout
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.interfaces.RenameTab
import kotlinx.android.synthetic.main.tab_rename_simple.view.*
import java.io.File
class RenameSimpleTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs), RenameTab {
var ignoreClicks = false
var activity: BaseSimpleActivity? = null
var paths = ArrayList<String>()
override fun initTab(paths: ArrayList<String>) {
override fun initTab(activity: BaseSimpleActivity, paths: ArrayList<String>) {
this.activity = activity
this.paths = paths
}
override fun dialogConfirmed() {
override fun dialogConfirmed(callback: () -> Unit) {
val valueToAdd = rename_simple_value.value
val append = rename_simple_radio_group.checkedRadioButtonId == rename_simple_radio_append.id
if (valueToAdd.isEmpty()) {
callback()
return
}
if (!valueToAdd.isAValidFilename()) {
activity?.toast(R.string.invalid_name)
return
}
val validPaths = paths.filter { File(it).exists() }
val sdFilePath = validPaths.firstOrNull { activity?.isPathOnSD(it) == true } ?: validPaths.firstOrNull()
if (sdFilePath == null) {
activity?.toast(R.string.unknown_error_occurred)
return
}
activity?.handleSAFDialog(sdFilePath) {
ignoreClicks = true
var pathsCnt = validPaths.size
for (path in validPaths) {
val fullName = path.getFilenameFromPath()
var dotAt = fullName.lastIndexOf(".")
if (dotAt == -1) {
dotAt = fullName.length
}
val name = fullName.substring(0, dotAt)
val extension = if (fullName.contains(".")) ".${fullName.getFilenameExtension()}" else ""
val newName = if (append) {
"$name$valueToAdd$extension"
} else {
"$valueToAdd$fullName"
}
val newPath = "${path.getParentPath()}/$newName"
if (File(newPath).exists()) {
continue
}
activity?.renameFile(path, newPath) {
if (it) {
pathsCnt--
if (pathsCnt == 0) {
callback()
}
} else {
ignoreClicks = false
activity?.toast(R.string.unknown_error_occurred)
}
}
}
}
}
}