-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20980 from neeldoshii/UI-Enhancement-`AddCategory…
…Fragment` `AddCategoryFragment.java` to `AddCategoryFragment.kt`
- Loading branch information
Showing
3 changed files
with
130 additions
and
140 deletions.
There are no files selected for viewing
139 changes: 0 additions & 139 deletions
139
WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.java
This file was deleted.
Oops, something went wrong.
129 changes: 129 additions & 0 deletions
129
WordPress/src/main/java/org/wordpress/android/ui/posts/AddCategoryFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
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 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 | ||
|
||
@Inject | ||
lateinit var taxonomyStore: TaxonomyStore | ||
|
||
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 fun onStart() { | ||
super.onStart() | ||
val dialog = requireDialog() as AlertDialog | ||
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener { | ||
if (addCategory()) { | ||
dismiss() | ||
} | ||
} | ||
} | ||
|
||
private fun initSite(savedInstanceState: Bundle?) { | ||
site = if (savedInstanceState == null) { | ||
if (arguments != null) { | ||
requireArguments().getSerializableCompat(WordPress.SITE) as SiteModel? | ||
} else { | ||
requireActivity().intent.getSerializableExtraCompat(WordPress.SITE) as SiteModel? | ||
} | ||
} else { | ||
savedInstanceState.getSerializableCompat(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 | ||
val parentId = selectedCategory?.categoryId | ||
|
||
if (categoryName.replace(" ".toRegex(), "") == "") { | ||
binding?.categoryName?.error = getText(R.string.cat_name_required) | ||
return false | ||
} | ||
|
||
val newCategory = parentId?.let { | ||
TermModel( | ||
TaxonomyStore.DEFAULT_TAXONOMY_CATEGORY, | ||
categoryName, | ||
it | ||
) | ||
} | ||
(requireActivity() as SelectCategoriesActivity).categoryAdded(newCategory) | ||
return true | ||
} | ||
|
||
private fun loadCategories() { | ||
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) { | ||
val categoryAdapter = | ||
ParentCategorySpinnerAdapter( | ||
activity, | ||
R.layout.categories_row_parent, | ||
categoryLevels | ||
) | ||
binding?.parentCategory?.adapter = categoryAdapter | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters