Skip to content

WCProductCategoryModel to Room #13996

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

Open
wants to merge 10 commits into
base: migrate_product_variation_to_room_tests
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowSnackbar
import com.woocommerce.android.viewmodel.fixedHiltNavGraphViewModels
import com.woocommerce.android.widgets.CustomProgressDialog
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.runBlocking
import org.wordpress.android.util.ActivityUtils
import javax.inject.Inject

Expand Down Expand Up @@ -111,7 +112,7 @@ class AddProductCategoryFragment :
}

with(binding.productCategoryParent) {
viewModel.getSelectedParentCategoryName()?.let { setText(it) }
runBlocking { viewModel.getSelectedParentCategoryName()?.let { setText(it) } }
setClickListener {
val action = AddProductCategoryFragmentDirections
.actionAddProductCategoryFragmentToParentCategoryListFragment(
Expand Down Expand Up @@ -163,7 +164,7 @@ class AddProductCategoryFragment :
}
}
new.selectedParentId.takeIfNotEqualTo(old?.selectedParentId) {
val parentCategoryName = viewModel.getSelectedParentCategoryName()
val parentCategoryName = runBlocking { viewModel.getSelectedParentCategoryName() }
if (parentCategoryName != null) {
binding.productCategoryParent.setHtmlText(parentCategoryName)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class AddProductCategoryViewModel @Inject constructor(

fun getSelectedParentId() = addProductCategoryViewState.selectedParentId ?: 0L

fun getSelectedParentCategoryName(): String? =
suspend fun getSelectedParentCategoryName(): String? =
productCategoriesRepository.getProductCategoryByRemoteId(getSelectedParentId())?.name

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,22 @@ class ProductCategoriesRepository @Inject constructor(
/**
* Returns all product categories for the current site that are in the database
*/
fun getProductCategoriesList(): List<ProductCategory> {
suspend fun getProductCategoriesList(): List<ProductCategory> {
return productStore.getProductCategoriesForSite(selectedSite.get())
.map { it.toProductCategory() }
}

fun getProductCategoryByRemoteId(remoteId: Long) =
suspend fun getProductCategoryByRemoteId(remoteId: Long) =
productStore.getProductCategoryByRemoteId(selectedSite.get(), remoteId)

fun getProductCategoryByNameAndParentId(categoryName: String, parentId: Long): ProductCategory? =
productStore.getProductCategoryByNameAndParentId(selectedSite.get(), categoryName, parentId)
?.toProductCategory()

suspend fun addProductCategories(categories: List<ProductCategory>): Result<List<ProductCategory>> {
val result = productStore.addProductCategories(
site = selectedSite.get(),
categories = categories.map {
WCProductCategoryModel().apply {
name = it.name
WCProductCategoryModel(
name = it.name,
parent = it.parentId
}
)
}
)

Expand All @@ -115,10 +111,10 @@ class ProductCategoriesRepository @Inject constructor(
suspend fun addProductCategory(categoryName: String, parentId: Long): Result<ProductCategory> {
val result = productStore.addProductCategory(
site = selectedSite.get(),
category = WCProductCategoryModel().apply {
name = categoryName
category = WCProductCategoryModel(
name = categoryName,
parent = parentId
}
)
)
return when {
result.isError -> {
Expand All @@ -136,11 +132,11 @@ class ProductCategoriesRepository @Inject constructor(
suspend fun updateProductCategory(remoteId: Long, categoryName: String, parentId: Long): Result<ProductCategory> {
val result = productStore.updateProductCategory(
site = selectedSite.get(),
category = WCProductCategoryModel().apply {
remoteCategoryId = remoteId
name = categoryName
category = WCProductCategoryModel(
remoteCategoryId = remoteId,
name = categoryName,
parent = parentId
}
)
)
return when {
result.isError -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class GetCategoriesByIdsTest : BaseUnitTest() {
}

private val databaseCategories = List(3) { n ->
WCProductCategoryModel().apply {
localSiteId = 3
remoteCategoryId = n.toLong()
name = "category $n"
}
WCProductCategoryModel(
localSiteId = 3,
remoteCategoryId = n.toLong(),
name = "category $n",
)
}

private var productCategories = databaseCategories.map { it.toProductCategory() }
Expand Down
Loading