From 7a9a92b8455b3f815fbcc86b147b7bc2350b7670 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 8 Jul 2024 11:52:46 +0200 Subject: [PATCH 1/4] Rename .java to .kt Signed-off-by: alperozturk --- ...Fragment.java => ChooseRichDocumentsTemplateDialogFragment.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/com/owncloud/android/ui/dialog/{ChooseRichDocumentsTemplateDialogFragment.java => ChooseRichDocumentsTemplateDialogFragment.kt} (100%) diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/ChooseRichDocumentsTemplateDialogFragment.java b/app/src/main/java/com/owncloud/android/ui/dialog/ChooseRichDocumentsTemplateDialogFragment.kt similarity index 100% rename from app/src/main/java/com/owncloud/android/ui/dialog/ChooseRichDocumentsTemplateDialogFragment.java rename to app/src/main/java/com/owncloud/android/ui/dialog/ChooseRichDocumentsTemplateDialogFragment.kt From 8e746617f264dc96cc32d3e89fefd1f92b9f29c0 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 8 Jul 2024 11:52:47 +0200 Subject: [PATCH 2/4] Fix and convert to kt Signed-off-by: alperozturk --- ...ooseRichDocumentsTemplateDialogFragment.kt | 695 +++++++++--------- 1 file changed, 356 insertions(+), 339 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 f87616c50c93..13137c83d1e9 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 @@ -10,447 +10,464 @@ * * SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only */ - -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 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.utils.extensions.BundleExtensionsKt; -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.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.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.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 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 { +class ChooseRichDocumentsTemplateDialogFragment : + DialogFragment(), + View.OnClickListener, + RichDocumentsTemplateAdapter.ClickListener, + Injectable { + private var fileNames: MutableSet? = null + + @Inject + lateinit var currentAccount: CurrentAccountProvider + + @Inject + lateinit var clientFactory: ClientFactory + + @Inject + lateinit var viewThemeUtils: ViewThemeUtils + + @Inject + lateinit var fileDataStorageManager: FileDataStorageManager + + @Inject + lateinit var keyboardUtils: KeyboardUtils + + 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; - - @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 - public void onStart() { - super.onStart(); + private lateinit var binding: ChooseTemplateBinding - AlertDialog alertDialog = (AlertDialog) getDialog(); + override fun onStart() { + super.onStart() - if (alertDialog != null) { - positiveButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); - viewThemeUtils.material.colorMaterialButtonPrimaryTonal(positiveButton); + val alertDialog = dialog as AlertDialog? - MaterialButton negativeButton = (MaterialButton) alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE); - if (negativeButton != null) { - viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(negativeButton); + alertDialog?.let { + positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE) as? MaterialButton + positiveButton?.let { + viewThemeUtils.material.colorMaterialButtonPrimaryTonal(it) + it.setOnClickListener(this) + it.isEnabled = false } - positiveButton.setOnClickListener(this); - positiveButton.setEnabled(false); + val negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) as? MaterialButton + negativeButton?.let { + viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(negativeButton) + } } - checkEnablingCreateButton(); + checkEnablingCreateButton() } - @Override - public void onResume() { - super.onResume(); - keyboardUtils.showKeyboardForEditText(requireDialog().getWindow(), binding.filename); + override fun onResume() { + super.onResume() + keyboardUtils.showKeyboardForEditText(requireDialog().window, binding.filename) } - @NonNull - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Bundle arguments = getArguments(); - if (arguments == null) { - throw new IllegalArgumentException("Arguments may not be null"); - } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + val inflater = requireActivity().layoutInflater + binding = ChooseTemplateBinding.inflate(inflater, null, false) + } - Activity activity = getActivity(); - if (activity == null) { - throw new IllegalArgumentException("Activity may not be null"); - } + @Suppress("DEPRECATION") + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + val arguments = arguments ?: throw IllegalArgumentException("Arguments may not be null") + val activity = activity ?: throw IllegalArgumentException("Activity may not be null") + + initClient() + initFilenames(arguments) + viewThemeUtils.material.colorTextInputLayout(binding.filenameContainer) + + val type = Type.valueOf(arguments.getString(ARG_TYPE) ?: "") + FetchTemplateTask(this, client).execute(type) + + initList(type) + addTextChangeListener() + val titleTextId = getTitle(type) + val builder = getDialogBuilder(activity, titleTextId) + return builder.create() + } + + @Suppress("DEPRECATION", "TooGenericExceptionThrown") + private fun initClient() { try { - client = clientFactory.create(currentAccount.getUser()); - } catch (ClientFactory.CreationException e) { - throw new RuntimeException(e); // we'll NPE without the client + client = clientFactory.create(currentAccount.user) + } catch (e: CreationException) { + throw RuntimeException(e) } + } - parentFolder = BundleExtensionsKt.getParcelableArgument(arguments, ARG_PARENT_FOLDER, OCFile.class); - List folderContent = fileDataStorageManager.getFolderContent(parentFolder, false); - fileNames = Sets.newHashSetWithExpectedSize(folderContent.size()); + private fun initFilenames(arguments: Bundle) { + parentFolder = arguments.getParcelableArgument(ARG_PARENT_FOLDER, OCFile::class.java) + val folderContent = fileDataStorageManager.getFolderContent(parentFolder, false) + fileNames = Sets.newHashSetWithExpectedSize(folderContent.size) - for (OCFile file : folderContent) { - fileNames.add(file.getFileName()); + for (file in folderContent) { + fileNames?.add(file.fileName) } + } - // Inflate the layout for the dialog - LayoutInflater inflater = requireActivity().getLayoutInflater(); - binding = ChooseTemplateBinding.inflate(inflater, null, false); - View view = binding.getRoot(); + private fun initList(type: Type) { + binding.list.setHasFixedSize(true) + binding.list.layoutManager = GridLayoutManager(activity, 2) + adapter = RichDocumentsTemplateAdapter( + type, + this, + context, + currentAccount, + clientFactory, + viewThemeUtils + ) + binding.list.adapter = adapter + } - viewThemeUtils.material.colorTextInputLayout(binding.filenameContainer); + private fun addTextChangeListener() { + binding.filename.addTextChangedListener(object : TextWatcher { + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) = Unit + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) = Unit + override fun afterTextChanged(s: Editable) { + checkEnablingCreateButton() + } + }) + } - Type type = Type.valueOf(arguments.getString(ARG_TYPE)); - new FetchTemplateTask(this, client).execute(type); + private fun getDialogBuilder(activity: Activity, titleTextId: Int): MaterialAlertDialogBuilder { + val builder = MaterialAlertDialogBuilder(activity) + .setView(binding.root) + .setPositiveButton(R.string.create, null) + .setNegativeButton(R.string.common_cancel, null) + .setTitle(titleTextId) - 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) - binding.filename.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { + return builder + } + private fun getTitle(type: Type): Int { + return when (type) { + Type.DOCUMENT -> { + R.string.create_new_document } - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - // not needed + Type.SPREADSHEET -> { + R.string.create_new_spreadsheet } - @Override - public void afterTextChanged(Editable s) { - checkEnablingCreateButton(); + Type.PRESENTATION -> { + R.string.create_new_presentation } - }); - - 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(); - } - - 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; + else -> R.string.select_template } - - return R.string.select_template; } - @Override - public void onDestroyView() { - super.onDestroyView(); - binding = null; + @Suppress("DEPRECATION") + 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() } - 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(); + @SuppressLint("NotifyDataSetChanged") + fun setTemplateList(templateList: List