Convert Remove Files Dialog to Kotlin

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-07-04 14:09:31 +02:00
parent df1673b44c
commit 62931a7d06
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF

View file

@ -9,183 +9,84 @@
* SPDX-FileCopyrightText: 2015 David A. Velasco <dvelasco@solidgear.es> * SPDX-FileCopyrightText: 2015 David A. Velasco <dvelasco@solidgear.es>
* SPDX-License-Identifier: GPL-2.0-only AND (AGPL-3.0-or-later OR GPL-2.0-only) * SPDX-License-Identifier: GPL-2.0-only AND (AGPL-3.0-or-later OR GPL-2.0-only)
*/ */
package com.owncloud.android.ui.dialog; package com.owncloud.android.ui.dialog
import android.app.Dialog; import android.app.Dialog
import android.os.Bundle; import android.os.Bundle
import android.view.ActionMode; import android.view.ActionMode
import androidx.appcompat.app.AlertDialog
import com.google.android.material.button.MaterialButton; import com.google.android.material.button.MaterialButton
import com.nextcloud.client.di.Injectable; import com.nextcloud.client.di.Injectable
import com.owncloud.android.R; import com.owncloud.android.R
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.ui.activity.ComponentsGetter; import com.owncloud.android.ui.activity.ComponentsGetter
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener; import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener
import java.util.ArrayList;
import java.util.Collection;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
/** /**
* Dialog requiring confirmation before removing a collection of given OCFiles. * Dialog requiring confirmation before removing a collection of given OCFiles.
* Triggers the removal according to the user response. * Triggers the removal according to the user response.
*/ */
public class RemoveFilesDialogFragment extends ConfirmationDialogFragment implements class RemoveFilesDialogFragment : ConfirmationDialogFragment(), ConfirmationDialogFragmentListener, Injectable {
ConfirmationDialogFragmentListener, Injectable { private var mTargetFiles: Collection<OCFile>? = null
private var actionMode: ActionMode? = null
private static final int SINGLE_SELECTION = 1; override fun onStart() {
private static final String ARG_TARGET_FILES = "TARGET_FILES"; super.onStart()
private Collection<OCFile> mTargetFiles; val alertDialog = dialog as AlertDialog? ?: return
private ActionMode actionMode;
/** val positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE) as? MaterialButton
* Public factory method to create new RemoveFilesDialogFragment instances. positiveButton?.let {
* viewThemeUtils?.material?.colorMaterialButtonPrimaryTonal(positiveButton)
* @param files Files to remove.
* @param actionMode ActionMode to finish on confirmation
* @return Dialog ready to show.
*/
public static RemoveFilesDialogFragment newInstance(ArrayList<OCFile> files, ActionMode actionMode) {
RemoveFilesDialogFragment dialogFragment = newInstance(files);
dialogFragment.setActionMode(actionMode);
return dialogFragment;
} }
/** val negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) as? MaterialButton
* Public factory method to create new RemoveFilesDialogFragment instances. negativeButton?.let {
* viewThemeUtils?.material?.colorMaterialButtonPrimaryBorderless(negativeButton)
* @param files Files to remove.
* @return Dialog ready to show.
*/
public static RemoveFilesDialogFragment newInstance(ArrayList<OCFile> files) {
RemoveFilesDialogFragment frag = new RemoveFilesDialogFragment();
Bundle args = new Bundle();
int messageStringId;
boolean containsFolder = false;
boolean containsDown = false;
for (OCFile file: files) {
containsFolder |= file.isFolder();
containsDown |= file.isDown();
} }
if (files.size() == SINGLE_SELECTION) { val neutralButton = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL) as? MaterialButton
// choose message for a single file neutralButton?.let {
OCFile file = files.get(0); viewThemeUtils?.material?.colorMaterialButtonPrimaryBorderless(neutralButton)
messageStringId = file.isFolder() ?
R.string.confirmation_remove_folder_alert :
R.string.confirmation_remove_file_alert;
} else {
// choose message for more than one file
messageStringId = containsFolder ?
R.string.confirmation_remove_folders_alert :
R.string.confirmation_remove_files_alert;
}
args.putInt(ARG_MESSAGE_RESOURCE_ID, messageStringId);
if (files.size() == SINGLE_SELECTION) {
args.putStringArray(ARG_MESSAGE_ARGUMENTS, new String[] { files.get(0).getFileName() } );
}
args.putInt(ARG_POSITIVE_BTN_RES, R.string.file_delete);
if (containsFolder || containsDown) {
args.putInt(ARG_NEGATIVE_BTN_RES, R.string.confirmation_remove_local);
}
args.putInt(ARG_NEUTRAL_BTN_RES, R.string.file_keep);
args.putParcelableArrayList(ARG_TARGET_FILES, files);
frag.setArguments(args);
return frag;
}
/**
* Convenience factory method to create new RemoveFilesDialogFragment instances for a single file
*
* @param file File to remove.
* @return Dialog ready to show.
*/
public static RemoveFilesDialogFragment newInstance(OCFile file) {
ArrayList<OCFile> list = new ArrayList<>();
list.add(file);
return newInstance(list);
}
@Override
public void onStart() {
super.onStart();
AlertDialog alertDialog = (AlertDialog) getDialog();
if (alertDialog != null) {
MaterialButton positiveButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
viewThemeUtils.material.colorMaterialButtonPrimaryTonal(positiveButton);
MaterialButton negativeButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(negativeButton);
MaterialButton neutralButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
if (neutralButton != null) {
viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(neutralButton);
}
} }
} }
@NonNull override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
@Override val dialog = super.onCreateDialog(savedInstanceState)
public Dialog onCreateDialog(Bundle savedInstanceState) { val arguments = arguments ?: return dialog
Dialog dialog = super.onCreateDialog(savedInstanceState);
Bundle arguments = getArguments();
if (arguments == null) { mTargetFiles = arguments.getParcelableArrayList(ARG_TARGET_FILES)
return dialog; setOnConfirmationListener(this)
} return dialog
mTargetFiles = arguments.getParcelableArrayList(ARG_TARGET_FILES);
setOnConfirmationListener(this);
return dialog;
} }
/** /**
* Performs the removal of the target file, both locally and in the server and * Performs the removal of the target file, both locally and in the server and
* finishes the supplied ActionMode if one was given. * finishes the supplied ActionMode if one was given.
*/ */
@Override override fun onConfirmation(callerTag: String?) {
public void onConfirmation(String callerTag) { removeFiles(false)
removeFiles(false);
} }
/** /**
* Performs the removal of the local copy of the target file * Performs the removal of the local copy of the target file
*/ */
@Override override fun onCancel(callerTag: String?) {
public void onCancel(String callerTag) { removeFiles(true)
removeFiles(true);
} }
private void removeFiles(boolean onlyLocalCopy) { private fun removeFiles(onlyLocalCopy: Boolean) {
ComponentsGetter cg = (ComponentsGetter) getActivity(); val cg = activity as ComponentsGetter?
if (cg != null) { cg?.fileOperationsHelper?.removeFiles(mTargetFiles, onlyLocalCopy, false)
cg.getFileOperationsHelper().removeFiles(mTargetFiles, onlyLocalCopy, false); finishActionMode()
}
finishActionMode();
} }
@Override override fun onNeutral(callerTag: String?) {
public void onNeutral(String callerTag) {
// nothing to do here // nothing to do here
} }
private void setActionMode(ActionMode actionMode) { private fun setActionMode(actionMode: ActionMode?) {
this.actionMode = actionMode; this.actionMode = actionMode
} }
/** /**
@ -193,9 +94,73 @@ public class RemoveFilesDialogFragment extends ConfirmationDialogFragment implem
* for example if we want to exit the selection mode * for example if we want to exit the selection mode
* after deleting the target files. * after deleting the target files.
*/ */
private void finishActionMode() { private fun finishActionMode() {
if (actionMode != null) { actionMode?.finish()
actionMode.finish(); }
companion object {
private const val SINGLE_SELECTION = 1
private const val ARG_TARGET_FILES = "TARGET_FILES"
@JvmStatic
fun newInstance(files: ArrayList<OCFile>, actionMode: ActionMode?): RemoveFilesDialogFragment {
return newInstance(files).apply {
setActionMode(actionMode)
}
}
@JvmStatic
fun newInstance(files: ArrayList<OCFile>): RemoveFilesDialogFragment {
val messageStringId: Int
var containsFolder = false
var containsDown = false
for (file in files) {
containsFolder = containsFolder or file.isFolder
containsDown = containsDown or file.isDown
}
if (files.size == SINGLE_SELECTION) {
val file = files[0]
messageStringId =
if (file.isFolder) R.string.confirmation_remove_folder_alert else R.string.confirmation_remove_file_alert
} else {
messageStringId =
if (containsFolder) R.string.confirmation_remove_folders_alert else R.string.confirmation_remove_files_alert
}
val bundle = Bundle().apply {
putInt(ARG_MESSAGE_RESOURCE_ID, messageStringId)
if (files.size == SINGLE_SELECTION) {
putStringArray(
ARG_MESSAGE_ARGUMENTS, arrayOf(
files[0].fileName
)
)
}
putInt(ARG_POSITIVE_BTN_RES, R.string.file_delete)
if (containsFolder || containsDown) {
putInt(ARG_NEGATIVE_BTN_RES, R.string.confirmation_remove_local)
}
putInt(ARG_NEUTRAL_BTN_RES, R.string.file_keep)
putParcelableArrayList(ARG_TARGET_FILES, files)
}
return RemoveFilesDialogFragment().apply {
arguments = bundle
}
}
@JvmStatic
fun newInstance(file: OCFile): RemoveFilesDialogFragment {
val list = ArrayList<OCFile>().apply {
add(file)
}
return newInstance(list)
} }
} }
} }