Skip to content

Commit

Permalink
Moving all logic into KonamiCodeLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagokimo committed Jul 29, 2015
1 parent d13f7d7 commit a53151f
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 348 deletions.
142 changes: 15 additions & 127 deletions library/src/main/java/io/kimo/konamicode/KonamiCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,36 @@
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import io.kimo.konamicode.listener.ButtonListener;
import io.kimo.konamicode.listener.DirectionListener;
import android.widget.FrameLayout;

public class KonamiCode {

private Context mContext;
private ViewGroup mRootView;
private AlertDialog mButtonDialog;
private int mSwipeThreshold;
private int mSwipeVelocityThreshold;
private DirectionListener mDirectionsListener;
private ButtonListener mButtonListener;
private Callback mCallback;

public AlertDialog getButtonDialog() {
return mButtonDialog;
}
private KonamiCodeLayout.Callback mCallback;

public ButtonListener getButtonListener() {
return mButtonListener;
}

public Callback getCallback() {
public KonamiCodeLayout.Callback getCallback() {
return mCallback;
}

public Context getContext() {
return mContext;
}

public DirectionListener getDirectionsListener() {
return mDirectionsListener;
}

public int getSwipeThreshold() {
return mSwipeThreshold;
}

public int getSwipeVelocityThreshold() {
return mSwipeVelocityThreshold;
}

public ViewGroup getRootView() {
return mRootView;
}

private KonamiCode(@NonNull Builder builder) {
this.mContext = builder.context;
this.mRootView = builder.rootView;
this.mSwipeThreshold = builder.swipeThreshold;
this.mSwipeVelocityThreshold = builder.swipeVelocityThreshold;
this.mDirectionsListener = builder.directionsListener;
this.mButtonListener = builder.buttonListener;
this.mButtonDialog = builder.dialog;
this.mCallback = builder.callback;
}

public static class Builder {

public static final int DEFAULT_THRESHOLD_VALUE = 150;

private GestureDetectorCompat gestureListener;

protected Context context;
protected ViewGroup rootView;
protected DirectionListener directionsListener;
protected ButtonListener buttonListener;
protected int swipeThreshold = DEFAULT_THRESHOLD_VALUE;
protected int swipeVelocityThreshold = DEFAULT_THRESHOLD_VALUE;
protected AlertDialog dialog;
protected Callback callback = new Callback() {
@Override
public void onFinish() {
Toast.makeText(context, R.string.default_callback_msg, Toast.LENGTH_LONG).show();
dialog.dismiss();
}
};
protected boolean hasDrawerLayout;
protected KonamiCodeLayout.Callback callback;

/**
* Konami Code's builder
Expand Down Expand Up @@ -127,34 +70,11 @@ public Builder into(@NonNull View view) {
return this;
}

public Builder withDrawerLayout(boolean hasDrawerLayout) {
this.hasDrawerLayout = hasDrawerLayout;
return this;
}

/**
* withSwipeThreshold - distance of the swipe movment
* @param value
*/
public Builder withSwipeThreshold(int value) {
swipeThreshold = value;
return this;
}

/**
* withSwipeVelocityThreshold - speed of the swipe movment
* @param value
*/
public Builder withSwipeVelocityThreshold(int value) {
swipeVelocityThreshold = value;
return this;
}

/**
* withCallback - interface executed after the whole code is executed
* @param callback
*/
public Builder withCallback(@NonNull Callback callback) {
public Builder withCallback(@NonNull KonamiCodeLayout.Callback callback) {
this.callback = callback;
return this;
}
Expand All @@ -164,53 +84,21 @@ public Builder withCallback(@NonNull Callback callback) {
*/
public KonamiCode install() {

configureButtonsDialog();
configureGestureListener();

return new KonamiCode(this);
}

private void configureButtonsDialog() {
View currentView = rootView.getChildAt(0);
rootView.removeView(currentView);

View buttonsView = LayoutInflater.from(context).inflate(R.layout.dialog_buttons, rootView, false);
KonamiCodeLayout konamiCodeLayout = new KonamiCodeLayout(context);
konamiCodeLayout.addView(currentView);

View aButton = buttonsView.findViewById(R.id.konami_button_a);
View bButton = buttonsView.findViewById(R.id.konami_button_b);
View startButton = buttonsView.findViewById(R.id.konami_button_start);
rootView.addView(konamiCodeLayout, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT)
);

dialog = new AlertDialog.Builder(context)
.setView(buttonsView)
.create();
konamiCodeLayout.setCallback(callback);

buttonListener = new ButtonListener(aButton, bButton, startButton, dialog, callback);
}

private void configureGestureListener() {
directionsListener = new DirectionListener(dialog, swipeThreshold, swipeVelocityThreshold);
gestureListener = new GestureDetectorCompat(context, directionsListener);

View targetView;
if (hasDrawerLayout) {
DrawerLayout drawerLayout = (DrawerLayout) rootView.getChildAt(0);
targetView = drawerLayout.getChildAt(0);
} else {
targetView = rootView.getChildAt(0);
}

targetView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
gestureListener.onTouchEvent(event);
return true;
}
});
return new KonamiCode(this);
}
}

/**
* Callback - Interface that's executed when the code finishes
*/
public interface Callback {
void onFinish();
}
}
Loading

0 comments on commit a53151f

Please sign in to comment.