Skip to content

Instantly share code, notes, and snippets.

@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Timers trigger more timers.
takePicture = () => {
this.camera.takePictureAsync({
quality: 0.1,
base64: true,
exif: false
}).then(photo => {
this.setState({ photo });
// In 27 seconds, turn the camera back on
setTimeout(() => {
@dceddia
dceddia / server.js
Created June 15, 2018 02:03
Handle GET requests
// View latest image
app.get('/', (req, res) => {
// Does this session have an image yet?
if(!latestPhoto) {
return res.status(404).send("Nothing here yet");
}
console.log('sending photo');
try {
@dceddia
dceddia / server.js
Created June 15, 2018 02:03
Bare bones Express server
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// If your phone has a modern camera (unlike my iPhone 4S)
// you might wanna make this bigger.
app.use(bodyParser.json({ limit: '10mb' }));
// TODO: handle requests
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Upload pictures to the server
uploadPicture = () => {
return fetch(SERVER_URL, {
body: JSON.stringify({
image: this.state.photo.base64
}),
headers: {
'content-type': 'application/json'
},
method: 'POST'
})
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Adding a button to take the picture
render() {
const { photo } = this.state;
return (
<Camera
style={{ flex: 1 }}
type={Camera.Constants.Type.back}
ref={cam => this.camera = cam}>
<TouchableOpacity
style={{ flex: 1 }}
@dceddia
dceddia / server.js
Created June 15, 2018 02:03
Handle POST requests
// Store the single image in memory.
let latestPhoto = null;
// Upload the latest photo for this session
app.post('/', (req, res) => {
// Very light error handling
if(!req.body) return res.sendStatus(400);
console.log('got photo')
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Take a picture after 30 seconds.
const PHOTO_INTERVAL = 30000;
const FOCUS_TIME = 3000;
class Autoshoot extends React.Component {
componentDidMount() {
this.countdown = setTimeout(
this.takePicture,
PHOTO_INTERVAL
);
}
@dceddia
dceddia / App.js
Created June 15, 2018 02:03
Asking for permission to use the camera.
export default class App extends React.Component {
...
componentDidMount() {
Permissions.askAsync(Permissions.CAMERA)
.then(({ status }) =>
this.setState({
cameraPermission: status === 'granted'
})
);
@dceddia
dceddia / new-post.sh
Created February 1, 2019 03:30
Create a new post based in Gatsby. (make sure to create a new-post-template.md file)
#!/bin/bash
# Print usage if args are missing
if [ -z $1 ]; then
echo "Usage: new-post <slug>"
exit
fi
SLUG=$1
DAY=$(date +%Y-%m-%d)
@dceddia
dceddia / javascript.json
Created October 3, 2019 20:03
VSCode Snippets for React
{
"Insert a function": {
"prefix": "f",
"body": [
"function $1($2) {\n $0\n}\n"
],
"description": "Insert a function"
},
"const arrow": {
"prefix": "c>",