From 4751a1cb6b016a056c13e50561e014a7f61a21a5 Mon Sep 17 00:00:00 2001 From: Neel Doshi Date: Thu, 13 Jun 2024 16:39:58 +0530 Subject: [PATCH 1/5] Rename .java to .kt --- .../ui/posts/{AddCategoryFragment.java => AddCategoryFragment.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename WordPress/src/main/java/org/wordpress/android/ui/posts/{AddCategoryFragment.java => AddCategoryFragment.kt} (100%) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt similarity index 100% rename from WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.java rename to WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt From 31f4802af348ef7afac4d4abea4015e95853e21c Mon Sep 17 00:00:00 2001 From: Neel Doshi Date: Thu, 13 Jun 2024 17:43:42 +0530 Subject: [PATCH 2/5] AddCategoryFragment converted to kotlin --- .../android/ui/posts/AddCategoryFragment.kt | 217 +++++++++--------- .../ui/posts/SelectCategoriesActivity.java | 2 +- 2 files changed, 105 insertions(+), 114 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt index 840908ba2f0a..7ffb19df41ea 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt @@ -1,139 +1,130 @@ -package org.wordpress.android.ui.posts; - -import android.app.Dialog; -import android.content.DialogInterface; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; - -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatDialogFragment; -import androidx.appcompat.view.ContextThemeWrapper; - -import com.google.android.material.dialog.MaterialAlertDialogBuilder; - -import org.wordpress.android.R; -import org.wordpress.android.WordPress; -import org.wordpress.android.databinding.AddCategoryBinding; -import org.wordpress.android.fluxc.model.SiteModel; -import org.wordpress.android.fluxc.model.TermModel; -import org.wordpress.android.fluxc.store.TaxonomyStore; -import org.wordpress.android.models.CategoryNode; -import org.wordpress.android.util.ToastUtils; - -import java.util.ArrayList; - -import javax.inject.Inject; - -public class AddCategoryFragment extends AppCompatDialogFragment { - private SiteModel mSite; - private AddCategoryBinding mBinding; - - @Inject TaxonomyStore mTaxonomyStore; - - public static AddCategoryFragment newInstance(SiteModel site) { - AddCategoryFragment fragment = new AddCategoryFragment(); - Bundle bundle = new Bundle(); - bundle.putSerializable(WordPress.SITE, site); - fragment.setArguments(bundle); - return fragment; +package org.wordpress.android.ui.posts + +import android.app.Dialog +import android.content.DialogInterface +import android.os.Bundle +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatDialogFragment +import androidx.appcompat.view.ContextThemeWrapper +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import org.wordpress.android.R +import org.wordpress.android.WordPress +import org.wordpress.android.databinding.AddCategoryBinding +import org.wordpress.android.fluxc.model.SiteModel +import org.wordpress.android.fluxc.model.TermModel +import org.wordpress.android.fluxc.store.TaxonomyStore +import org.wordpress.android.models.CategoryNode +import org.wordpress.android.util.ToastUtils +import javax.inject.Inject + +class AddCategoryFragment : AppCompatDialogFragment() { + private var mSite: SiteModel? = null + private var binding: AddCategoryBinding? = null + + @set:Inject + var mTaxonomyStore: TaxonomyStore? = null + + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + (requireActivity().application as WordPress).component().inject(this) + initSite(savedInstanceState) + val builder = + MaterialAlertDialogBuilder(ContextThemeWrapper(activity, R.style.PostSettingsTheme)) + binding = AddCategoryBinding.inflate(layoutInflater, null, false) + loadCategories() + builder.setView(binding!!.root) + .setPositiveButton(android.R.string.ok, null) + .setNegativeButton(android.R.string.cancel, null) + + return builder.create() } - @Override - @NonNull - public Dialog onCreateDialog(Bundle savedInstanceState) { - ((WordPress) requireActivity().getApplication()).component().inject(this); - - initSite(savedInstanceState); - - AlertDialog.Builder builder = - new MaterialAlertDialogBuilder(new ContextThemeWrapper(getActivity(), R.style.PostSettingsTheme)); - // Get the layout inflater - LayoutInflater inflater = requireActivity().getLayoutInflater(); - //noinspection InflateParams - mBinding = AddCategoryBinding.inflate(inflater, null, false); - - loadCategories(); - - builder.setView(mBinding.getRoot()) - .setPositiveButton(android.R.string.ok, null) - .setNegativeButton(android.R.string.cancel, null); - - return builder.create(); - } - - @Override - public void onStart() { - super.onStart(); - AlertDialog dialog = (AlertDialog) requireDialog(); - dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (addCategory()) { - dismiss(); - } + override fun onStart() { + super.onStart() + val dialog = requireDialog() as AlertDialog + dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener { + if (addCategory()) { + dismiss() } - }); + } } - private void initSite(Bundle savedInstanceState) { - if (savedInstanceState == null) { - if (getArguments() != null) { - mSite = (SiteModel) getArguments().getSerializable(WordPress.SITE); + @Suppress("Deprecation") + private fun initSite(savedInstanceState: Bundle?) { + mSite = if (savedInstanceState == null) { + if (arguments != null) { + requireArguments().getSerializable(WordPress.SITE) as SiteModel? } else { - mSite = (SiteModel) requireActivity().getIntent().getSerializableExtra(WordPress.SITE); + requireActivity().intent.getSerializableExtra(WordPress.SITE) as SiteModel? } } else { - mSite = (SiteModel) savedInstanceState.getSerializable(WordPress.SITE); + savedInstanceState.getSerializable(WordPress.SITE) as SiteModel? } if (mSite == null) { - ToastUtils.showToast(requireActivity(), R.string.blog_not_found, ToastUtils.Duration.SHORT); - getParentFragmentManager().popBackStack(); + ToastUtils.showToast( + requireActivity(), + R.string.blog_not_found, + ToastUtils.Duration.SHORT + ) + parentFragmentManager.popBackStack() } } - private boolean addCategory() { - String categoryName = mBinding.categoryName.getText().toString(); - CategoryNode selectedCategory = (CategoryNode) mBinding.parentCategory.getSelectedItem(); - long parentId = (selectedCategory != null) ? selectedCategory.getCategoryId() : 0; + private fun addCategory(): Boolean { + val categoryName = binding!!.categoryName.text.toString() + val selectedCategory = binding!!.parentCategory.selectedItem as CategoryNode + val parentId = selectedCategory.categoryId - if (categoryName.replaceAll(" ", "").equals("")) { - mBinding.categoryName.setError(getText(R.string.cat_name_required)); - return false; + if (categoryName.replace(" ".toRegex(), "") == "") { + binding!!.categoryName.error = getText(R.string.cat_name_required) + return false } - TermModel newCategory = new TermModel( - TaxonomyStore.DEFAULT_TAXONOMY_CATEGORY, - categoryName, - parentId - ); - ((SelectCategoriesActivity) requireActivity()).categoryAdded(newCategory); - - return true; + val newCategory = TermModel( + TaxonomyStore.DEFAULT_TAXONOMY_CATEGORY, + categoryName, + parentId + ) + (requireActivity() as SelectCategoriesActivity).categoryAdded(newCategory) + return true } - private void loadCategories() { - CategoryNode rootCategory = CategoryNode.createCategoryTreeFromList(mTaxonomyStore.getCategoriesForSite(mSite)); - ArrayList categoryLevels = CategoryNode.getSortedListOfCategoriesFromRoot(rootCategory); - categoryLevels.add(0, new CategoryNode(0, 0, getString(R.string.top_level_category_name))); - if (categoryLevels.size() > 0) { - ParentCategorySpinnerAdapter categoryAdapter = - new ParentCategorySpinnerAdapter(getActivity(), R.layout.categories_row_parent, categoryLevels); - mBinding.parentCategory.setAdapter(categoryAdapter); + private fun loadCategories() { + val rootCategory = CategoryNode.createCategoryTreeFromList( + mTaxonomyStore!!.getCategoriesForSite( + mSite!! + ) + ) + val categoryLevels = CategoryNode.getSortedListOfCategoriesFromRoot(rootCategory) + categoryLevels.add(0, CategoryNode(0, 0, getString(R.string.top_level_category_name))) + if (categoryLevels.size > 0) { + val categoryAdapter = + ParentCategorySpinnerAdapter( + activity, + R.layout.categories_row_parent, + categoryLevels + ) + binding!!.parentCategory.adapter = categoryAdapter } } - @Override - public void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putSerializable(WordPress.SITE, mSite); + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + outState.putSerializable(WordPress.SITE, mSite) } - @Override - public void onDestroy() { - super.onDestroy(); - mBinding = null; + override fun onDestroy() { + super.onDestroy() + binding = null + } + + companion object { + fun newInstance(site: SiteModel?): AddCategoryFragment { + val fragment = AddCategoryFragment() + val bundle = Bundle() + bundle.putSerializable(WordPress.SITE, site) + fragment.arguments = bundle + return fragment + } } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/SelectCategoriesActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/SelectCategoriesActivity.java index b0b15b3cb746..1c6c93ad2fa0 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/SelectCategoriesActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/SelectCategoriesActivity.java @@ -196,7 +196,7 @@ private void showAddCategoryFragment() { ft.addToBackStack(null); // Create and show the dialog. - AddCategoryFragment newFragment = AddCategoryFragment.newInstance(mSite); + AddCategoryFragment newFragment = AddCategoryFragment.Companion.newInstance(mSite); newFragment.show(ft, "dialog"); } From 0ebd5d5d07932c7bd1e5db01bf6f9bea11198754 Mon Sep 17 00:00:00 2001 From: Neel Doshi Date: Thu, 13 Jun 2024 17:49:18 +0530 Subject: [PATCH 3/5] Replaced !! to ? and Refactored : naming convention --- .../android/ui/posts/AddCategoryFragment.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt index 7ffb19df41ea..90ccf593c0b9 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt @@ -18,7 +18,7 @@ import org.wordpress.android.util.ToastUtils import javax.inject.Inject class AddCategoryFragment : AppCompatDialogFragment() { - private var mSite: SiteModel? = null + private var site: SiteModel? = null private var binding: AddCategoryBinding? = null @set:Inject @@ -50,7 +50,7 @@ class AddCategoryFragment : AppCompatDialogFragment() { @Suppress("Deprecation") private fun initSite(savedInstanceState: Bundle?) { - mSite = if (savedInstanceState == null) { + site = if (savedInstanceState == null) { if (arguments != null) { requireArguments().getSerializable(WordPress.SITE) as SiteModel? } else { @@ -60,7 +60,7 @@ class AddCategoryFragment : AppCompatDialogFragment() { savedInstanceState.getSerializable(WordPress.SITE) as SiteModel? } - if (mSite == null) { + if (site == null) { ToastUtils.showToast( requireActivity(), R.string.blog_not_found, @@ -71,8 +71,8 @@ class AddCategoryFragment : AppCompatDialogFragment() { } private fun addCategory(): Boolean { - val categoryName = binding!!.categoryName.text.toString() - val selectedCategory = binding!!.parentCategory.selectedItem as CategoryNode + val categoryName = binding?.categoryName?.text.toString() + val selectedCategory = binding?.parentCategory?.selectedItem as CategoryNode val parentId = selectedCategory.categoryId if (categoryName.replace(" ".toRegex(), "") == "") { @@ -92,7 +92,7 @@ class AddCategoryFragment : AppCompatDialogFragment() { private fun loadCategories() { val rootCategory = CategoryNode.createCategoryTreeFromList( mTaxonomyStore!!.getCategoriesForSite( - mSite!! + site!! ) ) val categoryLevels = CategoryNode.getSortedListOfCategoriesFromRoot(rootCategory) @@ -110,7 +110,7 @@ class AddCategoryFragment : AppCompatDialogFragment() { override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) - outState.putSerializable(WordPress.SITE, mSite) + outState.putSerializable(WordPress.SITE, site) } override fun onDestroy() { From 2ac2694a74b8adc36c5383ade9002a0f12cee35b Mon Sep 17 00:00:00 2001 From: Neel Doshi Date: Tue, 18 Jun 2024 15:52:08 +0530 Subject: [PATCH 4/5] Refactored hungarion notation to kotlin style, and replaced bang notion to ? --- .../android/ui/posts/AddCategoryFragment.kt | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt index 90ccf593c0b9..931a562f6f2f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt @@ -15,6 +15,8 @@ import org.wordpress.android.fluxc.model.TermModel import org.wordpress.android.fluxc.store.TaxonomyStore import org.wordpress.android.models.CategoryNode import org.wordpress.android.util.ToastUtils +import org.wordpress.android.util.extensions.getSerializableCompat +import org.wordpress.android.util.extensions.getSerializableExtraCompat import javax.inject.Inject class AddCategoryFragment : AppCompatDialogFragment() { @@ -22,7 +24,7 @@ class AddCategoryFragment : AppCompatDialogFragment() { private var binding: AddCategoryBinding? = null @set:Inject - var mTaxonomyStore: TaxonomyStore? = null + var taxonomyStore: TaxonomyStore? = null override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { (requireActivity().application as WordPress).component().inject(this) @@ -31,7 +33,7 @@ class AddCategoryFragment : AppCompatDialogFragment() { MaterialAlertDialogBuilder(ContextThemeWrapper(activity, R.style.PostSettingsTheme)) binding = AddCategoryBinding.inflate(layoutInflater, null, false) loadCategories() - builder.setView(binding!!.root) + builder.setView(binding?.root) .setPositiveButton(android.R.string.ok, null) .setNegativeButton(android.R.string.cancel, null) @@ -48,16 +50,15 @@ class AddCategoryFragment : AppCompatDialogFragment() { } } - @Suppress("Deprecation") private fun initSite(savedInstanceState: Bundle?) { site = if (savedInstanceState == null) { if (arguments != null) { - requireArguments().getSerializable(WordPress.SITE) as SiteModel? + requireArguments().getSerializableCompat(WordPress.SITE) as SiteModel? } else { - requireActivity().intent.getSerializableExtra(WordPress.SITE) as SiteModel? + requireActivity().intent.getSerializableExtraCompat(WordPress.SITE) as SiteModel? } } else { - savedInstanceState.getSerializable(WordPress.SITE) as SiteModel? + savedInstanceState.getSerializableCompat(WordPress.SITE) as SiteModel? } if (site == null) { @@ -72,26 +73,28 @@ class AddCategoryFragment : AppCompatDialogFragment() { private fun addCategory(): Boolean { val categoryName = binding?.categoryName?.text.toString() - val selectedCategory = binding?.parentCategory?.selectedItem as CategoryNode - val parentId = selectedCategory.categoryId + val selectedCategory = binding?.parentCategory?.selectedItem as? CategoryNode + val parentId = selectedCategory?.categoryId if (categoryName.replace(" ".toRegex(), "") == "") { - binding!!.categoryName.error = getText(R.string.cat_name_required) + binding?.categoryName?.error = getText(R.string.cat_name_required) return false } - val newCategory = TermModel( - TaxonomyStore.DEFAULT_TAXONOMY_CATEGORY, - categoryName, - parentId - ) + val newCategory = parentId?.let { + TermModel( + TaxonomyStore.DEFAULT_TAXONOMY_CATEGORY, + categoryName, + it + ) + } (requireActivity() as SelectCategoriesActivity).categoryAdded(newCategory) return true } private fun loadCategories() { val rootCategory = CategoryNode.createCategoryTreeFromList( - mTaxonomyStore!!.getCategoriesForSite( + taxonomyStore!!.getCategoriesForSite( site!! ) ) @@ -104,7 +107,7 @@ class AddCategoryFragment : AppCompatDialogFragment() { R.layout.categories_row_parent, categoryLevels ) - binding!!.parentCategory.adapter = categoryAdapter + binding?.parentCategory?.adapter = categoryAdapter } } From 4cf519e96a273e5b6a8340b9ec1373f0be6ff586 Mon Sep 17 00:00:00 2001 From: Neel Doshi Date: Wed, 19 Jun 2024 14:49:59 +0530 Subject: [PATCH 5/5] Remove !! from loadCategories() function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Irfan Ömür <2471769+irfano@users.noreply.github.com> --- .../wordpress/android/ui/posts/AddCategoryFragment.kt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt b/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt index 931a562f6f2f..c4030d07572b 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt @@ -23,8 +23,8 @@ class AddCategoryFragment : AppCompatDialogFragment() { private var site: SiteModel? = null private var binding: AddCategoryBinding? = null - @set:Inject - var taxonomyStore: TaxonomyStore? = null + @Inject + lateinit var taxonomyStore: TaxonomyStore override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { (requireActivity().application as WordPress).component().inject(this) @@ -93,11 +93,7 @@ class AddCategoryFragment : AppCompatDialogFragment() { } private fun loadCategories() { - val rootCategory = CategoryNode.createCategoryTreeFromList( - taxonomyStore!!.getCategoriesForSite( - site!! - ) - ) + val rootCategory = site?.let { CategoryNode.createCategoryTreeFromList(taxonomyStore.getCategoriesForSite(it)) } val categoryLevels = CategoryNode.getSortedListOfCategoriesFromRoot(rootCategory) categoryLevels.add(0, CategoryNode(0, 0, getString(R.string.top_level_category_name))) if (categoryLevels.size > 0) {