Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
22_23-J 18
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
22_23-J 18
22_23-J 18
Commits
3cfe670f
Commit
3cfe670f
authored
Apr 20, 2023
by
Lelkada L L P S M
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
card game images, audio from urls
parent
979b9747
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
37 deletions
+80
-37
App/Frontend/screens/FlipCardGameScreen.js
App/Frontend/screens/FlipCardGameScreen.js
+77
-34
App/Frontend/screens/WordGenerationScreen.js
App/Frontend/screens/WordGenerationScreen.js
+3
-3
No files found.
App/Frontend/screens/FlipCardGameScreen.js
View file @
3cfe670f
import
React
,
{
useState
,
useEffect
}
from
'
react
'
;
import
React
,
{
useState
,
useEffect
,
useRef
}
from
'
react
'
;
import
{
StyleSheet
,
View
,
TouchableOpacity
,
Image
,
Text
,
Modal
,
Button
}
from
'
react-native
'
;
import
{
Audio
}
from
'
expo-av
'
;
const
Card
=
({
image
,
audio
,
onFlip
,
gameOver
,
correct
})
=>
{
const
Card
=
({
image
,
audio
,
onFlip
,
gameOver
,
correct
,
soundRef
})
=>
{
const
[
isFlipped
,
setIsFlipped
]
=
useState
(
false
);
const
[
loadingAudio
,
setLoadingAudio
]
=
useState
(
false
);
useEffect
(()
=>
{
if
(
gameOver
)
{
setIsFlipped
(
true
);
}
},
[
gameOver
]);
const
handleFlip
=
async
()
=>
{
if
(
!
isFlipped
&&
!
gameOver
)
{
if
(
!
isFlipped
&&
!
gameOver
&&
!
loadingAudio
)
{
setIsFlipped
(
true
);
await
Audio
.
Sound
.
createAsync
(
audio
,
{
shouldPlay
:
true
});
onFlip
(
image
);
if
(
audio
&&
audio
.
uri
)
{
setLoadingAudio
(
true
);
try
{
await
soundRef
.
current
.
unloadAsync
();
await
soundRef
.
current
.
loadAsync
({
uri
:
audio
.
uri
},
{
shouldPlay
:
true
});
}
catch
(
error
)
{
console
.
log
(
'
Error loading audio:
'
,
error
);
}
finally
{
setLoadingAudio
(
false
);
}
}
else
{
console
.
log
(
'
audio.uri not set
'
);
}
onFlip
(
image
);
// Pass the entire image object, not just the uri
setTimeout
(()
=>
{
setIsFlipped
(
true
);
setIsFlipped
(
false
);
// Set isFlipped to false, not true
},
800
);
}
};
return
(
<
TouchableOpacity
style
=
{[
styles
.
card
,
correct
&&
gameOver
?
styles
.
correctCard
:
null
]}
...
...
@@ -45,53 +61,79 @@ const MessageBox = ({ visible, onClose }) => {
);
};
export
default
function
FlipCardGame
({
route
})
{
export
default
function
FlipCardGame
({
navigation
})
{
const
[
gameOver
,
setGameOver
]
=
useState
(
false
);
const
[
imagesData
,
setImagesData
]
=
useState
([]);
//const { cards } = route.params;
//console.log(route.params);
const
soundRef
=
React
.
useRef
(
new
Audio
.
Sound
());
useEffect
(()
=>
{
const
unloadAudio
=
async
()
=>
{
await
soundRef
.
current
.
unloadAsync
();
};
return
()
=>
{
soundRef
.
current
.
unloadAsync
();
};
},
[]);
useEffect
(()
=>
{
fetchImagesData
();
},
[]);
const
mainImage
=
imagesData
.
find
((
img
)
=>
img
.
isMainImage
);
const
fetchImagesData
=
async
()
=>
{
try
{
const
response
=
await
fetch
(
'
http://192.168.137.111:5000/api/images_data
'
);
const
data
=
await
response
.
json
();
setImagesData
(
data
);
//console.log(data);
const
formattedData
=
[
{
...
data
.
main_word
,
isMainImage
:
true
,
uri
:
data
.
main_word
.
audio_link
},
{
...
data
.
card_word1
,
isMainImage
:
false
,
uri
:
data
.
card_word1
.
audio_link
},
{
...
data
.
card_word2
,
isMainImage
:
false
,
uri
:
data
.
card_word2
.
audio_link
},
{
...
data
.
card_word3
,
isMainImage
:
false
,
uri
:
data
.
card_word3
.
audio_link
},
];
//console.log(formattedData);
setImagesData
(
formattedData
);
}
catch
(
error
)
{
console
.
error
(
'
Error fetching images data:
'
,
error
);
}
};
useEffect
(()
=>
{
fetchImagesData
();
},
[]);
const
mainImage
=
imagesData
.
find
((
img
)
=>
img
.
isMainImage
);
const
handleCardFlip
=
(
image
)
=>
{
if
(
image
===
mainImage
)
{
const
handleCardFlip
=
(
flippedImage
)
=>
{
if
(
flippedImage
.
uri
===
mainImage
.
image_link
)
{
setGameOver
(
true
);
navigation
.
navigate
(
'
ContentGeneration
'
);
}
};
return
(
<
View
style
=
{
styles
.
container
}
>
{
mainImage
&&
<
Image
source
=
{{
uri
:
mainImage
.
image
}}
style
=
{
styles
.
mainImage
}
/>
}
{
mainImage
&&
<
Image
source
=
{{
uri
:
mainImage
.
image
_link
}}
style
=
{
styles
.
mainImage
}
/>
}
<
View
style
=
{
styles
.
cardsContainer
}
>
{
imagesData
.
map
((
img
,
index
)
=>
(
<
Card
key
=
{
index
}
image
=
{{
uri
:
img
.
image
}}
audio
=
{{
uri
:
img
.
audio
}}
onFlip
=
{
handleCardFlip
}
gameOver
=
{
gameOver
}
correct
=
{
img
.
isMainImage
}
/
>
))}
{
imagesData
.
filter
((
img
)
=>
!
img
.
isMainImage
)
// Filter out the main image
.
map
((
img
,
index
)
=>
(
<
Card
key
=
{
index
}
image
=
{{
uri
:
img
.
image_link
}}
audio
=
{{
uri
:
img
.
audio_link
}}
onFlip
=
{
handleCardFlip
}
gameOver
=
{
gameOver
}
correct
=
{
img
.
isMainImage
}
soundRef
=
{
soundRef
}
/
>
))}
<
/View
>
<
MessageBox
visible
=
{
gameOver
}
onClose
=
{()
=>
setGameOver
(
false
)}
/
>
<
/View
>
);
}
const
styles
=
StyleSheet
.
create
({
container
:
{
flex
:
1
,
...
...
@@ -100,13 +142,14 @@ const styles = StyleSheet.create({
backgroundColor
:
'
#fff
'
,
},
mainImage
:
{
width
:
'
10
0%
'
,
width
:
'
8
0%
'
,
height
:
'
40%
'
,
resizeMode
:
'
co
ntain
'
,
resizeMode
:
'
co
ver
'
,
},
cardsContainer
:
{
flexDirection
:
'
row
'
,
justifyContent
:
'
space-around
'
,
alignItems
:
'
center
'
,
// Add this line
width
:
'
100%
'
,
paddingTop
:
20
,
},
...
...
App/Frontend/screens/WordGenerationScreen.js
View file @
3cfe670f
...
...
@@ -19,9 +19,9 @@ const screenHeight = Dimensions.get("window").height;
const
fetchCards
=
async
()
=>
{
try
{
const
response
=
await
fetch
(
'
http://192.168.137.111:5000/api/similar-words?word=
school
'
);
const
response
=
await
fetch
(
'
http://192.168.137.111:5000/api/similar-words?word=
tomato
'
);
const
data
=
await
response
.
json
();
console
.
log
(
data
)
//
console.log(data)
return
data
.
similar_words
;
}
catch
(
error
)
{
console
.
error
(
'
Error fetching cards:
'
,
error
);
...
...
@@ -34,7 +34,7 @@ export default function ContentGenerationScreen({ navigation }) {
const
[
cards
,
setCards
]
=
useState
([]);
const
scrollViewRef
=
useRef
(
null
);
const
soundRef
=
useRef
(
new
Audio
.
Sound
());
console
.
log
(
cards
);
//
console.log(cards);
useEffect
(()
=>
{
const
loadCards
=
async
()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment