Skip to content

Commit

Permalink
fix: fixed tagline request
Browse files Browse the repository at this point in the history
fix: fixed historyproduct not being added

ref: warn instead of error on HomeFragment.kt

fix: LoginActivity with null arg
  • Loading branch information
VaiTon committed Apr 18, 2021
1 parent 732777e commit 1b39563
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 29 deletions.
Expand Up @@ -198,7 +198,7 @@ class HomeFragment : NavigationBaseFragment() {
productsApi.getTagline(getUserAgent())
.subscribeOn(Schedulers.io()) // io for network
.observeOn(AndroidSchedulers.mainThread()) // Move to main thread for UI changes
.doOnError { Log.e(LOG_TAG, "Could not retrieve tag-line from server.", it) }
.doOnError { Log.w(LOG_TAG, "Could not retrieve tag-line from server.", it) }
.subscribe { tagLines ->
val appLanguage = localeManager.getLanguage()
var isLanguageFound = false
Expand Down
Expand Up @@ -240,8 +240,8 @@ class LoginActivity : BaseActivity() {
}

companion object {
class LoginContract : ActivityResultContract<Unit, Boolean>() {
override fun createIntent(context: Context, input: Unit) = Intent(context, LoginActivity::class.java)
class LoginContract : ActivityResultContract<Unit?, Boolean>() {
override fun createIntent(context: Context, input: Unit?) = Intent(context, LoginActivity::class.java)

override fun parseResult(resultCode: Int, intent: Intent?) = resultCode == RESULT_OK
}
Expand Down
Expand Up @@ -26,7 +26,6 @@ import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContract
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.afollestad.materialdialogs.MaterialDialog
Expand Down Expand Up @@ -313,14 +312,14 @@ class ProductEditActivity : BaseActivity() {
productDetails[ApiFields.Keys.IMAGE_NUTRITION_UPLOADED] = true.toString()
}
val barcode = this@ProductEditActivity.productDetails[ApiFields.Keys.BARCODE]!!
val toSaveOfflineProduct = OfflineSavedProduct(
val toSaveOffline = OfflineSavedProduct(
barcode,
this@ProductEditActivity.productDetails
)
daoSession.offlineSavedProductDao!!.insertOrReplace(toSaveOfflineProduct)
daoSession.offlineSavedProductDao!!.insertOrReplace(toSaveOffline)

scheduleSync(this, sharedPreferences)
daoSession.historyProductDao.addToHistorySync(toSaveOfflineProduct)
daoSession.historyProductDao.addToHistorySync(toSaveOffline)

Toast.makeText(this, R.string.productSavedToast, Toast.LENGTH_SHORT).show()
hideKeyboard(this)
Expand Down
Expand Up @@ -623,7 +623,10 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
* @param editTextView EditText with spinner for entering the nutrients
* @param targetMap map to enter the nutrient value received from edit texts
*/
private fun addNutrientToMap(editTextView: CustomValidatingEditTextView, targetMap: MutableMap<String, String?>) {
private fun addNutrientToMap(
editTextView: CustomValidatingEditTextView,
targetMap: MutableMap<String, String?>
) {
// For impl reference, see https://wiki.openfoodfacts.org/Nutrients_handling_in_Open_Food_Facts#Data_display
val fieldName = getCompleteEntryName(editTextView)

Expand Down Expand Up @@ -770,10 +773,9 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
*/
private fun convertToGrams(value: Float, index: Int): Float {
val unit = NUTRIENTS_UNITS[index]
//can't be converted to grams.
return if (UNIT_DV == unit || UNIT_IU == unit) {
0F
} else UnitUtils.convertToGrams(value, unit)
// Can't be converted to grams.
return if (UNIT_DV == unit || UNIT_IU == unit) 0F
else UnitUtils.convertToGrams(value, unit)
}

private fun isCarbohydrateRelated(editText: CustomValidatingEditTextView): Boolean {
Expand All @@ -794,17 +796,18 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {

var carbsValue = binding.carbohydrates.getFloatValueOr(0f)
var sugarValue = binding.sugars.getFloatValueOr(0f)
// check that value of (sugar + starch) is not greater than value of carbohydrates
//convert all the values to grams

// Check that value of (sugar + starch) is not greater than value of carbohydrates
// Convert all the values to grams
carbsValue = convertToGrams(carbsValue, binding.carbohydrates.unitSpinner!!.selectedItemPosition)
sugarValue = convertToGrams(sugarValue, binding.sugars.unitSpinner!!.selectedItemPosition)

val newStarch = convertToGrams(starchValue, starchUnitSelectedIndex).toDouble()

return if (sugarValue + newStarch > carbsValue) {
binding.carbohydrates.showError(getString(R.string.error_in_carbohydrate_value))
ValueState.NOT_VALID
} else {
ValueState.VALID
}
} else ValueState.VALID
}

/**
Expand Down
Expand Up @@ -215,7 +215,9 @@ class OpenFoodAPIClient @Inject constructor(
/**
* Add a product to ScanHistory asynchronously
*/
fun addToHistory(product: Product) = Completable.fromAction { daoSession.historyProductDao.addToHistorySync(product, localeManager.getLanguage()) }
fun addToHistory(product: Product) = Completable.fromAction {
daoSession.historyProductDao.addToHistorySync(product, localeManager.getLanguage())
}

fun getProductsByContributor(contributor: String, page: Int) =
rawApi.getProductsByContributor(contributor, page, fieldsToFetchFacets).subscribeOn(Schedulers.io())
Expand Down Expand Up @@ -413,19 +415,24 @@ class OpenFoodAPIClient @Inject constructor(
companion object {
val MIME_TEXT: MediaType = MediaType.get("text/plain")
const val PNG_EXT = ".png"
fun HistoryProductDao.addToHistorySync(product: OfflineSavedProduct) {
val savedProduct = queryBuilder().where(HistoryProductDao.Properties.Barcode.eq(product.barcode)).uniqueOrThrow()
val details = product.productDetails

fun HistoryProductDao.addToHistorySync(newProd: OfflineSavedProduct) {
val savedProduct: HistoryProduct? =
queryBuilder().where(HistoryProductDao.Properties.Barcode.eq(newProd.barcode)).unique()
val details = newProd.productDetails

val hp = HistoryProduct(
product.name,
newProd.name,
details[Keys.ADD_BRANDS],
product.imageFrontLocalUrl,
product.barcode,
newProd.imageFrontLocalUrl,
newProd.barcode,
details[Keys.QUANTITY],
null,
null,
null
details[Keys.NUTRITION_GRADE_FR],
details[Keys.ECOSCORE],
details[Keys.NOVA_GROUPS
]
)

if (savedProduct != null) hp.id = savedProduct.id
insertOrReplace(hp)
}
Expand Down
Expand Up @@ -20,7 +20,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode
import io.reactivex.Single
import okhttp3.RequestBody
import okhttp3.ResponseBody
import openfoodfacts.github.scrachx.openfood.BuildConfig.FLAVOR
import openfoodfacts.github.scrachx.openfood.BuildConfig.FLAVOR_versionCode
import openfoodfacts.github.scrachx.openfood.models.ProductState
import openfoodfacts.github.scrachx.openfood.models.Search
import openfoodfacts.github.scrachx.openfood.models.TagLineLanguage
Expand Down Expand Up @@ -331,7 +331,7 @@ interface ProductsAPI {
/**
* This method gives the news in all languages
*/
@GET("/files/tagline/tagline-$FLAVOR.json")
@GET("/files/tagline/tagline-$FLAVOR_versionCode.json")
fun getTagline(@Header("User-Agent") header: String): Single<ArrayList<TagLineLanguage>>

/**
Expand Down

0 comments on commit 1b39563

Please sign in to comment.