This repository has been archived by the owner on May 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use local storage to persist the current playtime
- Loading branch information
1 parent
fa0e12c
commit 17f78db
Showing
16 changed files
with
150 additions
and
105 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
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,22 @@ | ||
import actions from '../actions' | ||
|
||
let idle = null | ||
const IDLE_TIMEOUT = 10 * 60 * 1000 | ||
|
||
export default (store, action) => { | ||
switch (action.type) { | ||
case 'INIT': | ||
if (action.payload.playtime > 0) { | ||
store.dispatch(actions.idle()) | ||
} | ||
break | ||
case 'PLAY': | ||
clearTimeout(idle) | ||
break | ||
case 'PAUSE': | ||
idle = setTimeout(() => { | ||
store.dispatch(actions.idle()) | ||
}, IDLE_TIMEOUT) | ||
break | ||
} | ||
} |
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 @@ | ||
import mediaEffects from './media' | ||
import idleEffects from './idle' | ||
import storageEffects from './storage' | ||
|
||
export default store => next => action => { | ||
mediaEffects(store, action) | ||
storageEffects(store, action) | ||
idleEffects(store, action) | ||
|
||
return next(action) | ||
} |
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,39 @@ | ||
import mediaPlayer from '../../media-player' | ||
import actions from '../actions' | ||
|
||
const initMediaPlayer = (dispatch, config) => | ||
mediaPlayer(config.audio, { | ||
setPlaytime: playtime => dispatch(actions.setPlaytime(playtime)), | ||
setBufferState: buffer => dispatch(actions.setBuffer(buffer)), | ||
setDuration: duration => dispatch(actions.setDuration(duration)), | ||
onPlay: () => dispatch(actions.playEvent()), | ||
onPause: () => dispatch(actions.pauseEvent()), | ||
onStop: () => dispatch(actions.stopEvent()), | ||
onLoad: () => dispatch(actions.loading()) | ||
}) | ||
|
||
let mediaElement | ||
|
||
export default (store, action) => { | ||
const state = store.getState() | ||
|
||
switch (action.type) { | ||
case 'INIT': | ||
mediaElement = initMediaPlayer(store.dispatch, action.payload) | ||
break | ||
case 'UI_PLAY': | ||
mediaElement.setPlaytime(state.playtime) | ||
mediaElement.play() | ||
break | ||
case 'UI_PAUSE': | ||
mediaElement.pause() | ||
break | ||
case 'UI_RESTART': | ||
mediaElement.setPlaytime(0) | ||
mediaElement.play() | ||
break | ||
case 'UPDATE_PLAYTIME': | ||
mediaElement.setPlaytime(action.payload) | ||
break | ||
} | ||
} |
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,24 @@ | ||
import actions from '../actions' | ||
|
||
import storage from 'utils/storage' | ||
import hash from 'object-hash' | ||
|
||
let podloveStorage | ||
|
||
export default (store, action) => { | ||
switch (action.type) { | ||
case 'INIT': | ||
podloveStorage = storage(hash(action.payload)) | ||
|
||
let storedPlaytime = podloveStorage.get('playtime') | ||
|
||
if (storedPlaytime !== undefined) { | ||
store.dispatch(actions.setPlaytime(storedPlaytime)) | ||
store.dispatch(actions.idle()) | ||
} | ||
break | ||
case 'SET_PLAYTIME': | ||
podloveStorage.set('playtime', action.payload) | ||
break | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,41 @@ | ||
import curry from 'lodash/fp/curry' | ||
import get from 'lodash/get' | ||
|
||
const getItem = curry((hash, key) => { | ||
try { | ||
const fromStore = window.localStorage.getItem(hash) || '' | ||
let obj = JSON.parse(fromStore) | ||
|
||
if (!key) { | ||
return obj || {} | ||
} | ||
|
||
return get(obj, key) | ||
} catch (err) { | ||
return undefined | ||
} | ||
}) | ||
|
||
const setItem = curry((hash, first, second) => { | ||
let data | ||
|
||
if (!second) { | ||
data = first | ||
} else { | ||
data = {[first]: second} | ||
} | ||
|
||
try { | ||
const currentStore = getItem(hash, null) | ||
const toStore = JSON.stringify(Object.assign({}, currentStore, data)) | ||
|
||
return window.localStorage.setItem(hash, toStore) | ||
} catch (err) { | ||
return undefined | ||
} | ||
}) | ||
|
||
export default hash => ({ | ||
set: setItem(hash), | ||
get: getItem(hash) | ||
}) |