Skip to content

Commit

Permalink
Project completed...
Browse files Browse the repository at this point in the history
  • Loading branch information
asantos committed May 21, 2015
1 parent d97963a commit f2f4806
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
23 changes: 19 additions & 4 deletions AlbumDetailScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,22 @@ function fullSizeUrl(thumbUrl) {
}

var AlbumDetailScreen = React.createClass({

getInitialState() {
return {};
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() {
Expand All @@ -39,18 +46,26 @@ var AlbumDetailScreen = React.createClass({
});
},

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 = 'Favorite';
var favoriteButtonText = this.state.isFavorite ? 'Unfavorite' : 'Favorite';
var artworkContainerHeight = this.state.artworkContainerHeight;

return (
Expand Down
4 changes: 3 additions & 1 deletion FavoritesListScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ var FavoritesListScreen = React.createClass({
},

componentDidMount() {
FavoritesStore.on(FavoritesStore.CHANGE_EVENT, this.onFavoritesChange);

FavoritesStore.loadPersistedFavorites();
},

componentWillUnmount() {

FavoritesStore.removeListener(FavoritesStore.CHANGE_EVENT, this.onFavoritesChange);
},

onFavoritesChange() {
Expand Down
41 changes: 38 additions & 3 deletions FavoritesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,59 @@ var {
AsyncStorage
} = React;

// You will use it soon
var STORAGE_KEY = 'iTunesCatalog:Favorites';

var _favorites = {};

var FavoritesStore = assign({}, EventEmitter.prototype, {

CHANGE_EVENT: 'change',

loadPersistedFavorites() {
AsyncStorage.getItem(STORAGE_KEY)
.then((value) => {
if (value !== null) {
_favorites = JSON.parse(value);

this.emit(this.CHANGE_EVENT);
}
})
.done();
},

getAll() {
return [];
return Object.keys(_favorites).map(key => _favorites[key]);
},

isFavorite(album) {

return _favorites[album.collectionId] !== undefined;
},

toggleFavorite(album) {
if (this.isFavorite(album)) {
this.unfavorite(album);
} else {
this.favorite(album);
}
},

favorite(album) {
_favorites[album.collectionId] = album;

this.saveChanges();
},

unfavorite(album) {
delete _favorites[album.collectionId];

this.saveChanges();
},

saveChanges() {
AsyncStorage.setItem(STORAGE_KEY, JSON.stringify(_favorites)).done();

this.emit(this.CHANGE_EVENT);
}
});

module.exports = FavoritesStore;

0 comments on commit f2f4806

Please sign in to comment.