307 lines
8.6 KiB
JavaScript
Raw Normal View History

2016-05-31 14:46:49 +03:00
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
ListView,
2016-06-02 14:08:06 +03:00
TouchableOpacity,
Image,
2016-06-07 18:42:59 +03:00
AlertIOS
2016-05-31 14:46:49 +03:00
} from 'react-native';
2016-06-02 14:08:06 +03:00
import _ from 'lodash';
import Immutable from 'seamless-immutable';
import {
CameraKitGallery,
CameraKitCamera,
} from 'react-native-camera-kit';
const FLASH_MODE_AUTO = "auto";
const FLASH_MODE_ON = "on";
const FLASH_MODE_OFF = "off";
2016-05-31 14:46:49 +03:00
class example extends Component {
constructor(props) {
super(props);
2016-06-02 14:08:06 +03:00
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
2016-05-31 14:46:49 +03:00
this.state = {
2016-06-07 18:42:59 +03:00
albums:{},
2016-06-02 14:08:06 +03:00
albumsDS: ds,
2016-05-31 14:46:49 +03:00
shouldOpenCamera: false,
2016-06-02 14:08:06 +03:00
shouldShowListView: false,
image:{imageURI:""},
flashMode:FLASH_MODE_AUTO
2016-05-31 14:46:49 +03:00
}
}
render() {
2016-06-07 18:42:59 +03:00
if (this.state.shouldOpenCamera) {
return (
this._renderCameraView()
)
}
2016-05-31 14:46:49 +03:00
return (
<View style={styles.container}>
2016-06-07 18:42:59 +03:00
<TouchableOpacity style={styles.apiButton} onPress={this.onGetAlbumsPressed.bind(this)}>
2016-05-31 14:46:49 +03:00
<Text style={styles.button}>get albums</Text>
</TouchableOpacity>
2016-06-07 18:42:59 +03:00
<TouchableOpacity style={styles.apiButton} onPress={this.onOpenCameraPressed.bind(this)}>
2016-05-31 14:46:49 +03:00
<Text style={styles.button}>{this.state.shouldOpenCamera ? "close camera" : "open camera"}</Text>
</TouchableOpacity>
2016-06-07 18:42:59 +03:00
<TouchableOpacity style={styles.apiButton} onPress={this.onCheckAuthoPressed.bind(this)}>
<Text style={styles.button}>check device authorizarion status </Text>
</TouchableOpacity>
2016-06-02 14:08:06 +03:00
{this._renderListView()}
2016-06-07 18:42:59 +03:00
{}
2016-05-31 14:46:49 +03:00
</View>
);
}
2016-06-02 14:08:06 +03:00
_renderListView() {
if (this.state.shouldShowListView) {
return(
<ListView
style={styles.listView}
dataSource={this.state.albumsDS}
renderRow={(rowData) =>
this._renderRow(rowData)
}
/>
)
}
}
2016-05-31 14:46:49 +03:00
_renderRow(rowData) {
2016-06-07 18:42:59 +03:00
console.log('rannnn', rowData.image);
const base64Image = 'data:image/png;base64,' + rowData.image;
2016-05-31 14:46:49 +03:00
return (
2016-06-07 18:42:59 +03:00
<View style={{flex:1, backgroundColor: 'green'}}>
2016-06-02 14:08:06 +03:00
<Image
2016-06-07 18:42:59 +03:00
style={{width: 60, height: 60, backgroundColor: 'white'}}
source={{uri: base64Image, scale: 3}}
2016-06-02 14:08:06 +03:00
/>
2016-06-07 18:42:59 +03:00
<TouchableOpacity style={{marginTop: 10}} onPress={this.onAlbumNamePressed.bind(this, rowData)}>
<Text style={{fontSize: 18}}>{rowData.name}</Text>
2016-06-02 14:08:06 +03:00
</TouchableOpacity>
</View>
2016-05-31 14:46:49 +03:00
)
}
async onGetAlbumsPressed() {
2016-06-07 18:42:59 +03:00
let albums = await CameraKitGallery.getAlbums();
albums = albums.albums;
console.log('albums',albums);
//if (!albums) return;
//const albumsNames = _.map(albums, 'albumName');
//const albumsThumbnails = _.map(albums, 'albumName');
const kk = this.state.albumsDS.cloneWithRows(albums);
//console.log('kkkkkkkkkkk', kk);
this.setState({...this.state, albumsDS:this.state.albumsDS.cloneWithRows(albums), albums:{albums}, shouldShowListView: true});
2016-06-02 14:08:06 +03:00
}
2016-06-07 18:42:59 +03:00
async onAlbumNamePressed(album) {
let base64Image = await CameraKitGallery.getThumbnailForAlbumName(album.name);
2016-06-02 14:08:06 +03:00
base64Image = 'data:image/png;base64,' + base64Image;
2016-06-07 18:42:59 +03:00
album.image = base64Image;
let albums = {};
_.merge(albums, this.state.albums);
albums = albums.albums;
console.log('before', albums);
const key = album.name;
albums.key = album;
//
//console.log('after', _.toArray(albums));
//
////console.log('llll', albums);
//
this.setState({...this.state, albumsDS:this.state.albumsDS.cloneWithRows(albums), albums:{albums}, shouldShowListView: true});
//let album = _.find(newAlbums, function(o) {
// return o === albumName;
//});
//
//console.log('newAlbums', newAlbums);
//console.log('album', album);
//const albumIndex = _.indexOf(newAlbums, album);
//if (albumIndex < 0) {
// console.error('ERROR: albumIndex is' + albumIndex);
// return;
//}
//
//let newArray = _.remove(newAlbums, function(o) {
// return o === album;
//});
//let albumWithImage = {...album, image:base64Image };
//console.log('a', album);
//newAlbums[albumIndex] = album;
//console.error(album);
//
//let albums = this.state.albums;
//albums = albums.albums;
//console.log('before', albums);
////const key = album.name;
////albums.key = album;
////
////console.log('after', _.toArray(albums));
////
//////console.log('llll', albums);
////
//this.setState({...this.state, albumsDS:this.state.albumsDS.cloneWithRows(albums), albums:{albums}, shouldShowListView: true});
2016-06-02 14:08:06 +03:00
2016-05-31 14:46:49 +03:00
}
_renderCameraView() {
return (
2016-06-07 18:42:59 +03:00
<View style={{ flex:1, backgroundColor: 'gray', marginBottom:8}}>
2016-06-02 14:08:06 +03:00
2016-06-07 18:42:59 +03:00
<View style={{flex: 1, flexDirection:'column', backgroundColor:'black'}} onPress={this.onTakeIt.bind(this)}>
2016-06-02 14:08:06 +03:00
<CameraKitCamera
ref={(cam) => {
this.camera = cam;
}}
style={{flex: 1}}
2016-06-02 16:39:01 +03:00
cameraOptions= {{
2016-06-07 18:42:59 +03:00
flashMode: 'auto', // on/off/auto(default)
focusMode: 'on', // off/on(default)
zoomMode: 'on' // off/on(default)
2016-06-02 16:39:01 +03:00
}}
2016-06-02 14:08:06 +03:00
/>
2016-06-07 18:42:59 +03:00
<TouchableOpacity style={{alignSelf:'center', marginHorizontal: 4}} onPress={this.onTakeIt.bind(this)}>
<Text style={{fontSize: 22, color: 'lightgray', backgroundColor: 'hotpink'}}>TAKE IT!</Text>
</TouchableOpacity>
</View>
2016-06-02 14:08:06 +03:00
<View style={{flexDirection: 'row'}}>
<Image
2016-06-07 18:42:59 +03:00
style={{ flexDirection:'row', backgroundColor: 'black', width: 100, height: 100}}
2016-06-02 14:08:06 +03:00
source={{uri: this.state.image.imageURI, scale: 3}}
/>
2016-06-07 18:42:59 +03:00
2016-06-02 14:08:06 +03:00
<TouchableOpacity style={{alignSelf:'center', marginHorizontal: 4}} onPress={this.onSwitchCameraPressed.bind(this)}>
<Text>switch camera</Text>
</TouchableOpacity>
<View style={{ flexDirection:'column', justifyContent: 'space-between'}}>
<TouchableOpacity style={{ marginHorizontal: 4}} onPress={this.onSetFlash.bind(this, FLASH_MODE_AUTO)}>
<Text>flash auto</Text>
</TouchableOpacity>
<TouchableOpacity style={{ marginHorizontal: 4, }} onPress={this.onSetFlash.bind(this, FLASH_MODE_ON)}>
<Text>flash on</Text>
</TouchableOpacity>
2016-06-07 18:42:59 +03:00
2016-06-02 14:08:06 +03:00
<TouchableOpacity style={{ marginHorizontal: 4,}} onPress={this.onSetFlash.bind(this, FLASH_MODE_OFF)}>
<Text>flash off</Text>
</TouchableOpacity>
</View>
</View>
2016-06-07 18:42:59 +03:00
<TouchableOpacity style={{position: 'absolute', width:25, height: 100,top:20, left:10, backgroundColor: 'transparent'}} onPress={this.onOpenCameraPressed.bind(this)}>
<Text style={{fontWeight:'200', fontSize: 40, color:'lightgray'}}>X</Text>
</TouchableOpacity>
2016-06-02 14:08:06 +03:00
</View>
2016-05-31 14:46:49 +03:00
)
}
2016-06-02 14:08:06 +03:00
async onSwitchCameraPressed() {
const success = await this.camera.changeCamera();
}
2016-06-07 18:42:59 +03:00
async onCheckAuthoPressed() {
const success = await CameraKitCamera.checkDeviceAuthorizarionStatus();
if (success){
AlertIOS.alert('You rock!')
}
else {
AlertIOS.alert('You fucked!')
}
}
2016-06-02 14:08:06 +03:00
async onSetFlash(flashMode) {
const success = await this.camera.setFleshMode(flashMode);
}
async onTakeIt() {
2016-06-07 18:42:59 +03:00
const imageURI = await this.camera.capture(true);
2016-06-02 14:08:06 +03:00
let newImage = {imageURI: imageURI};
this.setState({...this.state, image:newImage});
}
2016-05-31 14:46:49 +03:00
onOpenCameraPressed() {
this.setState({shouldOpenCamera:!this.state.shouldOpenCamera});
}
2016-06-02 14:08:06 +03:00
2016-05-31 14:46:49 +03:00
}
const styles = StyleSheet.create({
container: {
flex: 1,
2016-06-02 14:08:06 +03:00
//justifyContent: 'center',
//alignItems: 'center',
2016-05-31 14:46:49 +03:00
backgroundColor: '#F5FCFF',
marginTop: 20
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
row: {
flexDirection: 'row',
justifyContent: 'center',
padding: 10,
backgroundColor: '#F6F6F6',
},
text: {
flex: 1,
},
button: {
2016-06-07 18:42:59 +03:00
fontSize: 22,
2016-06-02 14:08:06 +03:00
alignSelf: 'center',
2016-06-07 18:42:59 +03:00
backgroundColor: 'transparent'
2016-05-31 14:46:49 +03:00
},
listView: {
2016-06-02 14:08:06 +03:00
//flex:1,
//flexDirection:'column',
2016-05-31 14:46:49 +03:00
margin: 8,
backgroundColor: '#D6DAC2',
2016-06-02 14:08:06 +03:00
//alignSelf: 'stretch'
2016-05-31 14:46:49 +03:00
},
2016-06-07 18:42:59 +03:00
apiButton:{
marginTop: 20,
backgroundColor: 'gray',
padding: 10
}
2016-05-31 14:46:49 +03:00
});
AppRegistry.registerComponent('example', () => example);