From 565cf8fec16eb9cf6194ced1491cc3dd7f90e609 Mon Sep 17 00:00:00 2001 From: "Corentin \"Koroh\" RICHARD" Date: Fri, 17 Mar 2023 09:01:31 +0100 Subject: [PATCH] Adding asynStorage methods --- models/Card.tsx | 11 +++++++++- screens/ListScreen.tsx | 27 +++++++++++++---------- service/AsyncStorage.tsx | 46 ++++++++++++++++++++++++++++++---------- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/models/Card.tsx b/models/Card.tsx index d85f1f0..655bd73 100644 --- a/models/Card.tsx +++ b/models/Card.tsx @@ -5,7 +5,7 @@ import {Classe} from "./Classe"; 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._name=name //this._set=set @@ -22,6 +22,7 @@ export class Card { this._img = img this._imgGold = imgGold // this._cropImg = cropImg + this._fav = fav } @@ -155,4 +156,12 @@ export class Card { // set cropImg(value: string) { // this._cropImg = value; // } + + private _fav : boolean + get fav(): boolean{ + return this._fav; + } + set fav(value : boolean){ + this._fav = value; + } } \ No newline at end of file diff --git a/screens/ListScreen.tsx b/screens/ListScreen.tsx index 15a84cf..22c6699 100644 --- a/screens/ListScreen.tsx +++ b/screens/ListScreen.tsx @@ -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 React, { useState, useEffect } from "react"; import { FlatList } from 'react-native-gesture-handler'; @@ -20,16 +20,16 @@ import { ImageURISource } from 'react-native'; //@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 = () => { - console.log('addfavorite'); + item.fav = !item.fav } return( - + @@ -45,7 +45,6 @@ export default function ListScreen({navigation}){ // // Initialize the binding content with the application initial state - //@ts-ignore const nList = useSelector(state => state.appReducer.cards); // 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') //https://us.api.blizzard.com/hearthstone/cards/678?locale=en_US @@ -93,7 +90,7 @@ export default function ListScreen({navigation}){ data={filteredList} renderItem={({item}) => navigation.navigate("ListFav")}> - + } keyExtractor={(item: Card) => item.id.toString()} @@ -127,11 +124,19 @@ const styles = StyleSheet.create({ item: { }, - favoriteButton: { + favoriteButtonNonFav: { + position: 'absolute', + top: 10, + right: 10, + backgroundColor: 'red', + borderRadius: 50, + padding: 10, + }, + favoriteButtonFav: { position: 'absolute', top: 10, right: 10, - backgroundColor: 'transparent', + backgroundColor: 'red', borderRadius: 50, padding: 10, }, diff --git a/service/AsyncStorage.tsx b/service/AsyncStorage.tsx index c7c237b..ff6e412 100644 --- a/service/AsyncStorage.tsx +++ b/service/AsyncStorage.tsx @@ -2,17 +2,41 @@ import { Card } from "../models/Card"; import AsyncStorage from "@react-native-async-storage/async-storage"; -export class AsyncStorageCard{ +export default class Storage { - static AddCardStorage(name : string, cards : Card[]){ - const storeFavoriteNounours = async (cards : Card[]) => { - try { - const jsonCard = JSON.stringify(cards) - await AsyncStorage.setItem(name, jsonCard); - } catch (e) { - console.log("An error occurred", e); - } + static async getItem(key: string): Promise { + try { + const value = await AsyncStorage.getItem(key); + if (value !== null) { + return JSON.parse(value); } + } catch (e) { + console.error(`AsyncStorage getItem error: ${e}`); + } + return null; } - -} \ No newline at end of file + + static async setItem(key: string, value: any): Promise { + try { + await AsyncStorage.setItem(key, JSON.stringify(value)); + } catch (e) { + console.error(`AsyncStorage setItem error: ${e}`); + } + } + + static async removeItem(key: string): Promise { + try { + await AsyncStorage.removeItem(key); + } catch (e) { + console.error(`AsyncStorage removeItem error: ${e}`); + } + } + + static async clear(): Promise { + try { + await AsyncStorage.clear(); + } catch (e) { + console.error(`AsyncStorage clear error: ${e}`); + } + } +}