-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusView.kt
1148 lines (884 loc) · 39.2 KB
/
StatusView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package params.com.stepview
import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
import android.support.annotation.FloatRange
import android.support.v4.content.res.ResourcesCompat
import android.support.v4.view.ViewCompat
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.HorizontalScrollView
import kotlin.properties.Delegates
import kotlin.reflect.KProperty
class StatusViewScroller @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : HorizontalScrollView(context, attrs, defStyleAttr) {
var statusView: StatusView = StatusView(context, attrs)
init {
addView(statusView)
}
fun scrollToStep(stepCount: Int) {
scrollTo(statusView.getScrollPosForStep(stepCount), scrollY)
}
fun smoothScrollToStep(stepCount: Int) {
smoothScrollTo(statusView.getScrollPosForStep(stepCount), scrollY)
}
}
/**
* Created by Parminder Saini on 12/06/18.
*/
class StatusView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
companion object {
const val CIRCLE_COLOR_TYPE_FILL = 1
const val CIRCLE_COLOR_TYPE_STROKE = 2
const val INVALID_STATUS_COUNT = -1
}
/**
* Circle stroke paints for incomplete & complete status
*/
private var mCircleStrokePaint: Paint? = null
private var mCircleStrokePaintIncomplete: Paint? = null
private var mCircleStrokePaintCurrent: Paint? = null
private var mCircleFillPaint: Paint? = null
private var mCircleFillPaintIncomplete: Paint? = null
private var mCircleFillPaintCurrent: Paint? = null
private lateinit var mLinePaint: Paint
private lateinit var mLinePaintIncomplete: Paint
private lateinit var mLinePaintCurrent: Paint
private lateinit var mTextPaintStatus: TextPaint
private lateinit var mTextPaintLabelsIncomplete: TextPaint
private lateinit var mTextPaintLabelCurrent: TextPaint
private lateinit var mTextPaintLabels: TextPaint
/**
* Magnification value for current Circle
*/
@setparam:FloatRange(from = 0.0, to = 1.0)
var currentStatusZoom: Float = 0.0f
set(value) {
require(value in 0..1) { "Zoom should be in between 0 to 1, but was $value." }
currentStatusRadius = circleRadius * (1 + value)
}
/**
* A min margin that there should be between every adjacent status Text
* Note# This only applies if strictObeyLineLength is set to false
*/
var minStatusAdjacentMargin: Float by OnLayoutProp(5.0f.pxValue())
/**
* Set true to obey Status Text i.e alphabets or words belonging to one line would not cross
* to the other line so the line length between circles would be increased as per if needed.
*
* Set false to strictly obey line length.
*
*
*/
var strictObeyLineLength: Boolean by OnLayoutProp(false)
/**
* The total number of statuses to be displayed
*/
var stepCount: Int by OnLayoutProp(4)
/*
The count up to which status has been completed
*/
var currentCount: Int by OnLayoutProp(INVALID_STATUS_COUNT){
initCirclePaints()
}
/*
Radius of each circle to be drawn
#Note: This does not include stroke width
*/
var circleRadius: Float by OnLayoutProp(15.0f.pxValue()) //dp
/**
* Length of line to be drawn between circles
* #Note: This does not include line gap
*/
var lineLength: Float by OnLayoutProp(50.0f.pxValue()) //dp
/**
* Stroke width of each circle to be drawn
*/
var circleStrokeWidth: Float by OnLayoutProp(2.0f.pxValue()) //dp
/**
* Margin or gap on each side of circle.
* Note: This does not apply for extreme sides.
*/
var lineGap by OnLayoutProp(0.0f)
/**
* Top Margin of Status from circle
*/
var statusTopMargin by OnLayoutProp(4.0f.pxValue())
/**
* set true to align all status to align same as current Status y-pos
*
*/
var alignStatusWithCurrent by OnValidateProp(false) {
setDrawingDimensions()
}
/**
* Stroke width of the line between circles (dp)
*/
var lineWidth: Float by OnValidateProp(1.5f.pxValue()) {
mLinePaint.strokeWidth = lineWidth
mLinePaintIncomplete.strokeWidth = lineWidth
mLinePaintCurrent.strokeWidth = lineWidth
}
/**
* Color of line between circles
*/
var lineColor: Int by OnValidateProp(Color.GREEN) {
mLinePaint.color = lineColor
}
/**
* Color of line between circles for incomplete statuses
*/
var lineColorIncomplete: Int by OnValidateProp(Color.BLACK) {
mLinePaintIncomplete.color = lineColorIncomplete
}
/**
* Color of line adjacent to circles for current status
*/
var lineColorCurrent: Int by OnValidateProp(Color.BLACK) {
mLinePaintCurrent.color = lineColorCurrent
}
/**
* Fill Color of circles for complete statuses
*/
var circleFillColor: Int by OnValidateProp(Color.CYAN) {
mCircleFillPaint?.color = circleFillColor
}
/**
* Fill Color of circles for incomplete statuses
*/
var circleFillColorIncomplete: Int by OnValidateProp(Color.CYAN) {
mCircleFillPaintIncomplete?.color = circleFillColorIncomplete
}
/**
* Fill Color of circles for current status
*/
var circleFillColorCurrent: Int by OnValidateProp(Color.CYAN) {
mCircleFillPaintCurrent?.color = circleFillColorCurrent
}
/**
* Stroke Color of circles for complete statuses
*/
var circleStrokeColor: Int by OnValidateProp(Color.BLACK) {
mCircleStrokePaint?.color = circleStrokeColor
}
/**
* Stroke Color of circles for incomplete statuses
*/
var circleStrokeColorIncomplete: Int by OnValidateProp(Color.BLACK) {
mCircleStrokePaintIncomplete?.color = circleStrokeColorIncomplete
}
/**
* Stroke Color of circles for current Status
*/
var circleStrokeColorCurrent: Int by OnValidateProp(Color.BLACK) {
mCircleStrokePaintCurrent?.color = circleStrokeColorCurrent
}
/**
* Text Color of labels
*
*/
var textColorLabels: Int by OnValidateProp(Color.BLACK) {
mTextPaintLabels.color = textColorLabels
}
/**
* Text Color of labels Incomplete
*/
var textColorLabelsIncomplete: Int by OnValidateProp(Color.BLACK) {
mTextPaintLabelsIncomplete.color = textColorLabelsIncomplete
}
/**
* Text Color of labels Current
*/
var textColorLabelCurrent: Int by OnValidateProp(Color.BLACK) {
mTextPaintLabelCurrent.color = textColorLabelCurrent
}
/**
* Text Size of Labels
*/
var textSizeLabels: Float by OnValidateProp(15.0f.pxValue(TypedValue.COMPLEX_UNIT_SP)) {
//sp
mTextPaintLabels.textSize = textSizeLabels
mTextPaintLabelsIncomplete.textSize = textSizeLabels
}
/**
* Text Color of Statuses
*/
var textColorStatus: Int by OnValidateProp(Color.BLACK) {
mTextPaintStatus.color = textColorStatus
for (item in statusData) {
item.staticLayout?.paint?.color = textColorStatus
}
}
/**
* Text Size of statuses
*/
var textSizeStatus: Float by OnLayoutProp(14.0f.pxValue(TypedValue.COMPLEX_UNIT_SP)) {
mTextPaintStatus.textSize = textSizeStatus
}
/**
* Text Font of statuses
*/
var statusTypeface: Typeface? by OnLayoutProp(null) {
statusTypeface?.run {
mTextPaintStatus.typeface = this
}
}
/**
* Text Font of Labels
*/
var labelsTypeface: Typeface? by OnLayoutProp(null) {
labelsTypeface?.run {
mTextPaintLabels.typeface = this
mTextPaintLabelsIncomplete.typeface = this
mTextPaintLabelCurrent.typeface = this
}
}
/**
* A boolean which decides if to draw labels or not
*/
var drawLabels: Boolean by OnValidateProp(false) {
setDrawingDimensions()
}
/**
* A drawable for complete status
* #Note: If this is set then it would be given preference over the labels.
*/
var completeDrawable: Drawable? by OnValidateProp(null) {
setDrawingDimensions()
}
/**
* A drawable for current Status
* #Note: If this is set then it would be given preference over the labels.
*/
var currentDrawable: Drawable? by OnValidateProp(null) {
setDrawingDimensions()
}
/**
* A drawable for incomplete statuses
* #Note: If this is set then it would be given preference over the labels.
*/
var incompleteDrawable: Drawable? by OnValidateProp(null) {
setDrawingDimensions()
}
/**
* circle fill type. Has been set to fill by default.
*
* To change pass a single flag
* To set both the flags i.e (fill & stroke) pass (CIRCLE_COLOR_TYPE_FILL | CIRCLE_COLOR_TYPE_STROKE)
*
*/
var circleColorType: Int by Delegates.observable(CIRCLE_COLOR_TYPE_FILL)
{ prop, old, new ->
if (ViewCompat.isLaidOut(this)) {
initCirclePaints()
val oldHadStrokeFlagSet = containsFlag(old, CIRCLE_COLOR_TYPE_STROKE)
val newHasStrokeFlagSet = containsFlag(new, CIRCLE_COLOR_TYPE_STROKE)
if ((oldHadStrokeFlagSet && !newHasStrokeFlagSet) || (!oldHadStrokeFlagSet && newHasStrokeFlagSet)) {
requestLayout()
} else {
setDrawingDimensions()
invalidate()
}
}
}
/**
* This contains list of LabelInfo which stores static layout and text.
* To the caller. It is a list of string and custom getter setters have been made accordingly
*
*/
private var statusData: MutableList<StatusInfo> by OnLayoutProp(mutableListOf())
/**
* Extracts List<String> from List<LabelInfo> and returns back to the caller
*/
fun getStatusList(): List<String> = statusData.map { it.text }
/**
* Creates List<LabelInfo> from List<String> by passing default values.
* #Note: It trims any extra statuses (if size is greater than stepCount)
*/
fun setStatusList(list: List<String>) {
//to make sure original list is not modified convert to mutableList
val input = list.toMutableList()
if (stepCount > 0 && list.size - stepCount > 0) {
input.dropLast(list.size - stepCount)
}
statusData = (input.map { StatusInfo(it) }).toMutableList()
}
/**
* Stores all the drawing data that is used while drawing on canvas
*/
private var drawingData = mutableListOf<Item>()
private var currentStatusRadius: Float by OnLayoutProp(circleRadius)
private var lineLengthComputed = 0.0f //actual linelength that is calculated and set
//To store the data of each circle
private class Item(val textData: LabelItemText?, val circleItem: CircleItem, val lineItem: LineItem?, val labelData: StatusItemText? = null)
//Stores drawing data about labels to be drawn inside circles i.e the count of step
private class LabelItemText(val text: String? = null, val paint: Paint? = null, val x: Float = 0.0f, val y: Float = 0.0f, val drawableItem: DrawableItem? = null)
//Stores drawing data for statuses to be drawn below the circle
private class StatusItemText(val x: Float = 0.0f, val y: Float = 0.0f, val staticLayout: StaticLayout? = null)
//Stores drawing data for every circle
private class CircleItem(val center: PointF, val radius: Float, val strokePaint: Paint?, val fillPaint: Paint?)
//Stores drawing data for every line to be drawn between circles
private class LineItem(val start: PointF, val end: PointF, val paint: Paint)
//Stores drawable draw info
private class DrawableItem(val rect: Rect, val drawable: Drawable)
//Stores information about every status text and its dimension properties
private class StatusInfo(val text: String, var width: Float = 0.0f, var height: Float = 0.0f, var staticLayout: StaticLayout? = null)
private var propsIntialisedOnce = false
init {
id = ViewCompat.generateViewId()//as the same attrs are being passed this view would have the same id as scroller.
val a = context.theme.obtainStyledAttributes(attrs, R.styleable.StatusViewScroller, 0, 0)
try {
stepCount = a.getInt(R.styleable.StatusViewScroller_stepCount, stepCount)
currentCount = a.getInt(R.styleable.StatusViewScroller_currentCount, INVALID_STATUS_COUNT)
circleRadius = a.getDimension(R.styleable.StatusViewScroller_circleRadius, circleRadius)
lineLength = a.getDimension(R.styleable.StatusViewScroller_lineLength, lineLength)
circleStrokeWidth = a.getDimension(R.styleable.StatusViewScroller_circleStrokeWidth, circleStrokeWidth)
lineWidth = a.getDimension(R.styleable.StatusViewScroller_lineWidth, lineWidth)
completeDrawable = a.getDrawable(R.styleable.StatusViewScroller_completeDrawable)
incompleteDrawable = a.getDrawable(R.styleable.StatusViewScroller_incompleteDrawable)
currentDrawable = a.getDrawable(R.styleable.StatusViewScroller_currentDrawable)
drawLabels = a.getBoolean(R.styleable.StatusViewScroller_drawLabels, drawLabels)
strictObeyLineLength = a.getBoolean(R.styleable.StatusViewScroller_strictObeyLineLength, strictObeyLineLength)
lineGap = a.getDimension(R.styleable.StatusViewScroller_lineGap, lineGap)
minStatusAdjacentMargin = a.getDimension(R.styleable.StatusViewScroller_minStatusAdjacentMargin, minStatusAdjacentMargin)
statusTopMargin = a.getDimension(R.styleable.StatusViewScroller_statusTopMargin, statusTopMargin)
lineColor = a.getColor(R.styleable.StatusViewScroller_lineColor, lineColor)
circleFillColor = a.getColor(R.styleable.StatusViewScroller_circleColor, circleFillColor)
circleStrokeColor = a.getColor(R.styleable.StatusViewScroller_circleStrokeColor, circleStrokeColor)
textColorStatus = a.getColor(R.styleable.StatusViewScroller_textColorStatus, textColorStatus)
textColorLabels = a.getColor(R.styleable.StatusViewScroller_textColorLabels, textColorLabels)
textSizeStatus = a.getDimension(R.styleable.StatusViewScroller_textSizeStatus, textSizeStatus)
textSizeLabels = a.getDimension(R.styleable.StatusViewScroller_textSizeLabels, textSizeLabels)
circleColorType = a.getInteger(R.styleable.StatusViewScroller_circleColorType, circleColorType)
textColorLabelsIncomplete = a.getColor(R.styleable.StatusViewScroller_textColorLabelsIncomplete, textColorLabels)
textColorLabelCurrent = a.getColor(R.styleable.StatusViewScroller_textColorLabelsCurrent, textColorLabelsIncomplete)
lineColorIncomplete = a.getColor(R.styleable.StatusViewScroller_lineColorIncomplete, lineColor)
lineColorCurrent = a.getColor(R.styleable.StatusViewScroller_lineColorCurrent, lineColorIncomplete)
circleFillColorIncomplete = a.getColor(R.styleable.StatusViewScroller_circleColorIncomplete, circleFillColor)
circleStrokeColorIncomplete = a.getColor(R.styleable.StatusViewScroller_circleStrokeColorIncomplete, circleStrokeColor)
circleStrokeColorCurrent = a.getColor(R.styleable.StatusViewScroller_circleStrokeColorCurrent, circleStrokeColorIncomplete)
circleFillColorCurrent = a.getColor(R.styleable.StatusViewScroller_circleColorCurrent, circleFillColorIncomplete)
currentStatusZoom = a.getFloat(R.styleable.StatusViewScroller_currentStepZoom, currentStatusZoom)
alignStatusWithCurrent = a.getBoolean(R.styleable.StatusViewScroller_alignStatusWithCurrent, alignStatusWithCurrent)
val entries = a.getTextArray(R.styleable.StatusViewScroller_android_entries)
if (entries != null) {
for (entry in entries) {
statusData.add(StatusInfo(entry.toString()))
}
}
try {
val resource: Int = a.getResourceId(R.styleable.StatusViewScroller_statusFont, -1)
if (resource != -1) {
statusTypeface = ResourcesCompat.getFont(getContext(), resource)
}
} catch (e: Exception) {
e.printStackTrace()
}
try {
val resource: Int = a.getResourceId(R.styleable.StatusViewScroller_labelFont, -1)
if (resource != -1) {
labelsTypeface = ResourcesCompat.getFont(getContext(), resource)
}
} catch (e: Exception) {
e.printStackTrace()
}
} finally {
a.recycle()
}
initCirclePaints()
mLinePaint = Paint(Paint.ANTI_ALIAS_FLAG)
mLinePaint.style = Paint.Style.STROKE
mLinePaint.strokeWidth = lineWidth
mLinePaint.color = lineColor
mTextPaintStatus = TextPaint(Paint.ANTI_ALIAS_FLAG)
mTextPaintStatus.style = Paint.Style.FILL
mTextPaintStatus.textAlign = Paint.Align.CENTER
mTextPaintStatus.color = textColorStatus
mTextPaintStatus.textSize = textSizeStatus
statusTypeface?.run {
mTextPaintStatus.typeface = this
}
mLinePaintIncomplete = Paint(mLinePaint)
mLinePaintIncomplete.color = lineColorIncomplete
mLinePaintCurrent = Paint(mLinePaint)
mLinePaintCurrent.color = lineColorCurrent
mTextPaintLabels = TextPaint(Paint.ANTI_ALIAS_FLAG)
mTextPaintLabels.style = Paint.Style.FILL
mTextPaintLabels.textAlign = Paint.Align.CENTER
mTextPaintLabels.textSize = textSizeLabels
mTextPaintLabels.color = textColorLabels
mTextPaintLabelsIncomplete = TextPaint(mTextPaintLabels)
mTextPaintLabelsIncomplete.color = textColorLabelsIncomplete
mTextPaintLabelCurrent = TextPaint(mTextPaintLabels)
mTextPaintLabelCurrent.color = textColorLabelCurrent
labelsTypeface?.run {
mTextPaintLabels.typeface = this
mTextPaintLabelsIncomplete.typeface = this
mTextPaintLabelCurrent.typeface = this
}
propsIntialisedOnce = true
}
private fun initCirclePaints() {
if (containsFlag(circleColorType, CIRCLE_COLOR_TYPE_STROKE)) {
if (mCircleStrokePaint == null) {
mCircleStrokePaint = Paint(Paint.ANTI_ALIAS_FLAG)
mCircleStrokePaint?.style = Paint.Style.STROKE
mCircleStrokePaint?.strokeWidth = circleStrokeWidth
mCircleStrokePaint?.color = circleStrokeColor
}
if (isShowingIncompleteStatus()) {
if (mCircleStrokePaintIncomplete == null) {
mCircleStrokePaintIncomplete = Paint(mCircleStrokePaint)
mCircleStrokePaintIncomplete?.color = circleStrokeColorIncomplete
}
}
if (isShowingCurrentStatus()) {
if (mCircleStrokePaintCurrent == null) {
mCircleStrokePaintCurrent = Paint(mCircleStrokePaint)
mCircleStrokePaintCurrent?.color = circleStrokeColorCurrent
}
}
} else {
circleStrokeWidth = 0.0f
mCircleStrokePaint = null
mCircleStrokePaintIncomplete = null
mCircleStrokePaintCurrent = null
}
if (containsFlag(circleColorType, CIRCLE_COLOR_TYPE_FILL)) {
if (mCircleFillPaint == null) {
mCircleFillPaint = Paint(Paint.ANTI_ALIAS_FLAG)
mCircleFillPaint?.style = Paint.Style.FILL
mCircleFillPaint?.color = circleFillColor
}
if (isShowingIncompleteStatus()) {
if (mCircleFillPaintIncomplete == null) {
mCircleFillPaintIncomplete = Paint(mCircleFillPaint)
mCircleFillPaintIncomplete?.color = circleFillColorIncomplete
}
}
if (isShowingCurrentStatus()) {
if (mCircleFillPaintCurrent == null) {
mCircleFillPaintCurrent = Paint(mCircleFillPaint)
mCircleFillPaintCurrent?.color = circleFillColorCurrent
}
}
} else {
mCircleFillPaint = null
mCircleFillPaintIncomplete = null
mCircleFillPaintCurrent = null
}
}
private fun isShowingIncompleteStatus() =
currentCount > INVALID_STATUS_COUNT && currentCount < stepCount
private fun isShowingCurrentStatus() =
currentCount in 1..stepCount
override fun getSuggestedMinimumWidth(): Int {
if (stepCount <= 0) {
return 0
}
lineLengthComputed = lineLength
var extraWidth = if (strictObeyLineLength) {// extra width required by status at extreme positions
setWidthDataForObeyingLineLength()
} else {
setWidthDataForObeyingStatusText()
}
if (stepCount == 1) {
extraWidth *= 2
}
if (isShowingCurrentStatus()) {
extraWidth += (currentStatusRadius - circleRadius) * 2
}
return ((stepCount * (2 * (circleRadius + (circleStrokeWidth / 2)))) +
((stepCount - 1) * (lineLengthComputed + (lineGap * 2))) + extraWidth).toInt()
}
override fun getSuggestedMinimumHeight(): Int {
if (stepCount <= 0) {
return 0
}
var maxLabelHeight = 0.0f
for (item in statusData) {
maxLabelHeight = Math.max(maxLabelHeight, setLabelsHeight(mTextPaintStatus, item))
}
val circleAndStatusTextHeight = if (isShowingCurrentStatus()) {
val topRadius = currentStatusRadius
val labelHeightCurrentStatus = if (statusData.size > 0 && currentCountIndex() in 0..(statusData.size-1)) {
statusData[currentCountIndex()].height
} else {
0.0f
}
val bottomRadiusAndText = Math.max(currentStatusRadius + labelHeightCurrentStatus, circleRadius + maxLabelHeight)
topRadius + bottomRadiusAndText
} else {
circleRadius * 2 + maxLabelHeight
}
val statusTopMargin = if (statusData.size > 0) {
statusTopMargin
} else {
0.0f
}
return (circleAndStatusTextHeight + circleStrokeWidth + statusTopMargin).toInt()
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val desiredWidth = paddingLeft + paddingRight + suggestedMinimumWidth
val desiredHeight = paddingTop + paddingBottom + suggestedMinimumHeight
val measuredWidth = resolveSize(desiredWidth, widthMeasureSpec)
val measuredHeight = resolveSize(desiredHeight, heightMeasureSpec)
setMeasuredDimension(measuredWidth, measuredHeight)
}
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
setDrawingDimensions()
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
for (item in drawingData) {
if (item.circleItem.fillPaint != null) {
canvas?.drawCircle(item.circleItem.center.x, item.circleItem.center.y, item.circleItem.radius, item.circleItem.fillPaint)
}
if (item.circleItem.strokePaint != null) {
canvas?.drawCircle(item.circleItem.center.x, item.circleItem.center.y, item.circleItem.radius, item.circleItem.strokePaint)
}
if (item.textData != null) {
if (item.textData.drawableItem != null) {
val drawableItem: DrawableItem = item.textData.drawableItem
drawableItem.drawable.bounds = drawableItem.rect
item.textData.drawableItem.drawable.draw(canvas)
} else if (item.textData.text != null && item.textData.paint != null) {
canvas?.drawText(item.textData.text, item.textData.x, item.textData.y, item.textData.paint)
}
}
if (item.labelData != null) {
if (item.labelData.staticLayout != null) {
canvas?.save()
canvas?.translate(item.labelData.x, item.labelData.y)
item.labelData.staticLayout.draw(canvas)
canvas?.restore()
}
}
if (item.lineItem != null) {
canvas?.drawLine(item.lineItem.start.x, item.lineItem.start.y,
item.lineItem.end.x, item.lineItem.end.y, item.lineItem.paint)
}
}
}
/**
* Sets data in DrawingData class that is to be drawn by Canvas
*/
private fun setDrawingDimensions() {
val lastPoint = PointF()//For keeping reference where last point was drawn
lastPoint.x = paddingLeft.toFloat() + (circleStrokeWidth / 2)
lastPoint.y = paddingTop.toFloat() + (circleRadius + (circleStrokeWidth / 2))
if (isShowingCurrentStatus()) lastPoint.y += currentStatusRadius - circleRadius
for (pos in 0 until stepCount) {
var circleStrokePaint: Paint?
var circleFillPaint: Paint?
var textPaintLabel: TextPaint
var linePaint: Paint
var itemDrawable: Drawable?
var circleRadius = this.circleRadius
if (isShowingCurrentStatus() && pos == (currentCount - 1)) {
circleRadius = currentStatusRadius
circleStrokePaint = mCircleStrokePaintCurrent
circleFillPaint = mCircleFillPaintCurrent
textPaintLabel = mTextPaintLabelCurrent
linePaint = mLinePaintCurrent
itemDrawable = currentDrawable
} else if (isShowingIncompleteStatus() && pos in (currentCount)..stepCount) {
circleStrokePaint = mCircleStrokePaintIncomplete
circleFillPaint = mCircleFillPaintIncomplete
textPaintLabel = mTextPaintLabelsIncomplete
linePaint = mLinePaintIncomplete
itemDrawable = incompleteDrawable
} else {
circleStrokePaint = this.mCircleStrokePaint
circleFillPaint = this.mCircleFillPaint
textPaintLabel = this.mTextPaintLabels
linePaint = this.mLinePaint
itemDrawable = completeDrawable
}
var lineItem: LineItem? = null
var statusItemText: LabelItemText? = null
var labelItemText: StatusItemText? = null
if (pos == 0) {
if (statusData.size > 0) {
lastPoint.x += Math.max(0.0f, (statusData[0].width - minStatusWidthExtremes(pos)) / 2)
}
} else {
lastPoint.x += lineGap
lineItem = LineItem(PointF(lastPoint.x, lastPoint.y), PointF(lastPoint.x + lineLengthComputed, lastPoint.y), linePaint)
lastPoint.x = lineItem.end.x + lineGap + (circleStrokeWidth / 2)
}
val circleItem = CircleItem(PointF((lastPoint.x + circleRadius), lastPoint.y), circleRadius, circleStrokePaint, circleFillPaint)
lastPoint.x += ((circleRadius) * 2.0f) + (circleStrokeWidth / 2)
if (pos < statusData.size) {
val radii = if (isShowingCurrentStatus() && alignStatusWithCurrent) currentStatusRadius else circleRadius
labelItemText = StatusItemText(circleItem.center.x, circleItem.center.y + radii + circleStrokeWidth / 2 + statusTopMargin, statusData[pos].staticLayout)
}
if (itemDrawable != null) {
val width = itemDrawable.intrinsicWidth
val height = itemDrawable.intrinsicHeight
val xPos = circleItem.center.x.toInt()
val yPos = circleItem.center.y.toInt()
val drawableRect = Rect(xPos - width / 2, yPos - height / 2, xPos + width / 2, yPos + height / 2)
statusItemText = LabelItemText(drawableItem = DrawableItem(drawableRect, itemDrawable))
} else if (drawLabels) {
val text: String = (pos + 1).toString()
val measuringRect = Rect()
textPaintLabel.getTextBounds(text, 0, text.length, measuringRect)
statusItemText = LabelItemText(text, textPaintLabel, circleItem.center.x, circleItem.center.y - measuringRect.exactCenterY())
}
drawingData.add(Item(statusItemText, circleItem, lineItem, labelItemText))
}
}
/**
*
For non-extreme statuses:
* Function sets the width value to minStatusWidth
*
* For extreme statuses:
* It calculates min of extreme width required in bounds of lineLength as
* directly adding minStatusWidth may end up adding extra padding to view
*/
private fun setWidthDataForObeyingLineLength(): Float {
var adjacentExtraWidthForView = 0.0f
for (pos in 0 until statusData.size) {
val item = statusData[pos]
when (pos) {
0, stepCountIndex() -> {
val minStatusWidthExtremes = minStatusWidthExtremes(pos)
val minStatusWidth = minStatusWidth(pos)
val statusWidth = getTextWidth(mTextPaintStatus, item.text)
var extraExtremeWidth = 0.0f
if (statusWidth > minStatusWidthExtremes) {
extraExtremeWidth = Math.min(minStatusWidth - minStatusWidthExtremes, statusWidth - minStatusWidthExtremes) / 2
}
item.width = minStatusWidthExtremes + 2 * extraExtremeWidth
adjacentExtraWidthForView += extraExtremeWidth
}
else -> item.width = minStatusWidth(pos)
}
}
return adjacentExtraWidthForView
}
/**
* Sets required width in status data for every status
* This method increases linelength if required i.e
* it makes sure every line of status word does not cross over to next line
*
*/
private fun setWidthDataForObeyingStatusText(): Float {
var extraWidth = 0.0f
if (statusData.size == 0) return extraWidth
val widestLineData: StatusTextWidthInfo = getStatusTextWidthInfo(statusData.map { it.text }, mTextPaintStatus)
val minStatusWidthWidestPos = minStatusWidth(widestLineData.widestStatus.pos)
if (widestLineData.widestStatus.width > minStatusWidthWidestPos) {
lineLengthComputed += widestLineData.widestStatus.width - minStatusWidthWidestPos
}
widestLineData.subordinateWidestStatus?.run {
//in case increasing linelength as per widest Status does not satisfy statusWidth for second widest
//This can occur if widestStatus belongs to currentStatus and has a zoomed radius
val minStatusWidth = minStatusWidth(pos)
if (width > minStatusWidth) {
lineLengthComputed += width - minStatusWidth
}
}
var addMinStatusMargin = false
for (pos in 0 until statusData.size) {
val item = statusData[pos]
when (pos) {
0, stepCountIndex() -> {
item.width = if (pos == 0) {
widestLineData.extremeLeftStatusWidth
} else {
widestLineData.extremeRightStatusWidth
}
val extraExtremesWidth = (item.width - minStatusWidthExtremes(pos)) / 2
if (extraExtremesWidth > 0) {
extraWidth += extraExtremesWidth
}
}
else -> item.width = widestLineData.widestStatus.width
}
if (minStatusAdjacentMargin > 0 && !addMinStatusMargin && pos in 1 until statusData.size) {
if (minStatusWidth(pos) + minStatusWidth(pos - 1) - (item.width + statusData[pos - 1].width) < minStatusAdjacentMargin) {
addMinStatusMargin = true
}
}
}
if (addMinStatusMargin) {
//add additional padding only if it is required
lineLengthComputed += minStatusAdjacentMargin
}
return extraWidth
}
private fun minStatusWidth(pos: Int): Float {
var circleRadius = this.circleRadius
val lineWidth = (lineLengthComputed + lineGap * 2)
if (isShowingCurrentStatus() && pos == currentCountIndex()) {
circleRadius = currentStatusRadius
}
return (2 * circleRadius + circleStrokeWidth) + lineWidth
}
private fun minStatusWidthExtremes(pos: Int): Float {
var circleRadius = this.circleRadius
if (isShowingCurrentStatus() && pos == currentCountIndex()) {
circleRadius = currentStatusRadius
}
return (2 * circleRadius + circleStrokeWidth)
}
/**
* Maximum width that a text would need to be drawn
*/
private fun getTextWidth(paint: Paint, text: String): Float {
return paint.measureText(text)
}
/**
* @param textPaint textPaint to be set on Labels
* @param labelInfo LabelInfo which contains width and text
* @return Height that the label would require
*/
private fun setLabelsHeight(textPaint: TextPaint, labelInfo: StatusInfo): Float {
val staticLayoutHeight = getStaticLayout(labelInfo.text, textPaint, labelInfo.width)
labelInfo.staticLayout = staticLayoutHeight
labelInfo.height = staticLayoutHeight.height.toFloat()
return labelInfo.height
}
/**
* @param text text to be shown
* @param textPaint textPaint to be set on text
* @param width available width
* @return Static Layout which decides the height and auto-adjusts multiline text
*/
private fun getStaticLayout(text: String, textPaint: TextPaint, width: Float): StaticLayout {
val alignment = Layout.Alignment.ALIGN_NORMAL
val spacingMultiplier = 1f
val spacingAddition = 0f
val includePadding = false
return StaticLayout(text, textPaint, width.toInt(), alignment, spacingMultiplier, spacingAddition, includePadding)
}
/**
* This class stores info for widest Status info length which helps in deciding correct line length of StatusView
* @param widestStatus (widest line in all the texts)
* @param subordinateWidestStatus second Widest Status this is only set if widest status belongs to currentStatus
* @param extremeLeftStatusWidth Widest line in status at extreme left
* @param extremeRightStatusWidth Widest line in status at extreme right
*/
class StatusTextWidthInfo(var widestStatus: StatusWidth, var subordinateWidestStatus: StatusWidth? = null,
var extremeLeftStatusWidth: Float = 0.0f, var extremeRightStatusWidth: Float = 0.0f)
/**
* Stores Status Width Data
* @param width widestStatus
* @param pos Status position