This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
142 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "imagepicker", | ||
"private": true, | ||
"main": "view.js" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React, { | ||
Component, | ||
StyleSheet, | ||
TextInput, | ||
Text, | ||
View, | ||
Image, | ||
ScrollView, | ||
TouchableHighlight | ||
} from 'react-native'; | ||
import GridView from 'react-native-grid-view'; | ||
import Unsplash from '../unsplash'; | ||
|
||
var styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
}, | ||
gallery: { | ||
width: 200, | ||
height: 200, | ||
padding: 5, | ||
}, | ||
search: { | ||
elevation: 5, | ||
}, | ||
scroll: { | ||
backgroundColor: '#424242', | ||
}, | ||
credit: { | ||
color: '#ffffff', | ||
textShadowColor: '#222222', | ||
textShadowOffset: {height: 1, width: 0}, | ||
fontSize: 8 | ||
}, | ||
}); | ||
|
||
export default class ImagePicker extends Component { | ||
state = { | ||
query: '', | ||
images: [], | ||
end: false, | ||
page: 1, | ||
}; | ||
loading = false; | ||
|
||
render(){ | ||
return ( | ||
<View style={styles.container}> | ||
<TextInput | ||
style={styles.search} | ||
value={this.state.query} | ||
onChangeText={this.onQueryChange} | ||
onSubmitEditing={this.onSearch} | ||
placeholder="Search Unsplash. Use comma to separate terms" /> | ||
<GridView | ||
style={styles.scroll} | ||
items={this.state.images} | ||
itemsPerRow={3} | ||
renderItem={this.renderItem} | ||
onEndReached={this.state.end ? null : this.loadMore} | ||
/> | ||
</View> | ||
); | ||
} | ||
|
||
renderItem = (item) => { | ||
return ( | ||
<TouchableHighlight onPress={() => {this.onSelect(item)}} key={item.id} underlayColor={item.color}> | ||
<Image | ||
style={[styles.gallery, { | ||
backgroundColor: item.color | ||
}]} | ||
source={{uri: item.urls.small}}> | ||
<Text style={styles.credit}>{item.user.name}</Text> | ||
</Image> | ||
</TouchableHighlight> | ||
); | ||
}; | ||
|
||
onQueryChange = (val) => { | ||
this.setState({ | ||
query: val | ||
}); | ||
}; | ||
|
||
onSearch = () => { | ||
Unsplash.search(this.state.query).then((results) => { | ||
this.setState({ | ||
images: results, | ||
end: results.length < Unsplash.per_page, | ||
page: 1 | ||
}); | ||
}); | ||
}; | ||
|
||
loadMore = () => { | ||
if(this.state.end || this.loading){ | ||
return; | ||
} | ||
this.loading = true; | ||
|
||
Unsplash.search(this.state.query, this.state.page + 1).then((results) => { | ||
this.setState({ | ||
images: this.state.images.concat(results), | ||
end: results.length < Unsplash.per_page, | ||
page: this.state.page + 1 | ||
}); | ||
this.loading = false; | ||
}); | ||
}; | ||
|
||
onSelect(item){ | ||
console.log(item); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default class Unsplash{ | ||
static id = 'c4f04786a1a83f70796a2121c2021f427e8807e31fb5d8b6d32846c41a9b89a7'; | ||
static endpoint = 'https://api.unsplash.com/'; | ||
static per_page = 21; | ||
|
||
static search(query, page=1){ | ||
query = encodeURIComponent(query); | ||
return fetch(`${Unsplash.endpoint}photos/search?client_id=${Unsplash.id}&page=${page}&per_page=${Unsplash.per_page}&query=${query}`) | ||
.then((res) => res.json()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters