Skip to content

Commit 801ac23

Browse files
committed
chore: improve code quality
1 parent 8c93b27 commit 801ac23

File tree

1 file changed

+48
-56
lines changed

1 file changed

+48
-56
lines changed

library/src/main/java/com/alamkanak/weekview/WeekView.java

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import android.graphics.drawable.BitmapDrawable;
77
import android.graphics.drawable.Drawable;
88
import android.os.Build;
9+
10+
import androidx.annotation.NonNull;
911
import androidx.annotation.Nullable;
1012
import androidx.annotation.RequiresApi;
1113
import androidx.core.view.GestureDetectorCompat;
@@ -61,7 +63,7 @@ private enum AutoScrollDirection {
6163
private float mHeaderHeight;
6264
private GestureDetectorCompat mGestureDetector;
6365
private OverScroller mScroller;
64-
private PointF mCurrentOrigin = new PointF(0f, 0f);
66+
private final PointF mCurrentOrigin = new PointF(0f, 0f);
6567
private Direction mCurrentScrollDirection = Direction.NONE;
6668
private Paint mHeaderBackgroundPaint;
6769
private float mWidthPerDay;
@@ -98,15 +100,17 @@ private enum AutoScrollDirection {
98100
private EventRect mNewEventRect;
99101
private boolean creatingNewEvent = false;
100102
private boolean movingNewEvent = false;
103+
private MotionEvent mNewEventLastTouch;
101104
private NewEventScrollDirection mCurrentNewEventScrollDirection = NewEventScrollDirection.NONE;
102105
private float mNewEventDragOffset = 0;
103-
private Handler autoScrollHandler = new Handler(Looper.getMainLooper());
106+
private final Handler autoScrollHandler = new Handler(Looper.getMainLooper());
104107
private boolean isAutoScrolling = false;
105108
private Runnable autoScrollRunnable;
106-
private int mAutoScrollInterval = 1200;
107-
private int mAutoScrollDuration = 300;
108-
private int mAutoScrollLeftThreshold = 150;
109-
private int mAutoScrollRightThreshold = 150;
109+
private final int mNewEventVerticalScrollDuration = 100;
110+
private final int mAutoScrollInterval = 1200;
111+
private final int mAutoScrollDuration = 300;
112+
private final int mAutoScrollLeftThreshold = 150;
113+
private final int mAutoScrollRightThreshold = 150;
110114

111115
// Attributes and their default values.
112116
private int mHourHeight = 50;
@@ -171,8 +175,7 @@ private enum AutoScrollDirection {
171175
private boolean mAutoLimitTime = false;
172176
private boolean mEnableDropListener = false;
173177
private int mMinOverlappingMinutes = 0;
174-
private MotionEvent mNewEventLastTouch;
175-
private int mNewEventVerticalScrollDuration = 100;
178+
176179

177180
// Listeners.
178181
private EventClickListener mEventClickListener;
@@ -189,16 +192,15 @@ private enum AutoScrollDirection {
189192
private final GestureDetector.SimpleOnGestureListener mGestureListener = new GestureDetector.SimpleOnGestureListener() {
190193

191194
@Override
192-
public boolean onDown(MotionEvent e) {
195+
public boolean onDown(@NonNull MotionEvent e) {
193196
if (!wasOnNewEventRect(e)) {
194197
stopScrolling();
195198
}
196199
return true;
197200
}
198201

199202
@Override
200-
public boolean onSingleTapUp(MotionEvent e) {
201-
Log.d("QuivrWeekView", "singleTapUp");
203+
public boolean onSingleTapUp(@NonNull MotionEvent e) {
202204
goToNearestOrigin();
203205
boolean wasEmptyClick = true;
204206
boolean doRemoveNewEvent = false;
@@ -237,18 +239,19 @@ public boolean onSingleTapUp(MotionEvent e) {
237239

238240

239241
@Override
240-
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
241-
if ((movingNewEvent) || (creatingNewEvent && wasOnNewEventRect(e1))) {
242+
public boolean onScroll(MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
243+
if ((movingNewEvent) || (creatingNewEvent && e1 != null && wasOnNewEventRect(e1))) {
242244
return true;
243245
}
244246
// Check if view is zoomed.
245247
if (mIsZooming)
246248
return true;
247249

250+
boolean isHorizontal = Math.abs(distanceX) > Math.abs(distanceY);
248251
switch (mCurrentScrollDirection) {
249252
case NONE: {
250253
// Allow scrolling only in one direction.
251-
if (Math.abs(distanceX) > Math.abs(distanceY)) {
254+
if (isHorizontal) {
252255
if (distanceX > 0) {
253256
mCurrentScrollDirection = Direction.LEFT;
254257
} else {
@@ -261,14 +264,14 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
261264
}
262265
case LEFT: {
263266
// Change direction if there was enough change.
264-
if (Math.abs(distanceX) > Math.abs(distanceY) && (distanceX < -mScaledTouchSlop)) {
267+
if (isHorizontal && (distanceX < -mScaledTouchSlop)) {
265268
mCurrentScrollDirection = Direction.RIGHT;
266269
}
267270
break;
268271
}
269272
case RIGHT: {
270273
// Change direction if there was enough change.
271-
if (Math.abs(distanceX) > Math.abs(distanceY) && (distanceX > mScaledTouchSlop)) {
274+
if (isHorizontal && (distanceX > mScaledTouchSlop)) {
272275
mCurrentScrollDirection = Direction.LEFT;
273276
}
274277
break;
@@ -311,7 +314,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
311314
}
312315

313316
@Override
314-
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
317+
public boolean onFling(MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY) {
315318
if (mIsZooming || movingNewEvent)
316319
return true;
317320

@@ -342,7 +345,7 @@ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float ve
342345

343346

344347
@Override
345-
public void onLongPress(MotionEvent e) {
348+
public void onLongPress(@NonNull MotionEvent e) {
346349
super.onLongPress(e);
347350
goToNearestOrigin();
348351

@@ -649,7 +652,7 @@ private void initTextTimeWidth() {
649652
}
650653

651654
@Override
652-
protected void onDraw(Canvas canvas) {
655+
protected void onDraw(@NonNull Canvas canvas) {
653656
super.onDraw(canvas);
654657

655658
// Draw the header row.
@@ -662,7 +665,7 @@ protected void onDraw(Canvas canvas) {
662665
private void calculateHeaderHeight() {
663666
//Make sure the header is the right size (depends on AllDay events)
664667
boolean containsAllDayEvent = false;
665-
if (mEventRects != null && mEventRects.size() > 0) {
668+
if (mEventRects != null && !mEventRects.isEmpty()) {
666669
for (int dayNumber = 0;
667670
dayNumber < getRealNumberOfVisibleDays();
668671
dayNumber++) {
@@ -854,7 +857,7 @@ else if (mNewHourHeight > mMaxHourHeight)
854857
}
855858

856859
// Draw background color for each day.
857-
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel);
860+
float start = (Math.max(startPixel, mHeaderColumnWidth));
858861
if (mWidthPerDay + startPixel - start > 0) {
859862
if (mShowDistinctPastFutureColor) {
860863
boolean isWeekend = day.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || day.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY;
@@ -960,7 +963,7 @@ private Calendar getTimeFromPoint(float x, float y) {
960963
for (int dayNumber = leftDaysWithGaps + 1;
961964
dayNumber <= leftDaysWithGaps + getRealNumberOfVisibleDays() + 1;
962965
dayNumber++) {
963-
float start = (startPixel < mHeaderColumnWidth ? mHeaderColumnWidth : startPixel);
966+
float start = (Math.max(startPixel, mHeaderColumnWidth));
964967
if (mWidthPerDay + startPixel - start > 0 && x > start && x < startPixel + mWidthPerDay) {
965968
Calendar day = (Calendar) mHomeDate.clone();
966969
day.add(Calendar.DATE, dayNumber - 1);
@@ -982,7 +985,7 @@ private Calendar getTimeFromPoint(float x, float y) {
982985
* find smallest of start time & latest of end time
983986
*/
984987
private void limitEventTime(List<Calendar> dates) {
985-
if (mEventRects != null && mEventRects.size() > 0) {
988+
if (mEventRects != null && !mEventRects.isEmpty()) {
986989
Calendar startTime = null;
987990
Calendar endTime = null;
988991

@@ -1036,7 +1039,7 @@ private float getXStartPixel() {
10361039
* @param canvas The canvas to draw upon.
10371040
*/
10381041
private void drawEvents(Calendar date, float startFromPixel, Canvas canvas) {
1039-
if (mEventRects != null && mEventRects.size() > 0) {
1042+
if (mEventRects != null && !mEventRects.isEmpty()) {
10401043
for (int i = 0; i < mEventRects.size(); i++) {
10411044
if (isSameDay(mEventRects.get(i).event.getStartTime(), date) && !mEventRects.get(i).event.isAllDay()) {
10421045
float top = mHourHeight * mEventRects.get(i).top / 60 + getEventsTop();
@@ -1063,7 +1066,7 @@ top < getHeight() &&
10631066
canvas.drawRoundRect(mEventRects.get(i).rectF, 30, 30, mEventBackgroundPaint);
10641067
float topToUse = top;
10651068
if (mEventRects.get(i).event.getStartTime().get(Calendar.HOUR_OF_DAY) < mMinTime)
1066-
topToUse = mHourHeight * getPassedMinutesInDay(mMinTime, 0) / 60 + getEventsTop();
1069+
topToUse = mHourHeight * (getPassedMinutesInDay(mMinTime, 0) / 60) + getEventsTop();
10671070

10681071
if (!mNewEventIdentifier.equals(mEventRects.get(i).event.getIdentifier()))
10691072
drawEventTitle(mEventRects.get(i).event, mEventRects.get(i).rectF, canvas, topToUse, left);
@@ -1085,7 +1088,7 @@ top < getHeight() &&
10851088
* @param canvas The canvas to draw upon.
10861089
*/
10871090
private void drawAllDayEvents(Calendar date, float startFromPixel, Canvas canvas) {
1088-
if (mEventRects != null && mEventRects.size() > 0) {
1091+
if (mEventRects != null && !mEventRects.isEmpty()) {
10891092
for (int i = 0; i < mEventRects.size(); i++) {
10901093
if (isSameDay(mEventRects.get(i).event.getStartTime(), date) && mEventRects.get(i).event.isAllDay()) {
10911094

@@ -1204,7 +1207,7 @@ private void drawEmptyImage(WeekViewEvent event, RectF rect, Canvas canvas, floa
12041207
* stored in "originalEvent". But the event that corresponds to rectangle the rectangle
12051208
* instance will be stored in "event".
12061209
*/
1207-
private class EventRect {
1210+
private static class EventRect {
12081211
public WeekViewEvent event;
12091212
public WeekViewEvent originalEvent;
12101213
public RectF rectF;
@@ -1280,7 +1283,7 @@ private void getMoreEvents(Calendar day) {
12801283
mEventRects = new ArrayList<>();
12811284

12821285
// Iterate through each day with events to calculate the position of the events.
1283-
while (tempEvents.size() > 0) {
1286+
while (!tempEvents.isEmpty()) {
12841287
ArrayList<EventRect> eventRects = new ArrayList<>(tempEvents.size());
12851288

12861289
// Get first event for a day.
@@ -1341,16 +1344,16 @@ private void cacheAndSortEvents(List<? extends WeekViewEvent> events) {
13411344
* @param eventRects The events to be sorted.
13421345
*/
13431346
private void sortEventRects(List<EventRect> eventRects) {
1344-
Collections.sort(eventRects, new Comparator<EventRect>() {
1347+
eventRects.sort(new Comparator<EventRect>() {
13451348
@Override
13461349
public int compare(EventRect left, EventRect right) {
13471350
long start1 = left.event.getStartTime().getTimeInMillis();
13481351
long start2 = right.event.getStartTime().getTimeInMillis();
1349-
int comparator = start1 > start2 ? 1 : (start1 < start2 ? -1 : 0);
1352+
int comparator = Long.compare(start1, start2);
13501353
if (comparator == 0) {
13511354
long end1 = left.event.getEndTime().getTimeInMillis();
13521355
long end2 = right.event.getEndTime().getTimeInMillis();
1353-
comparator = end1 > end2 ? 1 : (end1 < end2 ? -1 : 0);
1356+
comparator = Long.compare(end1, end2);
13541357
}
13551358
return comparator;
13561359
}
@@ -1405,7 +1408,7 @@ private void expandEventsToMaxWidth(List<EventRect> collisionGroup) {
14051408
for (EventRect eventRect : collisionGroup) {
14061409
boolean isPlaced = false;
14071410
for (List<EventRect> column : columns) {
1408-
if (column.size() == 0) {
1411+
if (column.isEmpty()) {
14091412
column.add(eventRect);
14101413
isPlaced = true;
14111414
} else if (!isEventsCollide(eventRect.event, column.get(column.size() - 1).event)) {
@@ -1462,7 +1465,7 @@ private boolean isEventsCollide(WeekViewEvent event1, WeekViewEvent event2) {
14621465
long start2 = event2.getStartTime().getTimeInMillis();
14631466
long end2 = event2.getEndTime().getTimeInMillis();
14641467

1465-
long minOverlappingMillis = mMinOverlappingMinutes * 60 * 1000;
1468+
long minOverlappingMillis = (long) mMinOverlappingMinutes * 60 * 1000;
14661469

14671470
return !((start1 + minOverlappingMillis >= end2) || (end1 <= start2 + minOverlappingMillis));
14681471
}
@@ -2352,15 +2355,15 @@ public void setZoomFocusPointEnabled(boolean zoomFocusPointEnabled) {
23522355
mZoomFocusPointEnabled = zoomFocusPointEnabled;
23532356
}
23542357

2355-
/*
2358+
/**
23562359
* Is focus point enabled
23572360
* @return fixed focus point enabled?
23582361
*/
23592362
public boolean isZoomFocusPointEnabled() {
23602363
return mZoomFocusPointEnabled;
23612364
}
23622365

2363-
/*
2366+
/**
23642367
* Get focus point
23652368
* 0 = top of view, 1 = bottom of view
23662369
* The focused point (multiplier of the view height) where the week view is zoomed around.
@@ -2467,17 +2470,13 @@ public void setNewEventIconDrawable(Drawable newEventIconDrawable) {
24672470
public void enableDropListener() {
24682471
this.mEnableDropListener = true;
24692472
//set drag and drop listener, required Honeycomb+ Api level
2470-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
2471-
setOnDragListener(new DragListener());
2472-
}
2473+
setOnDragListener(new DragListener());
24732474
}
24742475

24752476
public void disableDropListener() {
24762477
this.mEnableDropListener = false;
24772478
//set drag and drop listener, required Honeycomb+ Api level
2478-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
2479-
setOnDragListener(null);
2480-
}
2479+
setOnDragListener(null);
24812480
}
24822481

24832482
public boolean isDropListenerEnabled() {
@@ -2500,7 +2499,6 @@ public int getMinOverlappingMinutes() {
25002499

25012500
@Override
25022501
public boolean onTouchEvent(MotionEvent event) {
2503-
// TODO: fix single tap up
25042502
mScaleDetector.onTouchEvent(event);
25052503
boolean val = mGestureDetector.onTouchEvent(event);
25062504
if (event.getAction() == MotionEvent.ACTION_DOWN && creatingNewEvent && wasOnNewEventRect(event)) {
@@ -2779,8 +2777,6 @@ public boolean dateIsValid(Calendar day) {
27792777
}
27802778

27812779
private void updateNewEvent(MotionEvent e) {
2782-
Log.d("QuivrWeekView", String.format("updateNewEvent: %s%n", e.toString()));
2783-
27842780
float x = e.getX();
27852781

27862782
// Ugly fix for when the user is dragging the event over the small left time column
@@ -2868,7 +2864,6 @@ private void removeNewEvent() {
28682864
}
28692865

28702866
private void startNewEventAdding(MotionEvent e) {
2871-
Log.d("QuivrWeekView", "start new event adding!");
28722867
playSoundEffect(SoundEffectConstants.CLICK);
28732868

28742869
Calendar selectedTime = getTimeFromPoint(e.getX(), e.getY());
@@ -2971,15 +2966,15 @@ private class WeekViewGestureListener implements ScaleGestureDetector.OnScaleGes
29712966
float mFocusedPointY;
29722967

29732968
@Override
2974-
public void onScaleEnd(ScaleGestureDetector detector) {
2969+
public void onScaleEnd(@NonNull ScaleGestureDetector detector) {
29752970
mIsZooming = false;
29762971
if (mZoomEndListener != null) {
29772972
mZoomEndListener.onZoomEnd(mHourHeight);
29782973
}
29792974
}
29802975

29812976
@Override
2982-
public boolean onScaleBegin(ScaleGestureDetector detector) {
2977+
public boolean onScaleBegin(@NonNull ScaleGestureDetector detector) {
29832978
mIsZooming = true;
29842979
goToNearestOrigin();
29852980

@@ -3014,19 +3009,16 @@ public boolean onScale(ScaleGestureDetector detector) {
30143009

30153010
}
30163011

3017-
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
30183012
private class DragListener implements View.OnDragListener {
30193013
@Override
30203014
public boolean onDrag(View v, DragEvent e) {
3021-
switch (e.getAction()) {
3022-
case DragEvent.ACTION_DROP:
3023-
if (e.getX() > mHeaderColumnWidth && e.getY() > (mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)) {
3024-
Calendar selectedTime = getTimeFromPoint(e.getX(), e.getY());
3025-
if (selectedTime != null) {
3026-
mDropListener.onDrop(v, selectedTime);
3027-
}
3015+
if (e.getAction() == DragEvent.ACTION_DROP) {
3016+
if (e.getX() > mHeaderColumnWidth && e.getY() > (mHeaderTextHeight + mHeaderRowPadding * 2 + mHeaderMarginBottom)) {
3017+
Calendar selectedTime = getTimeFromPoint(e.getX(), e.getY());
3018+
if (selectedTime != null) {
3019+
mDropListener.onDrop(v, selectedTime);
30283020
}
3029-
break;
3021+
}
30303022
}
30313023
return true;
30323024
}

0 commit comments

Comments
 (0)