Skip to content

Commit

Permalink
Fixed error reported in Issues #20 and #23
Browse files Browse the repository at this point in the history
  • Loading branch information
AviranAbady committed Aug 28, 2020
1 parent d3b0e3b commit af1d9c5
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 118 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -5,7 +5,7 @@ CookieBar 2
CookieBar is a lightweight library for showing a brief message at the top or bottom of the screen.<br/><br/>

```gradle
implementation 'org.aviran.cookiebar2:cookiebar2:1.1.3'
implementation 'org.aviran.cookiebar2:cookiebar2:1.1.4'
```

## Screenshot
Expand Down
16 changes: 8 additions & 8 deletions app/build.gradle
@@ -1,15 +1,15 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "org.aviran.cookiebarsample"
minSdkVersion 15
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
Expand All @@ -21,11 +21,11 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.12'
implementation project(path: ':cookiebar2')
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
}
@@ -1,8 +1,8 @@
package org.aviran.cookiebarsample;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
@@ -1,7 +1,7 @@
package org.aviran.cookiebarsample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
Expand All @@ -13,6 +13,8 @@

public class MainActivity extends AppCompatActivity {

int topCookieCounter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -25,7 +27,7 @@ public void onClick(View view) {
CookieBar.build(MainActivity.this)
.setTitle(R.string.top_cookie_title)
.setTitleColor(R.color.yellow)
.setMessage(R.string.top_cookie_message)
.setMessage(getString(R.string.top_cookie_message, ++topCookieCounter))
.setIcon(R.drawable.ic_android_white_48dp)
.setDuration(5000)
.setCookieListener(new CookieBarDismissListener() {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_main.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
Expand Down Expand Up @@ -85,4 +85,4 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_bottom_animated" />

</android.support.constraint.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 2 additions & 2 deletions app/src/main/res/layout/custom_cookie.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/cookie"
Expand Down Expand Up @@ -94,4 +94,4 @@
app:layout_constraintEnd_toEndOf="parent" />


</android.support.constraint.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Expand Up @@ -3,8 +3,8 @@
<string name="cookie_title">CookieBar title</string>
<string name="cookie_message">Hey! I\'m cookie, delivering fancy interactive messages on Android since 2017.</string>
<string name="cookie_action">Action</string>
<string name="top_cookie_title">Top cookie with an icon</string>
<string name="top_cookie_message">This is a top cookie with an icon.</string>
<string name="top_cookie_title">Top cookie with an icon!</string>
<string name="top_cookie_message">This is a top cookie with an icon (#%d)</string>
<string name="custom_anim_cookie_title">This cookie has custom in/out animations</string>
<string name="custom_anim_cookie_message">Did you notice it entered from the left?\nCan you guess how it will leave the screen?\nIf you guessed from the right, you\'re right!</string>
<string name="bottom_cookie_title">Bottom cookie with and icon</string>
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
}
Expand Down
12 changes: 6 additions & 6 deletions cookiebar2/build.gradle
Expand Up @@ -2,15 +2,15 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
compileSdkVersion 30
buildToolsVersion "30.0.2"

defaultConfig {
minSdkVersion 15
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}

buildTypes {
Expand All @@ -23,10 +23,10 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.12'
}

Expand Down

This file was deleted.

79 changes: 43 additions & 36 deletions cookiebar2/src/main/java/org/aviran/cookiebar2/Cookie.java
Expand Up @@ -3,13 +3,16 @@
import android.animation.Animator;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.AttrRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import androidx.annotation.AttrRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
Expand All @@ -24,6 +27,10 @@
import org.aviran.cookiebar2.CookieBarDismissListener.DismissType;

final class Cookie extends LinearLayout implements View.OnTouchListener {
//Used for debug purposes
//private static int cookieCounter = 0;
//private int cookieId;

private Animation slideOutAnimation;
private ViewGroup layoutCookie;
private TextView titleTextView;
Expand All @@ -45,10 +52,12 @@ final class Cookie extends LinearLayout implements View.OnTouchListener {
private CookieBarDismissListener dismissListener;
private boolean actionClickDismiss;
private boolean timeOutDismiss;

private boolean isCookieRemovalInProgress;
private Handler handler = new Handler();

public Cookie(@NonNull final Context context) {
this(context, null);
// cookieId = ++cookieCounter;
}

public Cookie(@NonNull final Context context, @Nullable final AttributeSet attrs) {
Expand Down Expand Up @@ -201,6 +210,21 @@ public void onClick(View view) {

createInAnim();
createOutAnim();

// Log.i("Cookiebar", "Dismiss delay activated for " + cookieId);
if (isAutoDismissEnabled) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
timeOutDismiss = true;
dismiss();
}
}, duration);
}
}

public boolean isRemovalInProgress() {
return isCookieRemovalInProgress;
}

private void setDefaultTextSize(TextView textView, @AttrRes int attr) {
Expand All @@ -225,33 +249,6 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
private void createInAnim() {
int animationResId = layoutGravity == Gravity.BOTTOM ? animationInBottom : animationInTop;
Animation slideInAnimation = AnimationUtils.loadAnimation(getContext(), animationResId);
slideInAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// no implementation
}

@Override
public void onAnimationEnd(Animation animation) {
if (!isAutoDismissEnabled) {
return;
}

postDelayed(new Runnable() {
@Override
public void run() {
timeOutDismiss = true;
dismiss();
}
}, duration);
}

@Override
public void onAnimationRepeat(Animation animation) {
// no implementation
}
});

setAnimation(slideInAnimation);
}

Expand All @@ -264,12 +261,14 @@ public void dismiss() {
dismiss(null);
}

public CookieBarDismissListener getDismissListenr() {
public CookieBarDismissListener getDismissListener() {
return dismissListener;
}

public void dismiss(final CookieBarDismissListener listener) {
getHandler().removeCallbacksAndMessages(null);
isCookieRemovalInProgress = true;
// Log.i("CookieBar", "Removing cookie #" + cookieId);
handler.removeCallbacksAndMessages(null);
if(listener != null) {
dismissListener = listener;
}
Expand Down Expand Up @@ -302,6 +301,14 @@ public void onAnimationRepeat(final Animation animation) {
startAnimation(slideOutAnimation);
}

void silentDismiss() {
// Log.i("Cookiebar", "Removing stale cooke " + cookieId);
isCookieRemovalInProgress = true;
handler.removeCallbacksAndMessages(null);
cookieListenerDismiss(DismissType.REPLACE_DISMISS);
removeFromParent();
}

private int getDismissType() {
int dismissType = DismissType.PROGRAMMATIC_DISMISS;
if(actionClickDismiss) {
Expand All @@ -320,7 +327,7 @@ private void cookieListenerDismiss(@DismissType int dismissType) {
}

private void removeFromParent() {
postDelayed(new Runnable() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
ViewParent parent = getParent();
Expand Down
32 changes: 22 additions & 10 deletions cookiebar2/src/main/java/org/aviran/cookiebar2/CookieBar.java
Expand Up @@ -3,12 +3,12 @@
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.support.annotation.AnimRes;
import android.support.annotation.AnimatorRes;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import androidx.annotation.AnimRes;
import androidx.annotation.AnimatorRes;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.StringRes;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -90,12 +90,13 @@ private void addCookie(final ViewGroup parent, final Cookie cookie) {
}

// if exists, remove existing cookie
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
int childCount = parent.getChildCount() - 1;
for (int i = childCount; i >= 0; i--) {
View child = parent.getChildAt(i);
if (child instanceof Cookie) {
if (child instanceof Cookie && !((Cookie) child).isRemovalInProgress()) {
removeStaleCookies(parent, i);
Cookie currentCookie = (Cookie) child;
final CookieBarDismissListener dismissListener = currentCookie.getDismissListenr();
final CookieBarDismissListener dismissListener = currentCookie.getDismissListener();
((Cookie) child).dismiss(new CookieBarDismissListener() {
@Override
public void onDismiss(int dismissType) {
Expand All @@ -112,6 +113,17 @@ public void onDismiss(int dismissType) {
parent.addView(cookie);
}

private void removeStaleCookies(ViewGroup parent, int topCookie) {
for(int i = 0; i < topCookie; i++) {
View child = parent.getChildAt(i);
if (child instanceof Cookie && !((Cookie) child).isRemovalInProgress()) {
Cookie currentCookie = (Cookie) child;
currentCookie.silentDismiss();
return;
}
}
}

public View getView() {
return cookieView;
}
Expand Down
Expand Up @@ -2,7 +2,7 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.AttrRes;
import androidx.annotation.AttrRes;

class ThemeResolver {

Expand Down

0 comments on commit af1d9c5

Please sign in to comment.