Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added singletap event to android module. #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
83 changes: 39 additions & 44 deletions android/src/ti/draggable/ViewProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package ti.draggable;

Expand All @@ -38,15 +38,15 @@
@Kroll.proxy(creatableInModule = DraggableModule.class)
public class ViewProxy extends TiViewProxy {
private static final String LCAT = "TiDraggable";

public TiCompositeLayout view;
private ViewProxy _proxy;
private TiCompositeLayout.LayoutParams _layout;
private float oldTop = 0;
private float oldLeft = 0;
private String directionVertical = "neutral";
private String directionHorizontal = "neutral";

private float positionTop = 0;
private float positionLeft = 0;
private float tempTop = 0;
Expand All @@ -63,13 +63,8 @@ public class ViewProxy extends TiViewProxy {
private boolean hasAxisY = false;
private boolean isDraggable = true;

// Still working on this
private boolean hasListenerStart = false;
private boolean hasListenerMove = false;
private boolean hasListenerEnd = false;

public ViewProxy() {}

public ViewProxy(TiContext tiContext) {
this();
_proxy = this;
Expand Down Expand Up @@ -115,6 +110,9 @@ public PEDraggableView(TiViewProxy proxy) {
* It can really impact the performance
*/
OnTouchListener listener = new OnTouchListener() {

private boolean wasMoved = false;

@Override
public boolean onTouch(View v, MotionEvent event) {
// Log.d(LCAT, "Event: "+event.toString());
Expand All @@ -130,35 +128,22 @@ public boolean onTouch(View v, MotionEvent event) {
// Get the "raw" x and y - which is the x and y in relation to the screen
// And declare the ints and reuse them
float eventX = Math.round(event.getRawX()),
eventY = Math.round(event.getRawY()),
_left = 0,
_top = 0;
eventY = Math.round(event.getRawY()),
_left = 0,
_top = 0;

// What to do in each case??
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Log.d(LCAT, "MotionEvent.ACTION_DOWN");

// Check for event listeners here, we need to be sure that the JS is loaded
// Also, store in boolean variable, we don't need to check every time, do we?
// I also do my check here in the ACTION_DOWN because it only gets called once on each move
if (hasListenerStart == false) {
hasListenerStart = _proxy.hasListeners("start");
}
if (hasListenerMove == false) {
hasListenerMove = _proxy.hasListeners("move");
}
if (hasListenerEnd == false) {
hasListenerEnd = _proxy.hasListeners("end");
}

// Get the difference between the touch and the left/top of the view
// Get the difference between the touch and the left/top of the view
tempLeft = (positionLeft - eventX);
tempTop = (positionTop - eventY);

// Log.d(LCAT, "MotionEvent.ACTION_DOWN - tempLeft: "+tempLeft);
// Log.d(LCAT, "MotionEvent.ACTION_DOWN - tempTop: "+tempTop);
if (hasListenerStart) {
if (_proxy.hasListeners("start")) {
KrollDict props = new KrollDict();
props.put("left", positionLeft);
props.put("top", positionTop);
Expand All @@ -169,10 +154,13 @@ public boolean onTouch(View v, MotionEvent event) {
_proxy.fireEvent("start", props);
}

break;
break;
case MotionEvent.ACTION_MOVE:
// Log.d(LCAT, "MotionEvent.ACTION_MOVE");

// If the item is moved, updated wasMoved instance variable.
wasMoved = true;

// If axis "y", leave the left position intact
if (_proxy.hasAxisY) {
_left = positionLeft;
Expand All @@ -181,11 +169,11 @@ public boolean onTouch(View v, MotionEvent event) {
// otherwise, adjust the "left" variable
else {
_left = (eventX + tempLeft);
// Don't move more that maxLeft
// Don't move more that maxLeft
if (_proxy.hasMaxLeft && _proxy.maxLeft < _left) {
_left = _proxy.maxLeft;
}
// Don't move more that minLeft
// Don't move more that minLeft
if (_proxy.hasMinLeft && _proxy.minLeft > _left) {
_left = _proxy.minLeft;
}
Expand All @@ -200,11 +188,11 @@ public boolean onTouch(View v, MotionEvent event) {
// otherwise, adjust the "top" variable
else {
_top = (eventY + tempTop);
// Don't move more that maxTop
// Don't move more that maxTop
if (_proxy.hasMaxTop == true && _proxy.maxTop < _top) {
_top = _proxy.maxTop;
}
// Don't move more that minTop
// Don't move more that minTop
if (_proxy.hasMinTop == true && _proxy.minTop > _top) {
_top = _proxy.minTop;
}
Expand Down Expand Up @@ -232,7 +220,7 @@ public boolean onTouch(View v, MotionEvent event) {
} else {
directionHorizontal = "neutral"+"";
}

oldTop = _top;
oldLeft = _left;

Expand All @@ -241,7 +229,8 @@ public boolean onTouch(View v, MotionEvent event) {
// set the layout on the view
view.setLayoutParams(_layout);

if (hasListenerMove) {

if(_proxy.hasListeners("move")) {
KrollDict props = new KrollDict();
props.put("left", _left);
props.put("top", _top);
Expand All @@ -252,7 +241,7 @@ public boolean onTouch(View v, MotionEvent event) {
_proxy.fireEvent("move", props);
}

break;
break;
case MotionEvent.ACTION_UP:
// Log.d(LCAT, "MotionEvent.ACTION_UP");

Expand Down Expand Up @@ -280,26 +269,32 @@ public boolean onTouch(View v, MotionEvent event) {
_layout.optionTop = new TiDimension(_top, TiDimension.TYPE_LEFT);
view.setLayoutParams(_layout);

if (hasListenerEnd) {

// If the view was not moved, let's fire a singletap event, otherwise treat it like an end event.
String eventToFire = wasMoved ? "end" : "singletap";

if (_proxy.hasListeners(eventToFire)) {

KrollDict center = new KrollDict();
KrollDict props = new KrollDict();
center.put("x", _left + view.getWidth() / 2);
center.put("y", _top + view.getHeight() / 2);

props.put("left", _left);
props.put("top", _top);
props.put("directionHorizontal", directionHorizontal);
props.put("directionVertical", directionVertical);

props.put("center", center);
_proxy.fireEvent("end", props);
_proxy.fireEvent(eventToFire, props);
}
// Log.d(LCAT, "MotionEvent.ACTION_UP - DONE");
positionLeft = 0;
positionTop = 0;
break;
}

// Reset wasMoved instance variable.
wasMoved = false;
break;
}
// Not too sure about this one, true or false?
return true;
}
Expand All @@ -319,7 +314,7 @@ public void registerForTouch() {
}
}

/**
/**
* Public API
*/
// ------- Axis ------
Expand All @@ -340,7 +335,7 @@ public boolean getAxisY() {
public boolean getAxisX() {
return this.hasAxisX;
}

// ------- Max Top ------
@Kroll.method @Kroll.setProperty
public void setMaxTop(Object n) {
Expand Down Expand Up @@ -391,7 +386,7 @@ public void setIsDraggable(Object v) {
public boolean getIsDraggable() {
return this.isDraggable;
}

@Override
public TiUIView createView(Activity activity) {
return new PEDraggableView(this);
Expand Down