Skip to content

Commit

Permalink
Optimized Typeface customization.
Browse files Browse the repository at this point in the history
  • Loading branch information
GrenderG committed Apr 29, 2017
1 parent b1713c9 commit bae332f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 32 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/es/dmoral/toastysample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package es.dmoral.toastysample;

import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -85,7 +86,7 @@ public void onClick(View view) {
public void onClick(View view) {
Toasty.Config.getInstance()
.setTextColor(Color.GREEN)
.setToastTypeface(getAssets(), "PCap Terminal.otf")
.setToastTypeface(Typeface.createFromAsset(getAssets(), "PCap Terminal.otf"))
.apply();
Toasty.custom(MainActivity.this, "sudo kill -9 everyone", getResources().getDrawable(R.drawable.laptop512),
Color.BLACK, Toast.LENGTH_SHORT, true, true).show();
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ ext {

supportLibVersion = '25.3.1'

versionCode = 122
versionName = '1.2.2'
versionCode = 123
versionName = '1.2.3'
}

task clean(type: Delete) {
Expand Down
40 changes: 11 additions & 29 deletions toasty/src/main/java/es/dmoral/toasty/Toasty.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -46,8 +45,9 @@ public class Toasty {
@ColorInt
private static int WARNING_COLOR = Color.parseColor("#FFA900");

private static String TOAST_TYPEFACE = "sans-serif-condensed";
private static AssetManager assetManager = null;
private static final Typeface LOADED_TOAST_TYPEFACE = Typeface.create("sans-serif-condensed", Typeface.NORMAL);
private static Typeface currentTypeface = LOADED_TOAST_TYPEFACE;

private static boolean tintIcon = true;

private Toasty() {
Expand Down Expand Up @@ -188,19 +188,14 @@ public static Toast custom(@NonNull Context context, @NonNull CharSequence messa

toastTextView.setTextColor(DEFAULT_TEXT_COLOR);
toastTextView.setText(message);
if (assetManager == null)
toastTextView.setTypeface(Typeface.create(TOAST_TYPEFACE, Typeface.NORMAL));
else
toastTextView.setTypeface(Typeface.createFromAsset(assetManager, TOAST_TYPEFACE));
toastTextView.setTypeface(currentTypeface);

currentToast.setView(toastLayout);
currentToast.setDuration(duration);
return currentToast;
}

public static class Config {
private static Config configInstance;

@ColorInt
private int DEFAULT_TEXT_COLOR = Toasty.DEFAULT_TEXT_COLOR;
@ColorInt
Expand All @@ -212,8 +207,8 @@ public static class Config {
@ColorInt
private int WARNING_COLOR = Toasty.WARNING_COLOR;

private String TOAST_TYPEFACE = Toasty.TOAST_TYPEFACE;
private AssetManager assetManager = Toasty.assetManager;
private Typeface typeface = Toasty.currentTypeface;

private boolean tintIcon = Toasty.tintIcon;

private Config() {
Expand All @@ -222,9 +217,7 @@ private Config() {

@CheckResult
public static Config getInstance() {
if (configInstance == null)
configInstance = new Config();
return configInstance;
return new Config();
}

public static void reset() {
Expand All @@ -233,8 +226,7 @@ public static void reset() {
Toasty.INFO_COLOR = Color.parseColor("#3F51B5");
Toasty.SUCCESS_COLOR = Color.parseColor("#388E3C");
Toasty.WARNING_COLOR = Color.parseColor("#FFA900");
Toasty.assetManager = null;
Toasty.TOAST_TYPEFACE = "sans-serif-condensed";
Toasty.currentTypeface = LOADED_TOAST_TYPEFACE;
Toasty.tintIcon = true;
}

Expand Down Expand Up @@ -269,16 +261,8 @@ public Config setWarningColor(@ColorInt int warningColor) {
}

@CheckResult
public Config setToastTypeface(AssetManager assetManager, @NonNull String fontPath) {
this.assetManager = assetManager;
TOAST_TYPEFACE = fontPath;
return this;
}

@CheckResult
public Config setToastTypeface(@NonNull String toastTypefaceFamily) {
this.assetManager = null;
TOAST_TYPEFACE = toastTypefaceFamily;
public Config setToastTypeface(@NonNull Typeface typeface) {
this.typeface = typeface;
return this;
}

Expand All @@ -294,10 +278,8 @@ public void apply() {
Toasty.INFO_COLOR = INFO_COLOR;
Toasty.SUCCESS_COLOR = SUCCESS_COLOR;
Toasty.WARNING_COLOR = WARNING_COLOR;
Toasty.assetManager = assetManager;
Toasty.TOAST_TYPEFACE = TOAST_TYPEFACE;
Toasty.currentTypeface = typeface;
Toasty.tintIcon = tintIcon;
configInstance = null;
}
}
}

0 comments on commit bae332f

Please sign in to comment.