Skip to content

Commit

Permalink
Merge pull request #8 from tylersuehr7/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
tylersuehr7 authored Oct 27, 2017
2 parents 9a3f86f + c959149 commit 7e75180
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 65 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Method | Summary
`setShowDetailedChipsEnabled(boolean)` | True if clicking a chip should show its details.
`setCustomChipsEnabled(boolean)` | True if user is allowed to enter custom chips.
`setMaxRows(int)` | Changes maximum number of rows used to display chips.
`setTypeface(Typeface)` | Changes the typeface of the ChipsInputLayout and all associated textual-based components.
`setChipTitleTextColor(ColorStateList)` | Changes text color of each chips' title and subtitle.
`setShowChipAvatarEnabled(boolean)` | True if each chip should show an avatar icon.
`setChipsDeletable(boolean)` | True if each chip should be deletable by the user.
Expand Down Expand Up @@ -203,6 +204,8 @@ Method | Summary
`doesChipExist(Chip)` | Checks if the given chip exists in either the filterable or selected chips.
`isChipFiltered(Chip)` | Checks if the given chip exists in the filtered chips.
`isChipSelected(Chip)` | Checks if the given chip exists in the selected chips.
`getChipDataSource()` | Gets the currently used chip data source.
`changeChipDataSource(ChipDataSource)` | Changes the chip data source being used to manage chips, cloning existing observers.

## Managing the Chips
Where this library capitalizes, is how it decentralizes where and how the selected and filterable chips are stored. This makes accessing and receiving updates to data source changes from various Android components really simple.
Expand Down Expand Up @@ -250,9 +253,9 @@ public class CoolActivity extends AppCompatActivity implements ChipSelectionObse

This is used internally by the library to trigger UI updates on `RecyclerView` adapters when the data has changed.

*Currently, `ChipsInputLayout`, does not have a method to set this type of observer on the `ChipDataSource` because this is for internal components of the library. However, this may be an addition to the library, if needed\requested, in the future.*
To use this functionality, you'll want to implement the `ChipChangedObserver` and register it on `ChipDataSource`. Be sure to manage unregistering the observer, if need be, as well.

To use this functionality, you'll want to implement the `ChipChangedObserver` and register it on `ChipDataSource`. Be sure to manage unregistering the observer, if need be, as well.
Since components outside of the library cannot, and should not, directly access `ChipDataSouce`, you'll use `ChipsInputLayout` to set the observer; using its `setChipChangedObserver(ChipChangedObserver)` method.

Here is a simple example:
```java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
Expand Down Expand Up @@ -73,6 +74,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

findViewById(R.id.button_toggle_custom_chips).setOnClickListener(this);
findViewById(R.id.button_max_rows).setOnClickListener(this);
findViewById(R.id.button_typeface).setOnClickListener(this);

findViewById(R.id.button_validate).setOnClickListener(this);
}
Expand Down Expand Up @@ -224,6 +226,9 @@ public void onClick(View v) {
case R.id.button_max_rows:
this.chipsInputLayout.setMaxRows(6);
break;
case R.id.button_typeface:
this.chipsInputLayout.setTypeface(Typeface.create("cursive", Typeface.BOLD));
break;

case R.id.button_validate:
validateChips();
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_chips_input_test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Max Rows"/>
<Button
android:id="@+id/button_typeface"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Toggle Typeface"/>

<Button
android:id="@+id/button_validate"
Expand Down
4 changes: 3 additions & 1 deletion library/src/main/java/com/tylersuehr/chips/ChipOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

Expand All @@ -23,7 +24,7 @@ final class ChipOptions {
ColorStateList chipDeleteIconColor;
ColorStateList chipBackgroundColor;
ColorStateList chipTextColor;
boolean hasAvatarIcon;
boolean hasAvatarIcon = true;
boolean showDetailedChips = true;
boolean chipDeletable = true;
Drawable chipDeleteIcon;
Expand All @@ -39,6 +40,7 @@ final class ChipOptions {
float filterableListElevation;

/* Properties pertaining to the ChipsInputLayout itself */
Typeface typeface = Typeface.DEFAULT;
boolean allowCustomChips = true;
int maxRows;

Expand Down
16 changes: 12 additions & 4 deletions library/src/main/java/com/tylersuehr/chips/ChipView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.ColorInt;
Expand All @@ -22,7 +23,6 @@
* @version 1.0
*/
public class ChipView extends FrameLayout {
private LetterTileProvider tileProvider;
private Chip chip;

private CircleImageView mAvatarImageView;
Expand Down Expand Up @@ -54,8 +54,6 @@ public ChipView(@NonNull Context c, @Nullable AttributeSet attrs) {
this.mAvatarImageView = findViewById(R.id.icon);
this.mTitleView = findViewById(R.id.label);
this.mButtonDelete = findViewById(R.id.button_delete);

this.tileProvider = new LetterTileProvider(c);
}

/**
Expand Down Expand Up @@ -128,7 +126,8 @@ public void setHasAvatarIcon(boolean hasAvatarIcon) {
} else if (avatarDrawable != null) { // Use the Drawable
this.mAvatarImageView.setImageDrawable(avatarDrawable);
} else { // Use the tile provider
this.mAvatarImageView.setImageBitmap(tileProvider.getLetterTile(getTitle()));
this.mAvatarImageView.setImageBitmap(LetterTileProvider.getInstance(
getContext()).getLetterTile(getTitle()));
}
} else { // Hide the avatar icon
this.mAvatarImageView.setVisibility(GONE);
Expand Down Expand Up @@ -256,6 +255,7 @@ static class Builder {
private Drawable deleteIcon;
private ColorStateList deleteIconColor;
private ColorStateList backgroundColor;
private Typeface typeface;
private Chip chip;


Expand Down Expand Up @@ -316,6 +316,11 @@ Builder chip(Chip chip) {
return this;
}

Builder typeface(Typeface typeface) {
this.typeface = typeface;
return this;
}

ChipView build() {
return newInstance(this);
}
Expand All @@ -333,6 +338,9 @@ private static ChipView newInstance(Builder builder) {
chipView.deleteIconColor = builder.deleteIconColor;
chipView.backgroundColor = builder.backgroundColor;
chipView.chip = builder.chip;
if (builder.typeface != null) {
chipView.mTitleView.setTypeface(builder.typeface);
}
chipView.inflateFromFields();
return chipView;
}
Expand Down
58 changes: 55 additions & 3 deletions library/src/main/java/com/tylersuehr/chips/ChipsInputLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
Expand All @@ -21,6 +22,7 @@
import android.widget.RelativeLayout;
import com.beloo.widget.chipslayoutmanager.ChipsLayoutManager;
import com.tylersuehr.chips.data.Chip;
import com.tylersuehr.chips.data.ChipChangedObserver;
import com.tylersuehr.chips.data.ChipDataSource;
import com.tylersuehr.chips.data.ListChipDataSource;
import com.tylersuehr.chips.data.ChipSelectionObserver;
Expand Down Expand Up @@ -411,6 +413,35 @@ public void addChipSelectionObserver(ChipSelectionObserver observer) {
this.chipDataSource.addChipSelectionObserver(observer);
}

/**
* Removes an observer from watching selection events on the chip data source.
*
* @param observer {@link ChipSelectionObserver}
*/
public void removeChipSelectionObserver(ChipSelectionObserver observer) {
this.chipDataSource.removeChipSelectionObserver(observer);
}

/**
* Adds an observer to watch for any change events on the chip data source.
*
* Note: please use this conservatively!
*
* @param observer {@link ChipChangedObserver}
*/
public void addChipChangedObserver(ChipChangedObserver observer) {
this.chipDataSource.addChipChangedObserver(observer);
}

/**
* Removes an observer from watching any change events on the chip data source.
*
* @param observer {@link ChipChangedObserver}
*/
public void removeChipChangedObserver(ChipChangedObserver observer) {
this.chipDataSource.removeChipChangedObserver(observer);
}

/**
* Changes the chip data source being used to manipulate chips, which will
* update the UI accordingly.
Expand All @@ -426,6 +457,14 @@ public void changeChipDataSource(ChipDataSource dataSource) {
this.chipsAdapter.notifyDataSetChanged();
}

/**
* Gets an instance of {@link LetterTileProvider}.
* @return {@link LetterTileProvider}
*/
public LetterTileProvider getLetterTileProvider() {
return LetterTileProvider.getInstance(getContext());
}

public void setInputTextColor(ColorStateList textColor) {
this.chipOptions.textColor = textColor;
if (chipsEditText != null) { // Can be null because its lazy loaded
Expand Down Expand Up @@ -512,13 +551,23 @@ public void setMaxRows(int rows) {
setMaxHeight(Utils.dp(40) * chipOptions.maxRows);
}

public void setTypeface(Typeface typeface) {
this.chipOptions.typeface = typeface;
LetterTileProvider.getInstance(getContext()).setTypeface(typeface);
if (chipsEditText != null) {
this.chipsEditText.setTypeface(typeface);
}
}

/**
* Gets the current chip data source being used.
* Note: package-private for now because no outside component should access this.
*
* Note: This method should be used conservatively. Most components should access chips
* through other methods on ChipsInputLayout instead of the chip data source directly.
*
* @return {@link ChipDataSource}
*/
ChipDataSource getChipDataSource() {
public ChipDataSource getChipDataSource() {
return chipDataSource;
}

Expand Down Expand Up @@ -567,6 +616,7 @@ ChipEditText getThemedChipsEditText() {
this.chipsEditText.setTextColor(chipOptions.textColor);
}
this.chipsEditText.setHint(chipOptions.hint);
this.chipsEditText.setTypeface(chipOptions.typeface);

// Prevent fullscreen on landscape
this.chipsEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI|EditorInfo.IME_ACTION_DONE);
Expand Down Expand Up @@ -597,6 +647,7 @@ ChipView getThemedChipView() {
.deleteIcon(chipOptions.chipDeleteIcon)
.deleteIconColor(chipOptions.chipDeleteIconColor)
.backgroundColor(chipOptions.chipBackgroundColor)
.typeface(chipOptions.typeface)
.build();

chipView.setPadding(padding, padding, padding, padding);
Expand All @@ -616,6 +667,7 @@ DetailedChipView getThemedDetailedChipView(Chip chip) {
.textColor(chipOptions.detailedChipTextColor)
.backgroundColor(chipOptions.detailedChipBackgroundColor)
.deleteIconColor(chipOptions.detailedChipDeleteIconColor)
.typeface(chipOptions.typeface)
.build();
}

Expand All @@ -638,7 +690,7 @@ private void createAndSetupFilterableRecyclerView() {
}

// Create and set the filterable chips adapter
this.filterableChipsAdapter = new FilterableChipsAdapter(getContext(), this, chipOptions, chipDataSource);
this.filterableChipsAdapter = new FilterableChipsAdapter(this, chipDataSource, chipOptions);
this.filterableRecyclerView.setAdapter(this, filterableChipsAdapter);

// To show our filterable recycler view, we need to make sure our ChipsInputLayout has
Expand Down
27 changes: 14 additions & 13 deletions library/src/main/java/com/tylersuehr/chips/DetailedChipView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.NonNull;
Expand All @@ -27,8 +28,6 @@
* @version 1.0
*/
public class DetailedChipView extends FrameLayout {
private static LetterTileProvider tileProvider;

private TextView mTitleView;
private TextView mSubtitleView;
private ImageButton mButtonDelete;
Expand All @@ -46,9 +45,6 @@ public DetailedChipView(@NonNull Context context) {
public DetailedChipView(@NonNull Context c, @Nullable AttributeSet attrs) {
super(c, attrs);

// Setup the tile provider
tileProvider = getTileProvider(c);

// Inflate the view
View v = inflate(c, R.layout.chip_view_detailed, this);
this.mContentLayout = v.findViewById(R.id.container);
Expand Down Expand Up @@ -170,13 +166,6 @@ private void hideOnTouchOutside() {
setClickable(true);
}

private static LetterTileProvider getTileProvider(Context c) {
if (tileProvider == null) {
tileProvider = new LetterTileProvider(c);
}
return tileProvider;
}


/**
* Builder for the detailed chip view.
Expand All @@ -190,6 +179,7 @@ static class Builder {
private ColorStateList textColor;
private ColorStateList backgroundColor;
private ColorStateList deleteIconColor;
private Typeface typeface;


Builder(Context context) {
Expand Down Expand Up @@ -239,6 +229,11 @@ Builder deleteIconColor(ColorStateList deleteIconColor) {
return this;
}

Builder typeface(Typeface typeface) {
this.typeface = typeface;
return this;
}

DetailedChipView build() {
return DetailedChipView.newInstance(this);
}
Expand All @@ -253,7 +248,8 @@ private static DetailedChipView newInstance(Builder builder) {
} else if(builder.avatarDrawable != null) {
detailedChipView.setAvatarIcon(builder.avatarDrawable);
} else {
detailedChipView.setAvatarIcon(tileProvider.getLetterTile(builder.title));
detailedChipView.setAvatarIcon(LetterTileProvider.getInstance(
builder.context).getLetterTile(builder.title));
}

// Set the background color, if available
Expand All @@ -279,6 +275,11 @@ private static DetailedChipView newInstance(Builder builder) {
detailedChipView.setDeleteIconColor(ColorStateList.valueOf(Color.BLACK));
}

if (builder.typeface != null) {
detailedChipView.mTitleView.setTypeface(builder.typeface);
detailedChipView.mSubtitleView.setTypeface(builder.typeface);
}

detailedChipView.setTitle(builder.title);
detailedChipView.setSubtitle(builder.subtitle);
return detailedChipView;
Expand Down
Loading

0 comments on commit 7e75180

Please sign in to comment.