Skip to content

Commit

Permalink
Spinner implementation (start async render algorithm, ref #123)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphamorim committed Jun 21, 2022
1 parent e327de6 commit 802a2da
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 37 deletions.
12 changes: 0 additions & 12 deletions packages/app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,13 @@ import {
withNavigation,
} from '../../react-ape/reactApeEntry';

// import Spinner from './Spinner';
import Sidebar from './Sidebar';
import Grid from './Grid';
import Clock from './Clock';
import Slideshow from './Slideshow';

const {width, height} = Dimensions.get('window');

// Register Custom Components
/*
<custom.Spinner
degrees={degrees}
style={{ top: height / 4 + 8, left: width / 2 - 60, color: 'white' }}
/>
*/
// const custom = {
// Spinner: registerComponent('Spinner', Spinner),
// };

const styles = StyleSheet.create({
surface: {
backgroundColor: '#202020',
Expand Down
10 changes: 10 additions & 0 deletions packages/app/src/Grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
StyleSheet,
} from '../../react-ape/reactApeEntry';

import Loader from './Loader';

const {height} = Dimensions.get('screen');

const styles = StyleSheet.create({
Expand Down Expand Up @@ -42,9 +44,17 @@ const list = [
class Grid extends Component {
constructor(props) {
super(props);
this.state = {
loading: true,
};
}

render() {
const {loading} = this.state;
if (loading) {
return <Loader />;
}

return (
<View style={styles.grid}>
<Text style={styles.title}>Rio de Janeiro</Text>
Expand Down
5 changes: 2 additions & 3 deletions packages/app/src/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const styles = StyleSheet.create({
position: 'absolute',
left: width / 2 - 40,
top: height / 4,
backgroundColor: 'blue',
backgroundColor: '#202020',
},
});

Expand Down Expand Up @@ -52,9 +52,8 @@ class Loader extends Component {
<View style={styles.container}>
<custom.Spinner
degrees={degrees}
style={{top: height / 4 + 8, left: width / 2 - 60, color: 'white'}}
style={{top: 550, left: 740, color: 'orange'}}
/>
<Text style={{color: 'white'}}>Loading...</Text>
</View>
);
}
Expand Down
27 changes: 8 additions & 19 deletions packages/app/src/Spinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ class Spinner {
console.log(error, errorInfo);
}

reset(prevProps, parentStyle, ape) {
const {ctx} = ape;
// parentStyle.backgroundColor // white
if (ctx) {
ctx.clearRect(0, 0, 18, 18);
}
}
// You can also use your own clear function although isn't recommended
// unsafeClear(prevProps, parentStyle, ape) {
// const {ctx} = ape;
// const parentBackgroundColor = parentStyle.style.backgroundColor;
// const newProps = {...prevProps, style: { color: parentBackgroundColor } };
// this.render(newProps, ape);
// }

render(props, ape) {
const {ctx} = ape;
Expand All @@ -18,24 +18,13 @@ class Spinner {

const offset = 8;
ctx.save();
// ctx.translate(offset, offset);
ctx.translate(style.left, style.top);
ctx.rotate(degrees);

// Draw half open circle
ctx.beginPath();
ctx.lineWidth = 3;
ctx.lineWidth = 50;
ctx.arc(8 - offset, 8 - offset, 6, 0, 1.75 * Math.PI);
ctx.strokeStyle = color;
ctx.stroke();

// Draw arrowhead
ctx.lineWidth = 3;
ctx.moveTo(13 - offset, 1 - offset);
ctx.lineTo(9 - offset, 5 - offset);
ctx.lineTo(13 - offset, 5 - offset);
ctx.lineTo(13 - offset, 1 - offset);
ctx.stroke();
ctx.restore();
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/react-ape/modules/Register/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ function registerComponent(componentName, Component) {
isResetPhase: true,
};

console.log('clearRender');
Component.render(clearProps, apeContext, parentLayout);
};

return {
type: componentName,
render: Component.render.bind(this, props),
clear: Component.reset || clearRender,
clear: Component.unsafeClear || clearRender,
};
};

Expand Down
12 changes: 10 additions & 2 deletions packages/react-ape/renderer/core/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ function renderApeQueue(
onFinish: () => mixed
) {
if (apeContextGlobal && apeContextGlobal.renderQueue.length) {
// TODO: Move to request animation frame
// const renderFrame = () => {
// requestAnimationFrame(renderFrame);
// apeContextGlobal.renderQueue.forEach(element => {
// // element.render(apeContextGlobal, element.parentLayout)
// renderApeElement(apeContextGlobal, element);
// });
// cancelAnimationFrame(renderFrame);
// }
// renderFrame();

apeContextGlobal.renderQueue.forEach(element => {
renderApeElement(apeContextGlobal, element);
});

onFinish();
}
}
Expand Down

0 comments on commit 802a2da

Please sign in to comment.