Skip to content

Commit

Permalink
Refactored hungarion notation to kotlin style, and replaced bang noti…
Browse files Browse the repository at this point in the history
…on to ?
  • Loading branch information
neeldoshii committed Jun 18, 2024
1 parent 0ebd5d5 commit 2ac2694
Showing 1 changed file with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ 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() {
private var site: SiteModel? = null
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)
Expand All @@ -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)

Expand All @@ -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) {
Expand All @@ -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!!
)
)
Expand All @@ -104,7 +107,7 @@ class AddCategoryFragment : AppCompatDialogFragment() {
R.layout.categories_row_parent,
categoryLevels
)
binding!!.parentCategory.adapter = categoryAdapter
binding?.parentCategory?.adapter = categoryAdapter
}
}

Expand Down

0 comments on commit 2ac2694

Please sign in to comment.