app/src/main/java/openfoodfacts/github/scrachx/openfood/fragments/AllergensAlertFragment.java
/**
     * Add an allergen to be checked for when browsing products.
     */
Add an allergen to be checked for when browsing products.
Add an allergen to be checked for when browsing products .
@OnClick(R.id.btn_add)
    protected void onAddAllergens() {
        if (mAllergensEnabled != null && mAllergensFromDao != null && mAllergensFromDao.size() > 0) {
            productRepository.getAllergensByEnabledAndLanguageCode(false, LocaleHelper.getLanguage(getContext()))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((List<AllergenName> allergens) -> {
                        Collections.sort(allergens, new Comparator<AllergenName>() {
                            @Override
                            public int compare(AllergenName a1, AllergenName a2) {
                                return a1.getName().compareToIgnoreCase(a2.getName());
                            }
                        });
                        List<String> allergensNames = new ArrayList<String>();
                        for (AllergenName allergenName : allergens) {
                            allergensNames.add(allergenName.getName());
                        }
                        new MaterialDialog.Builder(mView.getContext())
                                .title(R.string.title_dialog_alert)
                                .items(allergensNames)
                                .itemsCallback((dialog, view, position, text) -> {
                                    productRepository.setAllergenEnabled(allergens.get(position).getAllergenTag(), true);
                                    mAllergensEnabled.add(allergens.get(position));
                                    mAdapter.notifyItemInserted(mAllergensEnabled.size() - 1);
                                    mRvAllergens.scrollToPosition(mAdapter.getItemCount() - 1);
                                })
                                .show();
                    }, Throwable::printStackTrace);
        } else {
            ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
            boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
            if (isConnected) {
                final LoadToast lt = new LoadToast(getContext());
                lt.setText(getContext().getString(R.string.toast_retrieving));
                lt.setBackgroundColor(getContext().getResources().getColor(R.color.blue));
                lt.setTextColor(getContext().getResources().getColor(R.color.white));
                lt.show();
                final SharedPreferences.Editor editor = mSettings.edit();
                productRepository.getAllergens(true)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .toObservable()
                        .subscribe(allergens -> {
                            editor.putBoolean("errorAllergens", false).apply();
                            productRepository.saveAllergens(allergens);
                            mAdapter.setAllergens(mAllergensEnabled);
                            mAdapter.notifyDataSetChanged();
                            updateAllergenDao();
                            onAddAllergens();
                            lt.success();
                        }, e -> {
                            editor.putBoolean("errorAllergens", true).apply();
                            lt.error();
                        });
            } else {
                new MaterialDialog.Builder(mView.getContext())
                        .title(R.string.title_dialog_alert)
                        .content(R.string.info_download_data_connection)
                        .neutralText(R.string.txtOk)
                        .show();
            }
        }
    }
@ OnClick ( R . id . btn_add ) protected void onAddAllergens ( ) { if ( mAllergensEnabled != null && mAllergensFromDao != null && mAllergensFromDao . size ( ) > 0 ) { productRepository . getAllergensByEnabledAndLanguageCode ( false , LocaleHelper . getLanguage ( getContext ( ) ) ) . subscribeOn ( Schedulers . io ( ) ) . observeOn ( AndroidSchedulers . mainThread ( ) ) . subscribe ( ( List < AllergenName > allergens ) -> { Collections . sort ( allergens , new Comparator < AllergenName > ( ) { @ Override public int compare ( AllergenName a1 , AllergenName a2 ) { return a1 . getName ( ) . compareToIgnoreCase ( a2 . getName ( ) ) ; } } ) ; List < String > allergensNames = new ArrayList < String > ( ) ; for ( AllergenName allergenName : allergens ) { allergensNames . add ( allergenName . getName ( ) ) ; } new MaterialDialog . Builder ( mView . getContext ( ) ) . title ( R . string . title_dialog_alert ) . items ( allergensNames ) . itemsCallback ( ( dialog , view , position , text ) -> { productRepository . setAllergenEnabled ( allergens . get ( position ) . getAllergenTag ( ) , true ) ; mAllergensEnabled . add ( allergens . get ( position ) ) ; mAdapter . notifyItemInserted ( mAllergensEnabled . size ( ) - 1 ) ; mRvAllergens . scrollToPosition ( mAdapter . getItemCount ( ) - 1 ) ; } ) . show ( ) ; } , Throwable :: printStackTrace ) ; } else { ConnectivityManager cm = ( ConnectivityManager ) getActivity ( ) . getSystemService ( Context . CONNECTIVITY_SERVICE ) ; NetworkInfo activeNetwork = cm . getActiveNetworkInfo ( ) ; boolean isConnected = activeNetwork != null && activeNetwork . isConnectedOrConnecting ( ) ; if ( isConnected ) { final LoadToast lt = new LoadToast ( getContext ( ) ) ; lt . setText ( getContext ( ) . getString ( R . string . toast_retrieving ) ) ; lt . setBackgroundColor ( getContext ( ) . getResources ( ) . getColor ( R . color . blue ) ) ; lt . setTextColor ( getContext ( ) . getResources ( ) . getColor ( R . color . white ) ) ; lt . show ( ) ; final SharedPreferences . Editor editor = mSettings . edit ( ) ; productRepository . getAllergens ( true ) . subscribeOn ( Schedulers . io ( ) ) . observeOn ( AndroidSchedulers . mainThread ( ) ) . toObservable ( ) . subscribe ( allergens -> { editor . putBoolean ( "errorAllergens" , false ) . apply ( ) ; productRepository . saveAllergens ( allergens ) ; mAdapter . setAllergens ( mAllergensEnabled ) ; mAdapter . notifyDataSetChanged ( ) ; updateAllergenDao ( ) ; onAddAllergens ( ) ; lt . success ( ) ; } , e -> { editor . putBoolean ( "errorAllergens" , true ) . apply ( ) ; lt . error ( ) ; } ) ; } else { new MaterialDialog . Builder ( mView . getContext ( ) ) . title ( R . string . title_dialog_alert ) . content ( R . string . info_download_data_connection ) . neutralText ( R . string . txtOk ) . show ( ) ; } } }
Add an allergen to be checked for when browsing products.
Add an allergen to be checked for when browsing products .