Skip to content

Commit

Permalink
Fix multi toasts exception
Browse files Browse the repository at this point in the history
  • Loading branch information
DONGChuan committed Jan 10, 2019
1 parent a5bfd58 commit 2400b0a
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -34,6 +34,6 @@ public void onToastCustomClick(View view) {
.setPaddingHorizontal(64)
.setPaddingVertical(32)
.setDelay(3000)
.show(this);
.show();
}
}
6 changes: 0 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@
android:layout_height="wrap_content"
android:text="Toast Type"/>

<Button
android:id="@+id/btn_toast_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text Toast"/>

<Button
android:id="@+id/btn_toast_image_success"
android:layout_width="match_parent"
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_toast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:onClick="onToastCustomClick"
android:text="Toast Customized"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
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;
import com.nasduck.dialoglib.toast.TextToast;

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 **************************************************************************//
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ToastBuilder> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -102,7 +102,7 @@ public Integer getCornerRadius() {

public Integer getDelay() {
if (delay == null) {
delay = 1000;
delay = 1500;
}
return delay;
}
Expand Down
21 changes: 11 additions & 10 deletions dialoglib/src/main/java/com/nasduck/dialoglib/toast/TextToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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());
}

}

0 comments on commit 2400b0a

Please sign in to comment.