|
@@ -0,0 +1,58 @@
|
|
|
|
+import logo from './logo.svg';
|
|
|
|
+import React, { useState, useEffect } from 'react';
|
|
|
|
+import './App.scss';
|
|
|
|
+import { getTimeData, getTotalSeconds } from './utills';
|
|
|
|
+import './WatchFace';
|
|
|
|
+import { WatchFace } from './WatchFace';
|
|
|
|
+import { WatchFaceController } from './WatchFaceController';
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+const TimerContainer = ({ count, render, renderController, refresh = 100, running = false }) => {
|
|
|
|
+ let [state, setState] = useState({ paused: !running, count, ...getTimeData(count) });
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ let timer = undefined;
|
|
|
|
+ if (!state.paused) {
|
|
|
|
+ if (state.count > 0) {
|
|
|
|
+ const now = new Date();
|
|
|
|
+ const nextTime = Math.max(1, 1000 - (now.getTime() % refresh));
|
|
|
|
+ timer = setInterval(() => setStateInt({ count: state.count - 1 }), nextTime);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ pause();
|
|
|
|
+ }
|
|
|
|
+ return () => clearInterval(timer);
|
|
|
|
+ }, [state]);
|
|
|
|
+ const setStateInt = newState => {
|
|
|
|
+ setState(state = { ...state, ...newState });
|
|
|
|
+ }
|
|
|
|
+ const start = () => setStateInt({ paused: false });
|
|
|
|
+ const pause = () => setStateInt({ paused: true });
|
|
|
|
+ const reset = (totalSeconds = undefined) => setStateInt({ count: totalSeconds ?? count });
|
|
|
|
+ const stop = (totalSeconds = undefined) => {
|
|
|
|
+ pause();
|
|
|
|
+ reset(totalSeconds);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let Render = render;
|
|
|
|
+ let RenderController = renderController;
|
|
|
|
+ return (
|
|
|
|
+ <>
|
|
|
|
+ <Render timer={{ totalSeconds: state.count }} />
|
|
|
|
+ <br />
|
|
|
|
+ <RenderController timerController={{ start, pause, reset, stop, count }} /> {/*start count */}
|
|
|
|
+ </>
|
|
|
|
+ );
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function AppWatchController() {
|
|
|
|
+
|
|
|
|
+ return (
|
|
|
|
+ <>
|
|
|
|
+ <div className="App">
|
|
|
|
+ <TimerContainer count={10} render={WatchFace} renderController={WatchFaceController} />
|
|
|
|
+ </div>
|
|
|
|
+ </>
|
|
|
|
+ );
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+export default AppWatchController;
|