Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
danimahardhika authored Oct 16, 2017
1 parent 9b5e08d commit a1e4222
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 282 deletions.
242 changes: 70 additions & 172 deletions library/src/main/java/com/danimahardhika/cafebar/CafeBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.BoolRes;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.IntRange;
import android.support.annotation.LayoutRes;
Expand All @@ -48,11 +49,18 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.HashMap;

@SuppressWarnings("unused")
public class CafeBar {

static final String FONT_CONTENT = "content";
static final String FONT_POSITIVE = "positive";
static final String FONT_NEGATIVE = "negative";
static final String FONT_NEUTRAL = "neutral";

private Builder mBuilder;
private Snackbar mSnackBar;

Expand Down Expand Up @@ -263,7 +271,7 @@ private void setButtonAction(@NonNull String action, int color, @Nullable final
boolean tabletMode = mBuilder.mContext.getResources().getBoolean(R.bool.cafebar_tablet_mode);

if (tabletMode || configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
if (mBuilder.longContent()) {
if (mBuilder.mLongContent) {
LogUtil.d("content has multi lines");
root.setPadding(side, side, (side - buttonPadding), (side - bottom + navBar));
} else if (longAction){
Expand All @@ -274,7 +282,7 @@ private void setButtonAction(@NonNull String action, int color, @Nullable final
root.setPadding(side, (top - buttonPadding), (side - buttonPadding), (top - buttonPadding + navBar));
}
} else {
if (mBuilder.longContent()) {
if (mBuilder.mLongContent) {
LogUtil.d("content has multi lines");
root.setPadding(side, side, (side - buttonPadding + navBar), (side - bottom));
} else if (longAction) {
Expand All @@ -287,8 +295,8 @@ private void setButtonAction(@NonNull String action, int color, @Nullable final
}

TextView button = CafeBarUtil.getActionView(mBuilder, action, color);
if (mBuilder.mNeutralTypeface != null) {
button.setTypeface(mBuilder.mNeutralTypeface);
if (mBuilder.getTypeface(FONT_NEUTRAL) != null) {
button.setTypeface(mBuilder.getTypeface(FONT_NEUTRAL));
}

if (!longAction) {
Expand Down Expand Up @@ -372,7 +380,6 @@ public boolean onPreDraw() {

private boolean isAccessibilityManagerEnabled() {
String manufacturer = android.os.Build.MANUFACTURER;
LogUtil.d("Manufacturer: " +manufacturer);
if (manufacturer.equalsIgnoreCase("xiaomi")) {
//Seriously accessibility manager on xiaomi device is a mess
//Better to returns false
Expand Down Expand Up @@ -420,51 +427,50 @@ public static Builder builder(@NonNull Context context) {
@SuppressWarnings("unused")
public static class Builder {

private Context mContext;

private View mView;
private View mCustomView;
private CafeBarTheme.Custom mTheme = CafeBarTheme.Custom(CafeBarTheme.DARK.getColor());
private CafeBarGravity mGravity = CafeBarGravity.CENTER;

private int mDuration = CafeBarDuration.SHORT.getDuration();
private int mMaxLines = 2;
private int mPositiveColor = mTheme.getTitleColor();
private int mNegativeColor = mTheme.getTitleColor();
private int mNeutralColor = mTheme.getTitleColor();

private boolean mLongContent = false;
private boolean mAutoDismiss = true;
private boolean mShowShadow = true;
private boolean mFitSystemWindow = false;
private boolean mFloating = false;
private boolean mAdjustCustomView = false;
private boolean mTintIcon = true;
private boolean mSwipeToDismiss = true;

private Typeface mContentTypeface;
private Typeface mPositiveTypeface;
private Typeface mNegativeTypeface;
private Typeface mNeutralTypeface;
private Drawable mIcon = null;

private String mContent = "";
private String mPositiveText = null;
private String mNegativeText = null;
private String mNeutralText = null;

private SpannableStringBuilder mSpannableBuilder = null;

private CafeBarCallback mPositiveCallback;
private CafeBarCallback mNegativeCallback;
private CafeBarCallback mNeutralCallback;
Context mContext;

@Nullable View mTo;
@Nullable View mCustomView;
CafeBarTheme.Custom mTheme = CafeBarTheme.Custom(CafeBarTheme.DARK.getColor());
CafeBarGravity mGravity = CafeBarGravity.CENTER;

int mDuration = CafeBarDuration.SHORT.getDuration();
int mMaxLines = 2;
@ColorInt int mPositiveColor = mTheme.getTitleColor();
@ColorInt int mNegativeColor = mTheme.getTitleColor();
@ColorInt int mNeutralColor = mTheme.getTitleColor();

boolean mLongContent = false;
boolean mAutoDismiss = true;
boolean mShowShadow = true;
boolean mFitSystemWindow = false;
boolean mFloating = false;
boolean mAdjustCustomView = false;
boolean mTintIcon = true;
boolean mSwipeToDismiss = true;

private HashMap<String, WeakReference<Typeface>> mTypefaces;

@Nullable Drawable mIcon = null;

String mContent = "";
@Nullable String mPositiveText = null;
@Nullable String mNegativeText = null;
@Nullable String mNeutralText = null;

@Nullable SpannableStringBuilder mSpannableBuilder = null;

@Nullable CafeBarCallback mPositiveCallback;
@Nullable CafeBarCallback mNegativeCallback;
@Nullable CafeBarCallback mNeutralCallback;

public Builder(@NonNull Context context) {
mContext = context;
mTypefaces = new HashMap<>();
}

public Builder to(@Nullable View view) {
mView = view;
mTo = view;
return this;
}

Expand Down Expand Up @@ -618,8 +624,10 @@ public Builder typeface(String contentFontName, String buttonFontName) {
}

public Builder typeface(@Nullable Typeface content, @Nullable Typeface button) {
mContentTypeface = content;
mNeutralTypeface = mPositiveTypeface = mNegativeTypeface = button;
addTypeface(FONT_CONTENT, content);
addTypeface(FONT_POSITIVE, button);
addTypeface(FONT_NEGATIVE, button);
addTypeface(FONT_NEUTRAL, button);
return this;
}

Expand All @@ -628,7 +636,7 @@ public Builder contentTypeface(String fontName) {
}

public Builder contentTypeface(@Nullable Typeface typeface) {
mContentTypeface = typeface;
addTypeface(FONT_CONTENT, typeface);
return this;
}

Expand All @@ -637,7 +645,7 @@ public Builder positiveTypeface(String fontName) {
}

public Builder positiveTypeface(@Nullable Typeface typeface) {
mPositiveTypeface = typeface;
addTypeface(FONT_POSITIVE,typeface);
return this;
}

Expand All @@ -646,7 +654,7 @@ public Builder negativeTypeface(String fontName) {
}

public Builder negativeTypeface(@Nullable Typeface typeface) {
mNegativeTypeface = typeface;
addTypeface(FONT_NEGATIVE, typeface);
return this;
}

Expand All @@ -655,7 +663,7 @@ public Builder neutralTypeface(String fontName) {
}

public Builder neutralTypeface(@Nullable Typeface typeface) {
mNeutralTypeface = typeface;
addTypeface(FONT_NEUTRAL, typeface);
return this;
}

Expand All @@ -664,7 +672,9 @@ public Builder buttonTypeface(String fontName) {
}

public Builder buttonTypeface(@Nullable Typeface typeface) {
mNeutralTypeface = mPositiveTypeface = mNegativeTypeface = typeface;
addTypeface(FONT_POSITIVE, typeface);
addTypeface(FONT_NEGATIVE, typeface);
addTypeface(FONT_NEUTRAL, typeface);
return this;
}

Expand Down Expand Up @@ -739,130 +749,18 @@ public void show() {
build().show();
}

void longContent(boolean longContent) {
mLongContent = longContent;
}

boolean longContent() {
return mLongContent;
}

@NonNull
Context getContext() {
return mContext;
}

@Nullable
View getTo() {
return mView;
}

@Nullable
View getCustomView() {
return mCustomView;
}

boolean isAdjustCustomView() {
return mAdjustCustomView;
}

CafeBarTheme.Custom getTheme() {
return mTheme;
}

int getDuration() {
return mDuration;
}

boolean isAutoDismiss() {
return mAutoDismiss;
}

boolean isShowShadow() {
return mShowShadow;
}

boolean isFitSystemWindow() {
return mFitSystemWindow;
}

boolean isFloating() {
return mFloating;
}

@NonNull
CafeBarGravity getGravity() {
return mGravity;
}

@Nullable
Drawable getIcon() {
return mIcon;
}

boolean isTintIcon() {
return mTintIcon;
}

@NonNull
String getContent() {
return mContent;
}

@Nullable
Typeface getContentTypeface() {
return mContentTypeface;
}

@Nullable
SpannableStringBuilder getSpannableStringBuilder() {
return mSpannableBuilder;
}

int getMaxLines() {
return mMaxLines;
}

int getPositiveColor() {
return mPositiveColor;
}

int getNegativeColor() {
return mNegativeColor;
}

int getNeutralColor() {
return mNeutralColor;
}

@Nullable
Typeface getPositiveTypeface() {
return mPositiveTypeface;
}

@Nullable
Typeface getNegativeTypeface() {
return mNegativeTypeface;
}

@Nullable
Typeface getNeutralTypeface() {
return mNeutralTypeface;
}

@Nullable
String getPositiveText() {
return mPositiveText;
}

@Nullable
String getNegativeText() {
return mNegativeText;
private void addTypeface(String name, Typeface typeface) {
if (!mTypefaces.containsKey(name) || mTypefaces.get(name) == null) {
mTypefaces.put(name, new WeakReference<>(typeface));
}
}

@Nullable
String getNeutralText() {
return mNeutralText;
Typeface getTypeface(String name) {
if (mTypefaces.get(name) != null) {
return mTypefaces.get(name).get();
}
return null;
}
}
}
Loading

0 comments on commit a1e4222

Please sign in to comment.