How to properly use AsyncStorage in a Movie WatchList - react-native

First of all,i tried to make a watchlist for movies,a page that contains movies that you add.I can't get understand async storage and how it works within my code.
I've tried different tutorials,but I still don't get to make it work.I tried with the code that official react-native page has on their site,but with no results.
item2ADD = this.props.navigation.getParam('movie');
_retrieveData = async () => {
try {
await AsyncStorage.setItem('#MySuperStore:key', 'I like to save it.');
} catch (error) {
console.warn("data has not been saved");
}
try {
const value = await AsyncStorage.getItem('TASKS');
if (value !== null) {
console.warn("data has been loaded");
console.warn(value);
}
} catch (error) {
console.warn("data has not been loaded");
}
};
constructor(props) {
super(props);
this.state = {
data : [] ,
};
}
async componentDidMount() {
const data = this._retrieveData();
this.setState({ data });
}
render() {
if(this.state.data!==null) {
return (
<View style={styles.container}>
<Text style={styles.title}>Watchlist</Text>
<TouchableOpacity style={styles.backBtn} onPress={() => {
this.props.navigation.goBack()
}}>
<Image style={styles.backIMG} source={menuImages.back}/>
</TouchableOpacity>
<FlatList
data={this.state.data}
numColumns={2}
renderItem={({item}) =>
<View style={styles.fList}>
<TouchableOpacity onPress={() => this.props.navigation.navigate('Details', {movie: item})}>
<Image style={styles.img} source={{uri: item.coverUrl}}/>
</TouchableOpacity>
<Text style={styles.movieTitle}>{item.title}</Text>
<Text
style={styles.movieDate}>{moment.unix(Math.floor(parseInt(item.releaseDate)) / 1000).format("DD/MM/YYYY")}</Text>
</View>
} keyExtractor={(item, index) => index.toString()}
/>
</View>
);
}
else
{
return <Text>FAILED TO LOAD</Text>
}
}
}
I just want to know how can I implement AsyncStorage to store my added movies from the item2ADD,it contains only 1 item that i need to store in order to make a watchlist.(the item which contains all details that i need for a movie).
I want to save all the movies with asyncstorage then show them in that flatlist.
I keep getting :
invariant violation tried to get frame for out of range index nan(on android simulator) when i click to add a movie to the list.

I'm not sure what is the problem you are asking but there is a few things that maybe causing the problem.
1- In your function _retrieveData you don't return anything from it.
I'm not sure what you want to return but what you could do is return the value from the AsyncStorage.
_retrieveData = async () => {
try {
await AsyncStorage.setItem('#MySuperStore:key', 'I like to save it.');
} catch (error) {
console.warn("data has not been saved");
}
try {
const value = await AsyncStorage.getItem('TASKS');
if (value !== null) {
console.warn("data has been loaded");
console.warn(value);
}
// returning the value.
return value
} catch (error) {
console.warn("data has not been loaded");
}
};
2- You are setting state with the data from _retrieveData without waiting for it.
In you componentDidMount you set the data from _retrieveData but because it's a promise, you need to use await to get the data
async componentDidMount() {
// added await
const data = await this._retrieveData();
this.setState({ data });
}
3- The data you provide to FlatList needs to be an array.
When you call pass data={this.state.data} to FlatList, it needs to be an array, if it's not an array, you will have several problems.
Make sure that when you return the value from _retrieveData, it's an array.
// value needs to be an array
const value = await AsyncStorage.getItem('TASKS');
You should also notice that to setItem, it need to be parsed to a json, this means doing something like AsyncStorage.setItem('NAME_OF_YOUR_KEY', JSON.stringify(DATA_YOU_WANT_TO_STORE); and to getItem it need to be parsed to a javascript object, this means doing something like
const jsonValue = await AsyncStorage.getItem('NAME_OF_YOUR_KEY');
const value = JSON.parse(jsonValue);

Related

React Native add data to Async Storage

I'm developing application about travelling, so I have a map with places to travel and I want to implement adding to favourites functionality. When the user clicks on the marker on the map I show him modal window with all the information about the place and there I have icon-button "add-to-favorites". When user clicked on this "add-to-favourites" icon-button I want to save the place to the AsyncStorage, rerender icon to "remove-from-favourites" then if the place in favouries and user clicks on "remove-from-favorites" then remove place from AsyncStorage and rerender icon the add-to-favorites. I don't really understand how to do it in a right way in functional component. Give me an example please.
What I tried:
My ModalWindow code:
const [favourite, setFavourite] = useState(false);
const onHeartPress = async () => {
setFavourite(prev => !prev);
await storage.save({
key: 'markers',
data: {
marker: marker
},
expires: null
})
<TouchableOpacity onPress={() => onHeartPress()}>
<View style={{flexDirection: "row", alignItems: "center"}}>
{favourite ? <Ionicons size={height/20} name={'heart-dislike'} /> : <Ionicons size={height/20} name={'heart'} />}
</View>
</TouchableOpacity>
It's not difficult to understand how to add to the storage and then load from the storage but how to render the icon correctly like heart/dislike heart because now with this code when I press on button icon in one place in the other places it also changes, because favourite becomes true so I want to understand how in the correct way change icon only for place where user clicked on icon.
You can follow this code to achieve this functionality:
const STORAGE_KEY = '#icon_name'
// Store data
const saveData = async () => {
try {
await AsyncStorage.setItem(STORAGE_KEY, age)
alert('Data successfully saved')
} catch (e) {
alert('Failed to save the data to the storage')
}
}
// fetch data
const readData = async () => {
try {
const userAge = await AsyncStorage.getItem(STORAGE_KEY)
if (userAge !== null) {
setAge(userAge)
}
} catch (e) {
alert('Failed to fetch the data from storage')
}
}
//remove data
try {
await AsyncStorage.removeItem(STORAGE_KEY);
console.log('Data removed')
}
catch(exception) {
console.log(exception)
}

How to display data from a firebase firestore in React Native

For the Life of me, I cannot get this to render the data that I pull from the database.
Here is the code
function assetList(){
const [assetL, setAssetL] = useState([]);
const [errors, setErrors] = useState("");
const getAssets = async () =>{
try{
const list = [];
console.log("Break");
db.collection("assets").get().then(function(querySnapshot){
querySnapshot.forEach(function(doc){
list.push(doc.data());
});
});
//problem
setAssetL([...list]);
//problem
console.log("list");
console.log(list);
console.log("AssetL");
console.log
} catch (e) {
setErrors("Failed To Load Data");
}
};
useEffect(() => {
getAssets();
}, []);
console.log(assetL);
return(
<SafeAreaView style = {styles.Heading}>
<View style = {styles.Heading}>
<Text style = {styles.headText}>Asset List</Text>
</View>
<FlatList
data = {assetL}
renderItem={({item}) => <Text>{item.default}</Text>}
/>
</SafeAreaView>
);
}
I have narrowed down to at least the most pressing issue, other than my jank way of throwing this page of the app together, is that the setAssetL isnt actually setting the assetL const. Can anyone explain why this is happening and how to fix it?
For the getAssets function do something like this:
const getAssets = async () =>{
try {
const list = [];
console.log("Break");
db.collection("assets").get().then(function(querySnapshot){
querySnapshot.forEach(function(doc){
list.push(doc.data());
});
setAssetL(list);
});
...
} catch (e) {
setErrors("Failed To Load Data");
}
};
Your code did not work because db.collection("assets").get() function returns a promise and you handle it asynchronously while expecting it to be synchronous.
Here you can read more about async functions

How to get AsyncStorage key name from FlatList item to delete?

I am generating a random key name for AsyncStorage each time user saves an item. These are then displayed in FlatList (using SwipeListView library for swipe to delete button). Now if I call await AsyncStorage.removeItem(key); when the user taps "Delete", I presume the item will just disappear from the list. What I'm completely lost on is how I am supposed to get my random key name? Struggling to find much on FlatList and AsyncStorage, not sure what good practice is.
FlatList:
export default class RecentMealsScreen extends Component {
constructor() {
super();
this.state={
meals: []
}
}
componentDidMount() {
this.getAllMeals();
}
getAllMeals = async () => {
try {
const data = [];
let keys = await AsyncStorage.getAllKeys();
for (let inKey of keys) {
let obj = await AsyncStorage.getItem(inKey);
obj = JSON.parse(obj);
data.push(obj);
}
this.setState({
meals: data
})
} catch (error) {
console.log("Error saving all meals. Error: " + error)
}
}
renderHiddenItem = () => (
<View style={styles.rowBack}>
<View style={[styles.backRightBtn, styles.backRightBtnRight]}>
<Text style={styles.backTextWhite}>Delete</Text>
</View>
</View>
);
deleteMeal = async (key) => {
try {
await AsyncStorage.removeItem(key);
} catch (error) {
console.log('Error deleting Meal: ' + error)
}
}
// Get Meal IDs and display them in list
render() {
return (
<View style={styles.container}>
<SwipeListView
data={this.state.meals}
renderItem={ ({item}) =>
<View style={styles.container}>
<Meal
image = {item.image}
order={item.orderName}
company={item.companyName}
price={item.price}
dateTime={item.dateTime}
notes={item.notes}
rating = {item.rating}
/>
</View>
}
disableRightSwipe
renderHiddenItem={this.renderHiddenItem}
rightOpenValue={-Dimensions.get('window').width}
useNativeDriver={false}
onSwipeValueChange={this.deleteMeal()}
/>
</View>
);
}
}
Save Logic:
saveMeal = async () => {
try {
let meal = {
image: this.state.imageSource,
orderName: this.state.orderText,
companyName: this.state.selectedCompany,
price: this.state.priceText,
dateTime: this.state.dateTimeText,
notes: this.state.notesTextField,
rating: this.state.starCount
};
const ID = await Random.getRandomBytesAsync(16);
await AsyncStorage.setItem(ID.toString(), JSON.stringify(meal)).then(() => {
// Redirect to new screen
Actions.recentMeals();
})
} catch (error) {
console.log("Save Meal error: " + error)
}
}

Why is AsyncStorage getAllKeys not returning null? Have multiple views, want to render based on if data found, deleted all keys

I have a boolean called isDataReady stored in the state. If I find keys via AsyncStorage, I set it true and display a list of data. If nothing is found then I want to render a different view. My data is displaying fine but with everything deletef, I can't get my intro screen to display. Its because AsyncStorage is never null despite their being no keys. What am I doing wrong?
Code (view related code removed for clarity)
constructor() {
super();
this.state={
meals: [],
isDataReady: false,
}
}
componentDidMount() {
this.getAllMeals();
}
getAllMeals = async () => {
try {
const data = [];
let keys = await AsyncStorage.getAllKeys();
// await AsyncStorage.multiRemove(keys);
if (keys !== null) {
for (let inKey of keys) {
let obj = await AsyncStorage.getItem(inKey);
obj = JSON.parse(obj);
obj["key"] = inKey;
data.push(obj);
}
this.setState({
meals: data,
isDataReady: true
})
} else {
this.setState({
isDataReady: false
})
}
} catch (error) {
console.log("Error saving all meals. Error: " + error)
}
}
render() {
if (this.state.isDataReady === true) {
return (
<View style={styles.container}>
</View>
);
} else if (this.state.isDataReady === false) {
return (
<ScrollView>
<View style={styles.container}>
</View>
</ScrollView>
);
}
}
}
I change the if statement to if (keys.length !== 0), always returns array so its never null.

React Native - Expo Audio stop all sounds

I am using Expo Audio to play some short sounds from a list.
async playAudio(file) {
try {
await Audio.setIsEnabledAsync(true);
const sound = new Audio.Sound();
await sound.loadAsync(file);
await sound.playAsync();
} catch(error) {
console.error(error);
}
}
Which I'm calling from a list rendered with list.map()
renderTheList = (item, i) => {
return (
<View key={i}>
<TouchableOpacity onPress={ () => { this.onAudioSelected(item.audio) }}>
</TouchableOpacity>
</View>
)
}
onAudioSelected(audio) {
// Audio.clearSounds() <-- something like this
playAudio(audio)
...
}
The sounds play fine, but when I select the next item in the list, the previous sound does not stop. So if I touch a bunch in a row, a bunch of sounds play at once.
How can I stop all currently playing sounds?
I figured out that I should create the playback object in the constructor and use unloadAsync()
constructor(props)
{
super(props);
this.audioPlayer = new Audio.Sound();
}
playSound = async () => {
try {
await this.audioPlayer.unloadAsync()
await this.audioPlayer.loadAsync(require("../soundfile.mp3"));
await this.audioPlayer.playAsync();
} catch (err) {
console.warn("Couldn't Play audio", err)
}
}
The full documentation is found at AV - Expo Documentation