Skip to content

Commit 9bbe8c8

Browse files
websoptimizationwebsoptimization
authored andcommitted
solved multiple calling dashed progress
1 parent 816a4dd commit 9bbe8c8

File tree

2 files changed

+56
-50
lines changed

2 files changed

+56
-50
lines changed

dashed-progress/index.js

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,30 @@ const responsiveHeight = h => {
1111
};
1212

1313
var number = [];
14-
var bigCircle = [];
15-
var increment_timer = () => {};
16-
var decrement_timer = () => {};
1714
var stopIndicator = {};
1815

1916
export class DashedProgress extends PureComponent {
2017
constructor(props) {
2118
super(props);
22-
23-
//set animated state value
24-
this.state = {
25-
last_stroke_index: 0,
26-
last_trail_index: 0
27-
};
28-
2919
//calculate first position of stopIndicator
3020
const { radius, barWidth, strokeThickness, indicatorWidth } = props;
3121
const center =
3222
radius + Math.max(barWidth, indicatorWidth) + strokeThickness;
33-
stopIndicator = {
34-
fromX: center,
35-
fromY: center - (radius - barWidth),
36-
toX: center,
37-
toY: center - (radius + indicatorWidth)
23+
//set animated state value
24+
this.state = {
25+
last_stroke_index: 0,
26+
last_trail_index: 0,
27+
bigCircle: [],
28+
stopIndicator: {
29+
fromX: center,
30+
fromY: center - (radius - barWidth),
31+
toX: center,
32+
toY: center - (radius + indicatorWidth)
33+
}
3834
};
35+
36+
this.increment_timer = () => {};
37+
this.decrement_timer = () => {};
3938
}
4039

4140
componentDidUpdate(prevProps) {
@@ -79,12 +78,7 @@ export class DashedProgress extends PureComponent {
7978
divideEnabled,
8079
trailColor
8180
} = this.props;
82-
console.log(
83-
"radius >>>>>>>>> ",
84-
radius,
85-
" and divideEnabled >>>>>>> ",
86-
divideEnabled
87-
);
81+
8882
return new Promise((resolve, reject) => {
8983
try {
9084
var count = 0;
@@ -192,13 +186,18 @@ export class DashedProgress extends PureComponent {
192186
count = count + 1;
193187
}
194188

195-
bigCircle = dashed;
196-
this.setState({
197-
reaload: true
198-
});
199-
if (bigCircle.length <= 0) {
200-
resolve(false);
201-
}
189+
this.setState(
190+
{
191+
reaload: true,
192+
bigCircle: dashed
193+
},
194+
() => {
195+
if (this.state.bigCircle.length <= 0) {
196+
resolve(false);
197+
}
198+
}
199+
);
200+
202201
resolve(true);
203202
} catch (error) {
204203
console.log("catch error at getPathDirections >>>>> ", error);
@@ -210,7 +209,6 @@ export class DashedProgress extends PureComponent {
210209
increaseWeight() {
211210
try {
212211
const { strokeColor, fill, duration } = this.props;
213-
214212
//divide time interval for each dash
215213
var interval_time = 3;
216214

@@ -224,13 +222,15 @@ export class DashedProgress extends PureComponent {
224222
diff = parseInt(100 / interval_time);
225223
}
226224
//clear all remaining intervals
227-
clearInterval(increment_timer);
228-
clearInterval(decrement_timer);
225+
clearInterval(this.increment_timer);
226+
clearInterval(this.decrement_timer);
229227

230228
//intilize new increment interval
231-
increment_timer = setInterval(() => {
229+
this.increment_timer = setInterval(() => {
232230
try {
233231
if (i <= fill) {
232+
var bigCircle = this.state.bigCircle;
233+
var stopIndicator = this.state.stopIndicator;
234234
for (k = i; k <= i + diff; k++) {
235235
if (bigCircle.length > k) {
236236
if (bigCircle[k].stroke != strokeColor && k <= fill) {
@@ -246,11 +246,13 @@ export class DashedProgress extends PureComponent {
246246

247247
this.setState({
248248
last_stroke_index: k - 1,
249-
last_trail_index: k
249+
last_trail_index: k,
250+
bigCircle: bigCircle,
251+
stopIndicator: stopIndicator
250252
});
251253
i = k;
252254
} else {
253-
clearInterval(increment_timer);
255+
clearInterval(this.increment_timer);
254256
}
255257
} catch (err) {
256258
console.log(err);
@@ -285,13 +287,15 @@ export class DashedProgress extends PureComponent {
285287
}
286288

287289
//clear all remaining intervals
288-
clearInterval(decrement_timer);
289-
clearInterval(increment_timer);
290+
clearInterval(this.decrement_timer);
291+
clearInterval(this.increment_timer);
290292

291293
//intilize new decrement interval
292-
decrement_timer = setInterval(() => {
294+
this.decrement_timer = setInterval(() => {
293295
try {
294296
if (i >= last_length && i > 0) {
297+
var bigCircle = this.state.bigCircle;
298+
var stopIndicator = this.state.stopIndicator;
295299
for (k = i; k > i - diff; k--) {
296300
if (
297301
bigCircle[k].stroke != trailColor &&
@@ -307,12 +311,14 @@ export class DashedProgress extends PureComponent {
307311

308312
this.setState({
309313
last_trail_index: k + 1,
310-
last_stroke_index: k
314+
last_stroke_index: k,
315+
bigCircle: bigCircle,
316+
stopIndicator: stopIndicator
311317
});
312318

313319
i = k;
314320
} else {
315-
clearInterval(decrement_timer);
321+
clearInterval(this.decrement_timer);
316322
}
317323
} catch (err) {
318324
console.log(err);
@@ -367,7 +373,7 @@ export class DashedProgress extends PureComponent {
367373
style={{ backgroundColor: "transparent" }}
368374
>
369375
{/*display animated circle view*/}
370-
{bigCircle.map((item, index) => (
376+
{this.state.bigCircle.map((item, index) => (
371377
<Line
372378
key={`line_${index}`}
373379
x1={item.fromX}
@@ -398,10 +404,10 @@ export class DashedProgress extends PureComponent {
398404
{/* display stop indicator at fill */}
399405
{showIndicator && (
400406
<Line
401-
x1={stopIndicator.fromX}
402-
y1={stopIndicator.fromY}
403-
x2={stopIndicator.toX}
404-
y2={stopIndicator.toY}
407+
x1={this.state.stopIndicator.fromX}
408+
y1={this.state.stopIndicator.fromY}
409+
x2={this.state.stopIndicator.toX}
410+
y2={this.state.stopIndicator.toY}
405411
stroke={indicatorColor}
406412
strokeWidth={strokeThickness}
407413
strokeLinecap={strokeLinecap}

example/App.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {Component} from 'react';
2-
import {StyleSheet, View} from 'react-native';
2+
import {StyleSheet, View, Text} from 'react-native';
33

44
import {DashedProgress} from 'react-native-dashed-progress';
55

@@ -10,14 +10,14 @@ export default class App extends Component {
1010
<DashedProgress
1111
fill={20}
1212
countBars={50}
13-
radius={30}
14-
divideEnabled={true}
13+
radius={50}
1514
strokeThickness={2}
1615
/>
1716
<DashedProgress
18-
fill={20}
19-
countBars={50}
20-
radius={30}
17+
fill={this.state.fill}
18+
countBars={100}
19+
radius={50}
20+
divideEnabled={true}
2121
strokeColor="#00e0ff"
2222
/>
2323
</View>

0 commit comments

Comments
 (0)