Skip to content

Return the category from the API result rather than getting it from the DB #11614

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

Merged
Merged
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.woocommerce.android.ui.products.categories

import android.os.Parcelable
import android.text.TextUtils
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
Expand Down Expand Up @@ -137,7 +136,6 @@ class AddProductCategoryViewModel @Inject constructor(

launch {
if (networkStatus.isConnected()) {
val categoryNameTrimmed = TextUtils.htmlEncode(categoryName.trim())
val requestResult = when {
addProductCategoryViewState.isEditingMode -> updateProductCategory(categoryName, parentId)
else -> addNewProductCategory(categoryName, parentId)
Expand All @@ -149,12 +147,10 @@ class AddProductCategoryViewModel @Inject constructor(
else -> R.string.add_product_category_success
}
triggerEvent(ShowSnackbar(successString))
val addedCategory = productCategoriesRepository
.getProductCategoryByNameAndParentId(categoryNameTrimmed, parentId)
Comment on lines -152 to -153
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We already have the new or edited category from the API result, so I think we can safely remove this access to the database, which is the one leading to the crash. The code is now null safe.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch!

val action =
if (addProductCategoryViewState.isEditingMode) UpdateAction.Update else UpdateAction.Add
triggerEvent(
ExitWithResult(CategoryUpdateResult(addedCategory!!, action))
ExitWithResult(CategoryUpdateResult(it, action))
)
}
.onFailure {
Expand Down
Loading