Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignoreClass to Builder #189

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CalligraphySample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ dependencies {
compile 'com.android.support:support-v4:22.1.1'
compile 'com.android.support:appcompat-v7:22.1.1'

compile 'com.malinskiy:materialicons:1.0.2'

compile 'com.jakewharton:butterknife:5.1.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.app.Application;

import com.malinskiy.materialicons.widget.IconTextView;

import uk.co.chrisjenx.calligraphy.CalligraphyConfig;

/**
Expand All @@ -17,6 +19,7 @@ public void onCreate() {
.setDefaultFontPath("fonts/Roboto-ThinItalic.ttf")
.setFontAttrId(R.attr.fontPath)
.addCustomStyle(TextField.class, R.attr.textFieldStyle)
.ignoreClass(IconTextView.class)
.build()
);
}
Expand Down
13 changes: 13 additions & 0 deletions CalligraphySample/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@
android:layout_height="wrap_content"
android:text="@string/custom_view_style_text"/>

<com.malinskiy.materialicons.widget.IconTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/defined_ignore_class"/>

<com.malinskiy.materialicons.widget.IconButton
calligraphyIgnore="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:text="@string/defined_ignore_attribute"/>

<Button
android:id="@+id/button_default"
android:layout_width="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions CalligraphySample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<string name="defined_in_appears">\nThis has the font path defined in the Views TextAppearance as Oswald.\n</string>
<string name="defined_in_appears_caps">\nThis has the font path defined in the Views TextAppearance as Oswald. textAllCaps is applied.\n</string>
<string name="defined_custom_view">\nThis is a custom TextView with Oswald font.\n</string>
<string name="defined_ignore_class">\nIgnore class works for iconify libraries for {zmdi-android} {zmdi-favorite}</string>
<string name="defined_ignore_attribute">\nIgnore attribute works too {zmdi-thumb-up}</string>
<string name="defined_view_stub">\nThis is a TextView inflated from a ViewStub.\n</string>
<string name="defined_view_stub_font_path">\nThis is a TextView inflated from a ViewStub w/ fontPath declared.\n</string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Created by chris on 20/12/2013
Expand Down Expand Up @@ -86,6 +88,10 @@ public static CalligraphyConfig get() {
* Class Styles. Build from DEFAULT_STYLES and the builder.
*/
private final Map<Class<? extends TextView>, Integer> mClassStyleAttributeMap;
/**
* Classes that will not be injected
*/
private final Set<Class<?>> mIgnoredClassSet;

protected CalligraphyConfig(Builder builder) {
mIsFontSet = builder.isFontSet;
Expand All @@ -96,6 +102,7 @@ protected CalligraphyConfig(Builder builder) {
final Map<Class<? extends TextView>, Integer> tempMap = new HashMap<>(DEFAULT_STYLES);
tempMap.putAll(builder.mStyleClassMap);
mClassStyleAttributeMap = Collections.unmodifiableMap(tempMap);
mIgnoredClassSet = Collections.unmodifiableSet(new HashSet<>(builder.mIgnoredClassSet));
}

/**
Expand Down Expand Up @@ -131,6 +138,15 @@ public int getAttrId() {
return mAttrId;
}

/**
*
* @param viewClass class to be ignored by Calligraphy
* @return true if class should not be injected, false otherwise
*/
public boolean isIgnoredClass(Class<?> viewClass) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be on the Utils class as this isn't really a configuration method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrisjenx where should I store collection of ignored classes if it will be in utils?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be in the Config. Just iterating across the list should not be done inside of the Config. Config should be just immutable state storage.

return mIgnoredClassSet.contains(viewClass);
}

public static class Builder {
/**
* Default AttrID if not set.
Expand Down Expand Up @@ -160,7 +176,10 @@ public static class Builder {
* Additional Class Styles. Can be empty.
*/
private Map<Class<? extends TextView>, Integer> mStyleClassMap = new HashMap<>();

/**
* Ignored classes. Can be empty.
*/
private Set<Class<?>> mIgnoredClassSet = new HashSet<>();
/**
* This defaults to R.attr.fontPath. So only override if you want to use your own attrId.
*
Expand Down Expand Up @@ -257,6 +276,12 @@ public Builder addCustomStyle(final Class<? extends TextView> styleClass, final
return this;
}

public Builder ignoreClass(final Class<?> clazz) {
if(clazz == null) return this;
mIgnoredClassSet.add(clazz);
return this;
}

public CalligraphyConfig build() {
this.isFontSet = !TextUtils.isEmpty(fontAssetPath);
return new CalligraphyConfig(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ public View onViewCreated(View view, Context context, AttributeSet attrs) {
}

void onViewCreatedInternal(View view, final Context context, AttributeSet attrs) {
if (view instanceof TextView) {
if (!CalligraphyConfig.get().isIgnoredClass(view.getClass()) &&
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isIgnoredClass should take the current view and the Configuration separately as method arguments.

!CalligraphyUtils.pullIgnoredFromView(context, attrs) &&
view instanceof TextView) {

// Fast path the setting of TextView's font, means if we do some delayed setting of font,
// which has already been set by use we skip this TextView (mainly for inflating custom,
// TextView's inside the Toolbar/ActionBar).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@ static String pullFontPathFromView(Context context, AttributeSet attrs, int attr
: attrs.getAttributeValue(null, attributeName);
}

/**
* Tries to pull the Ingored Attribute directly from the View.
* @param context Activity Context
* @param attrs View Attributes
* @return false if attribute is not defined or added to View
*/
static boolean pullIgnoredFromView(Context context, AttributeSet attrs) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add this as a separate pull-request.

if (attrs == null)
return false;

final String attributeName;
try {
attributeName = context.getResources().getResourceEntryName(R.attr.calligraphyIgnore);
} catch (Resources.NotFoundException e) {
// invalid attribute ID
return false;
}

return attrs.getAttributeBooleanValue(null, attributeName, false);
}

/**
* Tries to pull the Font Path from the View Style as this is the next decendent after being
* defined in the View's xml.
Expand Down
1 change: 1 addition & 0 deletions calligraphy/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="fontPath" format="string"/>
<attr name="calligraphyIgnore" format="boolean"/>
</resources>