Search in sources :

Example 1 with InvalidLocaleException

use of org.edx.mobile.util.InvalidLocaleException in project edx-app-android by edx.

the class EditUserProfileFragment method setData.

public void setData(@Nullable final Account account, @Nullable FormDescription formDescription) {
    if (null == viewHolder) {
        return;
    }
    if (null == account || null == formDescription) {
        viewHolder.content.setVisibility(View.GONE);
        viewHolder.loadingIndicator.setVisibility(View.VISIBLE);
    } else {
        viewHolder.content.setVisibility(View.VISIBLE);
        viewHolder.loadingIndicator.setVisibility(View.GONE);
        viewHolder.changePhoto.setEnabled(!account.requiresParentalConsent());
        viewHolder.profileImage.setBorderColorResource(viewHolder.changePhoto.isEnabled() ? R.color.edx_brand_primary_base : R.color.edx_brand_gray_accent);
        if (account.getProfileImage().hasImage()) {
            Glide.with(viewHolder.profileImage.getContext()).load(account.getProfileImage().getImageUrlLarge()).into(viewHolder.profileImage);
        } else {
            Glide.with(EditUserProfileFragment.this).load(R.drawable.profile_photo_placeholder).into(viewHolder.profileImage);
        }
        final Gson gson = new GsonBuilder().serializeNulls().create();
        final JsonObject obj = (JsonObject) gson.toJsonTree(account);
        final boolean isLimited = account.getAccountPrivacy() != Account.Privacy.ALL_USERS || account.requiresParentalConsent();
        final LayoutInflater layoutInflater = LayoutInflater.from(viewHolder.fields.getContext());
        viewHolder.fields.removeAllViews();
        for (final FormField field : formDescription.getFields()) {
            if (null == field.getFieldType()) {
                // Missing field type; ignore this field
                continue;
            }
            switch(field.getFieldType()) {
                case SWITCH:
                    {
                        if (field.getOptions().getValues().size() != 2) {
                            // We expect to have exactly two options; ignore this field.
                            continue;
                        }
                        final boolean isAccountPrivacyField = field.getName().equals(Account.ACCOUNT_PRIVACY_SERIALIZED_NAME);
                        String value = gson.fromJson(obj.get(field.getName()), String.class);
                        if (isAccountPrivacyField && null == value || account.requiresParentalConsent()) {
                            value = Account.PRIVATE_SERIALIZED_NAME;
                        }
                        createSwitch(layoutInflater, viewHolder.fields, field, value, account.requiresParentalConsent() ? getString(R.string.profile_consent_needed_explanation) : field.getInstructions(), isAccountPrivacyField ? account.requiresParentalConsent() : isLimited, new SwitchListener() {

                            @Override
                            public void onSwitch(@NonNull String value) {
                                executeUpdate(field, value);
                            }
                        });
                        break;
                    }
                case SELECT:
                case TEXTAREA:
                    {
                        final String value;
                        final String text;
                        {
                            final JsonElement accountField = obj.get(field.getName());
                            if (null == accountField) {
                                value = null;
                                text = null;
                            } else if (null == field.getDataType()) {
                                // No data type is specified, treat as generic string
                                value = gson.fromJson(accountField, String.class);
                                text = value;
                            } else {
                                switch(field.getDataType()) {
                                    case COUNTRY:
                                        value = gson.fromJson(accountField, String.class);
                                        try {
                                            text = TextUtils.isEmpty(value) ? null : LocaleUtils.getCountryNameFromCode(value);
                                        } catch (InvalidLocaleException e) {
                                            continue;
                                        }
                                        break;
                                    case LANGUAGE:
                                        final List<LanguageProficiency> languageProficiencies = gson.fromJson(accountField, new TypeToken<List<LanguageProficiency>>() {
                                        }.getType());
                                        value = languageProficiencies.isEmpty() ? null : languageProficiencies.get(0).getCode();
                                        try {
                                            text = value == null ? null : LocaleUtils.getLanguageNameFromCode(value);
                                        } catch (InvalidLocaleException e) {
                                            continue;
                                        }
                                        break;
                                    default:
                                        // Unknown data type; ignore this field
                                        continue;
                                }
                            }
                        }
                        final String displayValue;
                        if (TextUtils.isEmpty(text)) {
                            final String placeholder = field.getPlaceholder();
                            if (TextUtils.isEmpty(placeholder)) {
                                displayValue = viewHolder.fields.getResources().getString(R.string.edit_user_profile_field_placeholder);
                            } else {
                                displayValue = placeholder;
                            }
                        } else {
                            displayValue = text;
                        }
                        createField(layoutInflater, viewHolder.fields, field, displayValue, isLimited && !field.getName().equals(Account.YEAR_OF_BIRTH_SERIALIZED_NAME), new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                startActivityForResult(FormFieldActivity.newIntent(getActivity(), field, value), EDIT_FIELD_REQUEST);
                            }
                        });
                        break;
                    }
                default:
                    {
                        // Unknown field type; ignore this field
                        break;
                    }
            }
        }
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) SpannableString(android.text.SpannableString) InvalidLocaleException(org.edx.mobile.util.InvalidLocaleException) CircleImageView(de.hdodenhof.circleimageview.CircleImageView) View(android.view.View) TextView(android.widget.TextView) IconImageView(com.joanzapata.iconify.widget.IconImageView) JsonElement(com.google.gson.JsonElement) LayoutInflater(android.view.LayoutInflater) NonNull(android.support.annotation.NonNull) List(java.util.List) FormField(org.edx.mobile.user.FormField)

Aggregations

NonNull (android.support.annotation.NonNull)1 SpannableString (android.text.SpannableString)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 TextView (android.widget.TextView)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 IconImageView (com.joanzapata.iconify.widget.IconImageView)1 CircleImageView (de.hdodenhof.circleimageview.CircleImageView)1 List (java.util.List)1 FormField (org.edx.mobile.user.FormField)1 InvalidLocaleException (org.edx.mobile.util.InvalidLocaleException)1