Skip to content

Commit 85e7a06

Browse files
committed
Fix CodeFactor
1 parent fc34de0 commit 85e7a06

File tree

2 files changed

+65
-63
lines changed

2 files changed

+65
-63
lines changed

pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal class PdfViewAdapter(
5555
pageLoadingLayout.pdfViewPageLoadingProgress.visibility = if (enableLoadingForPages) View.VISIBLE else View.GONE
5656

5757
renderer.getPageDimensionsAsync(position) { size ->
58-
val width = (pageView.width.takeIf { it > 0 } ?: context.resources.displayMetrics.widthPixels)
58+
val width = pageView.width.takeIf { it > 0 } ?: context.resources.displayMetrics.widthPixels
5959
val aspectRatio = size.width.toFloat() / size.height.toFloat()
6060
val bitmapWidth = (width * (renderQuality.ordinal * aspectRatio + 1)).toInt()
6161
val height = (width / aspectRatio).toInt()

pdfViewer/src/main/java/com/rajat/pdfviewer/PinchZoomRecyclerView.kt

Lines changed: 64 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -70,72 +70,74 @@ class PinchZoomRecyclerView : RecyclerView {
7070
mGestureDetector?.onTouchEvent(ev)
7171
mScaleDetector?.onTouchEvent(ev)
7272
when (ev.action and MotionEvent.ACTION_MASK) {
73-
MotionEvent.ACTION_DOWN -> {
74-
mLastTouchX = ev.x
75-
mLastTouchY = ev.y
76-
mActivePointerId = ev.getPointerId(0)
77-
}
78-
MotionEvent.ACTION_MOVE -> {
79-
val pointerIndex = ev.findPointerIndex(mActivePointerId)
80-
val x = ev.getX(pointerIndex)
81-
val y = ev.getY(pointerIndex)
82-
83-
if (mScaleFactor > 1f) {
84-
val dx = x - mLastTouchX
85-
val dy = y - mLastTouchY
86-
87-
mPosX += dx
88-
mPosY += dy
89-
mPosX = (maxWidth - width * mScaleFactor).coerceAtLeast(mPosX.coerceAtMost(0f))
90-
mPosY = (maxHeight - height * mScaleFactor).coerceAtLeast(mPosY.coerceAtMost(0f))
91-
}
92-
if (mPosY == 0f && !onTop) {
93-
onTopChange(true)
94-
onTop = true
95-
} else if (mPosY != 0f && onTop) {
96-
onTopChange(false)
97-
onTop = false
98-
}
99-
mLastTouchX = x
100-
mLastTouchY = y
101-
invalidate()
102-
}
103-
MotionEvent.ACTION_POINTER_UP -> {
104-
// Extract the index of the pointer that left the touch sensor
105-
val pointerIndex = (ev.action and MotionEvent.ACTION_POINTER_INDEX_MASK) shr MotionEvent.ACTION_POINTER_INDEX_SHIFT
106-
val pointerId = ev.getPointerId(pointerIndex)
107-
108-
if (pointerId == mActivePointerId) {
109-
// This was our active pointer going up. Choose a new active pointer and adjust accordingly.
110-
val newPointerIndex = if (pointerIndex == 0) 1 else 0
111-
112-
mLastTouchX = ev.getX(newPointerIndex)
113-
mLastTouchY = ev.getY(newPointerIndex)
114-
mActivePointerId = ev.getPointerId(newPointerIndex)
115-
}
116-
}
117-
MotionEvent.ACTION_CANCEL -> mActivePointerId = INVALID_POINTER_ID
118-
MotionEvent.ACTION_POINTER_UP -> {
119-
val pointerIndex = ev.actionIndex
120-
val pointerId = ev.getPointerId(pointerIndex)
121-
if (pointerId == mActivePointerId) {
122-
val newPointerIndex = if (pointerIndex == 0) 1 else 0
123-
mLastTouchX = ev.getX(newPointerIndex)
124-
mLastTouchY = ev.getY(newPointerIndex)
125-
mActivePointerId = ev.getPointerId(newPointerIndex)
126-
}
127-
}
128-
MotionEvent.ACTION_SCROLL -> {
129-
val dy = ev.getAxisValue(MotionEvent.AXIS_VSCROLL) * mScaleFactor
130-
mPosY += dy
131-
clampPosition()
132-
invalidate()
133-
}
73+
MotionEvent.ACTION_DOWN -> down(ev)
74+
MotionEvent.ACTION_MOVE -> move(ev)
75+
MotionEvent.ACTION_POINTER_UP -> pointerUp(ev)
76+
MotionEvent.ACTION_CANCEL -> cancel()
77+
MotionEvent.ACTION_SCROLL -> scroll(ev)
13478
}
135-
13679
return superHandled || mScaleFactor > 1f
13780
}
13881

82+
private fun cancel() {
83+
mActivePointerId = INVALID_POINTER_ID
84+
}
85+
86+
private fun down(ev: MotionEvent) {
87+
mLastTouchX = ev.x
88+
mLastTouchY = ev.y
89+
mActivePointerId = ev.getPointerId(0)
90+
}
91+
92+
private fun scroll(ev: MotionEvent) {
93+
val dy = ev.getAxisValue(MotionEvent.AXIS_VSCROLL) * mScaleFactor
94+
mPosY += dy
95+
clampPosition()
96+
invalidate()
97+
}
98+
99+
private fun pointerUp(ev: MotionEvent) {
100+
// Extract the index of the pointer that left the touch sensor
101+
val pointerIndex =
102+
(ev.action and MotionEvent.ACTION_POINTER_INDEX_MASK) shr MotionEvent.ACTION_POINTER_INDEX_SHIFT
103+
val pointerId = ev.getPointerId(pointerIndex)
104+
105+
if (pointerId == mActivePointerId) {
106+
// This was our active pointer going up. Choose a new active pointer and adjust accordingly.
107+
val newPointerIndex = if (pointerIndex == 0) 1 else 0
108+
109+
mLastTouchX = ev.getX(newPointerIndex)
110+
mLastTouchY = ev.getY(newPointerIndex)
111+
mActivePointerId = ev.getPointerId(newPointerIndex)
112+
}
113+
}
114+
115+
private fun move(ev: MotionEvent) {
116+
val pointerIndex = ev.findPointerIndex(mActivePointerId)
117+
val x = ev.getX(pointerIndex)
118+
val y = ev.getY(pointerIndex)
119+
120+
if (mScaleFactor > 1f) {
121+
val dx = x - mLastTouchX
122+
val dy = y - mLastTouchY
123+
124+
mPosX += dx
125+
mPosY += dy
126+
mPosX = (maxWidth - width * mScaleFactor).coerceAtLeast(mPosX.coerceAtMost(0f))
127+
mPosY = (maxHeight - height * mScaleFactor).coerceAtLeast(mPosY.coerceAtMost(0f))
128+
}
129+
if (mPosY == 0f && !onTop) {
130+
onTopChange(true)
131+
onTop = true
132+
} else if (mPosY != 0f && onTop) {
133+
onTopChange(false)
134+
onTop = false
135+
}
136+
mLastTouchX = x
137+
mLastTouchY = y
138+
invalidate()
139+
}
140+
139141
override fun onDraw(canvas: Canvas) {
140142
canvas.save()
141143
canvas.translate(mPosX, mPosY)

0 commit comments

Comments
 (0)