Skip to content

Commit

Permalink
ref: chores and IDE analysis
Browse files Browse the repository at this point in the history
fix: fixed ProductsAPITest.kt
  • Loading branch information
VaiTon committed Aug 4, 2021
1 parent c43c63a commit 97653bd
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 148 deletions.
9 changes: 8 additions & 1 deletion app/src/main/AndroidManifest.xml
Expand Up @@ -74,7 +74,8 @@

<provider
android:name=".utils.SearchSuggestionProvider"
android:authorities="${applicationId}.utils.SearchSuggestionProvider" />
android:authorities="${applicationId}.utils.SearchSuggestionProvider"
android:exported="false" />

<provider
android:name="androidx.startup.InitializationProvider"
Expand All @@ -94,6 +95,7 @@
<activity
android:name=".features.splash.SplashActivity"
android:label="@string/app_name"
android:exported="true"
android:theme="@style/SplashTheme">

<intent-filter>
Expand All @@ -109,6 +111,7 @@
<activity
android:name=".features.MainActivity"
android:launchMode="singleTop"
android:exported="true"
android:windowSoftInputMode="stateUnchanged|adjustResize">

<intent-filter>
Expand Down Expand Up @@ -136,6 +139,7 @@
android:name=".features.product.view.ProductViewActivity"
android:launchMode="singleTop"
android:parentActivityName=".features.MainActivity"
android:exported="true"
android:windowSoftInputMode="stateUnchanged|adjustResize">

<meta-data
Expand Down Expand Up @@ -189,6 +193,7 @@
<activity
android:name=".features.categories.activity.CategoryActivity"
android:launchMode="singleTop"
android:exported="true"
android:parentActivityName=".features.MainActivity">

<intent-filter>
Expand All @@ -212,6 +217,7 @@

<activity
android:name=".features.search.ProductSearchActivity"
android:exported="true"
android:theme="@style/OFFTheme.NoActionBar">
<intent-filter
android:autoVerify="true"
Expand Down Expand Up @@ -275,6 +281,7 @@
<activity
android:name=".features.additives.AdditiveListActivity"
android:launchMode="singleTop"
android:exported="true"
android:parentActivityName=".features.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
Expand Down
Expand Up @@ -219,7 +219,7 @@ class AllergensAlertFragment : NavigationBaseFragment() {
}

companion object {
private val LOG_TAG = this::class.simpleName
private val LOG_TAG = AllergensAlertFragment::class.simpleName

@JvmStatic
fun newInstance() = AllergensAlertFragment().apply { arguments = Bundle() }
Expand Down
Expand Up @@ -121,7 +121,7 @@ class ProductCompareAdapter(
ActivityCompat.requestPermissions(activity, arrayOf(permission.CAMERA), MY_PERMISSIONS_REQUEST_CAMERA)
}
else -> {
imageReturnedPosition = position
imageReturnedPosition = holder.bindingAdapterPosition
if (isHardwareCameraInstalled(activity)) {
EasyImage.openCamera(activity, 0)
} else {
Expand Down
Expand Up @@ -363,7 +363,7 @@ class EditOverviewFragment : ProductEditFragment() {
binding.btnEditImgFront.visibility = View.INVISIBLE
picasso
.load(imageFrontUrl)
.resize(requireContext().dpsToPixel(50).toInt(), requireContext().dpsToPixel(50).toInt())
.resize(requireContext().dpsToPixel(50), requireContext().dpsToPixel(50))
.centerInside()
.into(binding.imgFront, object : Callback {
override fun onSuccess() = frontImageLoaded()
Expand Down
Expand Up @@ -22,7 +22,6 @@ import javax.inject.Inject

/**
* @author prajwalm
* @see R.layout.fragment_product_photos
*/
@AndroidEntryPoint
class ProductPhotosFragment : BaseFragment() {
Expand Down
Expand Up @@ -3,7 +3,7 @@ package openfoodfacts.github.scrachx.openfood.images
import com.fasterxml.jackson.databind.JsonNode

/**
* @param this@extractImagesNameSortedByUploadTimeDesc json representing images entries given by api/v0/product/XXXX.json?fields=images
* @receiver json representing images entries given by api/v0/product/XXXX.json?fields=images
*/
internal fun JsonNode.extractImagesNameSortedByUploadTimeDesc(): List<String> {
// a json object referring to images
Expand Down
Expand Up @@ -63,7 +63,7 @@ class OpenFoodAPIClient @Inject constructor(
): ProductState {
sentryAnalytics.setBarcode(barcode)
return withContext(IO) {
rawApi.getProductByBarcode(barcode, fields, localeManager.getLanguage(), getUserAgent(userAgent)).await()
rawApi.getProductByBarcode(barcode, fields, localeManager.getLanguage(), getUserAgent(userAgent))
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ class OpenFoodAPIClient @Inject constructor(
fields,
localeManager.getLanguage(),
getUserAgent(Utils.HEADER_USER_AGENT_SEARCH)
).await()
)
}

// TODO: This is not part of the client, move it to another class (preferably a utility class)
Expand All @@ -138,7 +138,7 @@ class OpenFoodAPIClient @Inject constructor(
getAllFields(),
localeManager.getLanguage(),
getUserAgent(Utils.HEADER_USER_AGENT_SEARCH)
).await()
)
} catch (err: Exception) {
when (err) {
is IOException -> Toast.makeText(activity, R.string.something_went_wrong, Toast.LENGTH_LONG).show()
Expand All @@ -159,7 +159,6 @@ class OpenFoodAPIClient @Inject constructor(
}

/**
* @param barcode
* @return a list of product ingredients (can be empty)
*/
suspend fun getIngredients(product: Product) = withContext(IO) {
Expand Down Expand Up @@ -359,7 +358,7 @@ class OpenFoodAPIClient @Inject constructor(
fields,
localeManager.getLanguage(),
getUserAgent(Utils.HEADER_USER_AGENT_SEARCH)
).await()
)

if (state.status == 0L) throw IOException("Could not sync history. Error with product ${state.code} ")
else {
Expand Down
Expand Up @@ -46,12 +46,12 @@ interface ProductsAPI {
}

@GET("$API_P/product/{barcode}.json")
fun getProductByBarcode(
suspend fun getProductByBarcode(
@Path("barcode") barcode: String,
@Query("fields") fields: String,
@Query("lc") locale: String,
@Header("User-Agent") header: String
): Single<ProductState>
): ProductState

/**
* @param barcodes String of comma separated barcodes
Expand Down Expand Up @@ -198,46 +198,46 @@ interface ProductsAPI {
): Search

@GET("language/{language}.json")
fun getProductsByLanguage(@Path("language") language: String): Single<Search>
suspend fun getProductsByLanguage(@Path("language") language: String): Search

@GET("label/{label}.json")
fun getProductsByLabel(@Path("label") label: String): Single<Search>
suspend fun getProductsByLabel(@Path("label") label: String): Search

@GET("category/{category}.json")
fun getProductsByCategory(@Path("category") category: String): Single<Search>
suspend fun getProductsByCategory(@Path("category") category: String): Search

@GET("state/{state}.json")
fun getProductsByState(
suspend fun getProductsByState(
@Path("state") state: String,
@Query("fields") fields: String
): Single<Search>
): Search

@GET("packaging/{packaging}.json")
fun getProductsByPackaging(@Path("packaging") packaging: String): Single<Search>
suspend fun getProductsByPackaging(@Path("packaging") packaging: String): Search

@GET("brand/{brand}.json")
fun getProductsByBrand(@Path("brand") brand: String): Single<Search>
suspend fun getProductsByBrand(@Path("brand") brand: String): Search

@GET("purchase-place/{purchasePlace}.json")
fun getProductsByPurchasePlace(@Path("purchasePlace") purchasePlace: String): Single<Search>
suspend fun getProductsByPurchasePlace(@Path("purchasePlace") purchasePlace: String): Search

@GET("store/{store}.json")
fun getProductsByStore(@Path("store") store: String): Single<Search>
suspend fun getProductsByStore(@Path("store") store: String): Search

@GET("country/{country}.json")
fun byCountry(@Path("country") country: String): Single<Search>
suspend fun byCountry(@Path("country") country: String): Search

@GET("trace/{trace}.json")
fun getProductsByTrace(@Path("trace") trace: String): Single<Search>
suspend fun getProductsByTrace(@Path("trace") trace: String): Search

@GET("packager-code/{packager_code}.json")
fun getProductsByPackagerCode(@Path("packager_code") packagerCode: String): Single<Search>
suspend fun getProductsByPackagerCode(@Path("packager_code") packagerCode: String): Search

@GET("city/{city}.json")
fun getProducsByCity(@Path("city") city: String): Single<Search>
suspend fun getProductsByCity(@Path("city") city: String): Search

@GET("nutrition-grade/{nutriscore}.json")
fun getProductsByNutriScore(@Path("nutriscore") nutritionGrade: String): Single<Search>
suspend fun getProductsByNutriScore(@Path("nutriscore") nutritionGrade: String): Search

@GET("nutrient-level/{nutrient_level}.json")
fun byNutrientLevel(@Path("nutrient_level") nutrientLevel: String): Single<Search>
Expand Down Expand Up @@ -314,7 +314,7 @@ interface ProductsAPI {
fun getProductsByPeriodAfterOpening(@Path("PeriodAfterOpening") periodAfterOpening: String): Call<Search>

@GET("ingredient/{ingredient}.json")
fun getProductsByIngredient(@Path("ingredient") ingredient: String): Single<Search>
suspend fun getProductsByIngredient(@Path("ingredient") ingredient: String): Search

/**
* This method gives a list of incomplete products
Expand Down
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import openfoodfacts.github.scrachx.openfood.BuildConfig
import openfoodfacts.github.scrachx.openfood.utils.Utils
Expand All @@ -28,7 +29,7 @@ class TaxonomiesManager @Inject constructor(
* @return The timestamp of the last changes date of the taxonomy.json on the server
* or [TAXONOMY_NO_INTERNET] if there is no connection to the server.
*/
private suspend fun <T> getLastModifiedDateFromServer(taxonomy: Taxonomy<T>) = withContext(Dispatchers.IO) {
private suspend fun <T> getLastModifiedDateFromServer(taxonomy: Taxonomy<T>) = withContext(IO) {
var lastModifiedDate: Long
val taxoUrl = URL(BuildConfig.OFWEBSITE + taxonomy.jsonUrl)
try {
Expand Down Expand Up @@ -85,7 +86,7 @@ class TaxonomiesManager @Inject constructor(
private suspend fun <T> download(
taxonomy: Taxonomy<T>,
productRepository: ProductRepository
) = withContext(Dispatchers.IO) {
) = withContext(IO) {
val lastMod = getLastModifiedDateFromServer(taxonomy)

return@withContext if (lastMod != TAXONOMY_NO_INTERNET)
Expand All @@ -97,7 +98,7 @@ class TaxonomiesManager @Inject constructor(
taxonomy: Taxonomy<T>,
localDownloadTime: Long,
productRepository: ProductRepository
) = withContext(Dispatchers.IO) {
) = withContext(IO) {
val lastModRemote = getLastModifiedDateFromServer(taxonomy)

if (lastModRemote == 0L || lastModRemote > localDownloadTime)
Expand Down
@@ -1,11 +1,12 @@
package openfoodfacts.github.scrachx.openfood.utils

import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences
import androidx.core.content.edit

class PrefManager(context: Context) {
private val pref: SharedPreferences = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE)
private val pref: SharedPreferences = context.getSharedPreferences(PREF_NAME, MODE_PRIVATE)

// First time launch
var isFirstTimeLaunch: Boolean
Expand All @@ -28,7 +29,6 @@ class PrefManager(context: Context) {
get() = pref.getLong(FIRST_TIME_LAUNCH_TIME, System.currentTimeMillis())

companion object {
private const val PRIVATE_MODE = 0
private const val PREF_NAME = "open-facts-welcome"
private const val IS_FIRST_TIME_LAUNCH = "IsFirstTimeLaunch"
private const val FIRST_TIME_LAUNCH_TIME = "FirstTimeLaunchTime"
Expand Down
Expand Up @@ -28,7 +28,7 @@ class CategoryNameTest {
}

@Test
fun getWikiDataIdWithoutQuote_returnsWholeWikiDataId(): Unit {
fun getWikiDataIdWithoutQuote_returnsWholeWikiDataId() {
val fakeWikiDataId = "ThisOneIncludesenButNotAQuote"
mCategoryName.wikiDataId = fakeWikiDataId
assertThat(mCategoryName.wikiDataId).isEqualTo(fakeWikiDataId)
Expand Down

0 comments on commit 97653bd

Please sign in to comment.