Skip to content

Commit

Permalink
Preparing InfoDialog and PromptDialog for inheritance, assing BounceP…
Browse files Browse the repository at this point in the history
…romptDialog example and fixing some bugs
  • Loading branch information
techyourchance committed May 11, 2019
1 parent 2334453 commit e9172b5
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.BounceInterpolator;

public abstract class BaseDialog extends DialogFragment {

Expand All @@ -24,19 +23,17 @@ public abstract class BaseDialog extends DialogFragment {
private static final String SAVED_STATE_ENTER_ANIMATION = "SAVED_STATE_ENTER_ANIMATION";
private static final String SAVED_STATE_EXIT_ANIMATION = "SAVED_STATE_EXIT_ANIMATION";

private static final String COLOR_TRANSPARENT_HEX = "#00000000";

private DialogAnimatorsFactory mDialogAnimatorsFactory;

private boolean firstOnStart = true;
protected boolean mFirstOnStart = true;

private @Nullable DialogEnterAnimation mEnterAnimation;
private int mEnterAnimationDurationMs = DEFAULT_ENTER_ANIMATION_DURATION_MS;

private @Nullable DialogExitAnimation mExitAnimation;
private int mExitAnimationDurationMs = DEFAULT_EXIT_ANIMATION_DURATION_MS;

private boolean playingExitAnimation;
protected boolean mPlayingExitAnimation;

private @Nullable AlertDialog mAlertDialog;
private @Nullable View.OnClickListener mAlertDialogPositiveButtonListener;
Expand All @@ -47,7 +44,7 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDialogAnimatorsFactory = new DialogAnimatorsFactory();
if (savedInstanceState != null) {
firstOnStart = savedInstanceState.getBoolean(SAVED_STATE_FIRST_ON_START);
mFirstOnStart = savedInstanceState.getBoolean(SAVED_STATE_FIRST_ON_START);
mEnterAnimation = (DialogEnterAnimation) savedInstanceState.getSerializable(SAVED_STATE_ENTER_ANIMATION);
mExitAnimation = (DialogExitAnimation) savedInstanceState.getSerializable(SAVED_STATE_EXIT_ANIMATION);
}
Expand All @@ -56,22 +53,27 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_STATE_FIRST_ON_START, firstOnStart);
outState.putBoolean(SAVED_STATE_FIRST_ON_START, mFirstOnStart);
outState.putSerializable(SAVED_STATE_ENTER_ANIMATION, mEnterAnimation);
outState.putSerializable(SAVED_STATE_EXIT_ANIMATION, mExitAnimation);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
public void onStart() {
super.onStart();
if (mFirstOnStart) {

executeHackToPreventSystemNavigationUiToBlink();

executeHackToPreventSystemNavigationUiToBlink();
// currently cancellable dialogs not supported; see setCancellable method
super.setCancelable(false);

// dialog fragments have white bg by default, so this is needed to support transparent dialogs
getDialogWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor(COLOR_TRANSPARENT_HEX)));
setAlertDialogListenersIfNeeded();

playEnterAnimationIfNeeded();

// currently cancellable dialogs not supported; see setCancellable method
super.setCancelable(false);
mFirstOnStart = false;
}
}

private void executeHackToPreventSystemNavigationUiToBlink() {
Expand Down Expand Up @@ -116,16 +118,6 @@ public void setCancelable(boolean cancelable) {
}
}

@Override
public void onStart() {
super.onStart();
if (firstOnStart) {
playEnterAnimationIfNeeded();
setAlertDialogListenersIfNeeded();
firstOnStart = false;
}
}

private void playEnterAnimationIfNeeded() {
if (mEnterAnimation == null) {
return;
Expand Down Expand Up @@ -163,7 +155,7 @@ private void setAlertDialogListenersIfNeeded() {

@Override
public void dismiss() {
if (playingExitAnimation) {
if (mPlayingExitAnimation) {
return;
}
playExitAnimationIfNeededWithEndRunnable(new Runnable() {
Expand All @@ -176,7 +168,7 @@ public void run() {

@Override
public void dismissAllowingStateLoss() {
if (playingExitAnimation) {
if (mPlayingExitAnimation) {
return;
}
playExitAnimationIfNeededWithEndRunnable(new Runnable() {
Expand All @@ -201,7 +193,7 @@ public void onAnimationStart(Animator animation) {}

@Override
public void onAnimationEnd(Animator animation) {
playingExitAnimation = false;
mPlayingExitAnimation = false;
postAnimationRunnable.run();
}

Expand All @@ -213,7 +205,7 @@ public void onAnimationRepeat(Animator animation) {}
});

animator.start();
playingExitAnimation = true;
mPlayingExitAnimation = true;
}

public void setEnterAnimation(@Nullable DialogEnterAnimation enterAnimation) {
Expand Down Expand Up @@ -281,6 +273,15 @@ public ObjectAnimator newEnterAnimator(DialogEnterAnimation enterAnimation, long
0
);
break;
case SLIDE_IN_AND_BOUNCE_FROM_TOP:
animator = ObjectAnimator.ofFloat(
getDialogDecorView(),
"translationY",
-requireActivity().getWindow().getDecorView().getHeight(),
0
);
animator.setInterpolator(new BounceInterpolator());
break;
default:
throw new RuntimeException("unhandled enter animation: " + enterAnimation);
}
Expand All @@ -304,6 +305,14 @@ public ObjectAnimator newExitAnimator(DialogExitAnimation exitAnimation, long du
-requireActivity().getWindow().getDecorView().getWidth()
);
break;
case SLIDE_OUT_FROM_BOTTOM:
animator = ObjectAnimator.ofFloat(
getDialogDecorView(),
"translationY",
0,
requireActivity().getWindow().getDecorView().getHeight()
);
break;
default:
throw new RuntimeException("unhandled exit animation: " + exitAnimation);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.techyourchance.dialoghelpersample;

public enum DialogEnterAnimation {
SLIDE_IN_FROM_RIGHT
SLIDE_IN_FROM_RIGHT, SLIDE_IN_AND_BOUNCE_FROM_TOP
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package com.techyourchance.dialoghelpersample;

public enum DialogExitAnimation {
SLIDE_OUT_FROM_LEFT
SLIDE_OUT_FROM_LEFT, SLIDE_OUT_FROM_BOTTOM
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.widget.Toast;

import com.techyourchance.dialoghelper.DialogHelper;
import com.techyourchance.dialoghelpersample.bouncedialog.BouncePromptDialog;
import com.techyourchance.dialoghelpersample.infodialog.InfoDialog;
import com.techyourchance.dialoghelpersample.infodialog.InfoDialogDismissedEvent;
import com.techyourchance.dialoghelpersample.promptdialog.PromptDialog;
Expand All @@ -22,6 +23,7 @@ public class SampleActivity extends AppCompatActivity {
private static final String DIALOG_ID_PROMPT = "DIALOG_ID_PROMPT";
private static final String DIALOG_ID_FIRST_IN_CHAIN = "DIALOG_ID_FIRST_IN_CHAIN";
private static final String DIALOG_ID_SECOND_IN_CHAIN = "DIALOG_ID_SECOND_IN_CHAIN";
private static final String DIALOG_ID_BOUNCE = "DIALOG_ID_BOUNCE";

private DialogHelper mDialogHelper;

Expand All @@ -34,6 +36,7 @@ public class SampleActivity extends AppCompatActivity {

private AppCompatButton mBtnShowInfoDialog;
private AppCompatButton mBtnShowPromptDialog;
private AppCompatButton mBtnShowBouncePromptDialog;
private AppCompatButton mBtnShowDialogsChain;

@Override
Expand All @@ -48,6 +51,7 @@ protected void onCreate(Bundle savedInstanceState) {
mBtnShowInfoDialog = findViewById(R.id.btn_show_info_dialog);
mBtnShowPromptDialog = findViewById(R.id.btn_show_prompt_dialog);
mBtnShowDialogsChain = findViewById(R.id.btn_show_dialogs_chain);
mBtnShowBouncePromptDialog = findViewById(R.id.btn_show_bounce_prompt_dialog);

registerButtonListeners();
}
Expand Down Expand Up @@ -96,6 +100,14 @@ public void onClick(View v) {
mDialogHelper.showDialog(promptDialog, DIALOG_ID_FIRST_IN_CHAIN);
}
});

mBtnShowBouncePromptDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BouncePromptDialog bouncePromptDialog = BouncePromptDialog.newBouncePromptDialog();
mDialogHelper.showDialog(bouncePromptDialog, DIALOG_ID_BOUNCE);
}
});
}

@Override
Expand Down Expand Up @@ -144,6 +156,9 @@ public void onEvent(PromptDialogDismissedEvent event) {
Toast.makeText(this, "Dialogs chain cancelled", Toast.LENGTH_LONG).show();
}
break;
case DIALOG_ID_BOUNCE:
Toast.makeText(this, "Don't forget to try another option as well", Toast.LENGTH_LONG).show();
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.techyourchance.dialoghelpersample.bouncedialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;

import com.techyourchance.dialoghelpersample.DialogEnterAnimation;
import com.techyourchance.dialoghelpersample.DialogExitAnimation;
import com.techyourchance.dialoghelpersample.promptdialog.PromptDialog;
import com.techyourchance.dialoghelpersample.promptdialog.PromptDialogDismissedEvent;

public class BouncePromptDialog extends PromptDialog {

public static BouncePromptDialog newBouncePromptDialog() {
BouncePromptDialog bouncePromptDialog = new BouncePromptDialog();
Bundle args = new Bundle(4);
args.putString(ARG_TITLE, "Bounce dialog");
args.putString(ARG_MESSAGE, "Which direction would you like the dialog to exit?");
args.putString(ARG_POSITIVE_BUTTON_CAPTION, "Left");
args.putString(ARG_NEGATIVE_BUTTON_CAPTION, "Bottom");
bouncePromptDialog.setArguments(args);
bouncePromptDialog.setEnterAnimation(DialogEnterAnimation.SLIDE_IN_AND_BOUNCE_FROM_TOP);
return bouncePromptDialog;
}



@Override
protected void onPositiveButtonClicked() {
setExitAnimation(DialogExitAnimation.SLIDE_OUT_FROM_LEFT);
dismiss();
mEventBus.post(
new PromptDialogDismissedEvent(
mDialogHelper.getDialogId(this),
PromptDialogDismissedEvent.ClickedButton.POSITIVE
)
);
}

@Override
protected void onNegativeButtonClicked() {
setExitAnimation(DialogExitAnimation.SLIDE_OUT_FROM_BOTTOM);
dismiss();
mEventBus.post(
new PromptDialogDismissedEvent(
mDialogHelper.getDialogId(this),
PromptDialogDismissedEvent.ClickedButton.NEGATIVE
)
);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.techyourchance.dialoghelpersample.infodialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.View;

import com.techyourchance.dialoghelper.DialogHelper;
Expand All @@ -20,9 +17,9 @@
*/
public class InfoDialog extends BaseDialog {

private static final String ARG_TITLE = "ARG_TITLE";
private static final String ARG_MESSAGE = "ARG_MESSAGE";
private static final String ARG_BUTTON_CAPTION = "ARG_BUTTON_CAPTION";
protected static final String ARG_TITLE = "ARG_TITLE";
protected static final String ARG_MESSAGE = "ARG_MESSAGE";
protected static final String ARG_BUTTON_CAPTION = "ARG_BUTTON_CAPTION";

public static InfoDialog newInfoDialog(String title, String message, String buttonCaption) {
InfoDialog infoDialog = new InfoDialog();
Expand All @@ -34,19 +31,19 @@ public static InfoDialog newInfoDialog(String title, String message, String butt
return infoDialog;
}

private EventBus mEventBus;
private DialogHelper mDialogHelper;
protected EventBus mEventBus;
protected DialogHelper mDialogHelper;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
public final void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mEventBus = EventBus.getDefault();
mDialogHelper = new DialogHelper(requireActivity().getSupportFragmentManager());
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
public final Dialog onCreateDialog(Bundle savedInstanceState) {
if (getArguments() == null) {
throw new IllegalStateException("arguments mustn't be null");
}
Expand All @@ -59,17 +56,16 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
onButtonClicked();
}
},
null,
null
);
}

@Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
protected void onButtonClicked() {
dismiss();
mEventBus.post(new InfoDialogDismissedEvent(mDialogHelper.getDialogId(this)));
}

Expand Down
Loading

0 comments on commit e9172b5

Please sign in to comment.