Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddCategoryFragment.java to AddCategoryFragment.kt #20980

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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 site: SiteModel? = null
private var binding: AddCategoryBinding? = null

@set:Inject
var mTaxonomyStore: TaxonomyStore? = null
irfano marked this conversation as resolved.
Show resolved Hide resolved

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)
irfano marked this conversation as resolved.
Show resolved Hide resolved
.setPositiveButton(android.R.string.ok, null)
.setNegativeButton(android.R.string.cancel, null)

return builder.create()
}

override fun onStart() {
super.onStart()
val dialog = requireDialog() as AlertDialog
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
if (addCategory()) {
dismiss()
}
}
}

@Suppress("Deprecation")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't add any new @Suppress when migrating to Kotlin. You can use Bundle.getSerializableCompat() for getSerializable(). Check usages of it in other Kotlin classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in the latest commit 👍

private fun initSite(savedInstanceState: Bundle?) {
site = if (savedInstanceState == null) {
if (arguments != null) {
requireArguments().getSerializable(WordPress.SITE) as SiteModel?
} else {
requireActivity().intent.getSerializableExtra(WordPress.SITE) as SiteModel?
}
} else {
savedInstanceState.getSerializable(WordPress.SITE) as SiteModel?
}

if (site == null) {
ToastUtils.showToast(
requireActivity(),
R.string.blog_not_found,
ToastUtils.Duration.SHORT
)
parentFragmentManager.popBackStack()
}
}

private fun addCategory(): Boolean {
val categoryName = binding?.categoryName?.text.toString()
val selectedCategory = binding?.parentCategory?.selectedItem as CategoryNode
irfano marked this conversation as resolved.
Show resolved Hide resolved
val parentId = selectedCategory.categoryId

if (categoryName.replace(" ".toRegex(), "") == "") {
binding!!.categoryName.error = getText(R.string.cat_name_required)
irfano marked this conversation as resolved.
Show resolved Hide resolved
return false
}

val newCategory = TermModel(
TaxonomyStore.DEFAULT_TAXONOMY_CATEGORY,
categoryName,
parentId
)
(requireActivity() as SelectCategoriesActivity).categoryAdded(newCategory)
return true
}

private fun loadCategories() {
val rootCategory = CategoryNode.createCategoryTreeFromList(
mTaxonomyStore!!.getCategoriesForSite(
site!!
irfano marked this conversation as resolved.
Show resolved Hide resolved
)
)
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
irfano marked this conversation as resolved.
Show resolved Hide resolved
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putSerializable(WordPress.SITE, site)
}

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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Loading