Skip to content

Commit fad779d

Browse files
author
Parminder
committed
dev: code cleanup for fixedLineWidthData & methods reaaranged
1 parent 3f159a4 commit fad779d

File tree

3 files changed

+152
-160
lines changed

3 files changed

+152
-160
lines changed

app/src/main/java/jugnoo/com/learningcustomvviews/StatusView.kt

Lines changed: 147 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -500,93 +500,31 @@ class StatusView @JvmOverloads constructor(
500500
currentCount > INVALID_STATUS_COUNT && currentCount < statusCount
501501

502502
private fun isShowingCurrentStatus()=
503-
currentCount > 0 && currentCount <= statusCount
503+
currentCount in 1..statusCount
504504

505505

506506
override fun getSuggestedMinimumWidth(): Int {
507507

508-
var extraWidthInCase = 0.0f
509-
if(false){
510-
511-
val widestLineData:StatusTextWidthInfo = getStatusTextWidthInfo(statusData.map { it.text },mTextPaintStatus)
512-
513-
lineLength += widestLineData.widestStatus.width - minStatusWidth(widestLineData.widestStatus.pos)
514-
515-
widestLineData.subordinateWidestStatus?.run {
516-
val minStatusWidth = minStatusWidth(pos)
517-
518-
if (width > minStatusWidth) {
519-
lineLength += width - minStatusWidth
520-
}
521-
}
522-
523-
524-
for (pos in 0 until statusData.size) {
525-
val item = statusData[pos]
526-
when (pos) {
527-
0, (statusCount - 1) -> {
528-
529-
item.width = if (pos == 0) {
530-
widestLineData.extremeLeftStatusWidth
531-
} else {
532-
widestLineData.extremeRightStatusWidth
533-
}
534-
535-
val extraExtremesWidth = (item.width - minStatusWidthExtremes(pos)) / 2
536-
537-
if (extraExtremesWidth > 0) {
538-
extraWidthInCase += extraExtremesWidth
539-
}
540-
541-
}
542-
else -> item.width = widestLineData.widestStatus.width
543-
}
544-
545-
}
546-
547-
548-
549-
550508

509+
var extraWidth = if(true){
510+
setWidthDataForObeyingStatusText()
551511
}else{
552-
extraWidthInCase+= setWidthData()
553-
554-
512+
setWidthDataForObeyingLineLength()
555513
}
556514

557-
558515
if(isShowingCurrentStatus()){
559-
extraWidthInCase += (currentStatusRadius-circleRadius)*2;
560-
}
561-
562-
if(statusCount==1) {
563-
extraWidthInCase *= 2
516+
extraWidth += (currentStatusRadius-circleRadius)*2;
564517
}
565518

566-
return ((statusCount * (2 * (circleRadius + (circleStrokeWidth/2)))) + ((statusCount - 1) * ( lineLength + (lineGap * 2))) + extraWidthInCase).toInt()
567-
}
568-
569-
570-
private fun minStatusWidth(pos:Int): Float {
571-
572-
var circleRadius = this.circleRadius
573-
val lineWidth = (lineLength + lineGap*2)
574519

575-
if(isShowingCurrentStatus() && pos==currentCountIndex()){
576-
circleRadius = currentStatusRadius
520+
if(statusCount==1) {
521+
extraWidth *= 2
577522
}
578523

579-
return (2 * circleRadius + circleStrokeWidth) + lineWidth;
524+
return ((statusCount * (2 * (circleRadius + (circleStrokeWidth/2)))) + ((statusCount - 1) * ( lineLength + (lineGap * 2))) + extraWidth).toInt()
580525
}
581526

582-
private fun minStatusWidthExtremes(pos:Int): Float {
583-
var circleRadius = this.circleRadius
584527

585-
if(isShowingCurrentStatus() && pos==currentCountIndex()){
586-
circleRadius = currentStatusRadius
587-
}
588-
return (2 * circleRadius + circleStrokeWidth)
589-
}
590528

591529

592530
override fun getSuggestedMinimumHeight(): Int {
@@ -702,7 +640,6 @@ class StatusView @JvmOverloads constructor(
702640
var circleRadius = this.circleRadius
703641
if(isShowingCurrentStatus() && i==(currentCount-1)){
704642
circleRadius = currentStatusRadius
705-
706643
circleStrokePaint = mCircleStrokePaintCurrent
707644
circleFillPaint = mCircleFillPaintCurrent
708645
textPaintLabel = mTextPaintLabelCurrent
@@ -775,15 +712,6 @@ class StatusView @JvmOverloads constructor(
775712
}
776713

777714

778-
/**
779-
* checks if a flagSet contains a flag
780-
*
781-
*/
782-
783-
private fun containsFlag(flagSet: Int, flag: Int): Boolean {
784-
return flagSet or flag == flagSet
785-
}
786-
787715
/**
788716
* @param lineLength lineLength of StatusView
789717
* @param circleRadius circleRadius Of StatusView
@@ -795,47 +723,98 @@ class StatusView @JvmOverloads constructor(
795723
* It calculates value using findAdjustWidthForExtremes() as assigning the same width as
796724
* non-extreme may end up giving extraPadding to the view
797725
*/
798-
private fun setWidthData():Float {
726+
private fun setWidthDataForObeyingLineLength():Float {
799727
var adjacentExtraWidthForView = 0.0f
800-
for (i in 0 until statusData.size){
801728

802-
val circleRadius = if(isShowingCurrentStatus() && i==(currentCount-1))currentStatusRadius else circleRadius;
729+
for (pos in 0 until statusData.size){
730+
731+
val item = statusData[pos]
732+
when (pos) {
733+
0, statusCount - 1 -> {
734+
735+
val minStatusWidthExtremes = minStatusWidthExtremes(pos)
736+
val minStatusWidth = minStatusWidth(pos)
737+
val statusWidth = getTextWidth(mTextPaintStatus, item.text)
738+
var extraExtremeWidth = 0.0f
739+
740+
if (statusWidth > minStatusWidthExtremes) {
741+
extraExtremeWidth = Math.min(minStatusWidth - minStatusWidthExtremes, statusWidth - minStatusWidthExtremes) / 2
742+
}
743+
744+
item.width = minStatusWidthExtremes + 2 * extraExtremeWidth
745+
adjacentExtraWidthForView += extraExtremeWidth
803746

804-
if(i==0 || i==(statusCount-1)){
805-
val extraWidth = findAdjustWidthForExtremes(statusData[i].text,lineLength,circleRadius)
806-
adjacentExtraWidthForView+=extraWidth
807-
val minWidthForExtreme = (2 * circleRadius + circleStrokeWidth)
808-
if(extraWidth>0){
809-
statusData[i].width = minWidthForExtreme + 2 * extraWidth
810747

811-
}else{
812-
statusData[i].width = minWidthForExtreme
813748
}
814-
}else{
815-
statusData[i].width = (lineLength + lineGap*2) + (2 * circleRadius + circleStrokeWidth)
749+
else -> item.width = minStatusWidth(pos)
750+
816751
}
817752
}
818753
return adjacentExtraWidthForView
819754
}
820755

821-
/**
822-
* @param text Status text
823-
* @param lineLength lineLength of StatusView
824-
* @param circleRadius circleRadius Of StatusView
825-
* @return Returns max width that an extreme label would need besides the circle Width
826-
* The return value does not exceed half the linelength on each side, since it would affect the symmetry.
827-
* i.e circleWidth < return value > linelength
828-
*/
829-
private fun findAdjustWidthForExtremes(text: String, lineLength: Float, circleRadius: Float):Float {
830-
val totalWidth = getTextWidth(mTextPaintStatus, text)
831-
val actualWidth = (2*(circleRadius + circleStrokeWidth/2))
832-
val extraWidth = Math.max(totalWidth, actualWidth)
833-
return if(extraWidth==totalWidth){
834-
Math.min((lineLength + lineGap*2),(totalWidth-actualWidth))/2
835-
}else{
836-
0.0f
756+
757+
private fun setWidthDataForObeyingStatusText(): Float {
758+
var extraWidth = 0.0f
759+
760+
val widestLineData: StatusTextWidthInfo = getStatusTextWidthInfo(statusData.map { it.text }, mTextPaintStatus)
761+
762+
lineLength += widestLineData.widestStatus.width - minStatusWidth(widestLineData.widestStatus.pos)
763+
764+
widestLineData.subordinateWidestStatus?.run {
765+
val minStatusWidth = minStatusWidth(pos)
766+
767+
if (width > minStatusWidth) {
768+
lineLength += width - minStatusWidth
769+
}
770+
}
771+
772+
773+
for (pos in 0 until statusData.size) {
774+
val item = statusData[pos]
775+
when (pos) {
776+
0, (statusCount - 1) -> {
777+
778+
item.width = if (pos == 0) {
779+
widestLineData.extremeLeftStatusWidth
780+
} else {
781+
widestLineData.extremeRightStatusWidth
782+
}
783+
784+
val extraExtremesWidth = (item.width - minStatusWidthExtremes(pos)) / 2
785+
786+
if (extraExtremesWidth > 0) {
787+
extraWidth += extraExtremesWidth
788+
}
789+
790+
}
791+
else -> item.width = widestLineData.widestStatus.width
792+
}
793+
794+
}
795+
return extraWidth
796+
}
797+
798+
799+
private fun minStatusWidth(pos:Int): Float {
800+
801+
var circleRadius = this.circleRadius
802+
val lineWidth = (lineLength + lineGap*2)
803+
804+
if(isShowingCurrentStatus() && pos==currentCountIndex()){
805+
circleRadius = currentStatusRadius
837806
}
838807

808+
return (2 * circleRadius + circleStrokeWidth) + lineWidth;
809+
}
810+
811+
private fun minStatusWidthExtremes(pos:Int): Float {
812+
var circleRadius = this.circleRadius
813+
814+
if(isShowingCurrentStatus() && pos==currentCountIndex()){
815+
circleRadius = currentStatusRadius
816+
}
817+
return (2 * circleRadius + circleStrokeWidth)
839818
}
840819

841820
/**
@@ -851,7 +830,7 @@ class StatusView @JvmOverloads constructor(
851830
* @param labelInfo LabelInfo which contains width and text
852831
* @return Height that the label would require
853832
*/
854-
private fun setLabelsHeight(textPaint:TextPaint, labelInfo: StatusInfo):Float{
833+
private fun setLabelsHeight(textPaint:TextPaint, labelInfo: StatusInfo):Float{
855834
val staticLayoutHeight = getStaticLayout(labelInfo.text, textPaint, labelInfo.width)
856835
labelInfo.staticLayout = staticLayoutHeight
857836
labelInfo.height = staticLayoutHeight.height.toFloat()
@@ -874,50 +853,6 @@ class StatusView @JvmOverloads constructor(
874853
}
875854

876855

877-
/**
878-
* Delegate property used to requestLayout if any value changed
879-
*/
880-
inner class OnLayoutProp<T> (private var field:T, private inline var func:()->Unit={}){
881-
operator fun setValue(thisRef: Any?,p: KProperty<*>,v: T) {
882-
field = v
883-
if(ViewCompat.isLaidOut(this@StatusView)){
884-
drawingData.clear()
885-
func()
886-
requestLayout()
887-
888-
}
889-
890-
}
891-
operator fun getValue(thisRef: Any?,p: KProperty<*>):T{
892-
return field
893-
}
894-
895-
}
896-
897-
/**
898-
* Delegate Property used to invalidate a layout after executing a custom function
899-
*/
900-
inner class OnValidateProp<T> (private var field:T, private inline var func:()->Unit={}){
901-
operator fun setValue(thisRef: Any?,p: KProperty<*>,v: T) {
902-
field = v
903-
if(ViewCompat.isLaidOut(this@StatusView)){
904-
func()
905-
invalidate()
906-
907-
}
908-
909-
}
910-
operator fun getValue(thisRef: Any?,p: KProperty<*>):T{
911-
return field
912-
}
913-
914-
}
915-
916-
private fun Float.pxValue(unit:Int = TypedValue.COMPLEX_UNIT_DIP):Float{
917-
return TypedValue.applyDimension(unit,this,resources.displayMetrics)
918-
}
919-
920-
921856
class StatusTextWidthInfo(var widestStatus:StatusWidth, var subordinateWidestStatus:StatusWidth?=null,
922857
var extremeLeftStatusWidth:Float=0.0f, var extremeRightStatusWidth:Float=0.0f)
923858

@@ -962,7 +897,7 @@ class StatusView @JvmOverloads constructor(
962897
statusWidthInfo.widestStatus = widestStatus
963898
statusWidthInfo.subordinateWidestStatus = subordinateWidestStatus
964899

965-
return statusWidthInfo;
900+
return statusWidthInfo
966901

967902
}
968903

@@ -980,7 +915,64 @@ class StatusView @JvmOverloads constructor(
980915

981916
}
982917

918+
919+
/**
920+
* checks if a flagSet contains a flag
921+
*
922+
*/
923+
924+
private fun containsFlag(flagSet: Int, flag: Int): Boolean {
925+
return flagSet or flag == flagSet
926+
}
927+
928+
983929
private fun currentCountIndex() = currentCount-1
930+
984931
private fun statusCountIndex() = statusCount-1
985932

933+
934+
/**
935+
* Delegate property used to requestLayout if any value changed
936+
*/
937+
inner class OnLayoutProp<T> (private var field:T, private inline var func:()->Unit={}){
938+
operator fun setValue(thisRef: Any?,p: KProperty<*>,v: T) {
939+
field = v
940+
if(ViewCompat.isLaidOut(this@StatusView)){
941+
drawingData.clear()
942+
func()
943+
requestLayout()
944+
945+
}
946+
947+
}
948+
operator fun getValue(thisRef: Any?,p: KProperty<*>):T{
949+
return field
950+
}
951+
952+
}
953+
954+
/**
955+
* Delegate Property used to invalidate a layout after executing a custom function
956+
*/
957+
inner class OnValidateProp<T> (private var field:T, private inline var func:()->Unit={}){
958+
operator fun setValue(thisRef: Any?,p: KProperty<*>,v: T) {
959+
field = v
960+
if(ViewCompat.isLaidOut(this@StatusView)){
961+
func()
962+
invalidate()
963+
964+
}
965+
966+
}
967+
operator fun getValue(thisRef: Any?,p: KProperty<*>):T{
968+
return field
969+
}
970+
971+
}
972+
973+
private fun Float.pxValue(unit:Int = TypedValue.COMPLEX_UNIT_DIP):Float{
974+
return TypedValue.applyDimension(unit,this,resources.displayMetrics)
975+
}
976+
977+
986978
}

0 commit comments

Comments
 (0)