diff --git a/app/src/main/java/com/nasduck/duckandroiddialog/TestActivity.java b/app/src/main/java/com/nasduck/duckandroiddialog/TestActivity.java
index 64fe426..af43ef6 100644
--- a/app/src/main/java/com/nasduck/duckandroiddialog/TestActivity.java
+++ b/app/src/main/java/com/nasduck/duckandroiddialog/TestActivity.java
@@ -40,13 +40,6 @@ public void onToastDefaultTypeClick() {
startActivity(intentToToastDefaultType);
}
- @OnClick(R.id.btn_toast_text)
- public void onTextToast() {
- DuckDialog.showToast(this, "hello world!!");
- //DuckDialog.assignTextToast("hello world!!")
- // .show(this);
- }
-
@OnClick(R.id.btn_toast_image)
public void onImageToast() {
DuckDialog.assignImageToast()
diff --git a/app/src/main/java/com/nasduck/duckandroiddialog/ToastActivity.java b/app/src/main/java/com/nasduck/duckandroiddialog/ToastActivity.java
index 82a2875..62e9240 100644
--- a/app/src/main/java/com/nasduck/duckandroiddialog/ToastActivity.java
+++ b/app/src/main/java/com/nasduck/duckandroiddialog/ToastActivity.java
@@ -25,7 +25,7 @@ public void onToastDefaultClick(View view) {
}
public void onToastCustomClick(View view) {
- ToastBuilder.create()
+ ToastBuilder.create(this)
.setText("Toast Customized")
.setTextSize(16)
.setTextColor(android.R.color.holo_orange_dark)
@@ -34,6 +34,6 @@ public void onToastCustomClick(View view) {
.setPaddingHorizontal(64)
.setPaddingVertical(32)
.setDelay(3000)
- .show(this);
+ .show();
}
}
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 4f15bfd..400a801 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -19,12 +19,6 @@
android:layout_height="wrap_content"
android:text="Toast Type"/>
-
-
diff --git a/dialoglib/src/main/java/com/nasduck/dialoglib/base/DuckDialog.java b/dialoglib/src/main/java/com/nasduck/dialoglib/base/DuckDialog.java
index 54be030..7908132 100644
--- a/dialoglib/src/main/java/com/nasduck/dialoglib/base/DuckDialog.java
+++ b/dialoglib/src/main/java/com/nasduck/dialoglib/base/DuckDialog.java
@@ -46,9 +46,9 @@ public static void hide(FragmentActivity activity, String tag) {
}
public static void showToast(final FragmentActivity activity, String contentText) {
- ToastBuilder.create()
+ ToastBuilder.create(activity)
.setText(contentText)
- .show(activity);
+ .show();
}
/**
diff --git a/dialoglib/src/main/java/com/nasduck/dialoglib/builder/ToastBuilder.java b/dialoglib/src/main/java/com/nasduck/dialoglib/builder/ToastBuilder.java
index 9a489be..6829a2b 100644
--- a/dialoglib/src/main/java/com/nasduck/dialoglib/builder/ToastBuilder.java
+++ b/dialoglib/src/main/java/com/nasduck/dialoglib/builder/ToastBuilder.java
@@ -1,7 +1,8 @@
package com.nasduck.dialoglib.builder;
-import android.os.Handler;
+import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
import com.nasduck.dialoglib.base.DuckDialog;
import com.nasduck.dialoglib.config.ToastConfig;
@@ -9,30 +10,47 @@
public class ToastBuilder {
+ private final static String TAG = "toast";
private ToastConfig mConfig;
+ private FragmentActivity mActivity;
- private ToastBuilder(ToastConfig config) {
+ private static ToastHandler mHandler = new ToastHandler();
+
+ private ToastBuilder(FragmentActivity activity, ToastConfig config) {
this.mConfig = config;
+ this.mActivity = activity;
+ this.mHandler.set(this);
}
- public static ToastBuilder create() { return create(ToastConfig.getInstance()); }
+ public static ToastBuilder create(FragmentActivity activity) { return create(activity, ToastConfig.getInstance()); }
+
+ public static ToastBuilder create(FragmentActivity activity, ToastConfig config) {
+ return new ToastBuilder(activity, config);
+ }
- public static ToastBuilder create(ToastConfig config) {
- return new ToastBuilder(config);
+ public void show() {
+ show(getDelay());
}
- public void show(final FragmentActivity activity) {
- show(activity, getDelay());
+ public void show(int delayMillis) {
+ mHandler.removeMessages(ToastHandler.MSG_SHOW);
+
+ FragmentManager manager = this.mActivity.getSupportFragmentManager();
+ Fragment fragment = manager.findFragmentByTag(TAG);
+ if (fragment != null) {
+ // Update Toast existing
+ ((TextToast)fragment).updateUI(this.mConfig);
+ } else {
+ // Show Toast
+ TextToast.create(this.mConfig).show(this.mActivity.getSupportFragmentManager(), TAG);
+ }
+
+ // Send msg to dismiss dialog with delay
+ mHandler.sendEmptyMessageDelayed(ToastHandler.MSG_SHOW, delayMillis);
}
- public void show(final FragmentActivity activity, int delayMillis) {
- TextToast.create(mConfig).show(activity.getSupportFragmentManager(), "toast");
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- DuckDialog.hide(activity, "toast");
- }
- }, delayMillis);
+ public void hide() {
+ DuckDialog.hide(mActivity, TAG);
}
//* Getter & Setter **************************************************************************//
diff --git a/dialoglib/src/main/java/com/nasduck/dialoglib/builder/ToastHandler.java b/dialoglib/src/main/java/com/nasduck/dialoglib/builder/ToastHandler.java
new file mode 100644
index 0000000..f7613db
--- /dev/null
+++ b/dialoglib/src/main/java/com/nasduck/dialoglib/builder/ToastHandler.java
@@ -0,0 +1,30 @@
+package com.nasduck.dialoglib.builder;
+
+import android.os.Handler;
+import android.os.Message;
+
+import java.lang.ref.WeakReference;
+
+public class ToastHandler extends Handler {
+
+ public static final int MSG_SHOW = 999;
+
+ private WeakReference mBuilderRef;
+
+ public ToastHandler() {}
+
+ public void set(ToastBuilder builder) {
+ this.mBuilderRef = new WeakReference<>(builder);
+ }
+
+ @Override
+ public void handleMessage(Message msg) {
+ switch (msg.what) {
+ case MSG_SHOW:
+ this.removeMessages(MSG_SHOW);
+ ToastBuilder builder = mBuilderRef.get();
+ if (builder != null) builder.hide();
+ break;
+ }
+ }
+}
diff --git a/dialoglib/src/main/java/com/nasduck/dialoglib/config/ToastConfig.java b/dialoglib/src/main/java/com/nasduck/dialoglib/config/ToastConfig.java
index 7117490..8d0e4dc 100644
--- a/dialoglib/src/main/java/com/nasduck/dialoglib/config/ToastConfig.java
+++ b/dialoglib/src/main/java/com/nasduck/dialoglib/config/ToastConfig.java
@@ -12,7 +12,7 @@ public class ToastConfig implements Parcelable {
private Integer textSize; // 16
private Integer bgColor ; // 99000000
private Integer cornerRadius; // 3
- private Integer delay; // 1000
+ private Integer delay; // 1500
private Integer paddingHorizontal; // 32
private Integer paddingVertical; // 6
@@ -102,7 +102,7 @@ public Integer getCornerRadius() {
public Integer getDelay() {
if (delay == null) {
- delay = 1000;
+ delay = 1500;
}
return delay;
}
diff --git a/dialoglib/src/main/java/com/nasduck/dialoglib/toast/TextToast.java b/dialoglib/src/main/java/com/nasduck/dialoglib/toast/TextToast.java
index b1fbc29..6ca5fa7 100644
--- a/dialoglib/src/main/java/com/nasduck/dialoglib/toast/TextToast.java
+++ b/dialoglib/src/main/java/com/nasduck/dialoglib/toast/TextToast.java
@@ -9,7 +9,6 @@
import com.nasduck.dialoglib.base.BaseToast;
import com.nasduck.dialoglib.R;
import com.nasduck.dialoglib.config.ToastConfig;
-import com.nasduck.dialoglib.utils.DensityUtils;
public class TextToast extends BaseToast {
@@ -47,26 +46,28 @@ public void initData(@Nullable Bundle savedInstanceState) {
mTvContent = view.findViewById(R.id.tv_content);
mLayoutBackground = view.findViewById(R.id.background);
+ updateUI(this.mConfig);
+ }
+
+ public void updateUI(ToastConfig config) {
// Corner Radius && Background Color
GradientDrawable drawable = new GradientDrawable();
- drawable.setCornerRadius(mConfig.getCornerRadius());
- drawable.setColor(mContext.getResources().getColor(mConfig.getBackgroundColor()));
+ drawable.setCornerRadius(config.getCornerRadius());
+ drawable.setColor(mContext.getResources().getColor(config.getBackgroundColor()));
mLayoutBackground.setBackground(drawable);
// Text
- mTvContent.setText(mConfig.getText());
+ mTvContent.setText(config.getText());
// Text Size
- mTvContent.setTextSize(mConfig.getTextSize());
+ mTvContent.setTextSize(config.getTextSize());
// Text Color
- mTvContent.setTextColor(getResources().getColor(mConfig.getTextColor()));
+ mTvContent.setTextColor(getResources().getColor(config.getTextColor()));
// Padding
- mLayoutBackground.setPadding(mConfig.getPaddingHorizontal(), mConfig.getPaddingVertical(),
- mConfig.getPaddingHorizontal(), mConfig.getPaddingVertical());
-
-
+ mLayoutBackground.setPadding(config.getPaddingHorizontal(), config.getPaddingVertical(),
+ config.getPaddingHorizontal(), config.getPaddingVertical());
}
}