Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Iojjj committed May 16, 2016
1 parent 9bf1972 commit 79e14a6
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 42 deletions.
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
## Setup and usage

To include this library to your project add dependency in **build.gradle** file:

```groovy
dependencies {
compile '$coming_soon$'
}
```

There are three implementations of LikesLayout: **LikesFrameLayout**, **LikesLinearLayout**, **LikesRelativeLayout**.

```XML
<ua.vlasov.likeslayout.LikesLinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
app:likes_drawable="drawable resource id"
app:likes_drawableWidth="custom drawable width"
app:likes_drawableHeight="custom drawable height"
app:likes_produceInterval="produce interval in milliseconds"
app:likes_animationDuration="animationduration in milliseconds"
app:likes_tintMode="off|on_successively|on_random"
app:likes_tintColors="array resource id"
>

<ImageButton
...
app:likes_drawable="drawable resource id"
app:likes_drawableWidth="custom drawable width"
app:likes_drawableHeight="custom drawable height"
app:likes_mode="enabled|disabled"
app:likes_produceInterval="produce interval in milliseconds"
app:likes_animationDuration="animationduration in milliseconds"
app:likes_tintMode="off|on_successively|on_random"
app:likes_tintColors="array resource id"
/>

</ua.vlasov.likeslayout.LikesLinearLayout>
```

There are the list of available attributes:

| Attribute name | Applicable to LikesLayout? | Can be overridden by child? | Description | Default value |
|--|--|--|--|--|
| likes_animationDuration | yes | yes | Animation duration in milliseconds. | 1200 |
| likes_drawable | yes | yes | Drawable that will be drawn when user presses view. | n/a |
| likes_drawableWidth | yes | yes | Custom drawable width. | 0 |
| likes_drawableHeight | yes | yes | Custom drawable height. | 0 |
| likes_mode | no | yes | Switch that allows to enable/disable drawing likes on touching child view. If `likes_drawable` is null, `likes_mode` will be considered as `disabled`. | disabled |
| likes_produceInterval | yes | yes | Rate at which new likes drawables are produced in milliseconds. | 300 |
| likes_tintMode | yes | yes | Switch that allows to enable/disable tinting drawables. | not_set |
| likes_tintColors | yes | yes | Array of colors used for tinting drawables. If array is empty, `likes_tintMode` will be considered as `off`.| n/a |

###### Restrictions:
* If you will not set `likes_mode` or set it to `disabled`, this view will not draw any likes on touch event.
* All attributes applicable to LikesLayout will be used as default values for child with `likes_mode` set to `enabled`.
* Custom drawable width and height will set exact size ignoring aspect ratio.
* Likes floating from bottom to top.

<br />
## Changelog

| Version | Changes |
| --- | --- |
| v.1.0.0 | First public release |

<br />
## Support

You can support this library by creating a pull request with bug fixes and/or new features on `develop` branch. Any pull requests on `master` branch will be removed.

<br />
## License
* * *
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
55 changes: 45 additions & 10 deletions app/src/main/java/ua/vlasov/likes_layout/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,79 @@
package ua.vlasov.likes_layout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
import android.widget.TextView;

import java.util.Locale;

import ua.vlasov.likeslayout.LikesLinearLayout;
import ua.vlasov.likeslayout.OnChildTouchListener;

public class MainActivity extends AppCompatActivity implements OnChildTouchListener {

private Toast mToast;
private TextView mStatus;
private int mFavoriteCounter;
private int mGradeCounter;
private int mStarsCounter;

@SuppressLint("ShowToast")
@SuppressWarnings("ConstantConditions")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
mToast.setGravity(Gravity.TOP, 0, (int) (getResources().getDisplayMetrics().density * 56));
mStatus = (TextView) findViewById(R.id.status);
final LikesLinearLayout likesLayout = ((LikesLinearLayout) findViewById(R.id.likes_layout));
likesLayout.setOnChildTouchListener(this);
updateStatus();
}

@Override
public void onChildTouched(View child) {
mToast.setText(String.format("touched: %s", getResources().getResourceEntryName(child.getId())));
mToast.show();
switch (child.getId()) {
case R.id.btn_favorite: {
mFavoriteCounter = 0;
break;
}
case R.id.btn_grade: {
mGradeCounter = 0;
break;
}
case R.id.btn_stars: {
mStarsCounter = 0;
break;
}
}
updateStatus();
}

@Override
public void onLikeProduced(View child) {
// do something here
switch (child.getId()) {
case R.id.btn_favorite: {
mFavoriteCounter++;
break;
}
case R.id.btn_grade: {
mGradeCounter++;
break;
}
case R.id.btn_stars: {
mStarsCounter++;
break;
}
}
updateStatus();
}

@Override
public void onChildReleased(View child, boolean isCanceled) {
mToast.cancel();
updateStatus();
}

private void updateStatus() {
mStatus.setText(String.format(Locale.US, "Counter. favorites: %1$d, grades: %2$d, stars: %3$d",
mFavoriteCounter, mGradeCounter, mStarsCounter));
}
}
11 changes: 10 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@
android:layout_height="match_parent"
tools:context="ua.vlasov.likes_layout.MainActivity">

<TextView
android:id="@+id/status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
/>

<ua.vlasov.likeslayout.LikesLinearLayout
android:id="@+id/likes_layout"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="bottom|center_horizontal"
android:paddingBottom="16dp"
app:likes_animationDuration="1200"
app:likes_tintMode="on_sequence"
app:likes_tintMode="on_successively"
app:likes_tintColors="@array/drawable_colors"
>

Expand All @@ -31,6 +39,7 @@
android:id="@+id/btn_grade"
style="@style/LikeButton.Grade"
app:likes_produceInterval="200"
app:likes_tintMode="on_random"
/>


Expand Down
4 changes: 0 additions & 4 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@
<item name="android:src">@drawable/ic_favorite</item>
<item name="android:tint">@color/colorAccent</item>
<item name="likes_drawable">@drawable/ic_favorite_normal</item>
<item name="likes_tintMode">on_sequence</item>
<item name="likes_tintColors">@array/drawable_colors</item>
</style>

<style name="LikeButton.Grade">
<item name="android:src">@drawable/ic_grade</item>
<item name="android:tint">@color/colorAccent</item>
<item name="likes_drawable">@drawable/ic_grade_normal</item>
<item name="likes_tintMode">on_random</item>
<item name="likes_tintColors">@array/drawable_colors</item>
</style>

<style name="LikeButton.Stars">
Expand Down
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
resourcePrefix "likes_"
}

dependencies {
Expand Down
20 changes: 20 additions & 0 deletions library/src/main/java/ua/vlasov/likeslayout/LikesAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ public boolean isLikesModeEnabled() {
return mType == LIKES_MODE_ENABLED;
}

/**
* Check if drawable is set.
* @return true if drawable is set, false otherwise
*/
public boolean hasDrawable() {
return mDrawable != null;
}

/**
* Get tint colors array.
* @return tint colors array
Expand Down Expand Up @@ -308,6 +316,18 @@ public int[] getTintColors(@NonNull LikesAttributes defaultAttributes) {
return defaultAttributes.getTintColors();
}

/**
* Check if drawable is set or default value exists.
* @param defaultAttributes default attributes
* @return true if drawable is set, false otherwise
*/
public boolean hasDrawable(@NonNull LikesAttributes defaultAttributes) {
if (mDrawable != null) {
return true;
}
return defaultAttributes.hasDrawable();
}

/*
* FACTORY METHODS
*/
Expand Down
13 changes: 7 additions & 6 deletions library/src/main/java/ua/vlasov/likeslayout/LikesDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,23 @@ private class Producer implements Runnable {

private final LikesAttributes mAttributes;
private final float mCx, mCy;
private final int mViewId;
private final View mView;
private boolean isCancelled;

public Producer(@NonNull LikesAttributes attributes, @NonNull View v) {
public Producer(@NonNull LikesAttributes attributes, @NonNull View view) {
mAttributes = attributes;
mViewId = v.getId();
mCx = v.getLeft() + v.getWidth() / 2f;
mCy = v.getTop() + v.getHeight() / 2f;
mView = view;
mCx = view.getLeft() + view.getWidth() / 2f;
mCy = view.getTop() + view.getHeight() / 2f;
}

@Override
public void run() {
if (isCancelled) {
return;
}
startDrawing(mViewId);
startDrawing(mView.getId());
mLikesLayout.onLikeProduced(mView);
mUiHandler.postDelayed(this, mAttributes.getProduceInterval(mDefaultAttributes));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
*/
public class LikesFrameLayout extends FrameLayout implements LikesDrawer.LikesLayout {

private OnChildTouchListener mOnChildTouchListener;

@Nullable
private LikesDrawer mLikesDrawer;
private LikesAttributes mDefaultAttributes;
private OnChildTouchListener mOnChildTouchListener;

public LikesFrameLayout(Context context) {
this(context, null);
Expand All @@ -43,8 +43,10 @@ public LikesFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, i
private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
setWillNotDraw(false);
if (attrs != null) {
final LikesAttributes likesAttributes = LikesAttributes.create(context, attrs, LikesAttributes.LAYOUT_TYPE_LINEAR);
mLikesDrawer = new LikesDrawer(this, likesAttributes);
mDefaultAttributes = LikesAttributes.create(context, attrs, LikesAttributes.LAYOUT_TYPE_FRAME);
mLikesDrawer = new LikesDrawer(this, mDefaultAttributes);
} else {
mDefaultAttributes = LikesAttributes.empty();
}
}

Expand Down Expand Up @@ -75,7 +77,7 @@ protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
if (layoutParams.mAttributes.isLikesModeEnabled()) {
if (layoutParams.mAttributes.isLikesModeEnabled() && layoutParams.mAttributes.hasDrawable(mDefaultAttributes)) {
child.setOnTouchListener(mLikesDrawer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
public class LikesLinearLayout extends LinearLayout implements LikesDrawer.LikesLayout {


private OnChildTouchListener mOnChildTouchListener;

@Nullable
private LikesDrawer mLikesDrawer;
private LikesAttributes mDefaultAttributes;
private OnChildTouchListener mOnChildTouchListener;

public LikesLinearLayout(Context context) {
this(context, null);
Expand All @@ -44,8 +44,10 @@ public LikesLinearLayout(Context context, AttributeSet attrs, int defStyleAttr,
private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
setWillNotDraw(false);
if (attrs != null) {
final LikesAttributes likesAttributes = LikesAttributes.create(context, attrs, LikesAttributes.LAYOUT_TYPE_LINEAR);
mLikesDrawer = new LikesDrawer(this, likesAttributes);
mDefaultAttributes = LikesAttributes.create(context, attrs, LikesAttributes.LAYOUT_TYPE_LINEAR);
mLikesDrawer = new LikesDrawer(this, mDefaultAttributes);
} else {
mDefaultAttributes = LikesAttributes.empty();
}
}

Expand Down Expand Up @@ -76,7 +78,7 @@ protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
if (layoutParams.mAttributes.isLikesModeEnabled()) {
if (layoutParams.mAttributes.isLikesModeEnabled() && layoutParams.mAttributes.hasDrawable(mDefaultAttributes)) {
child.setOnTouchListener(mLikesDrawer);
}
}
Expand Down
Loading

0 comments on commit 79e14a6

Please sign in to comment.