Skip to content
This repository has been archived by the owner on Nov 22, 2017. It is now read-only.

Support for orientation, useful three new listeners and improvements about compatibility with ExoPlayer #86

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,28 @@ public interface DraggableListener {
* Called when the view is closed to the right.
*/
void onClosedToRight();

/**
* Called when the view is being touched at present
*/

void onTouchListener();

/**
* Called when the view is maximized with click instead of drag
*/

void clickedToMaximize();

/**
* Called when the view is minimized with click instead of drag
*/

void clickedToMinimize();

/**
* Called when the view is being dragged to either minimize or maximize
*/

void smoothSlide();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should change the naming of the methods you've added to match with the naming of the project. If you review the methods already implemented all start using on as prefix. Based on that, we should rename these last three methods to:

void onTouch()

void onClickedToMaximize()

void onClickedToMinimize();

void onSmoothSlide();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working on it.

}
117 changes: 100 additions & 17 deletions draggablepanel/src/main/java/com/github/pedrovgs/DraggableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public class DraggableView extends RelativeLayout {
private int marginBottom, marginRight;
private int dragViewId, secondViewId;

private CountDownTimer onMinimizeCountdownTimer;
private CountDownTimer onMaximizeCountdownTimer;

private boolean clickedToMaximize;
private boolean clickedToMinimize;

public DraggableView(Context context) {
super(context);
}
Expand Down Expand Up @@ -243,7 +249,7 @@ public void setTopViewResize(boolean topViewResize) {
*/
public void maximize() {
smoothSlideTo(SLIDE_TOP);
notifyMaximizeToListener();
onMaximizeCountdownTimer.start();
}

/**
Expand All @@ -252,15 +258,15 @@ public void maximize() {
*/
public void minimize() {
smoothSlideTo(SLIDE_BOTTOM);
notifyMinimizeToListener();
onMinimizeCountdownTimer.start();
}

/**
* Close the custom view applying an animation to close the view to the right side of the screen.
*/
public void closeToRight() {
if (viewDragHelper.smoothSlideViewTo(dragView, transformer.getOriginalWidth(),
getHeight() - transformer.getMinHeightPlusMargin())) {
getHeight() - transformer.getMinHeightPlusMargin())) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All this extra whitespace you have here and in the rest of the file will be gone if you format the project using the correct checkstyle.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be formatting the code with the correct checkstyle along with the other changes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks!

ViewCompat.postInvalidateOnAnimation(this);
notifyCloseToRightListener();
}
Expand All @@ -271,7 +277,7 @@ public void closeToRight() {
*/
public void closeToLeft() {
if (viewDragHelper.smoothSlideViewTo(dragView, -transformer.getOriginalWidth(),
getHeight() - transformer.getMinHeightPlusMargin())) {
getHeight() - transformer.getMinHeightPlusMargin())) {
ViewCompat.postInvalidateOnAnimation(this);
notifyCloseToLeftListener();
}
Expand Down Expand Up @@ -359,14 +365,17 @@ public boolean isClosed() {
* @return true if the touch event is realized over the drag or second view.
*/
@Override public boolean onTouchEvent(MotionEvent ev) {
listener.onTouchListener();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be useful to pass the event to the listener method.

int actionMasked = MotionEventCompat.getActionMasked(ev);
if ((actionMasked & MotionEventCompat.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
activePointerId = MotionEventCompat.getPointerId(ev, actionMasked);
}
if (activePointerId == INVALID_POINTER) {
return false;
}
viewDragHelper.processTouchEvent(ev);
if(getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
viewDragHelper.processTouchEvent(ev);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you process the touch event just if the configuration is portrait?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of usage a video player such as ExoPlayer, this processes the drag event when you try to hover the player controls, breaks the user experience about controlling the player. A control has been added for that purpose. Could be optional for people who doesn't have hover for player controls and aims to drag even on Landscape.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitively this should be configurable. You should add a onlyPortrait flag in to the attrs.xml file and use this value here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedrovgs I thought it is better to give all the options (all, landscape, portrait) for the user, in case of different needs I'll be adding a flag to support all of those to trigger touch events. You can check it out after the commit with new implementations.

if (isClosed()) {
return false;
}
Expand All @@ -390,8 +399,12 @@ private void analyzeTouchToMaximizeIfNeeded(MotionEvent ev, boolean isDragViewHi
float clickOffset = ev.getX() - lastTouchActionDownXPosition;
if (shouldMaximizeOnClick(ev, clickOffset, isDragViewHit)) {
if (isMinimized() && isClickToMaximizeEnabled()) {
listener.clickedToMaximize();
clickedToMaximize = true;
maximize();
} else if (isMaximized() && isClickToMinimizeEnabled()) {
listener.clickedToMinimize();
clickedToMinimize = true;
minimize();
}
}
Expand All @@ -403,8 +416,8 @@ private void analyzeTouchToMaximizeIfNeeded(MotionEvent ev, boolean isDragViewHi

public boolean shouldMaximizeOnClick(MotionEvent ev, float deltaX, boolean isDragViewHit) {
return (Math.abs(deltaX) < MIN_SLIDING_DISTANCE_ON_CLICK)
&& ev.getAction() != MotionEvent.ACTION_MOVE
&& isDragViewHit;
&& ev.getAction() != MotionEvent.ACTION_MOVE
&& isDragViewHit;
}

/**
Expand All @@ -419,7 +432,7 @@ public boolean shouldMaximizeOnClick(MotionEvent ev, float deltaX, boolean isDra
*/
private MotionEvent cloneMotionEventWithAction(MotionEvent event, int action) {
return MotionEvent.obtain(event.getDownTime(), event.getEventTime(), action, event.getX(),
event.getY(), event.getMetaState());
event.getY(), event.getMetaState());
}

/**
Expand Down Expand Up @@ -616,9 +629,9 @@ private boolean isViewHit(View view, int x, int y) {
int screenX = parentLocation[0] + x;
int screenY = parentLocation[1] + y;
return screenX >= viewLocation[0]
&& screenX < viewLocation[0] + view.getWidth()
&& screenY >= viewLocation[1]
&& screenY < viewLocation[1] + view.getHeight();
&& screenX < viewLocation[0] + view.getWidth()
&& screenY >= viewLocation[1]
&& screenY < viewLocation[1] + view.getHeight();
}

/**
Expand Down Expand Up @@ -659,14 +672,14 @@ private void initializeTransformer() {
private void initializeAttributes(AttributeSet attrs) {
TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.draggable_view);
this.enableHorizontalAlphaEffect =
attributes.getBoolean(R.styleable.draggable_view_enable_minimized_horizontal_alpha_effect,
DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT);
attributes.getBoolean(R.styleable.draggable_view_enable_minimized_horizontal_alpha_effect,
DEFAULT_ENABLE_HORIZONTAL_ALPHA_EFFECT);
this.enableClickToMaximize =
attributes.getBoolean(R.styleable.draggable_view_enable_click_to_maximize_view,
DEFAULT_ENABLE_CLICK_TO_MAXIMIZE);
attributes.getBoolean(R.styleable.draggable_view_enable_click_to_maximize_view,
DEFAULT_ENABLE_CLICK_TO_MAXIMIZE);
this.enableClickToMinimize =
attributes.getBoolean(R.styleable.draggable_view_enable_click_to_minimize_view,
DEFAULT_ENABLE_CLICK_TO_MINIMIZE);
attributes.getBoolean(R.styleable.draggable_view_enable_click_to_minimize_view,
DEFAULT_ENABLE_CLICK_TO_MINIMIZE);
this.topViewResize =
attributes.getBoolean(R.styleable.draggable_view_top_view_resize, DEFAULT_TOP_VIEW_RESIZE);
this.topViewHeight = attributes.getDimensionPixelSize(R.styleable.draggable_view_top_view_height,
Expand Down Expand Up @@ -694,6 +707,7 @@ private void initializeAttributes(AttributeSet attrs) {
* @return true if the view is slided.
*/
private boolean smoothSlideTo(float slideOffset) {
listener.smoothSlide();
final int topBound = getPaddingTop();
int x = (int) (slideOffset * (getWidth() - transformer.getMinWidthPlusMarginRight()));
int y = (int) (topBound + slideOffset * getVerticalDragRange());
Expand Down Expand Up @@ -784,4 +798,73 @@ private void notifyCloseToLeftListener() {
public int getDraggedViewHeightPlusMarginTop() {
return transformer.getMinHeightPlusMargin();
}

public void initializeTimers(){

onMaximizeCountdownTimer = new CountDownTimer(2000, 10) {
@Override
public void onTick(long l) {

if (getSecondViewAlpha() > 0.95) {
notifyMaximizeToListener();
new Handler().post(new Runnable() {
@Override
public void run() {
onMaximizeCountdownTimer.cancel();
}
});
}
}

@Override
public void onFinish() {

}
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rencsaridogan this is not the way to go to implement the listener mechanism. You are using a polling mechanism to notify your listener and depending on the device this approach will notify at the second X or X+1. You should notify your listener close to the method where the alpha is modified and remove the usage of this count down timer.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Since the alpha is modified constantly in that case, removing the extra timers and handling the notifiers within the alpha change sounds logical. I'll be implementing and testing that version. Thanks!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rencsaridogan if you add an example to the project using exo player could be great. We could use it to improve the library usage combined with this component.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedrovgs While I'm coding the changes through, I will add a sample for ExoPlayer along with others for the purpose of improving usage of both components together. I believe since industry is focused on using ExoPlayer based players, so this could be great to give it a full support!


onMinimizeCountdownTimer = new CountDownTimer(2000,10) {
@Override
public void onTick(long l) {
if (getSecondViewAlpha() < 0.15) {
notifyMinimizeToListener();
new Handler().post(new Runnable() {
@Override
public void run() {
onMinimizeCountdownTimer.cancel();
}
});
}
}

@Override
public void onFinish() {

}
};
}

public float getSecondViewAlpha(){
return secondView.getAlpha();
}

public boolean isClickedMaximize(){
return clickedToMaximize;
}

public void setClickedtoMaximize(){
clickedToMaximize = false;
}

public boolean isClickedMinimize(){
return clickedToMinimize;
}

public void minimizeButtonClicked(){
clickedToMinimize = true;
}

public void setClickedtoMinimize(){
clickedToMinimize = false;
}

}