Adding asynStorage methods

pull/5/head
Corentin RICHARD 2 years ago
parent 28b597d0cc
commit 565cf8fec1

@ -5,7 +5,7 @@ import {Classe} from "./Classe";
export class Card { export class Card {
constructor(id: number,name :string, img : string, imgGold : string){//,set : CardSet,type : Type,clas : Classe,rarity : string,cost : number,attack : number, health : number, desc : string,flavor : string,artist : string,collectible : boolean,elite : boolean,race : string, cropImg :string) { constructor(id: number,name :string, img : string, imgGold : string,fav : boolean = true){//,set : CardSet,type : Type,clas : Classe,rarity : string,cost : number,attack : number, health : number, desc : string,flavor : string,artist : string,collectible : boolean,elite : boolean,race : string, cropImg :string ) {
this._id=id this._id=id
this._name=name this._name=name
//this._set=set //this._set=set
@ -22,6 +22,7 @@ export class Card {
this._img = img this._img = img
this._imgGold = imgGold this._imgGold = imgGold
// this._cropImg = cropImg // this._cropImg = cropImg
this._fav = fav
} }
@ -155,4 +156,12 @@ export class Card {
// set cropImg(value: string) { // set cropImg(value: string) {
// this._cropImg = value; // this._cropImg = value;
// } // }
private _fav : boolean
get fav(): boolean{
return this._fav;
}
set fav(value : boolean){
this._fav = value;
}
} }

@ -1,5 +1,5 @@
import { StyleSheet, Text, View, Button, TouchableHighlight, TextInput } from 'react-native'; import { StyleSheet, Text, View, Button, TouchableHighlight, TextInput, ImageBackground } from 'react-native';
import { StatusBar } from 'expo-status-bar'; import { StatusBar } from 'expo-status-bar';
import React, { useState, useEffect } from "react"; import React, { useState, useEffect } from "react";
import { FlatList } from 'react-native-gesture-handler'; import { FlatList } from 'react-native-gesture-handler';
@ -20,16 +20,16 @@ import { ImageURISource } from 'react-native';
//@ts-ignore //@ts-ignore
const Item = ({url}) => { // a mettre dans components et definir une props pour passer le param const Item = ({url,item}) => { // a mettre dans components et definir une props pour passer le param
const HandleAddFav = () => { const HandleAddFav = () => {
console.log('addfavorite'); item.fav = !item.fav
} }
return( return(
<View style={styles.item}> <View style={styles.item}>
<ImageBackground source={{uri:url}} style={{flex:1, minHeight:250, minWidth:180}}> <ImageBackground source={{uri:url}} style={{flex:1, minHeight:250, minWidth:180}}>
<TouchableHighlight style={styles.favoriteButton} onPress={HandleAddFav}> <TouchableHighlight style={item.fav?styles.favoriteButtonFav:styles.favoriteButtonNonFav} onPress={HandleAddFav}>
<FontAwesome name="heart-o" size={50} color="#fff" /> <FontAwesome name="heart-o" size={50} color="#fff" />
</TouchableHighlight> </TouchableHighlight>
</ImageBackground> </ImageBackground>
@ -45,7 +45,6 @@ export default function ListScreen({navigation}){
// // Initialize the binding content with the application initial state // // Initialize the binding content with the application initial state
//@ts-ignore //@ts-ignore
const nList = useSelector(state => state.appReducer.cards); const nList = useSelector(state => state.appReducer.cards);
// Create a const that will hold the react-redux events dispatcher // Create a const that will hold the react-redux events dispatcher
@ -65,9 +64,7 @@ export default function ListScreen({navigation}){
//* Stub
// const {getCards} = new StubLib();
// const list: Card[] = getCards();
// const req = fetch('https://omgvamp-hearthstone-v1.p.rapidapi.com/cards') // const req = fetch('https://omgvamp-hearthstone-v1.p.rapidapi.com/cards')
//https://us.api.blizzard.com/hearthstone/cards/678?locale=en_US //https://us.api.blizzard.com/hearthstone/cards/678?locale=en_US
@ -93,7 +90,7 @@ export default function ListScreen({navigation}){
data={filteredList} data={filteredList}
renderItem={({item}) => renderItem={({item}) =>
<TouchableHighlight onPress={() => navigation.navigate("ListFav")}> <TouchableHighlight onPress={() => navigation.navigate("ListFav")}>
<Item url={item.img}/> <Item url={item.img} item={item}/>
</TouchableHighlight> </TouchableHighlight>
} }
keyExtractor={(item: Card) => item.id.toString()} keyExtractor={(item: Card) => item.id.toString()}
@ -127,11 +124,19 @@ const styles = StyleSheet.create({
item: { item: {
}, },
favoriteButton: { favoriteButtonNonFav: {
position: 'absolute',
top: 10,
right: 10,
backgroundColor: 'red',
borderRadius: 50,
padding: 10,
},
favoriteButtonFav: {
position: 'absolute', position: 'absolute',
top: 10, top: 10,
right: 10, right: 10,
backgroundColor: 'transparent', backgroundColor: 'red',
borderRadius: 50, borderRadius: 50,
padding: 10, padding: 10,
}, },

@ -2,17 +2,41 @@ import { Card } from "../models/Card";
import AsyncStorage from "@react-native-async-storage/async-storage"; import AsyncStorage from "@react-native-async-storage/async-storage";
export class AsyncStorageCard{ export default class Storage {
static AddCardStorage(name : string, cards : Card[]){ static async getItem(key: string): Promise<any> {
const storeFavoriteNounours = async (cards : Card[]) => {
try { try {
const jsonCard = JSON.stringify(cards) const value = await AsyncStorage.getItem(key);
await AsyncStorage.setItem(name, jsonCard); if (value !== null) {
return JSON.parse(value);
}
} catch (e) { } catch (e) {
console.log("An error occurred", e); console.error(`AsyncStorage getItem error: ${e}`);
}
return null;
}
static async setItem(key: string, value: any): Promise<void> {
try {
await AsyncStorage.setItem(key, JSON.stringify(value));
} catch (e) {
console.error(`AsyncStorage setItem error: ${e}`);
}
} }
static async removeItem(key: string): Promise<void> {
try {
await AsyncStorage.removeItem(key);
} catch (e) {
console.error(`AsyncStorage removeItem error: ${e}`);
} }
} }
static async clear(): Promise<void> {
try {
await AsyncStorage.clear();
} catch (e) {
console.error(`AsyncStorage clear error: ${e}`);
}
}
} }
Loading…
Cancel
Save