diff --git a/README.md b/README.md index 5313fd9..45d3e17 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ public class MainActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - mFluentSnackbar = new FluentSnackbar(this); + mFluentSnackbar = FluentSnackbar.create(this); // you can also use any View instead of Activity mFluentSnackbar.create("Text") .maxLines(2) // default is 1 line @@ -58,6 +58,6 @@ and then add this in your module `build.gradle`: ```gradle dependencies { - compile 'com.github.antonygolovin:fluentsnackbar:0.1.3' + compile 'com.github.antonygolovin:fluentsnackbar:1.0.0' } ``` diff --git a/build.gradle b/build.gradle index aff4f41..74b2ab0 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.2' + classpath 'com.android.tools.build:gradle:2.2.3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/fluentsnackbar-sample/build.gradle b/fluentsnackbar-sample/build.gradle index b8a6750..526b9bd 100644 --- a/fluentsnackbar-sample/build.gradle +++ b/fluentsnackbar-sample/build.gradle @@ -1,13 +1,13 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 25 + buildToolsVersion "25.0.2" defaultConfig { applicationId "com.golovin.sample" minSdkVersion 15 - targetSdkVersion 23 + targetSdkVersion 25 versionCode 1 versionName "1.0" } @@ -20,9 +20,6 @@ android { } dependencies { - compile 'com.android.support:appcompat-v7:23.2.1' + compile 'com.android.support:appcompat-v7:25.1.0' compile project(':fluentsnackbar') - - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' } diff --git a/fluentsnackbar-sample/src/main/java/com/golovin/sample/MainActivity.java b/fluentsnackbar-sample/src/main/java/com/golovin/sample/MainActivity.java index f7c2124..af5b9a7 100644 --- a/fluentsnackbar-sample/src/main/java/com/golovin/sample/MainActivity.java +++ b/fluentsnackbar-sample/src/main/java/com/golovin/sample/MainActivity.java @@ -17,7 +17,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - mFluentSnackbar = new FluentSnackbar(this); + mFluentSnackbar = FluentSnackbar.create(this); findViewById(R.id.button_show_success).setOnClickListener(new View.OnClickListener() { @Override diff --git a/fluentsnackbar/build.gradle b/fluentsnackbar/build.gradle index 98cdbfc..1ebdca5 100644 --- a/fluentsnackbar/build.gradle +++ b/fluentsnackbar/build.gradle @@ -1,12 +1,12 @@ apply plugin: 'com.android.library' android { - compileSdkVersion 24 - buildToolsVersion "23.0.3" + compileSdkVersion 25 + buildToolsVersion "25.0.2" defaultConfig { minSdkVersion 15 - targetSdkVersion 24 + targetSdkVersion 25 versionCode 1 versionName "1.0" } @@ -19,5 +19,5 @@ android { } dependencies { - compile 'com.android.support:design:24.0.0' + compile 'com.android.support:design:25.1.0' } diff --git a/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/FluentSnackbar.java b/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/FluentSnackbar.java index e96d3b5..e5adae3 100644 --- a/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/FluentSnackbar.java +++ b/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/FluentSnackbar.java @@ -3,36 +3,37 @@ import android.app.Activity; import android.content.res.ColorStateList; import android.graphics.Color; -import android.os.Looper; import android.os.Message; import android.support.annotation.ColorInt; import android.support.annotation.ColorRes; import android.support.annotation.StringRes; import android.support.design.widget.Snackbar; import android.support.v4.content.ContextCompat; -import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView; +import com.golovin.fluentstackbar.helpers.ThreadHelper; import com.golovin.snackbarmanager.R; -public class FluentSnackbar { - private final Activity mActivity; +public final class FluentSnackbar { + private final View mView; private final SnackbarHandler mSnackbarHandler; - public FluentSnackbar(Activity activity) { - verifyMainThread(); + public static FluentSnackbar create(Activity activity) { + ThreadHelper.verifyMainThread(); - mActivity = activity; + return new FluentSnackbar(activity.findViewById(android.R.id.content)); + } - mSnackbarHandler = new SnackbarHandler(this); + public static FluentSnackbar create(View view) { + ThreadHelper.verifyMainThread(); + + return new FluentSnackbar(view); } - private void verifyMainThread() { - if (Looper.myLooper() != Looper.getMainLooper()) { - throw new IllegalStateException( - "Expected to be called on the main thread but was " + Thread.currentThread().getName()); - } + private FluentSnackbar(View view) { + mView = view; + mSnackbarHandler = new SnackbarHandler(this); } private void putToMessageQueue(Builder builder) { @@ -42,9 +43,7 @@ private void putToMessageQueue(Builder builder) { } void showSnackbar(Builder builder) { - //noinspection ConstantConditions,ResourceType - Snackbar snackbar = Snackbar.make(mActivity.findViewById(android.R.id.content), builder.getText(), - builder.getDuration()); + Snackbar snackbar = Snackbar.make(mView, builder.getText(), builder.getDuration()); View view = snackbar.getView(); view.setBackgroundColor(builder.getBackgroundColor()); @@ -64,7 +63,7 @@ void showSnackbar(Builder builder) { } if (builder.isImportant()) { - snackbar.setCallback(new Snackbar.Callback() { + snackbar.addCallback(new Snackbar.Callback() { @Override public void onDismissed(Snackbar snackbar, int event) { Message message = mSnackbarHandler.obtainMessage(SnackbarHandler.MESSAGE_DISMISSED); @@ -77,7 +76,7 @@ public void onDismissed(Snackbar snackbar, int event) { } public Builder create(@StringRes int text) { - return create(mActivity.getString(text)); + return create(mView.getContext().getString(text)); } public Builder create(String text) { @@ -97,7 +96,6 @@ public class Builder { private boolean mIsImportant; - @Snackbar.Duration private int mDuration; private CharSequence mActionText; @@ -113,10 +111,10 @@ private Builder(CharSequence text) { mText = text; mMaxLines = 1; mTextColor = Color.WHITE; - mBackgroundColor = ContextCompat.getColor(mActivity, R.color.default_background); + mBackgroundColor = ContextCompat.getColor(mView.getContext(), R.color.default_background); mIsImportant = false; mDuration = Snackbar.LENGTH_LONG; - mActionText = mActivity.getString(R.string.default_action); + mActionText = mView.getContext().getString(R.string.default_action); } public Builder maxLines(int maxLines) { @@ -125,7 +123,7 @@ public Builder maxLines(int maxLines) { } public Builder textColorRes(@ColorRes int color) { - mTextColor = ContextCompat.getColor(mActivity, color); + mTextColor = ContextCompat.getColor(mView.getContext(), color); return this; } @@ -135,27 +133,27 @@ public Builder textColor(@ColorInt int color) { } public Builder successBackgroundColor() { - mBackgroundColor = ContextCompat.getColor(mActivity, R.color.green_500); + mBackgroundColor = ContextCompat.getColor(mView.getContext(), R.color.green_500); return this; } public Builder errorBackgroundColor() { - mBackgroundColor = ContextCompat.getColor(mActivity, R.color.red_500); + mBackgroundColor = ContextCompat.getColor(mView.getContext(), R.color.red_500); return this; } public Builder warningBackgroundColor() { - mBackgroundColor = ContextCompat.getColor(mActivity, R.color.yellow_500); + mBackgroundColor = ContextCompat.getColor(mView.getContext(), R.color.yellow_700); return this; } public Builder neutralBackgroundColor() { - mBackgroundColor = ContextCompat.getColor(mActivity, R.color.default_background); + mBackgroundColor = ContextCompat.getColor(mView.getContext(), R.color.default_background); return this; } public Builder backgroundColorRes(@ColorRes int color) { - mBackgroundColor = ContextCompat.getColor(mActivity, color); + mBackgroundColor = ContextCompat.getColor(mView.getContext(), color); return this; } @@ -173,7 +171,7 @@ public Builder important(boolean isImportant) { return this; } - public Builder duration(@Snackbar.Duration int duration) { + public Builder duration(int duration) { mDuration = duration; return this; } @@ -184,7 +182,7 @@ public Builder action(View.OnClickListener listener) { } public Builder actionTextRes(@StringRes int text) { - mActionText = mActivity.getString(text); + mActionText = mView.getContext().getString(text); return this; } @@ -194,7 +192,7 @@ public Builder actionText(String text) { } public Builder actionTextColorRes(@ColorRes int color) { - return actionTextColor(ContextCompat.getColor(mActivity, color)); + return actionTextColor(ContextCompat.getColor(mView.getContext(), color)); } public Builder actionTextColor(@ColorInt int color) { @@ -220,7 +218,6 @@ int getMaxLines() { return mMaxLines; } - @Snackbar.Duration int getDuration() { return mDuration; } diff --git a/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/SnackbarHandler.java b/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/SnackbarHandler.java index 1f32caa..1a529aa 100644 --- a/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/SnackbarHandler.java +++ b/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/SnackbarHandler.java @@ -2,11 +2,12 @@ import android.os.Handler; import android.os.Message; + import java.lang.ref.WeakReference; import java.util.LinkedList; import java.util.Queue; -class SnackbarHandler extends Handler { +final class SnackbarHandler extends Handler { static final int MESSAGE_DISMISSED = 0; static final int MESSAGE_NEW = 1; @@ -23,7 +24,7 @@ public void handleMessage(Message msg) { switch (msg.what) { case MESSAGE_DISMISSED: mQueue.poll(); - iterateAndShow(); + showNext(); break; case MESSAGE_NEW: @@ -40,16 +41,14 @@ private void onNewMessage(Message msg) { mQueue.poll(); mQueue.add(newMessage); - iterateAndShow(); + showNext(); } else { mQueue.add(newMessage); } } - private void iterateAndShow() { - while (!mQueue.isEmpty() && !(hasLastAndNotImportant() || mQueue.peek().isImportant())) { - mQueue.poll(); - } + private void showNext() { + removeLowPriorityMessages(); if (!mQueue.isEmpty()) { show(mQueue.peek()); @@ -63,7 +62,20 @@ private void show(FluentSnackbar.Builder message) { } } - private boolean hasLastAndNotImportant() { - return !mQueue.peek().isImportant() && mQueue.size() == 1; + private void removeLowPriorityMessages() { + while (hasItemsToRemove()) { + mQueue.poll(); + } + } + + private boolean hasItemsToRemove() { + if (mQueue.isEmpty()) { + return false; + } + + boolean hasImportant = mQueue.peek().isImportant(); + boolean hasSingleNonImportant = mQueue.size() == 1 && !mQueue.peek().isImportant(); + + return !(hasImportant || hasSingleNonImportant); } -} +} \ No newline at end of file diff --git a/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/helpers/ThreadHelper.java b/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/helpers/ThreadHelper.java new file mode 100644 index 0000000..de633fe --- /dev/null +++ b/fluentsnackbar/src/main/java/com/golovin/fluentstackbar/helpers/ThreadHelper.java @@ -0,0 +1,16 @@ +package com.golovin.fluentstackbar.helpers; + +import android.os.Looper; + +public final class ThreadHelper { + private ThreadHelper() { + //no instance + } + + public static void verifyMainThread() { + if (Looper.myLooper() != Looper.getMainLooper()) { + throw new IllegalStateException( + "Expected to be called on the main thread but was " + Thread.currentThread().getName()); + } + } +} diff --git a/fluentsnackbar/src/main/res/values/colors.xml b/fluentsnackbar/src/main/res/values/colors.xml index 8935cc3..17b9a04 100644 --- a/fluentsnackbar/src/main/res/values/colors.xml +++ b/fluentsnackbar/src/main/res/values/colors.xml @@ -2,6 +2,6 @@ #F44336 #4CAF50 - #FFEB3B + #FBC02D #323232 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index d5eeba2..450bef8 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Apr 08 00:08:41 EEST 2016 +#Sun Dec 25 14:50:03 EET 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip