Skip to content

Commit 210944e

Browse files
authored
Merge pull request #437 from AppDevNext/KotlinUtils
Kotlin Utils
2 parents 463040a + 790288e commit 210944e

File tree

17 files changed

+272
-304
lines changed

17 files changed

+272
-304
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,12 @@ class BarLineChartTouchListener(
207207
MotionEvent.ACTION_UP -> {
208208
val velocityTracker = mVelocityTracker
209209
val pointerId = event.getPointerId(0)
210-
velocityTracker!!.computeCurrentVelocity(1000, Utils.getMaximumFlingVelocity().toFloat())
210+
velocityTracker!!.computeCurrentVelocity(1000, Utils.maximumFlingVelocity.toFloat())
211211
val velocityY = velocityTracker.getYVelocity(pointerId)
212212
val velocityX = velocityTracker.getXVelocity(pointerId)
213213

214-
if (abs(velocityX.toDouble()) > Utils.getMinimumFlingVelocity() ||
215-
abs(velocityY.toDouble()) > Utils.getMinimumFlingVelocity()
214+
if (abs(velocityX.toDouble()) > Utils.minimumFlingVelocity ||
215+
abs(velocityY.toDouble()) > Utils.minimumFlingVelocity
216216
) {
217217
if (mTouchMode == DRAG && mChart!!.isDragDecelerationEnabled) {
218218
stopDeceleration()
@@ -225,8 +225,8 @@ class BarLineChartTouchListener(
225225
mDecelerationVelocity.x = velocityX
226226
mDecelerationVelocity.y = velocityY
227227

228-
Utils.postInvalidateOnAnimation(mChart) // This causes computeScroll to fire, recommended for this by
229-
// Google
228+
// This causes computeScroll to fire, recommended for this by Google
229+
Utils.postInvalidateOnAnimation(mChart!!)
230230
}
231231
}
232232

@@ -249,7 +249,7 @@ class BarLineChartTouchListener(
249249
}
250250

251251
MotionEvent.ACTION_POINTER_UP -> {
252-
Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, mVelocityTracker)
252+
mVelocityTracker?.let { Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, it) }
253253

254254
mTouchMode = POST_ZOOM
255255
}
@@ -469,10 +469,9 @@ class BarLineChartTouchListener(
469469
val vph = mChart!!.viewPortHandler
470470

471471
val xTrans = x - vph.offsetLeft()
472-
var yTrans = 0f
473472

474473
// check if axis is inverted
475-
yTrans = if (inverted()) {
474+
val yTrans: Float = if (inverted()) {
476475
-(y - vph.offsetTop())
477476
} else {
478477
-(mChart!!.measuredHeight - y - vph.offsetBottom())

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/AxisRenderer.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import kotlin.math.abs
1212
import kotlin.math.ceil
1313
import kotlin.math.floor
1414
import kotlin.math.log10
15+
import kotlin.math.nextUp
1516
import kotlin.math.pow
1617

1718
/**
@@ -180,7 +181,10 @@ abstract class AxisRenderer(
180181
first -= interval
181182
}
182183

183-
val last = if (interval == 0.0) 0.0 else Utils.nextUp(floor(max / interval) * interval)
184+
val last = if (interval == 0.0)
185+
0.0
186+
else
187+
(floor(max / interval) * interval).nextUp()
184188

185189
var f: Double
186190

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BarChartRenderer.kt

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,14 @@ open class BarChartRenderer(
330330
px += iconsOffset.x
331331
py += iconsOffset.y
332332

333-
Utils.drawImage(
334-
canvas,
335-
icon,
336-
px.toInt(),
337-
py.toInt(),
338-
icon!!.intrinsicWidth,
339-
icon.intrinsicHeight
340-
)
333+
icon?.let {
334+
Utils.drawImage(
335+
canvas,
336+
it,
337+
px.toInt(),
338+
py.toInt()
339+
)
340+
}
341341
}
342342
j += 4
343343
}
@@ -390,14 +390,14 @@ open class BarChartRenderer(
390390
px += iconsOffset.x
391391
py += iconsOffset.y
392392

393-
Utils.drawImage(
394-
canvas,
395-
icon,
396-
px.toInt(),
397-
py.toInt(),
398-
icon!!.intrinsicWidth,
399-
icon.intrinsicHeight
400-
)
393+
icon?.let {
394+
Utils.drawImage(
395+
canvas,
396+
it,
397+
px.toInt(),
398+
py.toInt()
399+
)
400+
}
401401
}
402402

403403
// draw stack values
@@ -469,14 +469,14 @@ open class BarChartRenderer(
469469
if (entry.icon != null && dataSet.isDrawIconsEnabled) {
470470
val icon = entry.icon
471471

472-
Utils.drawImage(
473-
canvas,
474-
icon,
475-
(x + iconsOffset.x).toInt(),
476-
(y + iconsOffset.y).toInt(),
477-
icon!!.intrinsicWidth,
478-
icon.intrinsicHeight
479-
)
472+
icon?.let {
473+
Utils.drawImage(
474+
canvas,
475+
it,
476+
(x + iconsOffset.x).toInt(),
477+
(y + iconsOffset.y).toInt()
478+
)
479+
}
480480
}
481481
k += 2
482482
}

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/BubbleChartRenderer.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ open class BubbleChartRenderer(
155155
if (entry.icon != null && dataSet.isDrawIconsEnabled) {
156156
val icon = entry.icon
157157

158-
Utils.drawImage(
159-
canvas,
160-
icon,
161-
(x + iconsOffset.x).toInt(),
162-
(y + iconsOffset.y).toInt(),
163-
icon!!.intrinsicWidth,
164-
icon.intrinsicHeight
165-
)
158+
icon?.let {
159+
Utils.drawImage(
160+
canvas,
161+
it,
162+
(x + iconsOffset.x).toInt(),
163+
(y + iconsOffset.y).toInt()
164+
)
165+
}
166166
}
167167
j += 2
168168
}

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/CandleStickChartRenderer.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,14 @@ open class CandleStickChartRenderer(
265265
if (entry.icon != null && dataSet.isDrawIconsEnabled) {
266266
val icon = entry.icon
267267

268-
Utils.drawImage(
269-
canvas,
270-
icon,
271-
(x + iconsOffset.x).toInt(),
272-
(y + iconsOffset.y).toInt(),
273-
icon!!.intrinsicWidth,
274-
icon.intrinsicHeight
275-
)
268+
icon?.let {
269+
Utils.drawImage(
270+
canvas,
271+
it,
272+
(x + iconsOffset.x).toInt(),
273+
(y + iconsOffset.y).toInt()
274+
)
275+
}
276276
}
277277
j += 2
278278
}

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/HorizontalBarChartRenderer.kt

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ open class HorizontalBarChartRenderer(
252252
px += iconsOffset.x
253253
py += iconsOffset.y
254254

255-
Utils.drawImage(
256-
canvas,
257-
icon,
258-
px.toInt(),
259-
py.toInt(),
260-
icon!!.intrinsicWidth,
261-
icon.intrinsicHeight
262-
)
255+
icon?.let {
256+
Utils.drawImage(
257+
canvas,
258+
it,
259+
px.toInt(),
260+
py.toInt()
261+
)
262+
}
263263
}
264264
j += 4
265265
}
@@ -328,14 +328,14 @@ open class HorizontalBarChartRenderer(
328328
px += iconsOffset.x
329329
py += iconsOffset.y
330330

331-
Utils.drawImage(
332-
canvas,
333-
icon,
334-
px.toInt(),
335-
py.toInt(),
336-
icon!!.intrinsicWidth,
337-
icon.intrinsicHeight
338-
)
331+
icon?.let {
332+
Utils.drawImage(
333+
canvas,
334+
it,
335+
px.toInt(),
336+
py.toInt()
337+
)
338+
}
339339
}
340340
} else {
341341
val transformed = FloatArray(vals.size * 2)
@@ -416,14 +416,14 @@ open class HorizontalBarChartRenderer(
416416
if (entry.icon != null && dataSet.isDrawIconsEnabled) {
417417
val icon = entry.icon
418418

419-
Utils.drawImage(
420-
canvas,
421-
icon,
422-
(x + iconsOffset.x).toInt(),
423-
(y + iconsOffset.y).toInt(),
424-
icon!!.intrinsicWidth,
425-
icon.intrinsicHeight
426-
)
419+
icon?.let {
420+
Utils.drawImage(
421+
canvas,
422+
it,
423+
(x + iconsOffset.x).toInt(),
424+
(y + iconsOffset.y).toInt()
425+
)
426+
}
427427
}
428428
k += 2
429429
}

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/LineChartRenderer.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,14 @@ class LineChartRenderer(
543543
if (entry.icon != null && dataSet.isDrawIconsEnabled) {
544544
val icon = entry.icon
545545

546-
Utils.drawImage(
547-
canvas,
548-
icon,
549-
(x + iconsOffset.x).toInt(),
550-
(y + iconsOffset.y).toInt(),
551-
icon!!.intrinsicWidth,
552-
icon.intrinsicHeight
553-
)
546+
icon?.let {
547+
Utils.drawImage(
548+
canvas,
549+
it,
550+
(x + iconsOffset.x).toInt(),
551+
(y + iconsOffset.y).toInt()
552+
)
553+
}
554554
}
555555
}
556556
j += 2

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/PieChartRenderer.kt

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import android.graphics.Paint
77
import android.graphics.Paint.Align
88
import android.graphics.Path
99
import android.graphics.RectF
10-
import android.os.Build
1110
import android.text.Layout
1211
import android.text.StaticLayout
1312
import android.text.TextPaint
@@ -31,6 +30,7 @@ import kotlin.math.sin
3130
import kotlin.math.sqrt
3231
import kotlin.math.tan
3332
import androidx.core.graphics.withSave
33+
import androidx.core.graphics.createBitmap
3434

3535
open class PieChartRenderer(
3636
protected var chart: PieChart, animator: ChartAnimator,
@@ -95,7 +95,7 @@ open class PieChartRenderer(
9595
|| (drawBitmap.height != height)
9696
) {
9797
if (width > 0 && height > 0) {
98-
drawBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444)
98+
drawBitmap = createBitmap(width, height, Bitmap.Config.ARGB_4444)
9999
mDrawBitmap = WeakReference(drawBitmap)
100100
bitmapCanvas = Canvas(drawBitmap)
101101
} else return
@@ -106,7 +106,7 @@ open class PieChartRenderer(
106106
val pieData = chart.data
107107

108108
for (set in pieData!!.dataSets) {
109-
if (set.isVisible && set.entryCount > 0) drawDataSet(canvas, set)
109+
if (set.isVisible && set.entryCount > 0) drawDataSet(set)
110110
}
111111
}
112112

@@ -171,7 +171,7 @@ open class PieChartRenderer(
171171
return sliceSpace
172172
}
173173

174-
protected fun drawDataSet(canvas: Canvas?, dataSet: IPieDataSet) {
174+
protected fun drawDataSet(dataSet: IPieDataSet) {
175175
var angle = 0f
176176
val rotationAngle = chart.rotationAngle
177177

@@ -588,14 +588,14 @@ open class PieChartRenderer(
588588
var y = (labelRadius + iconsOffset.y) * sliceYBase + center.y
589589
y += iconsOffset.x
590590

591-
Utils.drawImage(
592-
this,
593-
icon,
594-
x.toInt(),
595-
y.toInt(),
596-
icon!!.intrinsicWidth,
597-
icon.intrinsicHeight
598-
)
591+
icon?.let {
592+
Utils.drawImage(
593+
this,
594+
it,
595+
x.toInt(),
596+
y.toInt()
597+
)
598+
}
599599
}
600600

601601
xIndex++
@@ -725,12 +725,10 @@ open class PieChartRenderer(
725725
val layoutHeight = centerTextLayout!!.height.toFloat()
726726

727727
canvas.save()
728-
if (Build.VERSION.SDK_INT >= 18) {
729-
val path = mDrawCenterTextPathBuffer
730-
path.reset()
731-
path.addOval(holeRect, Path.Direction.CW)
732-
canvas.clipPath(path)
733-
}
728+
val path = mDrawCenterTextPathBuffer
729+
path.reset()
730+
path.addOval(holeRect, Path.Direction.CW)
731+
canvas.clipPath(path)
734732

735733
canvas.translate(boundingRect.left, boundingRect.top + (boundingRect.height() - layoutHeight) / 2f)
736734
centerTextLayout!!.draw(canvas)

MPChartLib/src/main/java/com/github/mikephil/charting/renderer/RadarChartRenderer.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,14 @@ open class RadarChartRenderer(
180180

181181
pIcon.y += iconsOffset.x
182182

183-
Utils.drawImage(
184-
canvas,
185-
icon,
186-
pIcon.x.toInt(),
187-
pIcon.y.toInt(),
188-
icon!!.intrinsicWidth,
189-
icon.intrinsicHeight
190-
)
183+
icon?.let {
184+
Utils.drawImage(
185+
canvas,
186+
it,
187+
pIcon.x.toInt(),
188+
pIcon.y.toInt()
189+
)
190+
}
191191
}
192192
}
193193

0 commit comments

Comments
 (0)