Skip to content

Commit cb195c8

Browse files
author
Madelyn Kasula
authored
Merge pull request #234 from code-dot-org/recall-button
Add recall fish
2 parents 2bdff93 + 0983a3a commit cb195c8

File tree

5 files changed

+88
-20
lines changed

5 files changed

+88
-20
lines changed

src/demo/colors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const colors = {
22
white: 'white',
33
black: 'black',
4-
grey: 'grey',
4+
grey: '#4d575f',
55
darkGrey: '#2f353e',
66
lightGrey: '#e7e7e7',
77
green: '#56c568',

src/demo/models/pond.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,30 @@ import constants, {ClassType} from '../constants';
66
export const init = async () => {
77
const state = getState();
88
let fishWithConfidence = await predictAllFish(state);
9-
setState({totalPondFish: fishWithConfidence.length});
109
fishWithConfidence = _.sortBy(fishWithConfidence, ['confidence']);
11-
const pondFishWithConfidence = fishWithConfidence.splice(
10+
const fishByClassType = _.groupBy(
11+
fishWithConfidence,
12+
fish => fish.getResult().predictedClassId
13+
);
14+
15+
let pondFish = fishByClassType[ClassType.Like];
16+
setState({totalPondFish: pondFish.length});
17+
pondFish = pondFish.splice(0, constants.maxPondFish);
18+
const recallFish = fishByClassType[ClassType.Dislike].splice(
1219
0,
1320
constants.maxPondFish
1421
);
15-
arrangeFish(pondFishWithConfidence);
16-
setState({pondFish: pondFishWithConfidence});
22+
arrangeFish(pondFish);
23+
setState({pondFish, recallFish});
1724
};
1825

1926
const predictAllFish = state => {
2027
return new Promise(resolve => {
2128
let fishWithConfidence = [];
2229
state.fishData.map((fish, index) => {
2330
state.trainer.predict(fish).then(res => {
24-
if (res.predictedClassId === ClassType.Like) {
25-
fish.setResult(res);
26-
fishWithConfidence.push(fish);
27-
}
31+
fish.setResult(res);
32+
fishWithConfidence.push(fish);
2833

2934
if (index === state.fishData.length - 1) {
3035
resolve(fishWithConfidence);
@@ -34,7 +39,7 @@ const predictAllFish = state => {
3439
});
3540
};
3641

37-
const arrangeFish = fishes => {
42+
export const arrangeFish = fishes => {
3843
let fishPositions = formatArrangement();
3944

4045
fishes.forEach(fish => {

src/demo/renderer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,12 @@ const drawPredictBot = state => {
495495

496496
// Draw the fish for pond mode.
497497
const drawPondFishImages = () => {
498-
const canvas = getState().canvas;
499-
const ctx = canvas.getContext('2d');
500-
498+
const state = getState();
499+
const ctx = state.canvas.getContext('2d');
500+
const fishes = state.showRecallFish ? state.recallFish : state.pondFish;
501501
const fishBounds = [];
502502

503-
getState().pondFish.forEach(fish => {
503+
fishes.forEach(fish => {
504504
const pondClickedFish = getState().pondClickedFish;
505505
const pondClickedFishUs = pondClickedFish && fish.id === pondClickedFish.id;
506506

src/demo/state.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const initialState = {
55
currentMode: null,
66
fishData: [],
77
pondFish: [],
8+
recallFish: [],
9+
showRecallFish: false,
810
totalPondFish: null,
911
backgroundCanvas: null,
1012
canvas: null,

src/demo/ui.jsx

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import constants, {AppMode, Modes} from './constants';
77
import {toMode} from './toMode';
88
import {$time, currentRunTime, finishMovement, resetTraining} from './helpers';
99
import {onClassifyFish} from './models/train';
10+
import {arrangeFish} from './models/pond';
1011
import colors from './colors';
1112
import aiBotClosed from '../../public/images/ai-bot/ai-bot-closed.png';
1213
import counterIcon from '../../public/images/data.png';
@@ -22,7 +23,9 @@ import {
2223
faPause,
2324
faBackward,
2425
faForward,
25-
faEraser
26+
faEraser,
27+
faCheck,
28+
faBan
2629
} from '@fortawesome/free-solid-svg-icons';
2730

2831
const styles = {
@@ -42,6 +45,7 @@ const styles = {
4245
button: {
4346
cursor: 'pointer',
4447
backgroundColor: colors.white,
48+
color: colors.grey,
4549
borderRadius: 8,
4650
minWidth: 160,
4751
outline: 'none',
@@ -295,6 +299,30 @@ const styles = {
295299
transform: 'translateX(-45%)',
296300
pointerEvents: 'none'
297301
},
302+
recallContainer: {
303+
position: 'absolute',
304+
top: '4%',
305+
right: '2.25%',
306+
color: colors.white,
307+
display: 'flex',
308+
alignItems: 'center',
309+
justifyContent: 'space-between'
310+
},
311+
recallIcon: {
312+
width: 30,
313+
height: 30,
314+
border: `5px solid ${colors.white}`,
315+
borderRadius: 50,
316+
padding: 6,
317+
marginLeft: 8,
318+
backgroundColor: colors.lightGrey
319+
},
320+
bgRed: {
321+
backgroundColor: colors.red
322+
},
323+
bgGreen: {
324+
backgroundColor: colors.green
325+
},
298326
pill: {
299327
display: 'flex',
300328
alignItems: 'center'
@@ -855,12 +883,25 @@ let Predict = class Predict extends React.Component {
855883
};
856884
Predict = Radium(Predict);
857885

858-
class Pond extends React.Component {
886+
let Pond = class Pond extends React.Component {
859887
constructor(props) {
860888
super(props);
861889
}
862890

863-
onPondClick(e) {
891+
toggleRecall = () => {
892+
const state = getState();
893+
const showRecallFish = !state.showRecallFish;
894+
const fish = showRecallFish ? state.recallFish : state.pondFish;
895+
896+
// Don't call arrangeFish if fish have already been arranged.
897+
if (fish.length > 0 && !fish[0].getXY()) {
898+
arrangeFish(fish);
899+
}
900+
901+
setState({showRecallFish});
902+
};
903+
904+
onPondClick = e => {
864905
// Don't allow pond clicks if a Guide is currently showing.
865906
if (getCurrentGuide()) {
866907
return;
@@ -920,13 +961,32 @@ class Pond extends React.Component {
920961
playSound('no');
921962
}
922963
}
923-
}
964+
};
924965

925966
render() {
926967
const state = getState();
927968

928969
return (
929-
<Body onClick={e => this.onPondClick(e)}>
970+
<Body onClick={this.onPondClick}>
971+
<div style={styles.recallContainer}>
972+
Show
973+
<FontAwesomeIcon
974+
icon={faCheck}
975+
style={{
976+
...styles.recallIcon,
977+
...(!state.showRecallFish ? styles.bgGreen : {})
978+
}}
979+
onClick={this.toggleRecall}
980+
/>
981+
<FontAwesomeIcon
982+
icon={faBan}
983+
style={{
984+
...styles.recallIcon,
985+
...(state.showRecallFish ? styles.bgRed : {})
986+
}}
987+
onClick={this.toggleRecall}
988+
/>
989+
</div>
930990
<img style={styles.pondBot} src={aiBotClosed} />
931991
{state.canSkipPond && (
932992
<div>
@@ -971,7 +1031,8 @@ class Pond extends React.Component {
9711031
</Body>
9721032
);
9731033
}
974-
}
1034+
};
1035+
Pond = Radium(Pond);
9751036

9761037
class Guide extends React.Component {
9771038
onShowing() {

0 commit comments

Comments
 (0)