Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating functionality to download and play podcasts from filesystem
- Loading branch information
1 parent
2a3727f
commit 232cab2
Showing
10 changed files
with
264 additions
and
37 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
Binary file not shown.
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 |
---|---|---|
@@ -1,29 +1,72 @@ | ||
import { call, put } from 'redux-saga/effects'; | ||
import { call, select, put } from 'redux-saga/effects'; | ||
import { Platform } from 'react-native'; | ||
import RNFS from 'react-native-fs'; | ||
|
||
import { | ||
persistItemInStorage, | ||
getItemFromStorage, | ||
KEYS, | ||
} from '~/utils/AsyncStorageManager'; | ||
import { | ||
requestPermission, | ||
PERMISSIONS_TYPES, | ||
} from '~/utils/AndroidPermissionsManager'; | ||
|
||
import { Creators as PlayerCreators } from '../ducks/player'; | ||
|
||
const FILE_PREFIX = Platform.OS === 'android' ? 'file://' : ''; | ||
|
||
const _isPodcastAlreadyCached = async (currentPodcast) => { | ||
const rawPodcastsSaved = await getItemFromStorage(KEYS.PODCAST, []); | ||
|
||
const podcastsSaved = typeof rawPodcastsSaved === 'string' | ||
? JSON.parse(rawPodcastsSaved) | ||
: rawPodcastsSaved; | ||
|
||
const podcastCached = podcastsSaved.filter( | ||
podcast => podcast.id === currentPodcast.id, | ||
)[0]; | ||
|
||
return podcastCached; | ||
}; | ||
|
||
export function* setPodcast() { | ||
try { | ||
} catch (err) {} | ||
} | ||
const { playlistIndex, playlist } = yield select(state => state.player); | ||
const currentPodcast = playlist[playlistIndex]; | ||
|
||
export function* playPrevious() { | ||
/* const { podcast, playlist } = yield select(state => state.player); | ||
const previousPodcastIndex = playlist.findIndex(podcastFromPlaylist => podcastFromPlaylist._id === podcast._id) - 1; | ||
yield requestPermission(PERMISSIONS_TYPES.READ_EXTERNAL_STORAGE); | ||
|
||
try { | ||
yield cps(RNSound.getCurrentTime, PODCAST_ID); | ||
} catch (currentTimeInSeconds) { | ||
const shouldRepeatSamePodcast = currentTimeInSeconds > 3; | ||
if (shouldRepeatSamePodcast) { | ||
yield put(ActionCreators.playerSetPodcastRequest(podcast, playlist)); | ||
return; | ||
} | ||
if (previousPodcastIndex === -1) { | ||
yield put(ActionCreators.playerSetPodcastRequest(playlist[0], playlist)); | ||
} else { | ||
yield put(ActionCreators.playerSetPodcastRequest(playlist[previousPodcastIndex], playlist)); | ||
} | ||
} */ | ||
/* yield persistItemInStorage( | ||
KEYS.PODCAST, | ||
JSON.stringify([ | ||
{ | ||
...currentPodcast, | ||
path: `${FILE_PREFIX}${RNFS.ExternalDirectoryPath}/${ | ||
currentPodcast.id | ||
}.mp3`, | ||
}, | ||
]), | ||
); */ | ||
|
||
/* yield call(RNFS.downloadFile, { | ||
fromUrl: 'https://s3-sa-east-1.amazonaws.com/gonative/1.mp3', | ||
toFile: `${FILE_PREFIX}${RNFS.ExternalDirectoryPath}/${ | ||
currentPodcast.id | ||
}.mp3`, | ||
discretionary: true, | ||
}); */ | ||
|
||
const podcastCached = yield _isPodcastAlreadyCached(currentPodcast); | ||
|
||
const isPodcastAlreadyCached = !!podcastCached | ||
&& !!podcastCached.path | ||
&& typeof podcastCached.path === 'string'; | ||
|
||
const podcastURI = isPodcastAlreadyCached | ||
? podcastCached.path | ||
: currentPodcast.url; | ||
|
||
yield put(PlayerCreators.setPodcastSuccess(podcastURI)); | ||
} catch (err) {} | ||
} |
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,48 @@ | ||
import { PermissionsAndroid } from 'react-native'; | ||
|
||
export const PERMISSIONS_TYPES = { | ||
WRITE_EXTERNAL_STORAGE: 'WRITE_EXTERNAL_STORAGE', | ||
READ_EXTERNAL_STORAGE: 'READ_EXTERNAL_STORAGE', | ||
}; | ||
|
||
const PERMISSIONS = { | ||
[PERMISSIONS_TYPES.WRITE_EXTERNAL_STORAGE]: { | ||
type: 'WRITE_EXTERNAL_STORAGE', | ||
config: { | ||
title: 'MindCast App Create File Permission', | ||
message: | ||
'MindCast needs access to your storage ' | ||
+ 'so you can save your podcasts locally.', | ||
}, | ||
}, | ||
|
||
[PERMISSIONS_TYPES.READ_EXTERNAL_STORAGE]: { | ||
type: 'READ_EXTERNAL_STORAGE', | ||
config: { | ||
title: 'MindCast App Read File Permission', | ||
message: | ||
'MindCast needs access to your storage ' | ||
+ 'so you can listen your podcasts locally and offline.', | ||
}, | ||
}, | ||
}; | ||
|
||
export const requestPermission = async (type) => { | ||
try { | ||
const permissionConfig = PERMISSIONS[type]; | ||
|
||
const granted = await PermissionsAndroid.request( | ||
PermissionsAndroid.PERMISSIONS[type], | ||
{ | ||
...permissionConfig.config, | ||
buttonNeutral: 'Ask Me Later', | ||
buttonNegative: 'Cancel', | ||
buttonPositive: 'OK', | ||
}, | ||
); | ||
|
||
return granted === PermissionsAndroid.RESULTS.GRANTED; | ||
} catch (err) { | ||
console.tron.log(err); | ||
} | ||
}; |
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,43 @@ | ||
// @flow | ||
|
||
import { AsyncStorage } from 'react-native'; | ||
|
||
const APP_STORAGE_KEY = '@MIND_CAST'; | ||
|
||
export const KEYS = { | ||
PODCAST: 'PODCAST', | ||
}; | ||
|
||
export const getItemFromStorage = async ( | ||
key: string, | ||
defaultValue: any, | ||
): any => { | ||
try { | ||
const value = (await AsyncStorage.getItem(`${APP_STORAGE_KEY}:${key}`)) || defaultValue; | ||
|
||
return value; | ||
} catch (error) { | ||
console.tron.log(error); | ||
} | ||
|
||
return defaultValue; | ||
}; | ||
|
||
export const persistItemInStorage = async ( | ||
key: string, | ||
value: any, | ||
): Promise<void> => { | ||
try { | ||
await AsyncStorage.setItem(`${APP_STORAGE_KEY}:${key}`, value.toString()); | ||
} catch (err) { | ||
console.tron.log(err); | ||
} | ||
}; | ||
|
||
export const removeItemFromStorage = async (key: string) => { | ||
try { | ||
await AsyncStorage.removeItem(`${APP_STORAGE_KEY}:${key}`); | ||
} catch (err) { | ||
console.tron.log(err); | ||
} | ||
}; |
Oops, something went wrong.