Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0bec0a99 authored by tibbi's avatar tibbi
Browse files

adding a dialog for renaming multiple items at once

parent a4c52f0f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@ buildscript {
        propMinSdkVersion = 21
        propTargetSdkVersion = propCompileSdkVersion
        propVersionCode = 1
        propVersionName = '5.1.14'
        propVersionName = '5.1.15'
        kotlin_version = '1.2.71'
    }

+1 −1
Original line number Diff line number Diff line
@@ -33,7 +33,7 @@ class FileConflictDialog(val activity: Activity, val fileDirItem: FileDirItem, v
        }

        AlertDialog.Builder(activity)
                .setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
                .setPositiveButton(R.string.ok) { dialog, which -> dialogConfirmed() }
                .setNegativeButton(R.string.cancel, null)
                .create().apply {
                    activity.setupDialogStuff(view, this)
+88 −0
Original line number Diff line number Diff line
package com.simplemobiletools.commons.dialogs

import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.*
import kotlinx.android.synthetic.main.dialog_rename_items.*
import kotlinx.android.synthetic.main.dialog_rename_items.view.*
import java.util.*

class RenameItemsDialog(val activity: BaseSimpleActivity, val paths: ArrayList<String>, val callback: () -> Unit) {
    init {

        val view = activity.layoutInflater.inflate(R.layout.dialog_rename_items, null)

        AlertDialog.Builder(activity)
                .setPositiveButton(R.string.ok, null)
                .setNegativeButton(R.string.cancel, null)
                .create().apply {
                    activity.setupDialogStuff(view, this, R.string.rename) {
                        showKeyboard(view.rename_items_value)
                        getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
                            val valueToAdd = view.rename_items_value.value
                            val append = view.rename_items_radio_group.checkedRadioButtonId == rename_items_radio_append.id

                            if (valueToAdd.isEmpty()) {
                                callback()
                                dismiss()
                                return@setOnClickListener
                            }

                            if (!valueToAdd.isAValidFilename()) {
                                activity.toast(R.string.invalid_name)
                                return@setOnClickListener
                            }

                            val validPaths = paths.filter { activity.getDoesFilePathExist(it) }
                            val sdFilePath = validPaths.firstOrNull { activity.isPathOnSD(it) } ?: validPaths.firstOrNull()
                            if (sdFilePath == null) {
                                activity.toast(R.string.unknown_error_occurred)
                                dismiss()
                                return@setOnClickListener
                            }

                            var pathsCnt = validPaths.size

                            activity.handleSAFDialog(sdFilePath) {
                                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 (activity.getDoesFilePathExist(newPath)) {
                                        continue
                                    }

                                    activity.renameFile(path, newPath) {
                                        if (it) {
                                            pathsCnt--
                                            if (pathsCnt == 0) {
                                                callback()
                                                dismiss()
                                            }
                                        } else {
                                            activity.toast(R.string.unknown_error_occurred)
                                            dismiss()
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
    }
}
+41 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rename_items_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/activity_margin">

    <com.simplemobiletools.commons.views.MyEditText
        android:id="@+id/rename_items_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_margin"
        android:singleLine="true"
        android:textCursorDrawable="@null"
        android:textSize="@dimen/normal_text_size"/>

    <RadioGroup
        android:id="@+id/rename_items_radio_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.simplemobiletools.commons.views.MyCompatRadioButton
            android:id="@+id/rename_items_radio_append"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="true"
            android:paddingTop="@dimen/normal_margin"
            android:paddingBottom="@dimen/normal_margin"
            android:text="@string/append_filenames"/>

        <com.simplemobiletools.commons.views.MyCompatRadioButton
            android:id="@+id/rename_items_radio_prepend"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/normal_margin"
            android:paddingBottom="@dimen/normal_margin"
            android:text="@string/prepend_filenames"/>
    </RadioGroup>
</LinearLayout>