Skip to content

Commit

Permalink
Search products by labels and categories in recycler view (openfoodfa…
Browse files Browse the repository at this point in the history
  • Loading branch information
PrajwalM2212 authored and Karljoones committed Mar 12, 2018
1 parent 2b69ada commit e94f7fc
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
packagingProduct.setText(bold(getString(R.string.txtPackaging)));
packagingProduct.append(" ");
String[] packagings = product.getPackaging().split(",");
for(String packaging : packagings){
for (String packaging : packagings) {
packagingProduct.append(getPackagingsTag(packaging));
}
} else {
Expand Down Expand Up @@ -277,7 +277,8 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
if (tags != null && !tags.isEmpty()) {
categoryProduct.setText(bold(getString(R.string.txtCategories)));
categoryProduct.append(" ");

categoryProduct.setClickable(true);
categoryProduct.setMovementMethod(LinkMovementMethod.getInstance());
CategoryName categoryName;
String languageCode = Locale.getDefault().getLanguage();
List<String> categories = new ArrayList<>();
Expand Down Expand Up @@ -362,7 +363,7 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
storeProduct.setMovementMethod(LinkMovementMethod.getInstance());
storeProduct.append(" ");
String[] stores = product.getStores().split(",");
for (String store: stores){
for (String store : stores) {
storeProduct.append(getStoresTag(store));
}
} else {
Expand Down Expand Up @@ -537,7 +538,7 @@ private CharSequence getBrandsTag(String brand) {
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ProductBrowsingListActivity.class);
intent.putExtra("key", brand);
intent.putExtra("search_type","brand");
intent.putExtra("search_type", "brand");
startActivity(intent);
}
};
Expand All @@ -550,38 +551,37 @@ public void onClick(View view) {
}


private CharSequence getPackagingsTag(String packaging){
private CharSequence getPackagingsTag(String packaging) {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),ProductBrowsingListActivity.class);
intent.putExtra("key",packaging);
intent.putExtra("search_type","packaging");
Intent intent = new Intent(getActivity(), ProductBrowsingListActivity.class);
intent.putExtra("key", packaging);
intent.putExtra("search_type", "packaging");
startActivity(intent);
}
};
spannableStringBuilder.append(packaging);
spannableStringBuilder.setSpan(clickableSpan,0,spannableStringBuilder.length(),SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.setSpan(clickableSpan, 0, spannableStringBuilder.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.append(" ");
return spannableStringBuilder;
}



private CharSequence getStoresTag(String store){
private CharSequence getStoresTag(String store) {
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),ProductBrowsingListActivity.class);
intent.putExtra("key",store);
intent.putExtra("search_type","store");
Intent intent = new Intent(getActivity(), ProductBrowsingListActivity.class);
intent.putExtra("key", store);
intent.putExtra("search_type", "store");
startActivity(intent);
}
};
spannableStringBuilder.append(store);
spannableStringBuilder.setSpan(clickableSpan,0,spannableStringBuilder.length(),SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.setSpan(clickableSpan, 0, spannableStringBuilder.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.append(" ");
return spannableStringBuilder;
}
Expand All @@ -593,7 +593,7 @@ private CharSequence getCountryTag(String country) {
public void onClick(View view) {
Intent intent = new Intent(getActivity(), ProductBrowsingListActivity.class);
intent.putExtra("key", country);
intent.putExtra("search_type","country");
intent.putExtra("search_type", "country");
startActivity(intent);
}
};
Expand All @@ -609,8 +609,11 @@ private CharSequence getCategoriesTag(String category) {
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
CustomTabsIntent customTabsIntent = CustomTabsHelper.getCustomTabsIntent(getContext(), customTabActivityHelper.getSession());
CustomTabActivityHelper.openCustomTab(getActivity(), customTabsIntent, Uri.parse("https://world.openfoodfacts.org/category/" + category), new WebViewFallback());

Intent intent = new Intent(getActivity(), ProductBrowsingListActivity.class);
intent.putExtra("key", category);
intent.putExtra("search_type", "category");
startActivity(intent);
}
};

Expand All @@ -624,8 +627,10 @@ private CharSequence getLabelTag(String label) {
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
CustomTabsIntent customTabsIntent = CustomTabsHelper.getCustomTabsIntent(getContext(), customTabActivityHelper.getSession());
CustomTabActivityHelper.openCustomTab(getActivity(), customTabsIntent, Uri.parse("https://world.openfoodfacts.org/label/" + label), new WebViewFallback());
Intent intent = new Intent(getActivity(), ProductBrowsingListActivity.class);
intent.putExtra("key", label);
intent.putExtra("search_type", "label");
startActivity(intent);
}

};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,12 @@ public interface OnBrandCallback {
void onBrandResponse(boolean value, Search brand);
}

public interface OnStoreCallback{
void onStoreResponse(boolean value,Search store);
public interface OnStoreCallback {
void onStoreResponse(boolean value, Search store);
}

public interface OnPackagingCallback{
void onPackagingResponse(boolean value,Search packaging);
public interface OnPackagingCallback {
void onPackagingResponse(boolean value, Search packaging);
}


Expand All @@ -460,6 +460,14 @@ public interface onCountryCallback {
void onCountryResponse(boolean value, Search country);
}

public interface onLabelCallback {
void onLabelResponse(boolean value, Search label);
}

public interface onCategoryCallback {
void onCategoryResponse(boolean value, Search category);
}

/**
* Create an history product asynchronously
*/
Expand Down Expand Up @@ -701,4 +709,71 @@ public void onFailure(Call<Search> call, Throwable t) {
});

}

public void getProductsByLabel(String label, final int page, final onLabelCallback onLabelCallback) {
apiService.getProductByLabel(label, page).enqueue(new Callback<Search>() {
@Override
public void onResponse(Call<Search> call, Response<Search> response) {


if (!response.isSuccessful()) {
onLabelCallback.onLabelResponse(false, null);
return;
}

if (response.isSuccessful()) {

if (Integer.valueOf(response.body().getCount()) == 0) {
onLabelCallback.onLabelResponse(false, null);
return;
} else {
onLabelCallback.onLabelResponse(true, response.body());
}
}


}

@Override
public void onFailure(Call<Search> call, Throwable t) {

onLabelCallback.onLabelResponse(false, null);

}
});
}


public void getProductsByCategory(String category, final int page, final onCategoryCallback onCategoryCallback) {
apiService.getProductByCategory(category, page).enqueue(new Callback<Search>() {
@Override
public void onResponse(Call<Search> call, Response<Search> response) {


if (!response.isSuccessful()) {
onCategoryCallback.onCategoryResponse(false, null);
return;
}

if (response.isSuccessful()) {

if (Integer.valueOf(response.body().getCount()) == 0) {
onCategoryCallback.onCategoryResponse(false, null);
return;
} else {
onCategoryCallback.onCategoryResponse(true, response.body());
}
}


}

@Override
public void onFailure(Call<Search> call, Throwable t) {

onCategoryCallback.onCategoryResponse(false, null);

}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ Call<State> saveProduct(@Query("code") String code,
@GET("packaging/{packaging}/{page}.json")
Call<Search> getProductByPackaging(@Path("packaging") String packaging, @Path("page") int page);

@GET("label/{label}/{page}.json")
Call<Search> getProductByLabel(@Path("label") String label, @Path("page") int page);

@GET("category/{category}/{page}.json")
Call<Search> getProductByCategory(@Path("category") String category,@Path("page") int page);

@GET("language/{language}.json")
Call<Search> byLanguage(@Path("language") String language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();

typeStrings = new String[]{
"brand", "country", "additive", "search","store","packaging"
"brand", "country", "additive", "search", "store", "packaging", "label" , "category"
};

searchType = extras.getString("search_type");
Expand All @@ -101,12 +101,22 @@ protected void onCreate(Bundle savedInstanceState) {
break;
}
case "store": {
getSupportActionBar().setSubtitle("Store");
getSupportActionBar().setSubtitle(getString(R.string.store_string));
break;
}

case "packaging": {
getSupportActionBar().setSubtitle("Packaging");
getSupportActionBar().setSubtitle(getString(R.string.packaging_string));
break;
}

case "label": {
getSupportActionBar().setSubtitle(getString(R.string.label_string));
break;
}

case "category": {
getSupportActionBar().setSubtitle(getString(R.string.category_string));
break;
}
}
Expand Down Expand Up @@ -185,6 +195,24 @@ public void onProductsResponse(boolean isOk, Search searchResponse, int countPro
}
});
}

case "label": {
api.getProductsByLabel(key, pageAddress, new OpenFoodAPIClient.onLabelCallback() {
@Override
public void onLabelResponse(boolean value, Search label) {
loadData(value, label);
}
});
}

case "category": {
api.getProductsByCategory(key, pageAddress, new OpenFoodAPIClient.onCategoryCallback() {
@Override
public void onCategoryResponse(boolean value, Search label) {
loadData(value, label);
}
});
}
}
}

Expand All @@ -194,7 +222,7 @@ private void loadData(boolean isResponseOk, Search response) {
if (isResponseOk) {
mCountProducts = Integer.parseInt(response.getCount());
if (pageAddress == 1) {
countProductsView.append(" "+NumberFormat.getInstance(getResources().getConfiguration().locale).format(Long.parseLong(response.getCount()
countProductsView.append(" " + NumberFormat.getInstance(getResources().getConfiguration().locale).format(Long.parseLong(response.getCount()
)));
mProducts = new ArrayList<>();
mProducts.addAll(response.getProducts());
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -474,5 +474,9 @@
<string name="search_string">Search</string>
<string name="country_string">Countries where sold</string>
<string name="last_seen_string">%1$d %2$s ago</string>
<string name="store_string">Store</string>
<string name="packaging_string">Packaging</string>
<string name="label_string">Label</string>
<string name="category_string">Category</string>

</resources>

0 comments on commit e94f7fc

Please sign in to comment.