Skip to content

Commit

Permalink
fix: NPE in WikidataAPIClient
Browse files Browse the repository at this point in the history
Closes OPENFOODFACTS-ANDROID-41S
  • Loading branch information
VaiTon committed Aug 7, 2021
1 parent 093e271 commit ec8a0da
Show file tree
Hide file tree
Showing 9 changed files with 236 additions and 215 deletions.
Expand Up @@ -58,12 +58,12 @@ object AdditiveFragmentHelper {
* Returns additive tag from additive name using WikidataApiClient
*
* @param additive name of the additive
* @param apiClientForWikiData object of WikidataApiClient
* @param wikidataClient object of WikidataApiClient
* @param fragment holds a reference to the calling fragment
*/
private fun getAdditiveTag(
additive: AdditiveName,
apiClientForWikiData: WikiDataApiClient,
wikidataClient: WikiDataApiClient,
fragment: BaseFragment,
lifecycleOwner: LifecycleOwner
): CharSequence {
Expand All @@ -72,7 +72,7 @@ object AdditiveFragmentHelper {
override fun onClick(view: View) {
if (additive.isWikiDataIdPresent) {
lifecycleOwner.lifecycleScope.launch {
val result = apiClientForWikiData.doSomeThing(additive.wikiDataId)
val result = wikidataClient.getEntityData(additive.wikiDataId)
getOnWikiResponse(activity, additive)(result)
}
} else {
Expand Down
Expand Up @@ -63,7 +63,7 @@ object CategoryProductHelper {
override fun onClick(view: View) {
if (category.isWikiDataIdPresent == true) {
fragment.lifecycleScope.launch {
val result = category.wikiDataId?.let { apiClient.doSomeThing(it) }
val result = category.wikiDataId?.let { apiClient.getEntityData(it) }
if (result != null) {
val activity = fragment.activity
if (activity != null && !activity.isFinishing) {
Expand Down

Large diffs are not rendered by default.

Expand Up @@ -327,7 +327,7 @@ class IngredientsProductFragment : BaseFragment() {
override fun onClick(view: View) {
if (allergen.isWikiDataIdPresent) {
lifecycleScope.launch {
val result = wikidataClient.doSomeThing(
val result = wikidataClient.getEntityData(
allergen.wikiDataId
)
val activity = activity
Expand Down
Expand Up @@ -762,7 +762,7 @@ class SummaryProductFragment : BaseFragment(), ISummaryProductPresenter.View {
override fun onClick(view: View) {
if (label.isWikiDataIdPresent) {
lifecycleScope.launch {
val result = wikidataClient.doSomeThing(label.wikiDataId)
val result = wikidataClient.getEntityData(label.wikiDataId)
val activity = activity
if (activity?.isFinishing == false) {
showBottomSheet(result, label, activity.supportFragmentManager)
Expand Down
Expand Up @@ -13,12 +13,15 @@ import javax.inject.Singleton
*/
@Singleton
class WikiDataApiClient @Inject constructor(
private val wikidataAPI: WikidataAPI
private val wikidataAPI: WikidataAPI
) {
/**
* Get json response of the WikiData for additive/ingredient/category/label using their WikiDataID
*
* @param code WikiData ID of additive/ingredient/category/label
* @param entityId WikiData ID of additive/ingredient/category/label
*/
suspend fun doSomeThing(code: String): JsonNode = wikidataAPI.getWikiCategory(code)["entities"][code]
suspend fun getEntityData(entityId: String): JsonNode {
require(entityId[0] == 'Q') { "Entity ID should start with 'Q'. Got: $entityId" }
return wikidataAPI.getEntity(entityId)["entities"][entityId]
}
}
Expand Up @@ -9,6 +9,6 @@ import retrofit2.http.Path
* Get method to get json response from wikidata server.
*/
interface WikidataAPI {
@GET("{code}.json")
suspend fun getWikiCategory(@Path("code") code: String): ObjectNode
@GET("{id}.json")
suspend fun getEntity(@Path("id") entityId: String): ObjectNode
}
@@ -1,6 +1,5 @@
package openfoodfacts.github.scrachx.openfood.utils

import android.util.Log
import com.fasterxml.jackson.databind.JsonNode

object DeserializerHelper {
Expand All @@ -21,11 +20,10 @@ object DeserializerHelper {
*
* @param namesNode namesNode in Json response
*/
fun extractMapFromJsonNode(namesNode: JsonNode): Map<String, String> {
val names = hashMapOf<String, String>()
namesNode.fields().forEach { names[it.key] = it.value.asText() }
return names
}
fun extractMapFromJsonNode(namesNode: JsonNode) = namesNode.fields()
.asSequence()
.map { it.key to it.value.asText() }
.toMap()

/**
* Extracts child nodes from a map of subnodes
Expand All @@ -34,10 +32,6 @@ object DeserializerHelper {
* @param key get the JsonNode for the given key
*/
fun extractChildNodeAsText(subNode: Map.Entry<String?, JsonNode>, key: String?) =
subNode.value[key]?.toList()?.map {
if (Log.isLoggable(LOG_TAG, Log.INFO)) Log.i(LOG_TAG, "ExtractChildNodeAsText, ajout de ${it.asText()}")
it.asText()
} ?: listOf()
subNode.value[key]?.toList()?.map { it.asText() } ?: listOf()

private val LOG_TAG = DeserializerHelper::class.simpleName!!
}
187 changes: 97 additions & 90 deletions app/src/main/res/layout/fragment_product_attribute_details.xml
@@ -1,111 +1,118 @@
<?xml version="1.0" encoding="utf-8"?>

<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
<data>

</data>

<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
style="@style/ButtonFlat.Green"
<LinearLayout
android:layout_width="match_parent"
android:contentDescription="@string/app_name"
android:gravity="center"
android:padding="@dimen/padding_short"
android:scaleType="center"
android:text="@string/wikidata_information"
android:textAlignment="center"
android:textSize="@dimen/font_large" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
style="@style/ButtonFlat.Green"
android:layout_width="match_parent"
android:contentDescription="@string/app_name"
android:gravity="center"
android:padding="@dimen/padding_short"
android:scaleType="center"
android:text="@string/wikidata_information"
android:textAlignment="center"
android:textSize="@dimen/font_large" />

<LinearLayout
android:id="@+id/titleContainer"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_tiny"
android:background="@color/grey_50"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:padding="@dimen/spacing_small">
android:layout_height="wrap_content">

<LinearLayout
android:id="@+id/titleContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_tiny"
android:background="@color/grey_50"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal"
android:padding="@dimen/spacing_small">

<TextView
android:id="@+id/titleBottomSheet"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center"
android:text="@string/title_activity_product"
android:textAlignment="center"
android:textSize="@dimen/font_large"
android:textStyle="bold" />

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/titleBottomSheetIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>


<TextView
android:id="@+id/titleBottomSheet"
android:layout_width="0dp"
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_below="@+id/titleContainer"
android:layout_margin="@dimen/spacing_tiny"
android:background="@color/grey_50"
android:gravity="center"
android:text="@string/title_activity_product"
android:padding="@dimen/spacing_small"
android:text="@string/description_activity_product"
android:textAlignment="center"
android:textSize="@dimen/font_large"
android:textStyle="bold"/>
android:textSize="@dimen/font_normal" />

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/titleBottomSheetIcon"
android:layout_width="wrap_content"
<include
android:id="@+id/exposureEvalTable"
layout="@layout/exposure_eval_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/description"
android:visibility="gone" />
</LinearLayout>


<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/titleContainer"
android:layout_margin="@dimen/spacing_tiny"
android:background="@color/grey_50"
android:gravity="center"
android:padding="@dimen/spacing_small"
android:text="@string/description_activity_product"
android:textAlignment="center"
android:textSize="@dimen/font_normal"/>

<include
android:id="@+id/exposureEvalTable"
layout="@layout/exposure_eval_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/description"
android:visibility="gone"/>

<LinearLayout
android:id="@+id/buttonsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/exposureEvalTable"
android:layout_marginBottom="@dimen/spacing_small"
android:gravity="center"
android:weightSum="100">

<Button
android:id="@+id/wikipediaButton"
style="@style/ButtonFlat"
android:layout_width="0dp"
<LinearLayout
android:id="@+id/buttonsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_normal"
android:layout_weight="50"
android:text="@string/Wikipedia"
android:textStyle="normal" />
android:layout_below="@id/exposureEvalTable"
android:layout_marginBottom="@dimen/spacing_small"
android:gravity="center"
android:weightSum="100">

<Button
android:id="@+id/wikipediaButton"
style="@style/ButtonFlat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_normal"
android:layout_weight="50"
android:text="@string/Wikipedia"
android:textStyle="normal" />

<Button
android:id="@+id/buttonToBrowseProducts"
style="@style/ButtonFlat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_normal"
android:layout_weight="50"
android:text="@string/Browse_Products"
android:textStyle="normal" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

<Button
android:id="@+id/buttonToBrowseProducts"
style="@style/ButtonFlat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_normal"
android:layout_weight="50"
android:text="@string/Browse_Products"
android:textStyle="normal" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</layout>

0 comments on commit ec8a0da

Please sign in to comment.