Search in sources :

Example 1 with Icon

use of com.joanzapata.iconify.Icon in project edx-app-android by edx.

the class ParsingUtil method recursivePrepareSpannableIndexes.

// Analyse the text and replace {} blocks with the appropriate character
// Retain all transformations in the accumulator
private static void recursivePrepareSpannableIndexes(@NonNull TextView targetView, @NonNull String fullText, @NonNull SpannableStringBuilder text, @NonNull @Size(min = 1) List<IconFontDescriptorWrapper> iconFontDescriptors, @IntRange(from = 0) int start) {
    // Try to find a {...} in the string and extract expression from it
    String stringText = text.toString();
    int startIndex = stringText.indexOf("{", start);
    if (startIndex == -1)
        return;
    int endIndex = stringText.indexOf("}", startIndex) + 1;
    String expression = stringText.substring(startIndex + 1, endIndex - 1);
    // Split the expression and retrieve the icon key
    String[] strokes = expression.split(" ");
    String key = strokes[0];
    // Loop through the descriptors to find a key match
    IconFontDescriptorWrapper iconFontDescriptor = null;
    Icon icon = null;
    for (int i = 0; i < iconFontDescriptors.size(); i++) {
        iconFontDescriptor = iconFontDescriptors.get(i);
        icon = iconFontDescriptor.getIcon(key);
        if (icon != null)
            break;
    }
    // If no match, ignore and continue
    if (icon == null) {
        recursivePrepareSpannableIndexes(targetView, fullText, text, iconFontDescriptors, endIndex);
        return;
    }
    // See if any more stroke within {} should be applied
    Context context = targetView.getContext();
    float iconSizePx = -1;
    int iconColor = Integer.MAX_VALUE;
    float iconSizeRatio = -1;
    Animation animation = Animation.NONE;
    boolean baselineAligned = false;
    for (int i = 1; i < strokes.length; i++) {
        String stroke = strokes[i];
        // Look for "spin"
        if (stroke.equalsIgnoreCase("spin")) {
            animation = Animation.SPIN;
        } else // Look for "pulse"
        if (stroke.equalsIgnoreCase("pulse")) {
            animation = Animation.PULSE;
        } else // Look for "baseline"
        if (stroke.equalsIgnoreCase("baseline")) {
            baselineAligned = true;
        } else // Look for an icon size
        if (stroke.matches("([0-9]*(\\.[0-9]*)?)dp")) {
            iconSizePx = dpToPx(context, Float.valueOf(stroke.substring(0, stroke.length() - 2)));
        } else if (stroke.matches("([0-9]*(\\.[0-9]*)?)sp")) {
            iconSizePx = spToPx(context, Float.valueOf(stroke.substring(0, stroke.length() - 2)));
        } else if (stroke.matches("([0-9]*)px")) {
            iconSizePx = Integer.valueOf(stroke.substring(0, stroke.length() - 2));
        } else if (stroke.matches("@dimen/(.*)")) {
            iconSizePx = getPxFromDimen(context, context.getPackageName(), stroke.substring(7));
            if (iconSizePx < 0)
                throw new IllegalArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
        } else if (stroke.matches("@android:dimen/(.*)")) {
            iconSizePx = getPxFromDimen(context, ANDROID_PACKAGE_NAME, stroke.substring(15));
            if (iconSizePx < 0)
                throw new IllegalArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
        } else if (stroke.matches("([0-9]*(\\.[0-9]*)?)%")) {
            iconSizeRatio = Float.valueOf(stroke.substring(0, stroke.length() - 1)) / 100f;
        } else // Look for an icon color
        if (stroke.matches("#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})")) {
            iconColor = Color.parseColor(stroke);
        } else if (stroke.matches("@color/(.*)")) {
            iconColor = getColorFromResource(context, context.getPackageName(), stroke.substring(7));
            if (iconColor == Integer.MAX_VALUE)
                throw new IllegalArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
        } else if (stroke.matches("@android:color/(.*)")) {
            iconColor = getColorFromResource(context, ANDROID_PACKAGE_NAME, stroke.substring(15));
            if (iconColor == Integer.MAX_VALUE)
                throw new IllegalArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
        } else {
            throw new IllegalArgumentException("Unknown expression " + stroke + " in \"" + fullText + "\"");
        }
    }
    // Replace the character and apply the typeface
    text = text.replace(startIndex, endIndex, "" + icon.character());
    text.setSpan(new CustomTypefaceSpan(targetView, icon, iconFontDescriptor.getTypeface(context), iconSizePx, iconSizeRatio, iconColor, animation, baselineAligned), startIndex, startIndex + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    recursivePrepareSpannableIndexes(targetView, fullText, text, iconFontDescriptors, startIndex);
}
Also used : Context(android.content.Context) Icon(com.joanzapata.iconify.Icon)

Example 2 with Icon

use of com.joanzapata.iconify.Icon in project android-iconify by JoanZapata.

the class ParsingUtil method recursivePrepareSpannableIndexes.

private static void recursivePrepareSpannableIndexes(Context context, String fullText, SpannableStringBuilder text, List<IconFontDescriptorWrapper> iconFontDescriptors, int start) {
    // Try to find a {...} in the string and extract expression from it
    String stringText = text.toString();
    int startIndex = stringText.indexOf("{", start);
    if (startIndex == -1)
        return;
    int endIndex = stringText.indexOf("}", startIndex) + 1;
    if (endIndex == -1)
        return;
    String expression = stringText.substring(startIndex + 1, endIndex - 1);
    // Split the expression and retrieve the icon key
    String[] strokes = expression.split(" ");
    String key = strokes[0];
    // Loop through the descriptors to find a key match
    IconFontDescriptorWrapper iconFontDescriptor = null;
    Icon icon = null;
    for (int i = 0; i < iconFontDescriptors.size(); i++) {
        iconFontDescriptor = iconFontDescriptors.get(i);
        icon = iconFontDescriptor.getIcon(key);
        if (icon != null)
            break;
    }
    // If no match, ignore and continue
    if (icon == null) {
        recursivePrepareSpannableIndexes(context, fullText, text, iconFontDescriptors, endIndex);
        return;
    }
    // See if any more stroke within {} should be applied
    float iconSizePx = -1;
    int iconColor = Integer.MAX_VALUE;
    float iconSizeRatio = -1;
    boolean spin = false;
    boolean baselineAligned = false;
    for (int i = 1; i < strokes.length; i++) {
        String stroke = strokes[i];
        // Look for "spin"
        if (stroke.equalsIgnoreCase("spin")) {
            spin = true;
        } else // Look for "baseline"
        if (stroke.equalsIgnoreCase("baseline")) {
            baselineAligned = true;
        } else // Look for an icon size
        if (stroke.matches("([0-9]*(\\.[0-9]*)?)dp")) {
            iconSizePx = dpToPx(context, Float.valueOf(stroke.substring(0, stroke.length() - 2)));
        } else if (stroke.matches("([0-9]*(\\.[0-9]*)?)sp")) {
            iconSizePx = spToPx(context, Float.valueOf(stroke.substring(0, stroke.length() - 2)));
        } else if (stroke.matches("([0-9]*)px")) {
            iconSizePx = Integer.valueOf(stroke.substring(0, stroke.length() - 2));
        } else if (stroke.matches("@dimen/(.*)")) {
            iconSizePx = getPxFromDimen(context, context.getPackageName(), stroke.substring(7));
            if (iconSizePx < 0)
                throw new IllegalArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
        } else if (stroke.matches("@android:dimen/(.*)")) {
            iconSizePx = getPxFromDimen(context, ANDROID_PACKAGE_NAME, stroke.substring(15));
            if (iconSizePx < 0)
                throw new IllegalArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
        } else if (stroke.matches("([0-9]*(\\.[0-9]*)?)%")) {
            iconSizeRatio = Float.valueOf(stroke.substring(0, stroke.length() - 1)) / 100f;
        } else // Look for an icon color
        if (stroke.matches("#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})")) {
            iconColor = Color.parseColor(stroke);
        } else if (stroke.matches("@color/(.*)")) {
            iconColor = getColorFromResource(context, context.getPackageName(), stroke.substring(7));
            if (iconColor == Integer.MAX_VALUE)
                throw new IllegalArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
        } else if (stroke.matches("@android:color/(.*)")) {
            iconColor = getColorFromResource(context, ANDROID_PACKAGE_NAME, stroke.substring(15));
            if (iconColor == Integer.MAX_VALUE)
                throw new IllegalArgumentException("Unknown resource " + stroke + " in \"" + fullText + "\"");
        } else {
            throw new IllegalArgumentException("Unknown expression " + stroke + " in \"" + fullText + "\"");
        }
    }
    // Replace the character and apply the typeface
    text = text.replace(startIndex, endIndex, "" + icon.character());
    text.setSpan(new CustomTypefaceSpan(icon, iconFontDescriptor.getTypeface(context), iconSizePx, iconSizeRatio, iconColor, spin, baselineAligned), startIndex, startIndex + 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    recursivePrepareSpannableIndexes(context, fullText, text, iconFontDescriptors, startIndex);
}
Also used : Icon(com.joanzapata.iconify.Icon)

Example 3 with Icon

use of com.joanzapata.iconify.Icon in project edx-app-android by edx.

the class ErrorNotification method showError.

/**
 * Show the error notification with the message appropriate for the provided error.
 *
 * @param context         The Context, to be used for checking connectivity status.
 * @param error           The error that occurred while attempting to retrieve from or deliver to the
 *                        remote server. This may be an {@link IOException} if the request failed due to a
 *                        network failure, an {HttpResponseStatusException} if the failure was due to
 *                        receiving an error code, or any {@link Throwable} implementation if one was
 *                        thrown unexpectedly while creating the request or processing the response.
 * @param actionTextResId The resource ID of the action button text.
 * @param actionListener  The callback to be invoked when the action button is clicked.
 */
public void showError(@NonNull final Context context, @NonNull final Throwable error, @StringRes int actionTextResId, @Nullable View.OnClickListener actionListener) {
    @StringRes final int errorResId = ErrorUtils.getErrorMessageRes(context, error, this);
    final Icon icon = ErrorUtils.getErrorIcon(error);
    if (errorResId == R.string.app_version_unsupported) {
        actionTextResId = R.string.label_update;
        actionListener = AppStoreUtils.OPEN_APP_IN_APP_STORE_CLICK_LISTENER;
    }
    showError(errorResId, icon, actionTextResId, actionListener);
}
Also used : StringRes(android.support.annotation.StringRes) Icon(com.joanzapata.iconify.Icon)

Example 4 with Icon

use of com.joanzapata.iconify.Icon in project edx-app-android by edx.

the class UserProfileFragment method createView.

@NonNull
@Override
protected UserProfilePresenter.ViewInterface createView() {
    viewHolder = DataBindingUtil.getBinding(getView());
    snackbarErrorNotification = new SnackbarErrorNotification(viewHolder.getRoot());
    viewHolder.profileSectionPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            onChildScrollingPreferenceChanged();
        }
    });
    final StaticFragmentPagerAdapter adapter = createTabAdapter();
    viewHolder.profileSectionPager.setAdapter(adapter);
    viewHolder.profileSectionTabs.setupWithViewPager(viewHolder.profileSectionPager);
    return new UserProfilePresenter.ViewInterface() {

        @Override
        public void setEditProfileMenuButtonVisible(boolean visible) {
            setHasOptionsMenu(visible);
        }

        @Override
        public void showProfile(@NonNull UserProfileViewModel profile) {
            if (profile.limitedProfileMessage == UserProfileViewModel.LimitedProfileMessage.NONE) {
                viewHolder.sharingLimited.setVisibility(View.GONE);
            } else {
                viewHolder.sharingLimited.setVisibility(View.VISIBLE);
                viewHolder.sharingLimited.setText(profile.limitedProfileMessage == UserProfileViewModel.LimitedProfileMessage.OWN_PROFILE ? R.string.profile_sharing_limited_by_you : R.string.profile_sharing_limited_by_other_user);
            }
            if (TextUtils.isEmpty(profile.language)) {
                viewHolder.languageContainer.setVisibility(View.GONE);
            } else {
                viewHolder.languageText.setText(profile.language);
                viewHolder.languageText.setContentDescription(ResourceUtil.getFormattedString(getResources(), R.string.profile_language_description, "language", profile.language));
                viewHolder.languageContainer.setVisibility(View.VISIBLE);
            }
            if (TextUtils.isEmpty(profile.location)) {
                viewHolder.locationContainer.setVisibility(View.GONE);
            } else {
                viewHolder.locationText.setText(profile.location);
                viewHolder.locationText.setContentDescription(ResourceUtil.getFormattedString(getResources(), R.string.profile_location_description, "location", profile.location));
                viewHolder.locationContainer.setVisibility(View.VISIBLE);
            }
            viewHolder.contentLoadingIndicator.getRoot().setVisibility(View.GONE);
            viewHolder.contentError.getRoot().setVisibility(View.GONE);
            viewHolder.profileBodyContent.setVisibility(View.VISIBLE);
            viewHolder.profileHeader.setVisibility(View.VISIBLE);
            onFinish();
        }

        @Override
        public void showLoading() {
            ((AppBarLayout.LayoutParams) viewHolder.profileHeader.getLayoutParams()).setScrollFlags(0);
            viewHolder.profileBody.setBackgroundColor(getResources().getColor(R.color.edx_brand_gray_x_back));
            viewHolder.profileSectionTabs.setVisibility(View.GONE);
            viewHolder.contentError.getRoot().setVisibility(View.GONE);
            viewHolder.profileBodyContent.setVisibility(View.GONE);
            viewHolder.contentLoadingIndicator.getRoot().setVisibility(View.VISIBLE);
            viewHolder.profileHeader.setVisibility(View.GONE);
        }

        @Override
        public void showError(@NonNull Throwable error) {
            ((AppBarLayout.LayoutParams) viewHolder.profileHeader.getLayoutParams()).setScrollFlags(0);
            viewHolder.profileBody.setBackgroundColor(getResources().getColor(R.color.edx_brand_gray_x_back));
            viewHolder.profileSectionTabs.setVisibility(View.GONE);
            viewHolder.contentLoadingIndicator.getRoot().setVisibility(View.GONE);
            viewHolder.profileBodyContent.setVisibility(View.GONE);
            final Icon errorIcon = ErrorUtils.getErrorIcon(error);
            viewHolder.contentError.getRoot().setVisibility(View.VISIBLE);
            if (errorIcon != null) {
                viewHolder.contentError.contentErrorIcon.setIcon(errorIcon);
            }
            viewHolder.contentError.contentErrorText.setText(ErrorUtils.getErrorMessage(error, getContext()));
            viewHolder.contentError.contentErrorAction.setText(R.string.lbl_reload);
            viewHolder.contentError.contentErrorAction.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (NetworkUtil.isConnected(getContext())) {
                        onRefresh();
                    }
                }
            });
            viewHolder.contentError.contentErrorAction.setVisibility(View.VISIBLE);
            viewHolder.profileHeader.setVisibility(View.GONE);
            onFinish();
        }

        @Override
        public void showTabs(@NonNull List<UserProfileTab> tabs) {
            adapter.setItems(pagerItemsFromProfileTabs(tabs, getResources()));
            viewHolder.profileSectionTabs.setVisibility(tabs.size() < 2 ? View.GONE : View.VISIBLE);
        }

        @Override
        public void setPhotoImage(@NonNull UserProfileImageViewModel model) {
            if (null == model.uri) {
                Glide.with(UserProfileFragment.this).load(R.drawable.profile_photo_placeholder).into(viewHolder.profileImage);
            } else if (model.shouldReadFromCache) {
                Glide.with(UserProfileFragment.this).load(model.uri).into(viewHolder.profileImage);
            } else {
                Glide.with(UserProfileFragment.this).load(model.uri).skipMemoryCache(// URI is re-used in subsequent events; disable caching
                true).diskCacheStrategy(DiskCacheStrategy.NONE).into(viewHolder.profileImage);
            }
        }

        @Override
        public void setUsername(@NonNull String username) {
            viewHolder.nameText.setText(username);
            viewHolder.nameText.setContentDescription(ResourceUtil.getFormattedString(getResources(), R.string.profile_username_description, "username", username));
        }

        @Override
        public void navigateToProfileEditor(@NonNull String username) {
            router.showUserProfileEditor(getActivity(), username);
        }

        private void onFinish() {
            if (!EventBus.getDefault().isRegistered(UserProfileFragment.this)) {
                EventBus.getDefault().registerSticky(UserProfileFragment.this);
            }
        }
    };
}
Also used : StaticFragmentPagerAdapter(org.edx.mobile.view.adapters.StaticFragmentPagerAdapter) ViewPager(android.support.v4.view.ViewPager) View(android.view.View) SnackbarErrorNotification(org.edx.mobile.http.notifications.SnackbarErrorNotification) NonNull(android.support.annotation.NonNull) LinkedList(java.util.LinkedList) List(java.util.List) Icon(com.joanzapata.iconify.Icon) NonNull(android.support.annotation.NonNull)

Example 5 with Icon

use of com.joanzapata.iconify.Icon in project edx-app-android by edx.

the class CourseDiscussionResponsesAdapter method bindNumberCommentsView.

private void bindNumberCommentsView(NumberResponsesViewHolder holder, DiscussionComment response) {
    String text;
    Icon icon;
    int numChildren = response == null ? 0 : response.getChildCount();
    if (response.getChildCount() == 0) {
        if (discussionThread.isClosed() || courseData.isDiscussionBlackedOut()) {
            text = context.getString(R.string.discussion_add_comment_disabled_title);
            icon = FontAwesomeIcons.fa_lock;
        } else {
            text = context.getString(R.string.number_responses_or_comments_add_comment_label);
            icon = FontAwesomeIcons.fa_comment;
        }
    } else {
        text = context.getResources().getQuantityString(R.plurals.number_responses_or_comments_comments_label, numChildren, numChildren);
        icon = FontAwesomeIcons.fa_comment;
    }
    holder.numberResponsesOrCommentsLabel.setText(text);
    TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(holder.numberResponsesOrCommentsLabel, new IconDrawable(context, icon).colorRes(context, R.color.edx_brand_gray_base).sizeRes(context, R.dimen.edx_small), null, null, null);
}
Also used : IconDrawable(com.joanzapata.iconify.IconDrawable) Icon(com.joanzapata.iconify.Icon)

Aggregations

Icon (com.joanzapata.iconify.Icon)6 Context (android.content.Context)1 NonNull (android.support.annotation.NonNull)1 StringRes (android.support.annotation.StringRes)1 ViewPager (android.support.v4.view.ViewPager)1 View (android.view.View)1 IconDrawable (com.joanzapata.iconify.IconDrawable)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 SnackbarErrorNotification (org.edx.mobile.http.notifications.SnackbarErrorNotification)1 StaticFragmentPagerAdapter (org.edx.mobile.view.adapters.StaticFragmentPagerAdapter)1