Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
  • Loading branch information
AntonHolovin committed Dec 25, 2016
2 parents 9a2dcbb + 0474009 commit 690c665
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 59 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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'
}
```
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions 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"
}
Expand All @@ -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'
}
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions 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"
}
Expand All @@ -19,5 +19,5 @@ android {
}

dependencies {
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:design:25.1.0'
}
Expand Up @@ -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) {
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -97,7 +96,6 @@ public class Builder {

private boolean mIsImportant;

@Snackbar.Duration
private int mDuration;

private CharSequence mActionText;
Expand All @@ -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) {
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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) {
Expand All @@ -220,7 +218,6 @@ int getMaxLines() {
return mMaxLines;
}

@Snackbar.Duration
int getDuration() {
return mDuration;
}
Expand Down
Expand Up @@ -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;

Expand All @@ -23,7 +24,7 @@ public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_DISMISSED:
mQueue.poll();
iterateAndShow();
showNext();
break;

case MESSAGE_NEW:
Expand All @@ -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());
Expand All @@ -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);
}
}
}
@@ -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());
}
}
}
2 changes: 1 addition & 1 deletion fluentsnackbar/src/main/res/values/colors.xml
Expand Up @@ -2,6 +2,6 @@
<resources>
<color name="red_500">#F44336</color>
<color name="green_500">#4CAF50</color>
<color name="yellow_500">#FFEB3B</color>
<color name="yellow_700">#FBC02D</color>
<color name="default_background">#323232</color>
</resources>
4 changes: 2 additions & 2 deletions 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

0 comments on commit 690c665

Please sign in to comment.