Skip to content

Commit

Permalink
feat: Added bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed Oct 13, 2018
1 parent e4a3cfa commit 9a4f54f
Show file tree
Hide file tree
Showing 16 changed files with 247 additions and 54 deletions.
31 changes: 31 additions & 0 deletions app/modules/BookmarkAdapter/BookmarkAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import DefaultBookmarkAdapter from 'popcorn-sdk/BookmarkAdapter'

import Bookmarks from '../db/Bookmarks'

export default new (class BookmarkAdapter extends DefaultBookmarkAdapter {

getAll = Bookmarks.getAll

checkMovie = movie => new Promise(async(resolve) => {
const bookmarks = await Bookmarks.getAllMovies()

resolve({
...movie,
bookmarked: bookmarks.filter(bookmark => bookmark.id === movie.id).length === 1,
})
})

checkShow = show => new Promise(async(resolve) => {
const bookmarks = await Bookmarks.getAllShows()

resolve({
...show,
bookmarked: bookmarks.filter(bookmark => bookmark.id === show.id).length === 1,
})
})

addItem = Bookmarks.addItem

removeItem = Bookmarks.removeItem

})()
1 change: 1 addition & 0 deletions app/modules/BookmarkAdapter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './BookmarkAdapter'
8 changes: 8 additions & 0 deletions app/modules/PopcornSDK.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Popcorn from 'popcorn-sdk'

import BookmarkAdapter from './BookmarkAdapter'

const SDK = Popcorn
SDK.setBookmarkAdapter(BookmarkAdapter)

export default SDK
55 changes: 55 additions & 0 deletions app/modules/db/Bookmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Constants } from 'popcorn-sdk'
import Base from './Base'

export default new (class BookmarksDB extends Base {

KEY_BOOKMARKS = '@Popcorn:Bookmarks'

bookmarks = []

getAll = async() => {
if (this.bookmarks.length > 0) {
return this.bookmarks
}

const bookmarks = await this.getItem(this.KEY_BOOKMARKS)

if (bookmarks) {
this.bookmarks = JSON.parse(bookmarks)
console.log('this.bookmarks', this.bookmarks)
return this.bookmarks
}

return this.bookmarks
}

getAllMovies = async() => {
await this.getAll()

return this.bookmarks.filter(bookmark => bookmark.type === Constants.TYPE_MOVIE)
}

getAllShows = async() => {
await this.getAll()

return this.bookmarks.filter(bookmark => bookmark.type === Constants.TYPE_SHOW)
}

addItem = ({ id, title, type, images }) => {
this.bookmarks.push({
id,
title,
type,
images,
})

this.setItem(this.KEY_BOOKMARKS, JSON.stringify(this.bookmarks))
}

removeItem = ({ id }) => {
this.bookmarks = this.bookmarks.filter(bookmark => bookmark.id !== id)

this.setItem(this.KEY_BOOKMARKS, JSON.stringify(this.bookmarks))
}

})()
1 change: 1 addition & 0 deletions app/modules/i18n/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"Home": "Home",
"My List": "My List",
"Movies": "Movies",
"Shows": "Shows",
"Season {{number}}": "Season {{number}}",
Expand Down
1 change: 1 addition & 0 deletions app/modules/i18n/translations/nl.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"Home": "Home",
"My List": "Mijn Lijst",
"Movies": "Films",
"Shows": "Series",
"Season {{number}}": "Seizoen {{number}}",
Expand Down
13 changes: 12 additions & 1 deletion app/screens/Home/HomeActions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Popcorn, { Constants } from 'popcorn-sdk'
import { Constants } from 'popcorn-sdk'
import Popcorn from 'modules/PopcornSDK'

import * as HomeConstants from './HomeConstants'
import * as HomeSelectors from './HomeSelectors'
Expand Down Expand Up @@ -57,6 +58,16 @@ export const getItems = (mode, page = 1, givenFilters = {}) => (dispatch, getSta

return Popcorn.getShows(page, filters).then(shows => dispatch(fetchedItems(shows, mode))).catch(catchNoCon)

case Constants.TYPE_BOOKMARK:
return Popcorn.bookmarks.getAll().then(bookmarks => dispatch(fetchedItems(bookmarks, mode)))

case 'bookmarkSearch':
return Popcorn.bookmarks.getAll().then(bookmarks => dispatch(
fetchedItems(
bookmarks.filter(bookmark => bookmark.title.toLowerCase().indexOf(filters.keywords.toLowerCase()) > -1),
mode,
),
))

default:
return null
Expand Down
13 changes: 8 additions & 5 deletions app/screens/Home/HomeConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ export const INITIAL_STATE = {
isLoading : true,
hasInternet: true,
modes : {
bookmark : { page: 1, items: [], filters: {} },
movie : { page: 1, items: [], filters: { limit: 50, sort: 'trending' } },
movieSearch: { page: 1, items: [], filters: {} },
show : { page: 1, items: [], filters: { limit: 50, sort: 'trending' } },
showSearch : { page: 1, items: [], filters: {} },
bookmark : { items: [] },
bookmarkSearch: { items: [] },

movie : { page: 1, items: [], filters: { limit: 50, sort: 'trending' } },
movieSearch : { page: 1, items: [], filters: {} },

show : { page: 1, items: [], filters: { limit: 50, sort: 'trending' } },
showSearch : { page: 1, items: [], filters: {} },
},
}

Expand Down
28 changes: 28 additions & 0 deletions app/screens/Home/HomeReducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as HomeConstants from './HomeConstants'
import * as ItemConstants from '../Item/ItemConstants'

export default (state = HomeConstants.INITIAL_STATE, action) => {
switch (action.type) {
Expand Down Expand Up @@ -40,6 +41,33 @@ export default (state = HomeConstants.INITIAL_STATE, action) => {
},
}

case ItemConstants.ADD_TO_BOOKMARKS:
return {
...state,
modes: {
...state.modes,
bookmark: {
...state.modes.bookmark,
items: [
...state.modes.bookmark.items,
action.payload,
],
},
},
}

case ItemConstants.REMOVE_FROM_BOOKMARKS:
return {
...state,
modes: {
...state.modes,
bookmark: {
...state.modes.bookmark,
items: state.modes.bookmark.items.filter(bookmark => bookmark.id !== action.payload.id),
},
},
}

default:
return state
}
Expand Down
23 changes: 21 additions & 2 deletions app/screens/Home/HomeScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class Home extends React.PureComponent {

getItems(Constants.TYPE_MOVIE)
getItems(Constants.TYPE_SHOW)
getItems(Constants.TYPE_BOOKMARK)
}

componentWillUnmount() {
Expand All @@ -69,7 +70,11 @@ export default class Home extends React.PureComponent {
return null
}

getMyList = () => []
getMyList = () => {
const { modes } = this.props

return modes[Constants.TYPE_BOOKMARK].items.slice(0, 10)
}

getMovies = (withSlice = true) => {
const { modes } = this.props
Expand All @@ -92,6 +97,8 @@ export default class Home extends React.PureComponent {
render() {
const { isLoading, hasInternet } = this.props

const myList = this.getMyList()

return (
<View style={styles.root}>

Expand All @@ -104,8 +111,20 @@ export default class Home extends React.PureComponent {
onLoad={this.handleCoverLoaded}
item={this.getMainCover()} />

{myList && myList.length > 0 && (
<CardList
style={{ marginTop: -20, marginBottom: 8 }}
onPress={this.handleItemOpen}
loading={isLoading}
title={i18n.t('My List')}
items={myList} />
)}

<CardList
style={{ marginTop: -20, marginBottom: 8 }}
style={{
marginTop : myList.length > 0 ? 0 : -20,
marginBottom: 8,
}}
onPress={this.handleItemOpen}
loading={isLoading}
title={i18n.t('Movies')}
Expand Down
58 changes: 27 additions & 31 deletions app/screens/Item/ItemActions.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import Popcorn, { Constants } from 'popcorn-sdk'

import BookmarkAdapter from 'modules/BookmarkAdapter'
import { Constants } from 'popcorn-sdk'
import Popcorn from 'modules/PopcornSDK'

import * as ItemConstants from './ItemConstants'
import * as HomeSelectors from '../Home/HomeSelectors'

Popcorn.setBookmarkAdapter(BookmarkAdapter)

export function hasItem(itemId, mode, state) {
return HomeSelectors.getModes(state)[mode].items.find(item => item.id === itemId)
}
Expand All @@ -25,19 +22,6 @@ export function partlyFetchedItem(item) {
}
}

export function fetchEpisodeTorrents() {
return {
type: ItemConstants.FETCH_EPISODE_TORRENTS,
}
}

export function fetchedEpisodeTorrents(item) {
return {
type : ItemConstants.FETCHED_EPISODE_TORRENTS,
payload: item,
}
}

export function getItem(type, itemId) {
return (dispatch, getState) => new Promise(async(resolve) => {
dispatch({
Expand All @@ -48,29 +32,23 @@ export function getItem(type, itemId) {

if (type === Constants.TYPE_MOVIE) {
if (item) {
resolve(
dispatch(
fetchedItem(
await BookmarkAdapter.checkMovie(item),
),
),
)
resolve(dispatch(fetchedItem(
await Popcorn.bookmarks.checkMovie(item),
)))

} else {
resolve(Popcorn.getMovie(itemId).then(movie => dispatch(fetchedItem(movie))))
}

} else if (type === Constants.TYPE_SHOW) {
if (item) {
dispatch(
partlyFetchedItem(
await BookmarkAdapter.checkShow(item),
),
)
resolve(dispatch(partlyFetchedItem(
await Popcorn.bookmarks.checkMovie(item),
)))
}

return Popcorn.getShowBasic(itemId).then((basicShow) => {
resolve(dispatch(partlyFetchedItem(basicShow)))
dispatch(partlyFetchedItem(basicShow))

Popcorn.getShowMeta(basicShow).then(show => dispatch(fetchedItem(show)))
})
Expand All @@ -79,3 +57,21 @@ export function getItem(type, itemId) {
return null
})
}

export const addToBookmarks = (item) => (dispatch) => {
Popcorn.bookmarks.addItem(item)

dispatch({
type : ItemConstants.ADD_TO_BOOKMARKS,
payload: item,
})
}

export const removeFromBookmarks = (item) => (dispatch) => {
Popcorn.bookmarks.removeItem(item)

dispatch({
type : ItemConstants.REMOVE_FROM_BOOKMARKS,
payload: item,
})
}
6 changes: 2 additions & 4 deletions app/screens/Item/ItemConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export const INITIAL_STATE = {
export const FETCH_ITEM = `${REDUCER_NAME}.fetch.item`
export const PARTLY_FETCH_ITEM = `${REDUCER_NAME}.partly.fetch.item`
export const FETCHED_ITEM = `${REDUCER_NAME}.fetched.item`
export const UPDATE_ITEM = `${REDUCER_NAME}.update.item`
export const SELECT_SEASON_EPISODE = `${REDUCER_NAME}.select.season.episode`

export const FETCH_EPISODE_TORRENTS = `${REDUCER_NAME}.fetch.episode.torrents`
export const FETCHED_EPISODE_TORRENTS = `${REDUCER_NAME}.fetched.episode.torrents`
export const ADD_TO_BOOKMARKS = `${REDUCER_NAME}.add.to.bookmarks`
export const REMOVE_FROM_BOOKMARKS = `${REDUCER_NAME}.remove.from.bookmarks`
21 changes: 10 additions & 11 deletions app/screens/Item/ItemReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,22 @@ export default (state = ItemConstants.INITIAL_STATE, action) => {
item : action.payload,
}

case ItemConstants.FETCH_EPISODE_TORRENTS:
case ItemConstants.ADD_TO_BOOKMARKS:
return {
...state,
fetchingEpisodeTorrents: true,
item: {
...state.item,
bookmarked: true,
},
}

case ItemConstants.FETCHED_EPISODE_TORRENTS:
case ItemConstants.REMOVE_FROM_BOOKMARKS:
return {
...state,
fetchingEpisodeTorrents: false,
item : action.payload,
}

case ItemConstants.SELECT_SEASON_EPISODE:
return {
...state,
...action.payload,
item: {
...state.item,
bookmarked: false,
},
}

default:
Expand Down
Loading

0 comments on commit 9a4f54f

Please sign in to comment.