forked from alexissan/ReactNativeWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlbumDetailScreen.js
150 lines (125 loc) · 3.43 KB
/
AlbumDetailScreen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
var React = require('react-native');
var FavoritesStore = require('./FavoritesStore');
var {
StyleSheet,
View,
ScrollView,
TouchableHighlight,
Text,
Image,
LinkingIOS
} = React;
function yearFromReleaseDate(releaseDate) {
return (new Date(releaseDate)).getFullYear();
}
function fullSizeUrl(thumbUrl) {
return thumbUrl.replace('.100x100-75', '');
}
var AlbumDetailScreen = React.createClass({
getInitialState() {
return this.computedState();
},
computedState() {
return {
isFavorite: FavoritesStore.isFavorite(this.props.album)
}
},
componentDidMount() {
// way of calculating relative size:
// https://github.com/facebook/react-native/issues/953
setTimeout(this.measureArtworkContainer);
FavoritesStore.on(FavoritesStore.CHANGE_EVENT, this.onFavoritesChange);
},
measureArtworkContainer() {
this.refs.artworkContainer.measure((ox, oy, width, height) => {
this.setState({artworkContainerHeight: width});
});
},
componentWillUnmount() {
FavoritesStore.removeListener(FavoritesStore.CHANGE_EVENT, this.onFavoritesChange);
},
onFavoritesChange() {
this.setState(this.computedState());
},
onShowMoreButtonPressed() {
LinkingIOS.openURL(this.props.album.collectionViewUrl);
},
onFavoriteButtonPressed() {
FavoritesStore.toggleFavorite(this.props.album);
},
render() {
var album = this.props.album;
var year = yearFromReleaseDate(album.releaseDate);
var favoriteButtonText = this.state.isFavorite ? 'Unfavorite' : 'Favorite';
var artworkContainerHeight = this.state.artworkContainerHeight;
return (
<ScrollView alwaysBounceVertical={false}>
<View style={{
flex: 1,
alignItems: 'stretch',
height: artworkContainerHeight}}
ref="artworkContainer">
<Image style={styles.artwork} source={{uri: fullSizeUrl(album.artworkUrl100)}} />
</View>
<View style={styles.bodyContainer}>
<Text style={styles.title}
numberOfLines={2}>{album.collectionName} ({year})</Text>
<Text style={styles.subtitle}
numberOfLines={1}>{album.artistName}</Text>
<TouchableHighlight
style={[styles.button, styles.favoriteButton]}
underlayColor='#AA6600'
onPress={this.onFavoriteButtonPressed} >
<Text style={styles.buttonText}>{favoriteButtonText}</Text>
</TouchableHighlight>
<TouchableHighlight
style={[styles.button, styles.showMoreButton]}
underlayColor='#FF0040'
onPress={this.onShowMoreButtonPressed} >
<Text style={styles.buttonText}>Show on iTunes</Text>
</TouchableHighlight>
</View>
</ScrollView>
);
}
});
var styles = StyleSheet.create({
bodyContainer: {
padding: 10
},
artwork: {
flex: 1
},
artworkContainer: {
flex: 1,
alignItems: 'stretch',
},
title: {
fontSize: 20,
color: '#FF8000',
fontWeight: 'bold'
},
subtitle: {
fontSize: 16,
color: '#656565'
},
button: {
justifyContent: 'center',
borderRadius: 8,
marginTop: 10,
height: 36,
},
favoriteButton: {
backgroundColor: '#FF9900',
},
showMoreButton: {
backgroundColor: '#FE2E64',
},
buttonText: {
fontSize: 18,
color: 'white',
alignSelf: 'center',
},
});
module.exports = AlbumDetailScreen;