From de1a1cb9ae506a272866302f92ad4b84af9c045d Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 17 Nov 2023 13:38:50 +0100 Subject: [PATCH] Convert to kt Signed-off-by: alperozturk --- ...ooseRichDocumentsTemplateDialogFragment.kt | 655 +++++++++--------- 1 file changed, 328 insertions(+), 327 deletions(-) diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/ChooseRichDocumentsTemplateDialogFragment.kt b/app/src/main/java/com/owncloud/android/ui/dialog/ChooseRichDocumentsTemplateDialogFragment.kt index b62576fd822c..477aeb78d939 100644 --- a/app/src/main/java/com/owncloud/android/ui/dialog/ChooseRichDocumentsTemplateDialogFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/dialog/ChooseRichDocumentsTemplateDialogFragment.kt @@ -21,437 +21,438 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - -package com.owncloud.android.ui.dialog; - -import android.app.Activity; -import android.app.Dialog; -import android.content.Intent; -import android.os.AsyncTask; -import android.os.Bundle; -import android.text.Editable; -import android.text.TextWatcher; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.Button; - -import com.google.android.material.button.MaterialButton; -import com.google.android.material.dialog.MaterialAlertDialogBuilder; -import com.google.common.collect.Sets; -import com.nextcloud.client.account.CurrentAccountProvider; -import com.nextcloud.client.account.User; -import com.nextcloud.client.di.Injectable; -import com.nextcloud.client.network.ClientFactory; -import com.owncloud.android.MainApp; -import com.owncloud.android.R; -import com.owncloud.android.databinding.ChooseTemplateBinding; -import com.owncloud.android.datamodel.FileDataStorageManager; -import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.datamodel.Template; -import com.owncloud.android.files.CreateFileFromTemplateOperation; -import com.owncloud.android.files.FetchTemplateOperation; -import com.owncloud.android.lib.common.OwnCloudClient; -import com.owncloud.android.lib.common.operations.RemoteOperationResult; -import com.owncloud.android.lib.common.utils.Log_OC; -import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation; -import com.owncloud.android.lib.resources.files.model.RemoteFile; -import com.owncloud.android.ui.activity.ExternalSiteWebView; -import com.owncloud.android.ui.activity.RichDocumentsEditorWebView; -import com.owncloud.android.ui.adapter.RichDocumentsTemplateAdapter; -import com.owncloud.android.utils.DisplayUtils; -import com.owncloud.android.utils.FileStorageUtils; -import com.owncloud.android.utils.KeyboardUtils; -import com.owncloud.android.utils.NextcloudServer; -import com.owncloud.android.utils.theme.ViewThemeUtils; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.Set; - -import javax.inject.Inject; - -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.fragment.app.DialogFragment; -import androidx.recyclerview.widget.GridLayoutManager; +package com.owncloud.android.ui.dialog + +import android.annotation.SuppressLint +import android.app.Dialog +import android.content.Intent +import android.os.AsyncTask +import android.os.Bundle +import android.text.Editable +import android.text.TextWatcher +import android.view.View +import androidx.appcompat.app.AlertDialog +import androidx.fragment.app.DialogFragment +import androidx.recyclerview.widget.GridLayoutManager +import com.google.android.material.button.MaterialButton +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import com.google.common.collect.Sets +import com.nextcloud.client.account.CurrentAccountProvider +import com.nextcloud.client.account.User +import com.nextcloud.client.di.Injectable +import com.nextcloud.client.network.ClientFactory +import com.nextcloud.client.network.ClientFactory.CreationException +import com.nextcloud.utils.extensions.getParcelableArgument +import com.owncloud.android.MainApp +import com.owncloud.android.R +import com.owncloud.android.databinding.ChooseTemplateBinding +import com.owncloud.android.datamodel.FileDataStorageManager +import com.owncloud.android.datamodel.OCFile +import com.owncloud.android.datamodel.Template +import com.owncloud.android.files.CreateFileFromTemplateOperation +import com.owncloud.android.files.FetchTemplateOperation +import com.owncloud.android.lib.common.OwnCloudClient +import com.owncloud.android.lib.common.utils.Log_OC +import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation +import com.owncloud.android.lib.resources.files.model.RemoteFile +import com.owncloud.android.ui.activity.ExternalSiteWebView +import com.owncloud.android.ui.activity.RichDocumentsEditorWebView +import com.owncloud.android.ui.adapter.RichDocumentsTemplateAdapter +import com.owncloud.android.ui.dialog.IndeterminateProgressDialog.Companion.newInstance +import com.owncloud.android.utils.DisplayUtils +import com.owncloud.android.utils.FileStorageUtils +import com.owncloud.android.utils.KeyboardUtils +import com.owncloud.android.utils.NextcloudServer +import com.owncloud.android.utils.theme.ViewThemeUtils +import java.lang.ref.WeakReference +import java.util.Objects +import javax.inject.Inject /** * Dialog to show templates for new documents/spreadsheets/presentations. */ -public class ChooseRichDocumentsTemplateDialogFragment extends DialogFragment implements View.OnClickListener, - RichDocumentsTemplateAdapter.ClickListener, Injectable { - - private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER"; - private static final String ARG_TYPE = "TYPE"; - private static final String TAG = ChooseRichDocumentsTemplateDialogFragment.class.getSimpleName(); - private static final String DOT = "."; - public static final int SINGLE_TEMPLATE = 1; - private static final String WAIT_DIALOG_TAG = "WAIT"; - - private Set fileNames; - - @Inject CurrentAccountProvider currentAccount; - @Inject ClientFactory clientFactory; - @Inject ViewThemeUtils viewThemeUtils; - @Inject FileDataStorageManager fileDataStorageManager; - @Inject KeyboardUtils keyboardUtils; - private RichDocumentsTemplateAdapter adapter; - private OCFile parentFolder; - private OwnCloudClient client; - private MaterialButton positiveButton; - private DialogFragment waitDialog; - - public enum Type { - DOCUMENT, - SPREADSHEET, - PRESENTATION +class ChooseRichDocumentsTemplateDialogFragment : + DialogFragment(), + View.OnClickListener, + RichDocumentsTemplateAdapter.ClickListener, + Injectable { + + private var fileNames: MutableSet? = null + + @JvmField + @Inject + var currentAccount: CurrentAccountProvider? = null + + @JvmField + @Inject + var clientFactory: ClientFactory? = null + + @JvmField + @Inject + var viewThemeUtils: ViewThemeUtils? = null + + @JvmField + @Inject + var fileDataStorageManager: FileDataStorageManager? = null + + @JvmField + @Inject + var keyboardUtils: KeyboardUtils? = null + + private var adapter: RichDocumentsTemplateAdapter? = null + private var parentFolder: OCFile? = null + private var client: OwnCloudClient? = null + private var positiveButton: MaterialButton? = null + private var waitDialog: DialogFragment? = null + + enum class Type { + DOCUMENT, SPREADSHEET, PRESENTATION } - ChooseTemplateBinding binding; + var binding: ChooseTemplateBinding? = null - @NextcloudServer(max = 18) // will be removed in favor of generic direct editing - public static ChooseRichDocumentsTemplateDialogFragment newInstance(OCFile parentFolder, Type type) { - ChooseRichDocumentsTemplateDialogFragment frag = new ChooseRichDocumentsTemplateDialogFragment(); - Bundle args = new Bundle(); - args.putParcelable(ARG_PARENT_FOLDER, parentFolder); - args.putString(ARG_TYPE, type.name()); - frag.setArguments(args); - return frag; + override fun onStart() { + super.onStart() + setupAlertDialogButtons() + checkEnablingCreateButton() } - @Override - public void onStart() { - super.onStart(); - - AlertDialog alertDialog = (AlertDialog) getDialog(); - - if (alertDialog != null) { - positiveButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); - viewThemeUtils.material.colorMaterialButtonPrimaryTonal(positiveButton); - - MaterialButton negativeButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); - if (negativeButton != null) { - viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(negativeButton); + private fun setupAlertDialogButtons() { + (dialog as AlertDialog?)?.run { + positiveButton = getButton(AlertDialog.BUTTON_POSITIVE) as MaterialButton + positiveButton?.let { + viewThemeUtils?.material?.colorMaterialButtonPrimaryTonal(it) } + positiveButton?.setOnClickListener(this@ChooseRichDocumentsTemplateDialogFragment) + positiveButton?.isEnabled = false - positiveButton.setOnClickListener(this); - positiveButton.setEnabled(false); + val negativeButton = getButton(AlertDialog.BUTTON_NEGATIVE) as? MaterialButton + negativeButton?.let { + viewThemeUtils?.material?.colorMaterialButtonPrimaryBorderless(it) + } } - - checkEnablingCreateButton(); } - @Override - public void onResume() { - super.onResume(); - keyboardUtils.showKeyboardForEditText(requireDialog().getWindow(), binding.filename); + override fun onResume() { + super.onResume() + binding?.let { + keyboardUtils?.showKeyboardForEditText(requireDialog().window, it.filename) + } } - @NonNull - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Bundle arguments = getArguments(); - if (arguments == null) { - throw new IllegalArgumentException("Arguments may not be null"); - } + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val bundle = arguments ?: throw IllegalArgumentException("Arguments may not be null") + val activity = activity ?: throw IllegalArgumentException("Activity may not be null") - Activity activity = getActivity(); - if (activity == null) { - throw new IllegalArgumentException("Activity may not be null"); - } + setupClient() + setupParentFolder(bundle) - try { - client = clientFactory.create(currentAccount.getUser()); - } catch (ClientFactory.CreationException e) { - throw new RuntimeException(e); // we'll NPE without the client - } + val inflater = requireActivity().layoutInflater + binding = ChooseTemplateBinding.inflate(inflater, null, false) - parentFolder = arguments.getParcelable(ARG_PARENT_FOLDER); - List folderContent = fileDataStorageManager.getFolderContent(parentFolder, false); - fileNames = Sets.newHashSetWithExpectedSize(folderContent.size()); + viewThemeUtils?.material?.colorTextInputLayout(binding!!.filenameContainer) - for (OCFile file : folderContent) { - fileNames.add(file.getFileName()); - } + val type = bundle.getString(ARG_TYPE)?.let { Type.valueOf(it) } + setupList(type) - // Inflate the layout for the dialog - LayoutInflater inflater = requireActivity().getLayoutInflater(); - binding = ChooseTemplateBinding.inflate(inflater, null, false); - View view = binding.getRoot(); + setupFileName() - viewThemeUtils.material.colorTextInputLayout(binding.filenameContainer); + // Build the dialog + val builder = MaterialAlertDialogBuilder(activity) + builder.setView(view) + .setPositiveButton(R.string.create, null) + .setNegativeButton(R.string.common_cancel, null) - Type type = Type.valueOf(arguments.getString(ARG_TYPE)); - new FetchTemplateTask(this, client).execute(type); + type?.let { + builder.setTitle(getTitle(it)) + } - binding.list.setHasFixedSize(true); - binding.list.setLayoutManager(new GridLayoutManager(activity, 2)); - adapter = new RichDocumentsTemplateAdapter(type, - this, - getContext(), - currentAccount, - clientFactory, - viewThemeUtils); - binding.list.setAdapter(adapter); + viewThemeUtils?.dialog?.colorMaterialAlertDialogBackground(activity, builder) + return builder.create() + } - binding.filename.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { + @Suppress("TooGenericExceptionThrown") + private fun setupClient() { + client = try { + clientFactory?.create(currentAccount?.user) + } catch (e: CreationException) { + // we'll NPE without the client + throw RuntimeException(e) + } + } + private fun setupParentFolder(bundle: Bundle?) { + parentFolder = bundle?.getParcelableArgument(ARG_PARENT_FOLDER, OCFile::class.java) + val folderContent = fileDataStorageManager?.getFolderContent(parentFolder, false) + fileNames = folderContent?.size?.let { Sets.newHashSetWithExpectedSize(it) } + if (folderContent != null) { + for (file in folderContent) { + fileNames?.add(file.fileName) } + } + } - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - // not needed - } + private fun setupList(type: Type?) { + FetchTemplateTask(this, client).execute(type) + binding?.list?.setHasFixedSize(true) + binding?.list?.layoutManager = GridLayoutManager(activity, 2) + adapter = RichDocumentsTemplateAdapter( + type, + this, + context, + currentAccount, + clientFactory, + viewThemeUtils + ) + binding?.list?.adapter = adapter + } - @Override - public void afterTextChanged(Editable s) { - checkEnablingCreateButton(); + @Suppress("EmptyFunctionBlock") + private fun setupFileName() { + binding?.filename?.addTextChangedListener(object : TextWatcher { + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { } - }); - - int titleTextId = getTitle(type); - // Build the dialog - MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(activity); - builder.setView(view) - .setPositiveButton(R.string.create, null) - .setNegativeButton(R.string.common_cancel, null) - .setTitle(titleTextId); - - viewThemeUtils.dialog.colorMaterialAlertDialogBackground(activity, builder); - - return builder.create(); + override fun afterTextChanged(s: Editable) { + checkEnablingCreateButton() + } + }) } - private int getTitle(Type type) { - if (type == Type.DOCUMENT) { - return R.string.create_new_document; - } else if (type == Type.SPREADSHEET) { - return R.string.create_new_spreadsheet; - } else if (type == Type.PRESENTATION) { - return R.string.create_new_presentation; + @Suppress("ReturnCount") + private fun getTitle(type: Type): Int { + when (type) { + Type.DOCUMENT -> { + return R.string.create_new_document + } + Type.SPREADSHEET -> { + return R.string.create_new_spreadsheet + } + Type.PRESENTATION -> { + return R.string.create_new_presentation + } + else -> return R.string.select_template } - - return R.string.select_template; } - @Override - public void onDestroyView() { - super.onDestroyView(); - binding = null; + override fun onDestroyView() { + super.onDestroyView() + binding = null } - private void createFromTemplate(Template template, String path) { - waitDialog = IndeterminateProgressDialog.newInstance(R.string.wait_a_moment, false); - waitDialog.show(getParentFragmentManager(), WAIT_DIALOG_TAG); - new CreateFileFromTemplateTask(this, client, template, path, currentAccount.getUser()).execute(); + private fun createFromTemplate(template: Template, path: String) { + waitDialog = newInstance(R.string.wait_a_moment, false) + waitDialog?.show(parentFragmentManager, WAIT_DIALOG_TAG) + CreateFileFromTemplateTask(this, client, template, path, currentAccount!!.user).execute() } - public void setTemplateList(List