@@ -14,15 +14,22 @@ import com.sn.lib.Constants.ANIM_DURATION
1414import com.sn.lib.Constants.CIRCLE_RADIUS
1515import com.sn.lib.Constants.COLOR_BLUE
1616import com.sn.lib.Constants.COLOR_LIGHT_BLUE
17+ import com.sn.lib.Constants.DESIRED_WH
1718import com.sn.lib.Constants.INNER_ANIM_INTERPOLATOR
1819import com.sn.lib.Constants.INNER_LOADER_LENGTH
1920import com.sn.lib.Constants.INNER_STROKE_WIDTH
21+ import com.sn.lib.Constants.MAX_STROKE
22+ import com.sn.lib.Constants.MAX_TOTAL_STROKE
2023import com.sn.lib.Constants.MID_POINT
24+ import com.sn.lib.Constants.MIN_STOKE
2125import com.sn.lib.Constants.OUTER_ANIM_INTERPOLATOR
2226import com.sn.lib.Constants.OUTER_LOADER_LENGTH
2327import com.sn.lib.Constants.OUTER_STROKE_WIDTH
24- import com.sn.lib.Constants.POSITION_COEFFICIENT
28+ import com.sn.lib.Constants.SPACE_BETWEEN_CIRCLES
29+ import com.sn.lib.Constants.START_POINT
2530import com.sn.lib.ext.dp
31+ import kotlin.math.min
32+ import kotlin.math.roundToInt
2633
2734/* *
2835 * @author Aydin Emre E.
@@ -64,17 +71,6 @@ class NestedProgress @JvmOverloads constructor(
6471 var innerAnimInterpolator = INNER_ANIM_INTERPOLATOR
6572 var outerAnimInterpolator = OUTER_ANIM_INTERPOLATOR
6673
67- /* * There is no limit value for stroke width, but correct values should be used for a smooth display
68- * - innerLoaderStrokeWidth
69- * - outerLoaderStrokeWidth
70- * @see [dp] An extensions written for float. It is used to convert the default value to dp.
71- */
72- @Dimension
73- var innerLoaderStrokeWidth: Float = INNER_STROKE_WIDTH .dp
74-
75- @Dimension
76- var outerLoaderStrokeWidth: Float = OUTER_STROKE_WIDTH .dp
77-
7874 @ColorInt
7975 var innerLoaderColor: Int = COLOR_LIGHT_BLUE
8076
@@ -85,10 +81,48 @@ class NestedProgress @JvmOverloads constructor(
8581 * - innerLoaderLength
8682 * - outerLoaderLength
8783 */
88-
8984 var innerLoaderLength: Float = INNER_LOADER_LENGTH
9085 var outerLoaderLength: Float = OUTER_LOADER_LENGTH
9186
87+ /* * Stroke can be in the range 0 to 10, otherwise the illegal argument exception error is thrown.
88+ * @throws IllegalArgumentException
89+ * - innerLoaderStrokeWidth
90+ * - outerLoaderStrokeWidth
91+ * @see [dp] An extensions written for float. It is used to convert the default value to dp.
92+ */
93+ @Dimension
94+ var innerLoaderStrokeWidth: Float = INNER_STROKE_WIDTH .dp
95+ set(value) {
96+ field =
97+ if (value > MAX_STROKE .dp || value < MIN_STOKE .dp) throw IllegalArgumentException (
98+ resources.getString(
99+ R .string.stroke_range_error
100+ )
101+ ) else value
102+ }
103+
104+ @Dimension
105+ var outerLoaderStrokeWidth: Float = OUTER_STROKE_WIDTH .dp
106+ set(value) {
107+ field =
108+ if (value > MAX_STROKE .dp || value < MIN_STOKE .dp) throw IllegalArgumentException (
109+ resources.getString(
110+ R .string.stroke_range_error
111+ )
112+ ) else value
113+ }
114+
115+ @Dimension
116+ var spaceBetweenCircles = SPACE_BETWEEN_CIRCLES .dp
117+ set(value) {
118+ field =
119+ if (value > MAX_STROKE .dp || value < MIN_STOKE .dp) throw IllegalArgumentException (
120+ resources.getString(
121+ R .string.stroke_range_error
122+ )
123+ ) else value
124+ }
125+
92126 /* *
93127 * You can find the attributes from the attrs.xml file.
94128 */
@@ -147,35 +181,71 @@ class NestedProgress @JvmOverloads constructor(
147181 this .outerAnimInterpolator
148182 )
149183
184+ spaceBetweenCircles =
185+ attributes.getDimension(
186+ R .styleable.NestedProgress_spaceBetweenCircles ,
187+ this .spaceBetweenCircles
188+ )
189+
150190 attributes.recycle()
151191
152192 innerLoaderAnimation()
153193 outerLoaderAnimation()
154194 }
155195
196+ override fun onMeasure (widthMeasureSpec : Int , heightMeasureSpec : Int ) {
197+ val widthMode = MeasureSpec .getMode(widthMeasureSpec)
198+ val widthSize = MeasureSpec .getSize(widthMeasureSpec)
199+ val heightMode = MeasureSpec .getMode(heightMeasureSpec)
200+ val heightSize = MeasureSpec .getSize(heightMeasureSpec)
201+ val desired = DESIRED_WH .dp.roundToInt()
202+
203+ val width = when (widthMode) {
204+ MeasureSpec .EXACTLY -> {
205+ widthSize
206+ }
207+ MeasureSpec .AT_MOST -> {
208+ min(desired, widthSize)
209+ }
210+ else -> {
211+ desired
212+ }
213+ }
156214
157- override fun onDraw (canvas : Canvas ) {
158- super .onDraw(canvas)
215+ val height = when (heightMode) {
216+ MeasureSpec .EXACTLY -> {
217+ heightSize
218+ }
219+ MeasureSpec .AT_MOST -> {
220+ min(desired, heightSize)
221+ }
222+ else -> {
223+ desired
224+ }
225+ }
159226
160- val centerW : Float = width / MID_POINT
161- val centerH : Float = height / MID_POINT
162- val position : Float = width / POSITION_COEFFICIENT
163- val distanceCircles : Float = (innerLoaderStrokeWidth + outerLoaderStrokeWidth) / MID_POINT
227+ setMeasuredDimension( width, height)
228+
229+ val highStroke = outerLoaderStrokeWidth + innerLoaderStrokeWidth
230+ val expansion = MAX_TOTAL_STROKE .dp - highStroke
164231
165232 outerLoadingRect.set(
166- centerW - (position / MID_POINT ) + distanceCircles ,
167- centerH - (position / MID_POINT ) + distanceCircles ,
168- centerW + (position / MID_POINT ) + distanceCircles ,
169- centerH + (position / MID_POINT ) + distanceCircles,
233+ START_POINT + expansion + (highStroke / MID_POINT ),
234+ START_POINT + expansion + (highStroke / MID_POINT ),
235+ width - (expansion + (highStroke / MID_POINT )) ,
236+ width - (expansion + (highStroke / MID_POINT ))
170237 )
171238
172239 innerLoadingRect.set(
173- centerW - position / MID_POINT ,
174- centerH - position / MID_POINT ,
175- centerW + position / MID_POINT ,
176- centerH + position / MID_POINT ,
240+ START_POINT + (expansion + highStroke + spaceBetweenCircles) ,
241+ START_POINT + (expansion + highStroke + spaceBetweenCircles) ,
242+ width - (expansion + highStroke + spaceBetweenCircles) ,
243+ width - (expansion + highStroke + spaceBetweenCircles)
177244 )
245+ }
178246
247+ override fun onDraw (canvas : Canvas ) {
248+ super .onDraw(canvas)
179249 updatePaint(outerLoaderColor, outerLoaderStrokeWidth)
180250 canvas.drawArc(
181251 outerLoadingRect,
0 commit comments