-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'trunk' into issue/connecting-shipping-rates
- Loading branch information
Showing
33 changed files
with
814 additions
and
350 deletions.
There are no files selected for viewing
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
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
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
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
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
94 changes: 94 additions & 0 deletions
94
...src/main/kotlin/com/woocommerce/android/ui/products/inventory/FetchProductByIdentifier.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,94 @@ | ||
package com.woocommerce.android.ui.products.inventory | ||
|
||
import com.woocommerce.android.model.Product | ||
import com.woocommerce.android.ui.orders.creation.CheckDigitRemoverFactory | ||
import com.woocommerce.android.ui.orders.creation.GoogleBarcodeFormatMapper | ||
import com.woocommerce.android.ui.products.list.ProductListRepository | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
import org.wordpress.android.fluxc.store.WCProductStore | ||
import javax.inject.Inject | ||
|
||
class FetchProductByIdentifier @Inject constructor( | ||
private val productRepository: ProductListRepository, | ||
private val checkDigitRemoverFactory: CheckDigitRemoverFactory, | ||
) { | ||
suspend operator fun invoke( | ||
codeScannerResultCode: String, | ||
codeScannerResultFormat: GoogleBarcodeFormatMapper.BarcodeFormat | ||
): Result<Product> = coroutineScope { | ||
val globalUniqueIdentifierSearch = async { | ||
searchProductByGlobalUniqueIdentifier( | ||
codeScannerResultCode, | ||
codeScannerResultFormat | ||
) | ||
} | ||
val skuSearch = async { searchProductBySku(codeScannerResultCode, codeScannerResultFormat) } | ||
|
||
val product = globalUniqueIdentifierSearch.await() ?: skuSearch.await() | ||
|
||
if (product != null) { | ||
Result.success(product) | ||
} else { | ||
Result.failure(Exception("Product not found")) | ||
} | ||
} | ||
|
||
private suspend fun searchProductBySku( | ||
codeScannerResultCode: String, | ||
codeScannerResultFormat: GoogleBarcodeFormatMapper.BarcodeFormat | ||
): Product? { | ||
val product = productRepository.searchProductList( | ||
searchQuery = codeScannerResultCode, | ||
skuSearchOptions = WCProductStore.SkuSearchOptions.ExactSearch | ||
)?.firstOrNull() | ||
?: removeCheckDigitIfPossible( | ||
codeScannerResultCode = codeScannerResultCode, | ||
codeScannerResultFormat = codeScannerResultFormat | ||
)?.let { | ||
productRepository.searchProductList( | ||
searchQuery = it, | ||
skuSearchOptions = WCProductStore.SkuSearchOptions.ExactSearch | ||
)?.firstOrNull() | ||
} | ||
return product | ||
} | ||
|
||
private suspend fun searchProductByGlobalUniqueIdentifier( | ||
codeScannerResultCode: String, | ||
codeScannerResultFormat: GoogleBarcodeFormatMapper.BarcodeFormat | ||
): Product? { | ||
val product = productRepository.searchProductListByGlobalUniqueId( | ||
globalUniqueId = codeScannerResultCode | ||
)?.firstOrNull() ?: removeCheckDigitIfPossible( | ||
codeScannerResultCode = codeScannerResultCode, | ||
codeScannerResultFormat = codeScannerResultFormat | ||
)?.let { | ||
productRepository.searchProductListByGlobalUniqueId( | ||
globalUniqueId = codeScannerResultCode | ||
)?.firstOrNull() | ||
} | ||
|
||
return product | ||
} | ||
|
||
private fun removeCheckDigitIfPossible( | ||
codeScannerResultCode: String, | ||
codeScannerResultFormat: GoogleBarcodeFormatMapper.BarcodeFormat | ||
): String? { | ||
if (codeScannerResultFormat.isEAN() || codeScannerResultFormat.isUPC()) { | ||
return checkDigitRemoverFactory.getCheckDigitRemoverFor(codeScannerResultFormat) | ||
.getSKUWithoutCheckDigit(codeScannerResultCode) | ||
} | ||
|
||
return null | ||
} | ||
|
||
private fun GoogleBarcodeFormatMapper.BarcodeFormat.isUPC() = | ||
this == GoogleBarcodeFormatMapper.BarcodeFormat.FormatUPCA || | ||
this == GoogleBarcodeFormatMapper.BarcodeFormat.FormatUPCE | ||
|
||
private fun GoogleBarcodeFormatMapper.BarcodeFormat.isEAN() = | ||
this == GoogleBarcodeFormatMapper.BarcodeFormat.FormatEAN13 || | ||
this == GoogleBarcodeFormatMapper.BarcodeFormat.FormatEAN8 | ||
} |
46 changes: 0 additions & 46 deletions
46
...mmerce/src/main/kotlin/com/woocommerce/android/ui/products/inventory/FetchProductBySKU.kt
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.